On 01/06/2013 08:57 PM, Sascha Swiercy wrote:
> I'd like to know if it is possible to use
> MHD_create_response_from_callback even if the content length is 0. I
> tried to pass 0 as first argument to MHD_create_response_from_callback
> and also returned 0 from the callback, but this made the library to
> invoke the callback function in an infinite loop. The documentation
> also states this behavior, but I was curious if there is a way to use
> MHD_create_response_from_callback even for an empty response body. If
> not, then I'd suggest to make MHD_create_response_from_callback fail
> in some way if 0 is passed as size. Thanks for your help.
> 
> -Sascha
> 

Dear Sascha,

Thanks for the report, this looks like an interesting corner case we
didn't handle nicely.  The attached patch (also in SVN 25703) should
address it.  Basically, if the message size is zero, your response
callback is unable to communicate the difference between "wait for a
response later (return 0)" and "here is the non-existing reply (return
0)".  The patch fixes it by checking if the body size is zero and then
simply never calling the callback in the first place.

Please let me know if the patch doesn't solve the issue.

Happy hacking!

Christian
Index: connection.c
===================================================================
--- connection.c	(revision 25698)
+++ connection.c	(working copy)
@@ -370,6 +370,8 @@
   response = connection->response;
   if (NULL == response->crc)
     return MHD_YES;
+  if (0 == response->data_size)
+    return MHD_YES; /* 0-byte response is always ready */
   if ( (response->data_start <=
 	connection->response_write_position) &&
        (response->data_size + response->data_start >
@@ -477,10 +479,13 @@
   else
     {
       /* buffer not in range, try to fill it */
-      ret = response->crc (response->crc_cls,
-			   connection->response_write_position,
-			   &connection->write_buffer[sizeof (cbuf)],
-			   connection->write_buffer_size - sizeof (cbuf) - 2);
+      if (0 == response->data_size)
+	ret = 0; /* response must be empty, don't bother calling crc */
+      else
+	ret = response->crc (response->crc_cls,
+			     connection->response_write_position,
+			     &connection->write_buffer[sizeof (cbuf)],
+			     connection->write_buffer_size - sizeof (cbuf) - 2);
     }
   if (MHD_CONTENT_READER_END_WITH_ERROR == ret)
     {
@@ -490,7 +495,8 @@
 			      "Closing connection (error generating response)\n");
       return MHD_NO;
     }
-  if (MHD_CONTENT_READER_END_OF_STREAM == ret) 
+  if ( (MHD_CONTENT_READER_END_OF_STREAM == ret) ||
+       (0 == response->data_size) )
     {
       /* end of message, signal other side! */
       strcpy (connection->write_buffer, "0\r\n");
@@ -499,7 +505,7 @@
       response->total_size = connection->response_write_position;
       return MHD_YES;
     }
-  if (0 == ret)
+  if (0 == ret) 
     {
       connection->state = MHD_CONNECTION_CHUNKED_BODY_UNREADY;
       return MHD_NO;

Reply via email to