【问题标题】:G-WAN URL rewrite for home page not working主页的 G-WAN URL 重写不起作用
【发布时间】:2012-11-20 17:47:58
【问题描述】:

我正在尝试对网站主页进行 URL 重写。这是我的处理程序的简化版本。

int init(int argc, char *argv[])
{
   u32 *states = (u32*)get_env(argv, US_HANDLER_STATES);
   *states =  (1 << HDL_AFTER_READ);
   return 0;
}

int main(int argc, char *argv[])
{
   xbuf_t *read_xbuf = (xbuf_t*)get_env(argv, READ_XBUF);
   xbuf_replfrto(read_xbuf, read_xbuf->ptr, read_xbuf->ptr + 16, " / ", " /?home ");
   return 255;
}

void clean(int argc, char *argv[]) 
{}

基本上它只是将“ / ”替换为“ /?home ”。所以当用户加载“www.domain.com”时,它会给他们“home.c”的内容。这是重写的结果。一切看起来都正确我不确定是什么导致了问题。

原始请求:

GET / HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

重写请求:

GET /?home HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

重写后的结果是这样的。

GET http://localhost:8000/


 -- response --
0 

【问题讨论】:

    标签: url-rewriting handlers g-wan


    【解决方案1】:

    您忘记在main() 函数中输入return 255;

    请记住,连接处理程序返回码有一个含义:

    return 255; // execute next connection step
    return   0; // close connection
    

    此外,即使它仍然为空,您也必须在连接处理程序中声明 clean() 函数:

    void clean(int argc, char *argv[]) 
    {}
    

    最后,您必须测试main() 中的处理程序状态。

    这给了我们下面的测试代码:

    // ============================================================================
    // 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 int state = (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, " / ", " /?home ");
       printf("req_2: %.20s\n-------------------\n\n", read_xbuf->ptr);
    
       return 255; // continue G-WAN's default execution path
    }
    // ============================================================================
    // End of Source Code
    // ============================================================================
    

    【讨论】:

    • 我修改了我的示例代码。我得到了干净并在我的原始代码上返回 255。但仍然是同样的问题。
    • 我重写了上面的回复以添加完整的(测试的)源代码。
    • 还是一样。我使用了您的代码,但我使用的是虚拟机。这可能会导致问题。今晚我会在家试一试。
    • 我得到了“req_2: GET /?home HTTP/1”。重写后还是什么都没有。
    • 证明重写 handler 有效。现在检查日志文件,看看为什么没有提供文件(404:找不到?)。
    【解决方案2】:

    此问题已在 G-WAN 版本 4+ 上得到解决

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多