[vlc-devel] [RFC] httpd: allow callback to be polled until they complete

Pierre Ynard linkfanel at yahoo.fr
Wed Jul 15 16:06:28 CEST 2009


Currently, if an httpd callback needs to do some operation that could
block, it cannot. Blocking would starve other clients of the httpd.
Yet, the callback is expected to do its thing and return at once.

This patch allows such callbacks to put clients in waiting state, and be
polled until they are ready to complete. I have a use case for it in my
private branch, but I thought that it might be useful to others too, and
I'd share.

diff --git a/src/network/httpd.c b/src/network/httpd.c
index 8f4b0f8..a4a4fde 100644
--- a/src/network/httpd.c
+++ b/src/network/httpd.c
@@ -2329,7 +2329,14 @@ static void* httpd_HostThread( void *data )
                         httpd_MsgAdd( answer, "Content-Type", "%s", "text/html" );
                     }
 
-                    cl->i_state = HTTPD_CLIENT_SENDING;
+                    if( cl->answer.i_type == HTTPD_MSG_NONE )
+                    {
+                        cl->i_state = HTTPD_CLIENT_WAITING;
+                    }
+                    else
+                    {
+                        cl->i_state = HTTPD_CLIENT_SENDING;
+                    }
                 }
             }
             else if( cl->i_state == HTTPD_CLIENT_SEND_DONE )
@@ -2414,12 +2421,19 @@ static void* httpd_HostThread( void *data )
                                           &cl->answer, &cl->query );
                 if( cl->answer.i_type != HTTPD_MSG_NONE )
                 {
-                    /* we have new data, so re-enter send mode */
-                    cl->i_buffer      = 0;
-                    cl->p_buffer      = cl->answer.p_body;
-                    cl->i_buffer_size = cl->answer.i_body;
-                    cl->answer.p_body = NULL;
-                    cl->answer.i_body = 0;
+                    if( cl->answer.i_body_offset > 0 )
+                    {
+                        /* we have new data, so re-enter send mode */
+                        cl->i_buffer      = 0;
+                        cl->p_buffer      = cl->answer.p_body;
+                        cl->i_buffer_size = cl->answer.i_body;
+                        cl->answer.p_body = NULL;
+                        cl->answer.i_body = 0;
+                    }
+                    else
+                    {
+                        cl->i_buffer = -1;
+                    }
                     cl->i_state = HTTPD_CLIENT_SENDING;
                 }
             }


Regards,

-- 
Pierre Ynard
"Une âme dans un corps, c'est comme un dessin sur une feuille de papier."



More information about the vlc-devel mailing list