0

我正在尝试为站点主页进行 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 
4

2 回答 2

0

你忘了return 255;main()函数中。

请记住,连接处理程序返回代码具有以下含义:

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
// ============================================================================
于 2012-11-21T15:27:18.127 回答
0

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

于 2013-02-13T04:30:18.300 回答