Url rewriting 主页的G-WAN URL重写不起作用

Url rewriting 主页的G-WAN URL重写不起作用,url-rewriting,handlers,g-wan,Url Rewriting,Handlers,G Wan,我正在尝试做一个网站主页的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

我正在尝试做一个网站主页的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[]) 
{}
重写请求:

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 

您忘记了
返回255main()
函数中选择code>

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

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
// ============================================================================
//============================================================================
//G-WAN Web应用程序服务器的处理程序C脚本(http://gwan.ch/)
// ----------------------------------------------------------------------------
//main.c:基本重写示例
// ============================================================================
#包括“gwan.h”//G-WAN导出功能
#包括//put(),printf()
// ----------------------------------------------------------------------------
//init()将初始化数据结构、加载文件等。
// ----------------------------------------------------------------------------
//如果失败,init()应该返回-1(例如分配内存)
int init(int argc,char*argv[]
{
//定义要在main()中通知的处理程序状态:
//枚举处理程序\u动作{
//HDL_INIT=0,
//HDL\u AFTER\u ACCEPT,//just AFTER ACCEPT(仅客户端IP地址设置)
//HDL_在_读取之后,//每次读取都完成,直到HTTP请求OK
//HDL\u在\u解析之前,//HTTP谓词/URI已验证,但HTTP头未验证
//解析后的HDL\u,//验证HTTP头,准备生成应答
//HDL_BEFORE_WRITE,//在生成回复之后,但在发送之前
//HDL_HTTP_错误,//当G-WAN将以HTTP错误进行回复时
//HDL_清理};
u32*状态=(u32*)获取环境(argv,美国处理程序状态);
*状态=(1 ptr);
xbuf_replfrto(读xbuf,读xbuf->ptr,读xbuf->ptr+16,“/”,“/?home”);
printf(“请求2:%.20s\n-----------------\n\n”,读取\u xbuf->ptr);
返回255;//继续G-WAN的默认执行路径
}
// ============================================================================
//源代码结束
// ============================================================================

这个问题在G-WAN版本4+上得到解决

我修改了我的示例代码。我得到了干净的,并返回了我的原始代码255。但还是一样的问题。我重写了上面的回复,添加了完整的(测试过的)源代码。还是一样的。我使用了你的代码,但我使用的是虚拟机。这可能会引起问题。今晚我将在家里尝试。重写后,我得到了“req_2:GET/?home HTTP/1”。但仍然没有结果。证明重写处理程序工作正常。现在检查日志文件,看看为什么不提供该文件(404:找不到?)。
// ============================================================================
// 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
// ============================================================================