-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
static void
-
internal_error( char* reason )
-
{
-
char* title = "500 Internal Error";
-
-
#if 0
-
(void)printf( "
-
Status: %sn
-
Content-type: text/htmln
-
n
-
%sn
-
%s
n
-
Something unusual went wrong during a redirection request:n
-
n
-
%sn
-
n
-
nn", title, title, title, reason );
-
-
#else
-
(void)printf("Content-Type: text/htmlnn
-
%sn
-
n
-
n
-
%sn
-
n
-
n", title, reason);
-
#endif
-
}
-
-
static void
-
not_found( char* script_name )
-
{
-
char* title = "404 Not Found";
-
-
(void) printf( "
-
Status: %sn
-
Content-type: text/htmln
-
n
-
%sn
-
%s
n
-
The requested filename, %s, is set up to be redirected to another URL;n
-
however, the new URL has not yet been specified.n
-
n", title, title, title, script_name );
-
fflush(stdout);
-
}
-
-
//COMSPEC = (null), DOCUMENT_ROOT = (null), GATEWAY_INTERFACE = CGI/1.1, HTTP_ACCEPT = text/html,
-
//application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8, HTTP_ACCEPT_ENCODING = gzip, deflate,
-
//HTTP_ACCEPT_LANGUAGE = zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3, HTTP_CONNECTION = (null), HTTP_HOST = 192.168.1.1,
-
//HTTP_USER_AGENT = Mozilla/5.0 (Windows NT 5.1; rv:20.0) Gecko/20100101 Firefox/20.0,
-
//PATH = /usr/local/bin:/usr/ucb:/bin:/usr/bin, QUERY_STRING = T1=1, REMOTE_ADDR = 192.168.1.224,
-
//REMOTE_PORT = (null), REQUEST_METHOD = GET, REQUEST_URI = (null), SCRIPT_FILENAME = (null),
-
//SCRIPT_NAME = /cgi-bin/phf, SERVER_ADDR = (null), SERVER_ADMIN = (null), SERVER_NAME = OpenWrt,
-
//SERVER_PORT = 8081, SERVER_PROTOCOL = HTTP/1.1, SERVER_SIGNATURE = (null), SERVER_SOFTWARE = thttpd/2.25b qiushui_007 V1.1
-
//REQUEST_METHOD = GET, REQUEST_URI = (null)
-
//REQUEST_METHOD = POST
-
void list_argument(void)
-
{
-
char *env_var[] = {
-
"COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE",
-
"HTTP_ACCEPT", "HTTP_ACCEPT_ENCODING",
-
"HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION",
-
"HTTP_HOST", "HTTP_USER_AGENT", "PATH",
-
"QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT",
-
"REQUEST_METHOD", "REQUEST_URI", "SCRIPT_FILENAME",
-
"SCRIPT_NAME", "SERVER_ADDR", "SERVER_ADMIN",
-
"SERVER_NAME","SERVER_PORT","SERVER_PROTOCOL",
-
"SERVER_SIGNATURE","SERVER_SOFTWARE", "CONTENT_LENGTH" };
-
char *value, i;
-
-
printf("Content-Type: text/htmlnn");
-
printf( "-//W3C//DTD HTML 4.0 Transitional//EN">n" );
-
printf( "n" );
-
printf( "-Type" CONTENT="text/html; charset=EUC-JP">n" );
-
printf( "" CONTENT="no-cache">n" );
-
printf( "TESTn" );
-
for (i=0; i<25; i++) {
-
value = getenv( env_var[i]);
-
//printf ( "%s%s%sn","",env_var[i],"" );
-
printf ( "%s = %s, ", env_var[i], value);
-
}
-
printf("n
n");
-
//printf("%sn", "Content-Type: text/hmln");
-
//fflush(stdout);
-
}
-
-
-
static char* argv0;
-
int main( int argc, char* argv[] )
-
{
-
char *script_name;
-
char *path_info;
-
char *cp;
-
char *str1;
-
unsigned char i;
-
char *envstr;
-
char *method;
-
char *envbuf;
-
int envlen;
-
size_t n;
-
-
argv0 = argv[0];
-
-
list_argument();
-
-
#if 0
-
script_name = getenv( "SCRIPT_NAME" );
-
if ( script_name == (char*) 0 )
-
{
-
internal_error( "Couldn't get SCRIPT_NAME environment variable." );
-
exit( 1 );
-
}
-
-
//Append the PATH_INFO, if any. This allows redirection of whole directories.
-
path_info = getenv( "PATH_INFO" );
-
if ( path_info != (char*) 0 )
-
{
-
cp = (char*) malloc( strlen( script_name ) + strlen( path_info ) + 1 );
-
if ( cp == (char*) 0 )
-
{
-
internal_error( "Out of memory." );
-
exit( 1 );
-
}
-
(void) sprintf( cp, "%s%s", script_name, path_info );
-
script_name = cp;
-
}
-
#endif
-
-
printf( "Content-type: text/htmlnn" );
-
printf( "-//W3C//DTD HTML 4.0 Transitional//EN">n" );
-
printf( "n" );
-
printf( "-Type" CONTENT="text/html; charset=EUC-JP">n" );
-
printf( "" CONTENT="no-cache">n" );
-
printf( "TESTn" );
-
-
method = getenv("REQUEST_METHOD");
-
if( (method != (char*)NULL) && !strcmp( method, "POST" ) )
-
{
-
printf("== POST REQUEST ==
n");
-
-
envstr = getenv("CONTENT_LENGTH");
-
if( (envstr != (char*)NULL) && ((envlen=atoi(envstr)) > 0) )
-
{
-
envbuf = malloc( envlen+1 );
-
n = fread( envbuf, 1, envlen, stdin );
-
envbuf[n] = '';
-
printf("%s
n", envbuf);
-
-
free(envbuf);
-
}
-
}
-
else if( (method != (char*)NULL) && !strcmp( method, "GET" ) ) {
-
//printf("== NOT POST REQUEST ==
n");
-
envstr = getenv("QUERY_STRING");
-
if ( envstr == (char*) 0 ) {
-
internal_error( "Couldn't get environment variable: QUERY_STRING" );
-
//exit( 1 );
-
}
-
-
printf("
QUERY_STRING = %s
n", envstr);
-
}
-
-
//最后统一
-
printf( "nn" );
-
-
return 0;
-
}
2. 利用thttpd, 再加上自写的CGI, 移植成功了原先的项目. 图例如下
阅读(9115) | 评论(1) | 转发(1) |