En:CGI
Aus YaCyWiki
Inhaltsverzeichnis |
Running CGI Software with YaCy
It is possible to start CGI programs via YaCy's HTTP server. CGI programs are usually Perl or Bash scripts, but can also be written in C or any other programming language. The only important thing is that data is passed on and returned from the program in a well defined manner which is the Common Gateway Interface (CGI).
Prerequisites
In order to run a script, it is necessary to have the according interpreter (if necessary) for example for Perl installed on the system. The script has to be located in a directory called cgi-bin or CGI-BIN or in a subdirectory of that name. In addition the script has to have a file postfix which is defined in yacy.conf in cgi.suffixes. By default this suffixes are cgi and pl. In case you want to run a regular shell script, you will have to grename it to a name ending with .cgi or you will have to add sh to the list.
Activating CGI
CGI is turned off in YaCy by default and has to be activated explicitly. The reason for this is that the user is supposed to think about what he is doing before using a potentially dangerous option. A script which is not well implemented can endanger the integrity of the system. Before developing software or using readily available scripts, one should inform himself about the risks and find out if the downloaded software is safe to use.
To activate CGI, cgi.allow has to be set to true.
Installation of a Script
To install a script on your peer, you have to create a directory called cgi-bin or CGI-BIN in your DATA/HTDOCS/www/ directory. Scripts which are not located in a directoyr by that name (or a subdirectory of it) will be returned as a text file. This is a security measure to make sure the user (you!) knows what he is doing.
If you upload scripts to your CGI directory via FTP, you have to make sure that you are using ASCII mode. Once the file is on the server you have to change its access rights to be able to run it. On systems running Linux you will have to use chmod 755. On systems running Windows all files are usually runnable.
Running a Script
To run a script, you acll it via HTTP and the get the output as a return. If you have for example created a script calle test.cgi]] in DATA/HTDOCS/cgi-bin/, you can start it via http://localhost:8080/www/cgi-bin/test.cgi.
Environment Variables
CGI programs get their input via STDIN (POST requsts) and via environment variables. YaCy sets the following environment variables:
- CONTENT_LENGTH
- CONTENT_TYPE
- DOCUMENT_ROOT (without trailing /)
- GATEWAY_INTERFACE
- QUERY_STRING
- REMOTE_ADDR
- REMOTE_HOST
- REQUEST_METHOD
- SCRIPT_NAME
- SERVER_NAME
- SERVER_PORT
- SERVER_PROTOCOL
- SERVER_SOFTWARE
Environment variables that are usually set and which are not set by YaCy are:
- AUTH_TYPE
- PATH_INFO
- PATH_TRANSLATED
- REMOTE_IDENT
- REMOTE_USER
Often more environment variables can be accessed which start with HTTP_. The values of this variables are passed on from the HTTP request to the server without changes. Usually the following values should be set:
- HTTP_ACCEPT
- HTTP_REFERER
- HTTP_USER_AGENT
Example
The skript provided below can be used to test CGI. It displays several environment variables and it shows the difference between POST and GET requests.
#!/usr/bin/perl
# test.pl #
use strict;
use CGI::Carp qw(fatalsToBrowser);
print "Content-Type: text/html\n\n";
my $query_string;
print "<html><head><title>CGI test</title></head><body>\n";
print "<p>Request via $ENV{'REQUEST_METHOD'}\n";
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$query_string = $ENV{'QUERY_STRING'};
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $query_string, $ENV{'CONTENT_LENGTH'});
}
print "<br />Undecoded query parameters: $query_string<br /><br />\n";
print "Environment variables:<br />\n";
my @params = ("SERVER_SOFTWARE",
"SERVER_NAME",
"GATEWAY_INTERFACE",
"SERVER_PROTOCOL",
"SERVER_PORT",
"SCRIPT_NAME",
"REMOTE_HOST",
"REMOTE_ADDR",
"CONTENT_TYPE",
"CONTENT_LENGTH",
"HTTP_ACCEPT",
"HTTP_REFERER",
"QUERY_STRING",
"HTTP_USER_AGENT",
"DOCUMENT_ROOT");
foreach my $param(@params) {
print "$param: $ENV{$param}<br />\n";
}
print "<br /><a href=\"$ENV{'SCRIPT_NAME'}?key1=value1&key2=value2\">Reload using GET</a>\n";
print "<br /><form action=\"$ENV{'SCRIPT_NAME'}\" method=\"POST\">\n";
print "<input type=\"hidden\" name=\"keyA\" value=\"valueA\">\n";
print "<input type=\"hidden\" name=\"keyB\" value=\"valueB\">\n";
print "<input type=\"submit\" value=\"Reload using POST\"></form>\n";
print "</p></body></html>\n";
Links
- The Common Gateway Interface Description of the CGI standard YaCy's implementation is based on
Von dieser Seite existiert auch eine deutsche Version.
