3

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

Tim Post
  • 33,371
  • 15
  • 110
  • 174

1 Answers1

1

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).

Gil
  • 3,279
  • 1
  • 15
  • 25
  • Thanks for your reply. I tested the developers page, so I created c servlet blog.c (which works well). In handlers, I created main.c which include the source code or rewriting in main function. Gwan starts well, but when I test with localhost I have a 404 Error. I have no errors when gwan starts and in logs, handlers and csp are loading well. Regards – gdevillepin Nov 13 '12 at 13:42
  • 404 means that the result of your rewrite is not found. Try using *printf()* to display it so you can check what's wrong. Also, if the answer above is correct, it will help other users if you accept it by clicking on its 'votes' number to get a green tick. – Gil Nov 13 '12 at 13:44
  • I know what a 404 means, but for me, gwan doesn't recognise rewrite url with my code. So, is it correct to create it in main.c in the folder handler ? How do I use printf (it doesn't want to launch the server) and where ?? in handlers ? in servlet ? I'm seriously lost ... – gdevillepin Nov 13 '12 at 14:23
  • Check that your handler (which is correctly named and copied under /handlers) works. Look at the /log/error.log file (or use printf() in the handler) to see what *rewritten* resource G-WAN is trying to serve (and failing to find). If you can't make it work then you can also get personalized support and send us your source code for review if you become a registered user. – Gil Nov 13 '12 at 16:41
  • My handlers is called url_wr.c in the path /0.0.0.0_80/#0.0.0.0/handlers. The script is exactly like in example : I created a servlet blog which is a copy of hello.c. In the error.log, I only have the 404 error. In gwan.log, it is not writen that my handler is loaded. Thanks for your help – gdevillepin Nov 15 '12 at 07:42
  • In your previous comment you stated that your handler was called 'main.c' and this is why I wrote that it was 'correctly named'. You can verify that a handler is executed by using *printf()* in the handler portions to test. Also check that the states supposed to be executed by the handler are defined in the *init()* call. All this is very basic G-WAN support. All those questions find their answers in the documentation. – Gil Nov 15 '12 at 08:04
  • Well, I guess I missed something in the documentation ! Anyway it's working, thanks for all. – gdevillepin Nov 15 '12 at 08:30