Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1278528
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: C/C++

2013-05-30 08:14:35


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. static void
  5. internal_error( char* reason )
  6. {
  7.   char* title = "500 Internal Error";

  8. #if 0
  9.   (void)printf( "
  10.     Status: %sn
  11.     Content-type: text/htmln
  12.     n
  13.     %sn
  14.     

    %s

    n
  15.     Something unusual went wrong during a redirection request:n
  16.     
    n
  17.     %sn
  18.     n
  19.     nn", title, title, title, reason );

  20. #else
  21.   (void)printf("Content-Type: text/htmlnn
  22.     %sn
  23.     n
  24.     
    n
  25.     %sn
  26.     n
  27.     
    n", title, reason);
  28. #endif
  29. }

  30. static void
  31. not_found( char* script_name )
  32. {
  33.   char* title = "404 Not Found";

  34.   (void) printf( "
  35.     Status: %sn
  36.     Content-type: text/htmln
  37.     n
  38.     %sn
  39.     

    %s

    n
  40.     The requested filename, %s, is set up to be redirected to another URL;n
  41.     however, the new URL has not yet been specified.n
  42.     n", title, title, title, script_name );
  43.   fflush(stdout);
  44. }

  45. //COMSPEC = (null), DOCUMENT_ROOT = (null), GATEWAY_INTERFACE = CGI/1.1, HTTP_ACCEPT = text/html,
  46. //application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8, HTTP_ACCEPT_ENCODING = gzip, deflate,
  47. //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,
  48. //HTTP_USER_AGENT = Mozilla/5.0 (Windows NT 5.1; rv:20.0) Gecko/20100101 Firefox/20.0,
  49. //PATH = /usr/local/bin:/usr/ucb:/bin:/usr/bin, QUERY_STRING = T1=1, REMOTE_ADDR = 192.168.1.224,
  50. //REMOTE_PORT = (null), REQUEST_METHOD = GET, REQUEST_URI = (null), SCRIPT_FILENAME = (null),
  51. //SCRIPT_NAME = /cgi-bin/phf, SERVER_ADDR = (null), SERVER_ADMIN = (null), SERVER_NAME = OpenWrt,
  52. //SERVER_PORT = 8081, SERVER_PROTOCOL = HTTP/1.1, SERVER_SIGNATURE = (null), SERVER_SOFTWARE = thttpd/2.25b qiushui_007 V1.1
  53. //REQUEST_METHOD = GET, REQUEST_URI = (null)
  54. //REQUEST_METHOD = POST
  55. void list_argument(void)
  56. {
  57.   char *env_var[] = {
  58.     "COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE",
  59.     "HTTP_ACCEPT", "HTTP_ACCEPT_ENCODING",
  60.     "HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION",
  61.     "HTTP_HOST", "HTTP_USER_AGENT", "PATH",
  62.     "QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT",
  63.     "REQUEST_METHOD", "REQUEST_URI", "SCRIPT_FILENAME",
  64.     "SCRIPT_NAME", "SERVER_ADDR", "SERVER_ADMIN",
  65.     "SERVER_NAME","SERVER_PORT","SERVER_PROTOCOL",
  66.     "SERVER_SIGNATURE","SERVER_SOFTWARE", "CONTENT_LENGTH" };
  67.   char *value, i;

  68.   printf("Content-Type: text/htmlnn");
  69.   printf( "-//W3C//DTD HTML 4.0 Transitional//EN">n" );
  70.   printf( "n" );
  71.   printf( "-Type" CONTENT="text/html; charset=EUC-JP">n" );
  72.   printf( "" CONTENT="no-cache">n" );
  73.   printf( "TESTn" );
  74.   for (i=0; i<25; i++) {
  75.     value = getenv( env_var[i]);
  76.     //printf ( "%s%s%sn","",env_var[i],"" );
  77.     printf ( "%s = %s, ", env_var[i], value);
  78.   }
  79.   printf("n
    n"
    );
  80.   //printf("%sn", "Content-Type: text/hmln");
  81.   //fflush(stdout);
  82. }


  83. static char* argv0;
  84. int main( int argc, char* argv[] )
  85. {
  86.   char *script_name;
  87.   char *path_info;
  88.   char *cp;
  89.   char *str1;
  90.   unsigned char i;
  91.   char *envstr;
  92.   char *method;
  93.   char *envbuf;
  94.   int envlen;
  95.   size_t n;

  96.   argv0 = argv[0];

  97.   list_argument();

  98. #if 0
  99.   script_name = getenv( "SCRIPT_NAME" );
  100.   if ( script_name == (char*) 0 )
  101.   {
  102.     internal_error( "Couldn't get SCRIPT_NAME environment variable." );
  103.     exit( 1 );
  104.   }

  105.   //Append the PATH_INFO, if any. This allows redirection of whole directories.
  106.   path_info = getenv( "PATH_INFO" );
  107.   if ( path_info != (char*) 0 )
  108.   {
  109.     cp = (char*) malloc( strlen( script_name ) + strlen( path_info ) + 1 );
  110.     if ( cp == (char*) 0 )
  111.     {
  112.       internal_error( "Out of memory." );
  113.       exit( 1 );
  114.     }
  115.     (void) sprintf( cp, "%s%s", script_name, path_info );
  116.     script_name = cp;
  117.   }
  118. #endif

  119.   printf( "Content-type: text/htmlnn" );
  120.   printf( "-//W3C//DTD HTML 4.0 Transitional//EN">n" );
  121.   printf( "n" );
  122.   printf( "-Type" CONTENT="text/html; charset=EUC-JP">n" );
  123.   printf( "" CONTENT="no-cache">n" );
  124.   printf( "TESTn" );
  125.     
  126.   method = getenv("REQUEST_METHOD");
  127.   if( (method != (char*)NULL) && !strcmp( method, "POST" ) )
  128.   {
  129.     printf("== POST REQUEST ==
    n"
    );

  130.     envstr = getenv("CONTENT_LENGTH");
  131.     if( (envstr != (char*)NULL) && ((envlen=atoi(envstr)) > 0) )
  132.     {
  133.       envbuf = malloc( envlen+1 );
  134.       n = fread( envbuf, 1, envlen, stdin );
  135.       envbuf[n] = '';
  136.       printf("%s
    n"
    , envbuf);
  137.       
  138.       free(envbuf);
  139.     }
  140.   }
  141.   else if( (method != (char*)NULL) && !strcmp( method, "GET" ) ) {
  142.     //printf("== NOT POST REQUEST ==
    n"
    );
  143.     envstr = getenv("QUERY_STRING");
  144.     if ( envstr == (char*) 0 ) {
  145.       internal_error( "Couldn't get environment variable: QUERY_STRING" );
  146.       //exit( 1 );
  147.     }
  148.     
  149.     printf("

    QUERY_STRING = %s

    n"
    , envstr);
  150.   }
  151.   
  152.   //最后统一
  153.   printf( "nn" );

  154.   return 0;
  155. }

2. 利用thttpd, 再加上自写的CGI,  移植成功了原先的项目. 图例如下




阅读(8001) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

sheepbao2015-06-24 20:35:00

你好,看了你的文章受益匪浅,但是我不明白,在openwrt里,cgi应该放在哪里?还有你写的程序编译后,是否要改为后缀 XX.cgi ?是需要配置uhttpd吗?