Simple and elegant, thanks a lot!

Le 2015-11-11 17:59, Christian Grothoff a écrit :
On 11/11/2015 11:52 PM, Nicolas Mora wrote:
Hello,

While trying to use the option MHD_OPTION_URI_LOG_CALLBACK to get the
full url from the client, the documentation says:

"the return value will be passed as (*con_cls) in calls to the
MHD_AccessHandlerCallback when this request is processed later;
returning a value of NULL has no special significance (however, note
that if you return non-NULL, you can no longer rely on the first call to
the access handler having NULL == *con_cls on entry;)"

The need is to store the full url called by the client in a char *
variable.

In my program, I detect the first iteration of MHD_AccessHandlerCallback
by testing if con_cls is NULL:
https://github.com/babelouest/ulfius/blob/master/src/ulfius.c#L111

Is there another way for MHD_AccessHandlerCallback to detect it's in the
first iteration, so I can properly use con_cls and the option
MHD_OPTION_URI_LOG_CALLBACK ?

Yes, very simple. In the LOG callback, just do this:

struct Context {
   char *full_uri;
   int first_call;
};


void *
log_cb (void *cls, const char *uri) {
   struct Context *ctx = malloc (sizeof (struct  Context));
   ctx->first_call = YES;
   ctx->full_uri = strdup(uri);
   return ctx;
}

Then, in the access handler callback you get that 'struct Context' and
upon "first call", you set first_call to NO.

Make sure to register a termination handler to clean up...

Happy hacking!

Christian


Reply via email to