Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL rewrite on G-WAN for .JPG

I am testing G-WAN server and I'd like using rewrite rules.

With apache the rule is :

RewriteRule ^(.+)-(.+)-(.+)-1.jpg$ imagesproduitnew/$3/$2.jpg [L]

I am trying to do it by handlers JPG, but I have lot of difficulties.

Has anybody already done something like that ?


My handlers is called url_wr.c in the path /0.0.0.0_80/#0.0.0.0/handlers Here is the script

int init(char *argv[], int argc);

int main(int argc, char *argv[])
{
   const long state = (long)argv[0];
   if(state == HDL_AFTER_READ)
   {
      xbuf_t *read_xbuf = (xbuf_t*)get_env(argv, READ_XBUF);
      xbuf_replfrto(read_xbuf, read_xbuf->ptr, read_xbuf->ptr + 16, "/blog", "/?blog");
   }
   return 255; // execute next connection step
}

int clean(char *argv[], int argc);

In gwan.log, it is not writen loaded url_wr.c If I put printf in each function, it doesn't work. The servlet bloc.c works well.

I also tried tu put the code in handlers/main.c and in the root of gwan directory.

I have only a error.log file for the site which says just error404 without any details of the handlers.

Thanks by advance for your support

like image 297
gdevillepin Avatar asked Nov 30 '25 12:11

gdevillepin


1 Answers

You must use a G-WAN connection handler, either to use:

  • a plain-rewrite: one example is given at the end of the developers page,

OR,

  • a regex library (libc provides regex calls) if you target a more general rewrite scheme. Here is an example in C and the explanations are there, courtesy of "Regular Expressions in C" from the "Linux Gazette".

This could also be made rom a servlet, but then you would have to trigger a redirection (unless the resource was explicitely placed into a cache). If this is acceptable, then v3.10+ will let you do it in C#, PHP, Python, etc.


UPDATE following the code published in the question:

Your init() call is empty so main() is never called. You should do this instead:

// ----------------------------------------------------------------------------
// 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; // we assume "GET /hello" sent in one shot
   puts("init()");
   return 0;
}

Also, make sure that connection handlers are named main.c. In contrast, content handlers carry the name of the targeted file extension (gif.c, html.c, etc).

like image 95
Gil Avatar answered Dec 05 '25 13:12

Gil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!