2

我正在尝试为我的脚本实现一个宁静的处理程序来替换/&这样我就可以将 url 像这样:?script.c&things变成 this: ?script/things。目前我只有一个基于Gil这篇文章的测试脚本。

  // ============================================================================
  // Handler C script for the G-WAN Web Application Server (http://gwan.ch/)
  // ----------------------------------------------------------------------------
  // main.c: basic rewrite example
  // ============================================================================
  #include "gwan.h"    // G-WAN exported functions

  #include <stdio.h> // puts(), printf()
  // ----------------------------------------------------------------------------
  // init() will initialize your data structures, load your files, etc.
  // ----------------------------------------------------------------------------
  // init() should return -1 if failure (to allocate memory for example)
  int init(int argc, char *argv[])
  {
     // define which handler states we want to be notified in main():
     // enum HANDLER_ACT { 
     //  HDL_INIT = 0, 
     //  HDL_AFTER_ACCEPT, // just after accept (only client IP address setup)
     //  HDL_AFTER_READ,   // each time a read was done until HTTP request OK
     //  HDL_BEFORE_PARSE, // HTTP verb/URI validated but HTTP headers are not 
     //  HDL_AFTER_PARSE,  // HTTP headers validated, ready to build reply
     //  HDL_BEFORE_WRITE, // after a reply was built, but before it is sent
     //  HDL_HTTP_ERRORS,  // when G-WAN is going to reply with an HTTP error
     //  HDL_CLEANUP };
     u32 *states = (u32*)get_env(argv, US_HANDLER_STATES);
     *states =  (1 << HDL_AFTER_READ);
     return 0;
  }
  // ----------------------------------------------------------------------------
  // clean() will free any allocated memory and possibly log summarized stats
  // ----------------------------------------------------------------------------
  void clean(int argc, char *argv[])
  {}
  // ----------------------------------------------------------------------------
  // main() does the job for all the connection states below:
  // (see 'HTTP_Env' in gwan.h for all the values you can fetch with get_env())
  // ----------------------------------------------------------------------------
  int main(int argc, char *argv[])
  {
     // HDL_HTTP_ERRORS return values:
     //   0: Close the client connection
     //   2: Send a server reply based on a custom reply buffer
     // 255: Continue (send a reply based on the request HTTP code)
     const long state = (long)argv[0];
     printf("Catching Gwan State: %i\n", (long)argv[0] );
     if(state != HDL_AFTER_READ)
        return 255;

     xbuf_t *read_xbuf = (xbuf_t*)get_env(argv, READ_XBUF);
     printf("req_1: %.20s\n", read_xbuf->ptr);
     xbuf_replfrto(read_xbuf, read_xbuf->ptr, read_xbuf->ptr + 16, "/", "&");
     printf("req_2: %.20s\n-------------------\n\n", read_xbuf->ptr);

     return 255; // continue G-WAN's default execution path
  }
  // ============================================================================
  // End of Source Code
  // ============================================================================

在这个脚本中,我有printf("Catching Gwan State: %lu\n", (long)argv[0] );一行应该打印它得到的状态(0-8,我猜),但它一直在打印

Catching Gwan State: -38241808

我不知道 -38241808 是什么

有什么帮助吗?我的操作系统是 Linux Mint 14,Gwan 版本 4.2.19

[编辑] 即使使用 Gwan 附带的 main_generic.c 处理程序示例也会给出这些奇怪的状态值

4

2 回答 2

2

我正在尝试为我的脚本实现一个宁静的处理程序,将 / 替换为 & 所以我可以将 url 像这样:?script.c&things变成 this: ?script/things

G-WAN自动完成。绝对不需要处理程序。该RESTful功能记录在 PDF 手册时间线中。

您甚至可以定义哪种编程语言是默认语言(不需要在 URI 中显式文件扩展名的语言)。请参阅下面的操作方法(此处来自处理程序):

int init(int argc, char *argv[])
{
   // the QUERY_CHAR character can be chosen from the following set: 
   //  - _ . ! ~ * ' ( ) 
   // (see RFC 2396, section "2.3. Unreserved Characters")
   //   
   u8 *query_char = (u8*)get_env(argv, QUERY_CHAR);
   *query_char = '!'; // use "/!hello.c" instead of "/?hello.c"

   // by default, DEFAULT_LANG = LG_C (ANSI C)
   // LG_C, LG_CPP, LG_JAVA, etc. are defined in /gwan/include/gwan.h
   // and in http://gwan.com/api#env
   //
   u8 *lang = (u8*)get_env(argv, DEFAULT_LANG);
   *lang = LG_CPP; // use "/!hello" instead of "/!hello.cpp"
   return 0;
}

只需使用/?argv.c&123&456G-WAN 示例对其进行测试,/?argv/123/456而不是使用...

于 2013-02-27T08:52:25.527 回答
-1

我在我的身上测试过。如果你喜欢这样
u32 *states = (u32*)get_env(argv, US_HANDLER_STATES);
*states = (1L << HDL_AFTER_ACCEPT)
| (1L << HDL_AFTER_READ)
| (1L << HDL_BEFORE_PARSE)
| (1L << HDL_AFTER_PARSE)
| (1L << HDL_HTTP_ERRORS)
| (1L << HDL_BEFORE_WRITE);

它将产生0-8。

于 2013-02-27T02:15:11.027 回答