gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r11709 - in libmicrohttpd/src: daemon examples include


From: gnunet
Subject: [GNUnet-SVN] r11709 - in libmicrohttpd/src: daemon examples include
Date: Sun, 13 Jun 2010 10:47:17 +0200

Author: grothoff
Date: 2010-06-13 10:47:17 +0200 (Sun, 13 Jun 2010)
New Revision: 11709

Modified:
   libmicrohttpd/src/daemon/daemon.c
   libmicrohttpd/src/examples/fileserver_example.c
   libmicrohttpd/src/examples/fileserver_example_dirs.c
   libmicrohttpd/src/examples/fileserver_example_external_select.c
   libmicrohttpd/src/examples/https_fileserver_example.c
   libmicrohttpd/src/examples/minimal_example.c
   libmicrohttpd/src/examples/minimal_example_comet.c
   libmicrohttpd/src/examples/querystring_example.c
   libmicrohttpd/src/examples/refuse_post_example.c
   libmicrohttpd/src/include/microhttpd.h
Log:
clean up example code

Modified: libmicrohttpd/src/daemon/daemon.c
===================================================================
--- libmicrohttpd/src/daemon/daemon.c   2010-06-13 08:07:01 UTC (rev 11708)
+++ libmicrohttpd/src/daemon/daemon.c   2010-06-13 08:47:17 UTC (rev 11709)
@@ -1254,6 +1254,14 @@
           break;
         case MHD_OPTION_THREAD_POOL_SIZE:
           daemon->worker_pool_size = va_arg (ap, unsigned int);
+         if (daemon->worker_pool_size >= SIZE_MAX / sizeof (struct MHD_Daemon))
+           {
+#if HAVE_MESSAGES
+             FPRINTF (stderr,
+                      "Specified thread pool size too big\n");
+#endif
+             return MHD_NO;
+           }
           break;
 #if HTTPS_SUPPORT
         case MHD_OPTION_PROTOCOL_VERSION:

Modified: libmicrohttpd/src/examples/fileserver_example.c
===================================================================
--- libmicrohttpd/src/examples/fileserver_example.c     2010-06-13 08:07:01 UTC 
(rev 11708)
+++ libmicrohttpd/src/examples/fileserver_example.c     2010-06-13 08:47:17 UTC 
(rev 11709)
@@ -38,6 +38,13 @@
   return fread (buf, 1, max, file);
 }
 
+static void
+free_callback (void *cls)
+{
+  FILE *file = cls;
+  fclose (file);
+}
+
 static int
 ahc_echo (void *cls,
           struct MHD_Connection *connection,
@@ -62,7 +69,10 @@
       return MHD_YES;
     }
   *ptr = NULL;                  /* reset when done */
-  file = fopen (&url[1], "rb");
+  if (0 == stat (&url[1], &buf))
+    file = fopen (&url[1], "rb");
+  else
+    file = NULL;
   if (file == NULL)
     {
       response = MHD_create_response_from_data (strlen (PAGE),
@@ -73,12 +83,15 @@
     }
   else
     {
-      stat (&url[1], &buf);
       response = MHD_create_response_from_callback (buf.st_size, 32 * 1024,    
 /* 32k page size */
                                                     &file_reader,
                                                     file,
-                                                    
(MHD_ContentReaderFreeCallback)
-                                                    & fclose);
+                                                    &free_callback);
+      if (response == NULL)
+       {
+         fclose (file);
+         return MHD_NO;
+       }
       ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
       MHD_destroy_response (response);
     }
@@ -90,9 +103,9 @@
 {
   struct MHD_Daemon *d;
 
-  if (argc != 3)
+  if (argc != 2)
     {
-      printf ("%s PORT SECONDS-TO-RUN\n", argv[0]);
+      printf ("%s PORT\n", argv[0]);
       return 1;
     }
   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
@@ -100,7 +113,7 @@
                         NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
   if (d == NULL)
     return 1;
-  sleep (atoi (argv[2]));
+  getc (stdin);
   MHD_stop_daemon (d);
   return 0;
 }

Modified: libmicrohttpd/src/examples/fileserver_example_dirs.c
===================================================================
--- libmicrohttpd/src/examples/fileserver_example_dirs.c        2010-06-13 
08:07:01 UTC (rev 11708)
+++ libmicrohttpd/src/examples/fileserver_example_dirs.c        2010-06-13 
08:47:17 UTC (rev 11709)
@@ -39,16 +39,32 @@
   return fread (buf, 1, max, file);
 }
 
+static void
+file_free_callback (void *cls)
+{
+  FILE *file = cls;
+  fclose (file);
+}
 
