$r Apache request in CGI? - apache

In trying to find a solution for another question (passing variables in cgi) I ran into the concept of apache_notes which then led me to a series of modules on CPAN that use the mysterious $rvariable (both of which I have run into before while looking other stuff up, but did not delve too deeply into). None of the modules really explain where $r comes from, they just all assume you have it readily available. From what I can understand the variable has something to do with mod_perl (which I also know little to nothing about) and/or apache handlers.
So my question is, is there a way to get that $r apache request variable in CGI script calls, without the use of mod_perl or apache handler definitions?
Also, if there is a way to access apache_notes in CGI, I think that would answer the aforementioned question, and an answer there for how to achieve this would be greatly appreciated!)

No. It's the Apache (libapr) request object. If you're not embedded in Apache, there's no Apache request object.
But there's no use for notes in a CGI anyway; there isn't any other piece of code that the script could be sharing information with by hanging it off of the request object, so you can just use a variable instead.

Related

Delphi Apache-Module with SSO

we successfully created an apache module with Embarcadero Delphi (10.3). Next step is the idea to extend this module with SSO-functionality (NTML/Kerberos).
I understand there are several modules for apache to enable the sso-features for php/html-content and directories by extending the httpd.conf-file (or even locations like those the module uses).
But i have no idea how to access the apache-server-variables or the information about the sso-credentials (windows logon-name) from inside my apache-module.
Perhaps someone can give me a hint here.
Possible alternatives:
Recode the negotiate-handshakes (ntml/krb) inside the module (already did this for indy)
Use a little php-script file to access the variables (with
redirect/ajax for example)
Somehow (would not know how) add those information to the request-headers inside apache before going into the module (sounds insecure)
But i would like to use an easier way ;)
Thanks
For the xxm project (which also has an Apache httpd module!) I've implemented NTLM authentication using the AcquireCredentialsHandle and AcceptSecurityContext calls.
It works using the WWW-Authenticate request and response values. First there's NTLM, followed by one or more round-trips with NTLM followed by a space and base64 encoded data you need to pass until you get a SEC_E_OK value back.

Solution to execute CGI scripts

I have some CGI scripts which are used internal to the organization. Due to some standard changes, the server/system team has commented the below line from httpd.conf (apache config file) that support the CGI scripts. Because of this change, the existing CGI scripts are affected and unable to execute them on browser.
### LoadModule cgid_module modules/mod_cgid.so
Is there a way to overcome this situation and make the scripts work as is.
NOTE : The above commented line cannot be uncommented/enabled.
Ways of circumventing your company's policy, from best to worse:
Load mod_cgi anyway.
Load mod_fcgi, and convert your CGI script into a Fast CGI daemon. It's a lot of work, but you'll can get faster code out of it!
Load your own module that does exactly the same thing as mod_cgi. mod_cgi is open source, so it should be easy to just rename it.
Load mod_fcgi, and write a Fast CGI daemon that executes your script.
Install a second apache web server with mod_cgi enabled. Link to it directly or use mod_proxy on the original server.
Write your own web server. Link to it directly or use mod_proxy on the original server.
Last time you asked this question, you talked about using mod_perl instead. The standard way to run CGI program unchanged (for some value of "unchanged") under mod_perl is by using ModPerl::Registry. Did you try that? How did it go?
Another alternative would be to convert your programs to use PSGI. You could try using Plack::App::Wrap::CGI or CGI::Emulate::PSGI. Using Plack would free you from any deployment restrictions. You could run the code under mod-perl or even as a separate service behind a proxy server.
But I can't help marvelling at how ridiculous this whole situation is. Your company has CGI programs that it (presumably) relies on to run part of its business. And they've just decided to turn off support for them. You need to find out why this decision has been made and try to buy some time in order to convert to an alternative technology.

does cgi-bin have something like servlet filters

Does cgi scripts have something like servlet filters. I.e. a piece of code that is given the opportunity to see and or modify a request before and or after it is processes by the "real" script.
If you are programming a CGI script via an apache module, then it is possible. mod_perl, for example, gives hooks into that. Regular CGI, where apache calls an outside process to deal with the script - nope. Sorry.

