G-WAN URL rewrite for home page not working

坚强是说给别人听的谎言 提交于 2019-12-12 01:23:46

问题


I am trying to do a URL rewrite for the site home page. Here is a simplified version of my handler.

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[]) 
{}

Basically it is just replacing " / " with " /?home ". So when user loaded "www.domain.com" it will give them the content of "home.c". Here is the result of the rewrite. Everything looks right I am not sure what's causing the issue.

Original Request:

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

Rewritten Request:

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

After the rewrite this is the result.

GET http://localhost:8000/


 -- response --
0 

回答1:


You forgot to return 255; in the main() function.

Remember that connection handler return codes have a signification:

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

Also, even if it remains empty, you must declare a clean() function in a connection handler:

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

Finally, you MUST test the handler state in main().

This gives us the tested code below:

// ============================================================================
// 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
// ============================================================================



回答2:


This issue is resolved on G-WAN version 4+



来源:https://stackoverflow.com/questions/13479044/g-wan-url-rewrite-for-home-page-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!