+static void
+dir_free_callback (void *cls)
+{
+  DIR *dir = cls;
+  if (dir != NULL)
+    closedir (dir);
+}
+
 static int
 dir_reader (void *cls, uint64_t pos, char *buf, int max)
 {
+  DIR *dir = cls;
   struct dirent *e;
+
   if (max < 512)
     return 0;
   do
     {
-      e = readdir (cls);
+      e = readdir (dir);
       if (e == NULL)
         return -1;
   } while (e->d_name[0] == '.');
@@ -72,7 +88,9 @@
   struct MHD_Response *response;
   int ret;
   FILE *file;
+  DIR *dir;
   struct stat buf;
+  char emsg[1024];
 
   if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
     return MHD_NO;              /* unexpected method */
@@ -86,13 +104,39 @@
   file = fopen (&url[1], "rb");
   if (file == NULL)
     {
-      response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
-                                                   32 * 1024,
-                                                   &dir_reader,
-                                                   opendir ("."),
-                                                   
(MHD_ContentReaderFreeCallback) &closedir);
-      ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
-      MHD_destroy_response (response);
+      dir = opendir (".");
+      if (dir == NULL)
+       {
+         /* most likely cause: more concurrent requests than  
+            available file descriptors / 2 */
+         snprintf (emsg,
+                   sizeof (emsg),
+                   "Failed to open directory `.': %s\n",
+                   strerror (errno));
+         response = MHD_create_response_from_data (strlen (emsg),
+                                                   emsg,
+                                                   MHD_NO,
+                                                   MHD_YES);
+         if (response == NULL)
+           return MHD_NO;          
+         ret = MHD_queue_response (connection, MHD_HTTP_SERVICE_UNAVAILABLE, 
response);
+         MHD_destroy_response (response);
+       }
+      else
+       {
+         response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
+                                                       32 * 1024,
+                                                       &dir_reader,
+                                                       dir,
+                                                       &dir_free_callback);
+         if (response == NULL)
+           {
+             closedir (dir);
+             return MHD_NO;
+           }
+         ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
+         MHD_destroy_response (response);
+       }
     }
   else
     {
@@ -100,8 +144,7 @@
       response = MHD_create_response_from_callback (buf.st_size, 32 * 1024,    
 /* 32k page size */
                                                     &file_reader,
                                                     file,
-                                                    
(MHD_ContentReaderFreeCallback)
-                                                    & fclose);
+                                                    &file_free_callback);
       ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
       MHD_destroy_response (response);
     }
@@ -123,7 +166,7 @@
                         NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
   if (d == NULL)
     return 1;
-  while (1) sleep (1);
+  getc (stdin);
   MHD_stop_daemon (d);
   return 0;
 }

Modified: libmicrohttpd/src/examples/fileserver_example_external_select.c
===================================================================
--- libmicrohttpd/src/examples/fileserver_example_external_select.c     
2010-06-13 08:07:01 UTC (rev 11708)
+++ libmicrohttpd/src/examples/fileserver_example_external_select.c     
2010-06-13 08:47:17 UTC (rev 11709)
@@ -34,10 +34,17 @@
 {
   FILE *file = cls;
 
-  fseek (file, pos, SEEK_SET);
+  (void) fseek (file, pos, SEEK_SET);
   return fread (buf, 1, max, file);
 }
 
+static void
+free_callback (void *cls)
+{
+  FILE *file = cls;
+  fclose (file);
+}
+
 static int
 ahc_echo (void *cls,
           struct MHD_Connection *connection,
@@ -77,8 +84,12 @@
       response = MHD_create_response_from_callback (buf.st_size, 32 * 1024,    
 /* 32k page size */
                                                     &file_reader,
                                                     file,
-                                                    
(MHD_ContentReaderFreeCallback)
-                                                    & fclose);
+                                                    &free_callback);
+      if (response == NULL)
+       {
+         fclose (file);
+         return MHD_NO;
+       }
       ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
       MHD_destroy_response (response);
     }

Modified: libmicrohttpd/src/examples/https_fileserver_example.c
===================================================================
--- libmicrohttpd/src/examples/https_fileserver_example.c       2010-06-13 
08:07:01 UTC (rev 11708)
+++ libmicrohttpd/src/examples/https_fileserver_example.c       2010-06-13 
08:47:17 UTC (rev 11709)
@@ -158,7 +158,7 @@
 {
   struct MHD_Daemon *TLS_daemon;
 
-  if (argc == 3)
+  if (argc == 2)
     {
       /* TODO check if this is truly necessary -  disallow usage of the 
blocking /dev/random */
       /* gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0); */
@@ -172,7 +172,7 @@
     }
   else
     {
-      printf ("Usage: %s HTTP-PORT SECONDS-TO-RUN\n", argv[0]);
+      printf ("Usage: %s HTTP-PORT\n", argv[0]);
       return 1;
     }
 
@@ -186,7 +186,7 @@
       printf ("MHD daemon listening on port %d\n", atoi (argv[1]));
     }
 
-  sleep (atoi (argv[2]));
+  getc (stdin);
 
   MHD_stop_daemon (TLS_daemon);
 