How to begin writing an Application Server over Apache?

For my college project, I want to create a simple application server in C that runs over Apache. Like .php, .asp, .jsp, the extension of my files would be .sas.
I have already written a parser which reads the .sas files and generates the output. For example, consider a file index.sas with the below code:
<%
echo "Hello";
%>
Now, if I execute:
sas index.sas
The result would be:
Hello
Now I want to use this program as an
application server over Apache just as
PHP, Tomcat, etc. work over Apache. I
have heard of cgi-bin but I think PHP
uses a different approach. I want to
learn the approach which PHP uses.
Please advice.
Little correction: Apache HTTP Server is not required to be able to run Apache Tomcat as webserver. Apache Tomcat is at its own already a full fledged webserver. Your confusion is probably caused by the Tomcat Connector which could be used to connect Apache HTTP Server and Apache Tomcat together to be able to serve PHP/JSP behind one same HTTP port.
As to your actual question, PHP can be installed as CGI module or ASAPI (Apache Server API) module. If you want to program a CGI module for Apache HTTP Server, then you may find this document useful. If you want to write an ASAPI module, then you may find those documentations useful.
You need to write a module utilizing the Apache API.
Some basic documentation with examples can be found here.
http://www.auburn.edu/docs/apache/mod/mod_example.html
No, no, no!!! Did I say "no" enough? :)
You don't need to create a new module or look at PHP source code. Talking about re-inventing the wheel using a square boulder.
The easiest thing to do is to use mod_cgi. That is, you use CGI to have Apache forward the request to your SAS interpreter.
[Apache 1.3x] - http://httpd.apache.org/docs/1.3/mod/mod_cgi.html
[Apache 2.0x] - http://httpd.apache.org/docs/2.0/mod/mod_cgi.html
[CGI] - http://en.wikipedia.org/wiki/Common_Gateway_Interface
Now, if you do not want to use CGI (don't know why unless it is expressively forbidden by your homework instructions), then yeah, you will have to create a module. For that take a look at this as an starting point (courtesy of google):
http://threebit.net/tutorials/apache2_modules/tut1/tutorial1.html
Good luck with that, though. It could become labor-intensive.
Hope it helps.

Are there alternatives to CGI (and do I really need one)?

I am designing an application that is going to consist of 3-4 services that run as separate processes and are linked by a suitable IPC. The system is going to have a web interface and I want to use whatever webserver is there.
The web interface should be accessed under some URL that allows to have other URLs on the same webserver doing totally different things. I'm planning to use the path below that URL to specify what the web interface should do. It has facilities for use by other applications over the net and for humans to interact with in a browser.
Off the cuff, I'd work as follows:
make the webserver fire up a CGI process for every request it receives (like SetHandler in Apache)
let the CGI connect to the IPC
let it get whatever it needs from the backend services
let the CGI return HTML / XML and whatever HTTP Status based on the services' answers
Now, what I really want is to avoid the first two steps, or if I can't, avoid the second one, because I'm afraid that I'm wasting performance on unneccesary overhead (the requests coming from other applications might be frequent).
PHP, for example, can open persistent connections to a MySQL database that survive the script's runtime and don't need to be recreated next time, though I don't know how they actually do it. Also, as I understand it, the Apache modules are loaded once when the server starts, so that might remove the first step but would tie me to Apache.
So, what are good ways to hook a handler for specific URLs into different webservers? I don't want to handle the HTTP, otherwise I might just use a proxy setup to a second server, but it just seems to be so reinventing-the-wheel. If you think, CGI is fine and have examples where it handles large numbers of request of a similar structure, please let me know.
OK, I overlooked this previously. Explaining my question here brought me onto it:
Instead of creating a new process for every request, FastCGI can use a single persistent process which handles many requests over its lifetime. -- Wikipedia: FastCGI
Even under moderate loads, CGI is a pretty unscalable beast. FastCGI is an option, but you'll probably also find a mod_XXXX package where XXXX is the name of your language. There's a mod for ruby, perl, and python for instance and probably a fair few others.