我正在尝试为网站主页进行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
您忘了在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: %.20sn", read_xbuf->ptr);
xbuf_replfrto(read_xbuf, read_xbuf->ptr, read_xbuf->ptr + 16, " / ", " /?home ");
printf("req_2: %.20sn-------------------nn", read_xbuf->ptr);
return 255; // continue G-WAN's default execution path
}
// ============================================================================
// End of Source Code
// ============================================================================
此问题
已在 G-WAN 版本 4+ 上解决