gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r5449 - libmicrohttpd/src/daemon


From: gnunet
Subject: [GNUnet-SVN] r5449 - libmicrohttpd/src/daemon
Date: Fri, 10 Aug 2007 17:31:07 -0600 (MDT)

Author: grothoff
Date: 2007-08-10 17:31:04 -0600 (Fri, 10 Aug 2007)
New Revision: 5449

Added:
   libmicrohttpd/src/daemon/fileserver_example.c
Modified:
   libmicrohttpd/src/daemon/Makefile.am
   libmicrohttpd/src/daemon/connection.c
   libmicrohttpd/src/daemon/daemon.c
   libmicrohttpd/src/daemon/daemontest_get.c
   libmicrohttpd/src/daemon/daemontest_post.c
   libmicrohttpd/src/daemon/daemontest_put.c
   libmicrohttpd/src/daemon/internal.h
   libmicrohttpd/src/daemon/minimal_example.c
Log:
bugfixes

Modified: libmicrohttpd/src/daemon/Makefile.am
===================================================================
--- libmicrohttpd/src/daemon/Makefile.am        2007-08-10 19:23:05 UTC (rev 
5448)
+++ libmicrohttpd/src/daemon/Makefile.am        2007-08-10 23:31:04 UTC (rev 
5449)
@@ -17,14 +17,19 @@
 
 # example programs
 
-noinst_PROGRAMS = minimal_example
+noinst_PROGRAMS = minimal_example fileserver_example
 
 minimal_example_SOURCES = \
  minimal_example.c 
 minimal_example_LDADD = \
  $(top_builddir)/src/daemon/libmicrohttpd.la 
 
+fileserver_example_SOURCES = \
+ fileserver_example.c 
+fileserver_example_LDADD = \
+ $(top_builddir)/src/daemon/libmicrohttpd.la 
 
+
 # No curl, no testcases
 if HAVE_CURL
 

Modified: libmicrohttpd/src/daemon/connection.c
===================================================================
--- libmicrohttpd/src/daemon/connection.c       2007-08-10 19:23:05 UTC (rev 
5448)
+++ libmicrohttpd/src/daemon/connection.c       2007-08-10 23:31:04 UTC (rev 
5449)
@@ -1070,7 +1070,7 @@
   /* prepare send buffer */
   if ( (response->data == NULL) ||
        (response->data_start > connection->messagePos) ||
-       (response->data_start + response->data_size < connection->messagePos) ) 
{
+       (response->data_start + response->data_size <= connection->messagePos) 
) {
     if (response->data_size == 0) {
       if (response->data != NULL)
        free(response->data);
@@ -1080,8 +1080,8 @@
     ret = response->crc(response->crc_cls,
                        connection->messagePos,
                        response->data,
-                       MAX(MHD_BUF_INC_SIZE,
-                           response->data_size - connection->messagePos));
+                       MIN(response->data_size,
+                           response->total_size - connection->messagePos));
     if (ret == -1) {
       /* end of message, signal other side by closing! */
       response->data_size = connection->messagePos;
@@ -1099,7 +1099,6 @@
       return MHD_YES;
     }
   }
-
   /* transmit */
   ret = SEND(connection->socket_fd,
             &response->data[connection->messagePos - response->data_start],
@@ -1118,9 +1117,9 @@
     return MHD_YES;
   }
   connection->messagePos += ret;
-  if (connection->messagePos > response->data_size)
+  if (connection->messagePos > response->total_size)
     abort(); /* internal error */
