libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] MHD_OPTION_URI_LOG_CALLBACK and con_cls


From: Christian Grothoff
Subject: Re: [libmicrohttpd] MHD_OPTION_URI_LOG_CALLBACK and con_cls
Date: Wed, 11 Nov 2015 23:59:34 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.7.0

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

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]