aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2007-08-10 23:31:04 +0000
committerChristian Grothoff <christian@grothoff.org>2007-08-10 23:31:04 +0000
commitef352bdd85a2dbcab9c800b19b5d36f5c32e2196 (patch)
treef5a931daa67571d682cbea6f3163e6d179ec42a9
parentc20800d66be396695bce32d3746f8e4d8e819d94 (diff)
downloadlibmicrohttpd-ef352bdd85a2dbcab9c800b19b5d36f5c32e2196.tar.gz
libmicrohttpd-ef352bdd85a2dbcab9c800b19b5d36f5c32e2196.zip
bugfixes
-rw-r--r--src/daemon/Makefile.am7
-rw-r--r--src/daemon/connection.c11
-rw-r--r--src/daemon/daemon.c7
-rw-r--r--src/daemon/daemontest_get.c8
-rw-r--r--src/daemon/daemontest_post.c8
-rw-r--r--src/daemon/daemontest_put.c8
-rw-r--r--src/daemon/fileserver_example.c114
-rw-r--r--src/daemon/internal.h1
-rw-r--r--src/daemon/minimal_example.c8
9 files changed, 134 insertions, 38 deletions
diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am
index 88292c88..80a8d751 100644
--- a/src/daemon/Makefile.am
+++ b/src/daemon/Makefile.am
@@ -17,13 +17,18 @@ libmicrohttpd_la_SOURCES = \
17 17
18# example programs 18# example programs
19 19
20noinst_PROGRAMS = minimal_example 20noinst_PROGRAMS = minimal_example fileserver_example
21 21
22minimal_example_SOURCES = \ 22minimal_example_SOURCES = \
23 minimal_example.c 23 minimal_example.c
24minimal_example_LDADD = \ 24minimal_example_LDADD = \
25 $(top_builddir)/src/daemon/libmicrohttpd.la 25 $(top_builddir)/src/daemon/libmicrohttpd.la
26 26
27fileserver_example_SOURCES = \
28 fileserver_example.c
29fileserver_example_LDADD = \
30 $(top_builddir)/src/daemon/libmicrohttpd.la
31
27 32
28# No curl, no testcases 33# No curl, no testcases
29if HAVE_CURL 34if HAVE_CURL
diff --git a/src/daemon/connection.c b/src/daemon/connection.c
index 4315a5bc..6da77002 100644
--- a/src/daemon/connection.c
+++ b/src/daemon/connection.c
@@ -1070,7 +1070,7 @@ MHD_connection_handle_write(struct MHD_Connection * connection) {
1070 /* prepare send buffer */ 1070 /* prepare send buffer */
1071 if ( (response->data == NULL) || 1071 if ( (response->data == NULL) ||
1072 (response->data_start > connection->messagePos) || 1072 (response->data_start > connection->messagePos) ||
1073 (response->data_start + response->data_size < connection->messagePos) ) { 1073 (response->data_start + response->data_size <= connection->messagePos) ) {
1074 if (response->data_size == 0) { 1074 if (response->data_size == 0) {
1075 if (response->data != NULL) 1075 if (response->data != NULL)
1076 free(response->data); 1076 free(response->data);
@@ -1080,8 +1080,8 @@ MHD_connection_handle_write(struct MHD_Connection * connection) {
1080 ret = response->crc(response->crc_cls, 1080 ret = response->crc(response->crc_cls,
1081 connection->messagePos, 1081 connection->messagePos,
1082 response->data, 1082 response->data,
1083 MAX(MHD_BUF_INC_SIZE, 1083 MIN(response->data_size,
1084 response->data_size - connection->messagePos)); 1084 response->total_size - connection->messagePos));
1085 if (ret == -1) { 1085 if (ret == -1) {
1086 /* end of message, signal other side by closing! */ 1086 /* end of message, signal other side by closing! */
1087 response->data_size = connection->messagePos; 1087 response->data_size = connection->messagePos;
@@ -1099,7 +1099,6 @@ MHD_connection_handle_write(struct MHD_Connection * connection) {
1099 return MHD_YES; 1099 return MHD_YES;
1100 } 1100 }
1101 } 1101 }
1102
1103 /* transmit */ 1102 /* transmit */
1104 ret = SEND(connection->socket_fd, 1103 ret = SEND(connection->socket_fd,
1105 &response->data[connection->messagePos - response->data_start], 1104 &response->data[connection->messagePos - response->data_start],
@@ -1118,9 +1117,9 @@ MHD_connection_handle_write(struct MHD_Connection * connection) {
1118 return MHD_YES; 1117 return MHD_YES;
1119 } 1118 }
1120 connection->messagePos += ret; 1119 connection->messagePos += ret;
1121 if (connection->messagePos > response->data_size) 1120 if (connection->messagePos > response->total_size)
1122 abort(); /* internal error */ 1121 abort(); /* internal error */
1123 if (connection->messagePos == response->data_size) { 1122 if (connection->messagePos == response->total_size) {
1124 if ( (connection->bodyReceived == 0) || 1123 if ( (connection->bodyReceived == 0) ||
1125 (connection->headersReceived == 0) ) 1124 (connection->headersReceived == 0) )
1126 abort(); /* internal error */ 1125 abort(); /* internal error */
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c
index 12a243ea..2fbd3abc 100644
--- a/src/daemon/daemon.c
+++ b/src/daemon/daemon.c
@@ -246,9 +246,10 @@ MHD_accept_connection(struct MHD_Daemon * daemon) {
246 CLOSE(s); 246 CLOSE(s);
247 return MHD_NO; 247 return MHD_NO;
248 } 248 }
249 if (MHD_NO == daemon->apc(daemon->apc_cls, 249 if ( (daemon->apc != NULL) &&
250 addr, 250 (MHD_NO == daemon->apc(daemon->apc_cls,
251 addrlen)) { 251 addr,
252 addrlen)) ) {
252 CLOSE(s); 253 CLOSE(s);
253 return MHD_YES; 254 return MHD_YES;
254 } 255 }
diff --git a/src/daemon/daemontest_get.c b/src/daemon/daemontest_get.c
index 2366c281..89f50461 100644
--- a/src/daemon/daemontest_get.c
+++ b/src/daemon/daemontest_get.c
@@ -35,12 +35,6 @@
35 35
36static int oneone; 36static int oneone;
37 37
38static int apc_all(void * cls,
39 const struct sockaddr * addr,
40 socklen_t addrlen) {
41 return MHD_YES;
42}
43
44struct CBC { 38struct CBC {
45 char * buf; 39 char * buf;
46 size_t pos; 40 size_t pos;
@@ -98,7 +92,7 @@ static int testInternalGet() {
98 cbc.pos = 0; 92 cbc.pos = 0;
99 d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, 93 d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
100 1080, 94 1080,
101 &apc_all, 95 NULL,
102 NULL, 96 NULL,
103 &ahc_echo, 97 &ahc_echo,
104 "GET", 98 "GET",
diff --git a/src/daemon/daemontest_post.c b/src/daemon/daemontest_post.c
index 3c69dadc..5db2d6f8 100644
--- a/src/daemon/daemontest_post.c
+++ b/src/daemon/daemontest_post.c
@@ -41,12 +41,6 @@
41 41
42static int oneone; 42static int oneone;
43 43
44static int apc_all(void * cls,
45 const struct sockaddr * addr,
46 socklen_t addrlen) {
47 return MHD_YES;
48}
49
50struct CBC { 44struct CBC {
51 char * buf; 45 char * buf;
52 size_t pos; 46 size_t pos;
@@ -121,7 +115,7 @@ static int testInternalPost() {
121 cbc.pos = 0; 115 cbc.pos = 0;
122 d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, 116 d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
123 1080, 117 1080,
124 &apc_all, 118 NULL,
125 NULL, 119 NULL,
126 &ahc_echo, 120 &ahc_echo,
127 NULL, 121 NULL,
diff --git a/src/daemon/daemontest_put.c b/src/daemon/daemontest_put.c
index dfcccb0a..7a6fe98a 100644
--- a/src/daemon/daemontest_put.c
+++ b/src/daemon/daemontest_put.c
@@ -34,12 +34,6 @@
34 34
35static int oneone; 35static int oneone;
36 36
37static int apc_all(void * cls,
38 const struct sockaddr * addr,
39 socklen_t addrlen) {
40 return MHD_YES;
41}
42
43struct CBC { 37struct CBC {
44 char * buf; 38 char * buf;
45 size_t pos; 39 size_t pos;
@@ -131,7 +125,7 @@ static int testInternalPut() {
131 cbc.pos = 0; 125 cbc.pos = 0;
132 d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG, 126 d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
133 1080, 127 1080,
134 &apc_all, 128 NULL,
135 NULL, 129 NULL,
136 &ahc_echo, 130 &ahc_echo,
137 &done_flag, 131 &done_flag,
diff --git a/src/daemon/fileserver_example.c b/src/daemon/fileserver_example.c
new file mode 100644
index 00000000..989ab7d5
--- /dev/null
+++ b/src/daemon/fileserver_example.c
@@ -0,0 +1,114 @@
1/*
2 This file is part of libmicrohttpd
3 (C) 2007 Christian Grothoff
4
5 libmicrohttpd is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
9
10 libmicrohttpd is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with libmicrohttpd; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/**
22 * @file fileserver_example.c
23 * @brief minimal example for how to use libmicrohttpd to server files
24 * @author Christian Grothoff
25 */
26
27#include "config.h"
28#include <microhttpd.h>
29#include <stdlib.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#ifndef MINGW
33#include <unistd.h>
34#endif
35#include <string.h>
36#include <stdio.h>
37
38#define PAGE "<html><head><title>File not found</title></head><body>File not found</body></html>"
39
40static int file_reader(void * cls,
41 size_t pos,
42 char * buf,
43 int max) {
44 FILE * file = cls;
45
46 fseek(file, pos, SEEK_SET);
47 return fread(buf,
48 1,
49 max,
50 file);
51}
52
53static int ahc_echo(void * cls,
54 struct MHD_Connection * connection,
55 const char * url,
56 const char * method,
57 const char * upload_data,
58 const char * version,
59 unsigned int * upload_data_size) {
60 struct MHD_Response * response;
61 int ret;
62 FILE * file;
63 struct stat buf;
64
65 if (0 != strcmp(method, "GET"))
66 return MHD_NO; /* unexpected method */
67 file = fopen(&url[1], "r");
68 if (file == NULL) {
69 response = MHD_create_response_from_data(strlen(PAGE),
70 (void*) PAGE,
71 MHD_NO,
72 MHD_NO);
73 ret = MHD_queue_response(connection,
74 MHD_HTTP_NOT_FOUND,
75 response);
76 MHD_destroy_response(response);
77 } else {
78 stat(&url[1],
79 &buf);
80 response = MHD_create_response_from_callback(buf.st_size,
81 &file_reader,
82 file,
83 (MHD_ContentReaderFreeCallback) &fclose);
84 ret = MHD_queue_response(connection,
85 MHD_HTTP_OK,
86 response);
87 MHD_destroy_response(response);
88 }
89 return ret;
90}
91
92int main(int argc,
93 char * const * argv) {
94 struct MHD_Daemon * d;
95
96 if (argc != 3) {
97 printf("%s PORT SECONDS-TO-RUN\n",
98 argv[0]);
99 return 1;
100 }
101 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
102 atoi(argv[1]),
103 NULL,
104 NULL,
105 &ahc_echo,
106 PAGE,
107 MHD_OPTION_END);
108 if (d == NULL)
109 return 1;
110 sleep(atoi(argv[2]));
111 MHD_stop_daemon(d);
112 return 0;
113}
114
diff --git a/src/daemon/internal.h b/src/daemon/internal.h
index 57e5ebf1..25d231ee 100644
--- a/src/daemon/internal.h
+++ b/src/daemon/internal.h
@@ -51,6 +51,7 @@
51#include <pthread.h> 51#include <pthread.h>
52 52
53#define MAX(a,b) ((a)<(b)) ? (b) : (a) 53#define MAX(a,b) ((a)<(b)) ? (b) : (a)
54#define MIN(a,b) ((a)<(b)) ? (a) : (b)
54 55
55 56
56/** 57/**
diff --git a/src/daemon/minimal_example.c b/src/daemon/minimal_example.c
index 5e67b981..10b54aac 100644
--- a/src/daemon/minimal_example.c
+++ b/src/daemon/minimal_example.c
@@ -35,12 +35,6 @@
35 35
36#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" 36#define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>"
37 37
38static int apc_all(void * cls,
39 const struct sockaddr * addr,
40 socklen_t addrlen) {
41 return MHD_YES;
42}
43
44static int ahc_echo(void * cls, 38static int ahc_echo(void * cls,
45 struct MHD_Connection * connection, 39 struct MHD_Connection * connection,
46 const char * url, 40 const char * url,
@@ -76,7 +70,7 @@ int main(int argc,
76 } 70 }
77 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG, 71 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
78 atoi(argv[1]), 72 atoi(argv[1]),
79 &apc_all, 73 NULL,
80 NULL, 74 NULL,
81 &ahc_echo, 75 &ahc_echo,
82 PAGE, 76 PAGE,