-  if (connection->messagePos == response->data_size) {
+  if (connection->messagePos == response->total_size) {
     if ( (connection->bodyReceived == 0) ||
         (connection->headersReceived == 0) )
       abort(); /* internal error */

Modified: libmicrohttpd/src/daemon/daemon.c
===================================================================
--- libmicrohttpd/src/daemon/daemon.c   2007-08-10 19:23:05 UTC (rev 5448)
+++ libmicrohttpd/src/daemon/daemon.c   2007-08-10 23:31:04 UTC (rev 5449)
@@ -246,9 +246,10 @@
     CLOSE(s);
     return MHD_NO;
   }
-  if (MHD_NO == daemon->apc(daemon->apc_cls,
-                           addr,
-                           addrlen)) {
+  if ( (daemon->apc != NULL) &&
+       (MHD_NO == daemon->apc(daemon->apc_cls,
+                             addr,
+                             addrlen)) ) {
     CLOSE(s);
     return MHD_YES;
   }

Modified: libmicrohttpd/src/daemon/daemontest_get.c
===================================================================
--- libmicrohttpd/src/daemon/daemontest_get.c   2007-08-10 19:23:05 UTC (rev 
5448)
+++ libmicrohttpd/src/daemon/daemontest_get.c   2007-08-10 23:31:04 UTC (rev 
5449)
@@ -35,12 +35,6 @@
 
 static int oneone;
 
-static int apc_all(void * cls,
-                  const struct sockaddr * addr,
-                  socklen_t addrlen) {
-  return MHD_YES;
-}
-
 struct CBC {
   char * buf;
   size_t pos;
@@ -98,8 +92,8 @@
   cbc.pos = 0;
   d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
                       1080,
-                      &apc_all,
                       NULL,
+                      NULL,
                       &ahc_echo,
                       "GET",
                       MHD_OPTION_END);

Modified: libmicrohttpd/src/daemon/daemontest_post.c
===================================================================
--- libmicrohttpd/src/daemon/daemontest_post.c  2007-08-10 19:23:05 UTC (rev 
5448)
+++ libmicrohttpd/src/daemon/daemontest_post.c  2007-08-10 23:31:04 UTC (rev 
5449)
@@ -41,12 +41,6 @@
 
 static int oneone;
 
-static int apc_all(void * cls,
-                  const struct sockaddr * addr,
-                  socklen_t addrlen) {
-  return MHD_YES;
-}
-
 struct CBC {
   char * buf;
   size_t pos;
@@ -121,8 +115,8 @@
   cbc.pos = 0;
   d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
                       1080,
-                      &apc_all,
                       NULL,
+                      NULL,
                       &ahc_echo,
                       NULL,
                       MHD_OPTION_END);

Modified: libmicrohttpd/src/daemon/daemontest_put.c
===================================================================
--- libmicrohttpd/src/daemon/daemontest_put.c   2007-08-10 19:23:05 UTC (rev 
5448)
+++ libmicrohttpd/src/daemon/daemontest_put.c   2007-08-10 23:31:04 UTC (rev 
5449)
@@ -34,12 +34,6 @@
 
 static int oneone;
 
-static int apc_all(void * cls,
-                  const struct sockaddr * addr,
-                  socklen_t addrlen) {
-  return MHD_YES;
-}
-
 struct CBC {
   char * buf;
   size_t pos;
@@ -131,8 +125,8 @@
   cbc.pos = 0;
   d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
                       1080,
-                      &apc_all,
                       NULL,
+                      NULL,
                       &ahc_echo,
                       &done_flag,
                       MHD_OPTION_END);

Added: libmicrohttpd/src/daemon/fileserver_example.c
===================================================================
--- libmicrohttpd/src/daemon/fileserver_example.c                               
(rev 0)
+++ libmicrohttpd/src/daemon/fileserver_example.c       2007-08-10 23:31:04 UTC 
(rev 5449)
@@ -0,0 +1,114 @@
+/*
+     This file is part of libmicrohttpd
+     (C) 2007 Christian Grothoff
+
+     libmicrohttpd is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 2, or (at your
+     option) any later version.
+
+     libmicrohttpd is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with libmicrohttpd; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file fileserver_example.c
+ * @brief minimal example for how to use libmicrohttpd to server files
+ * @author Christian Grothoff
+ */
+
+#include "config.h"
+#include <microhttpd.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#ifndef MINGW
+#include <unistd.h>
+#endif
+#include <string.h>
+#include <stdio.h>
+
+#define PAGE "<html><head><title>File not found</title></head><body>File not 
found</body></html>"
+
+static int file_reader(void * cls,
+                      size_t pos,
+                      char * buf,
+                      int max) {
+  FILE * file = cls;
+
+  fseek(file, pos, SEEK_SET);
+  return fread(buf,
+              1,
+              max,            
+              file);
+}
+
+static int ahc_echo(void * cls,
+                   struct MHD_Connection * connection,
+                   const char * url,
+                   const char * method,
+                   const char * upload_data,
+                   const char * version,
+                   unsigned int * upload_data_size) {
+  struct MHD_Response * response;
+  int ret;
+  FILE * file;
+  struct stat buf;
+
+  if (0 != strcmp(method, "GET"))
+    return MHD_NO; /* unexpected method */
+  file = fopen(&url[1], "r");
+  if (file == NULL) {
+    response = MHD_create_response_from_data(strlen(PAGE),
+                                            (void*) PAGE,
+                                            MHD_NO,
+                                            MHD_NO);
+    ret = MHD_queue_response(connection,
+                            MHD_HTTP_NOT_FOUND,
+                            response);
+    MHD_destroy_response(response);
+  } else {
+    stat(&url[1],
+        &buf);
+    response = MHD_create_response_from_callback(buf.st_size,
+                                                &file_reader,
+                                                file,
+                                                
(MHD_ContentReaderFreeCallback) &fclose);
+    ret = MHD_queue_response(connection,
+                            MHD_HTTP_OK,
+                            response);
+    MHD_destroy_response(response);
+  }
+  return ret;
+}
+
+int main(int argc,
+        char * const * argv) {
+  struct MHD_Daemon * d;
+
+  if (argc != 3) {
+    printf("%s PORT SECONDS-TO-RUN\n",
+          argv[0]);
+    return 1;
+  }
+  d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
+                      atoi(argv[1]),
+                      NULL,
+                      NULL,
+                      &ahc_echo,
+                      PAGE,
+                      MHD_OPTION_END);
+  if (d == NULL)
+    return 1;
+  sleep(atoi(argv[2]));
+  MHD_stop_daemon(d);
+  return 0;
+}
+


Property changes on: libmicrohttpd/src/daemon/fileserver_example.c
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: libmicrohttpd/src/daemon/internal.h
===================================================================
--- libmicrohttpd/src/daemon/internal.h 2007-08-10 19:23:05 UTC (rev 5448)
+++ libmicrohttpd/src/daemon/internal.h 2007-08-10 23:31:04 UTC (rev 5449)
@@ -51,6 +51,7 @@
 #include <pthread.h>
 
 #define MAX(a,b) ((a)<(b)) ? (b) : (a)
+#define MIN(a,b) ((a)<(b)) ? (a) : (b)
 
 
 /**

Modified: libmicrohttpd/src/daemon/minimal_example.c
===================================================================
--- libmicrohttpd/src/daemon/minimal_example.c  2007-08-10 19:23:05 UTC (rev 
5448)
+++ libmicrohttpd/src/daemon/minimal_example.c  2007-08-10 23:31:04 UTC (rev 
5449)
@@ -35,12 +35,6 @@
 
 #define PAGE "<html><head><title>libmicrohttpd 
demo</title></head><body>libmicrohttpd demo</body></html>"
 
-static int apc_all(void * cls,
-                  const struct sockaddr * addr,
-                  socklen_t addrlen) {
-  return MHD_YES;
-}
-
 static int ahc_echo(void * cls,
                    struct MHD_Connection * connection,
                    const char * url,
@@ -76,8 +70,8 @@
   }
   d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
                       atoi(argv[1]),
-                      &apc_all,
                       NULL,
+                      NULL,
                       &ahc_echo,
                       PAGE,
                       MHD_OPTION_END);





reply via email to

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