libmicrohttpd
[Top][All Lists]
Advanced

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

Re: [libmicrohttpd] Understanding minimal_example.c


From: Christian Grothoff
Subject: Re: [libmicrohttpd] Understanding minimal_example.c
Date: Tue, 21 Apr 2009 17:25:02 -0600
User-agent: KMail/1.11.2 (Linux/2.6.26-1-686; KDE/4.2.2; i686; ; )

On Monday 20 April 2009 03:58:58 am arnuld uttre wrote:
> This is some part of the code from minimal_example.c that is
> distributed with the libmicrohttpd source. I have some questions over
> it which manual does not explain:
>
>
>
> static int
> ahc_echo (void *cls,
>           struct MHD_Connection *connection,
>           const char *url,
>           const char *method,
>           const char *version,
>           const char *upload_data, size_t *upload_data_size, void **ptr)
> {
>   static int aptr;
>   const char *me = cls;
>   struct MHD_Response *response;
>   int ret;
>
>  
> printf("-------------------------------------------------------------------
>-------------\n"); printf("upload_datae = %s\n", upload_data);
>
>
>   if (0 != strcmp (method, "GET"))
>     return MHD_NO;              /* unexpected method */
>   if (&aptr != *ptr)
>     {
>       /* do never respond on first call */
>       *ptr = &aptr;
>       return MHD_YES;
>     }
>   *ptr = NULL;                  /* reset when done */
>   response = MHD_create_response_from_data (strlen (me),
>                                             (void *) me, MHD_NO, MHD_NO);
>   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
>   MHD_destroy_response (response);
>   return ret;
> }
> -------------------------------------------------------------
>
> > if (0 != strcmp (method, "GET"))
> >   return MHD_NO;              /* unexpected method */
>
> why we are returning with MHD_NO when the method is GET and not for
> other methods ?

You are not reading the check correctly.  We return MHD_NO for all methods 
except for GET -- this minimal example only handles "GET" requests.

> >      /* do never respond on first call */
> >      *ptr = &aptr;
> >      return MHD_YES;
> >     }
> >   *ptr = NULL;                  /* reset when done */
>
> Why never respond to the first attempt of the client ?  I am, in my
> software, not using any check for GET method. I also respond to the
> very first call and just set the pointer to some value and then I
> never set it to NULL again. What harm I am doing to my program ?

None really in this simple case.  However, if you are writing code that is 
supposed to properly process HTTP 1.1 requests that include the client waiting 
for "100 CONTINUE" before giving you an upload (POST/PUT in particular), 
sending a response during the first call will suppress the 100 CONTINUE.  
Which may be what you want if your response will be a 4xx-error code, but if 
you want to see the full upload, you should not queue a response during this 
first call.  I agree that this is not strictly required for a simple GET 
request.  The example is written this way to make the transition to more 
complex handlers trivial to do.

> Last, *ptr = &aptr and aptr is not initialized with some value. Can't
> it produce Segfault ?

No.  &aptr is a valid address, not very useful, but a valid address.  Besides, 
since we never really do anything with it, it doesn't matter.  All we really 
need here (for this simplistic example) is an address that is not NULL, and 
&aptr certainly will be non-NULL.

Christian





reply via email to

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