Question

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 
Was it helpful?

Solution 2

This issue is resolved on G-WAN version 4+

OTHER TIPS

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
// ============================================================================
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top