Modified: libmicrohttpd/src/examples/minimal_example.c
===================================================================
--- libmicrohttpd/src/examples/minimal_example.c        2010-06-13 08:07:01 UTC 
(rev 11708)
+++ libmicrohttpd/src/examples/minimal_example.c        2010-06-13 08:47:17 UTC 
(rev 11709)
@@ -61,9 +61,9 @@
 {
   struct MHD_Daemon *d;
 
-  if (argc != 3)
+  if (argc != 2)
     {
-      printf ("%s PORT SECONDS-TO-RUN\n", argv[0]);
+      printf ("%s PORT\n", argv[0]);
       return 1;
     }
   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
@@ -71,7 +71,7 @@
                         NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
   if (d == NULL)
     return 1;
-  sleep (atoi (argv[2]));
+  getc (stdin);
   MHD_stop_daemon (d);
   return 0;
 }

Modified: libmicrohttpd/src/examples/minimal_example_comet.c
===================================================================
--- libmicrohttpd/src/examples/minimal_example_comet.c  2010-06-13 08:07:01 UTC 
(rev 11708)
+++ libmicrohttpd/src/examples/minimal_example_comet.c  2010-06-13 08:47:17 UTC 
(rev 11709)
@@ -69,9 +69,9 @@
 {
   struct MHD_Daemon *d;
 
-  if (argc != 3)
+  if (argc != 2)
     {
-      printf ("%s PORT SECONDS-TO-RUN\n", argv[0]);
+      printf ("%s PORT\n", argv[0]);
       return 1;
     }
   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
@@ -79,7 +79,7 @@
                         NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
   if (d == NULL)
     return 1;
-  sleep (atoi (argv[2]));
+  getc (stdin);
   MHD_stop_daemon (d);
   return 0;
 }

Modified: libmicrohttpd/src/examples/querystring_example.c
===================================================================
--- libmicrohttpd/src/examples/querystring_example.c    2010-06-13 08:07:01 UTC 
(rev 11708)
+++ libmicrohttpd/src/examples/querystring_example.c    2010-06-13 08:47:17 UTC 
(rev 11709)
@@ -58,6 +58,11 @@
     return MHD_NO;
   sprintf (me, fmt, "q", val);
   response = MHD_create_response_from_data (strlen (me), me, MHD_YES, MHD_NO);
+  if (response == NULL)
+    {
+      free (me);
+      return MHD_NO;
+    }
   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
   MHD_destroy_response (response);
   return ret;
@@ -68,9 +73,9 @@
 {
   struct MHD_Daemon *d;
 
-  if (argc != 3)
+  if (argc != 2)
     {
-      printf ("%s PORT SECONDS-TO-RUN\n", argv[0]);
+      printf ("%s PORT\n", argv[0]);
       return 1;
     }
   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
@@ -78,7 +83,7 @@
                         NULL, NULL, &ahc_echo, PAGE, MHD_OPTION_END);
   if (d == NULL)
     return 1;
-  sleep (atoi (argv[2]));
+  getc (stdin);
   MHD_stop_daemon (d);
   return 0;
 }

Modified: libmicrohttpd/src/examples/refuse_post_example.c
===================================================================
--- libmicrohttpd/src/examples/refuse_post_example.c    2010-06-13 08:07:01 UTC 
(rev 11708)
+++ libmicrohttpd/src/examples/refuse_post_example.c    2010-06-13 08:47:17 UTC 
(rev 11709)
@@ -80,9 +80,9 @@
 {
   struct MHD_Daemon *d;
 
-  if (argc != 3)
+  if (argc != 2)
     {
-      printf ("%s PORT SECONDS-TO-RUN\n", argv[0]);
+      printf ("%s PORT\n", argv[0]);
       return 1;
     }
   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
@@ -91,7 +91,7 @@
                         MHD_OPTION_END);
   if (d == NULL)
     return 1;
-  sleep (atoi (argv[2]));
+  getc (stdin);
   MHD_stop_daemon (d);
   return 0;
 }

Modified: libmicrohttpd/src/include/microhttpd.h
===================================================================
--- libmicrohttpd/src/include/microhttpd.h      2010-06-13 08:07:01 UTC (rev 
11708)
+++ libmicrohttpd/src/include/microhttpd.h      2010-06-13 08:47:17 UTC (rev 
11709)
@@ -1,6 +1,6 @@
 /*
      This file is part of libmicrohttpd
-     (C) 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing 
authors)
+     (C) 2006, 2007, 2008, 2009, 2010 Christian Grothoff (and other 
contributing authors)
 
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public
@@ -97,7 +97,11 @@
  * Constant used to indicate unknown size (use when
  * creating a response).
  */
+#ifdef UINT64_MAX
+#define MHD_SIZE_UNKNOWN UINT64_MAX
+#else
 #define MHD_SIZE_UNKNOWN  ((uint64_t) -1LL)
+#endif
 
 /**
  * HTTP response codes.




reply via email to

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