This commit is contained in:
Christian Grothoff
2007-08-10 23:31:04 +00:00
parent c20800d66b
commit ef352bdd85
9 changed files with 134 additions and 38 deletions
+6 -1
View File
@@ -17,13 +17,18 @@ libmicrohttpd_la_SOURCES = \
# 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
+5 -6
View File
@@ -1070,7 +1070,7 @@ MHD_connection_handle_write(struct MHD_Connection * connection) {
/* 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 @@ MHD_connection_handle_write(struct MHD_Connection * connection) {
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 @@ MHD_connection_handle_write(struct MHD_Connection * connection) {
return MHD_YES;
}
}
/* transmit */
ret = SEND(connection->socket_fd,
&response->data[connection->messagePos - response->data_start],
@@ -1118,9 +1117,9 @@ MHD_connection_handle_write(struct MHD_Connection * connection) {
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 */
+4 -3
View File
@@ -246,9 +246,10 @@ MHD_accept_connection(struct MHD_Daemon * daemon) {
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;
}
+1 -7
View File
@@ -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,7 +92,7 @@ static int testInternalGet() {
cbc.pos = 0;
d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
1080,
&apc_all,
NULL,
NULL,
&ahc_echo,
"GET",
+1 -7
View File
@@ -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,7 +115,7 @@ static int testInternalPost() {
cbc.pos = 0;
d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
1080,
&apc_all,
NULL,
NULL,
&ahc_echo,
NULL,
+1 -7
View File
@@ -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,7 +125,7 @@ static int testInternalPut() {
cbc.pos = 0;
d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
1080,
&apc_all,
NULL,
NULL,
&ahc_echo,
&done_flag,
+114
View File
@@ -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;
}
+1
View File
@@ -51,6 +51,7 @@
#include <pthread.h>
#define MAX(a,b) ((a)<(b)) ? (b) : (a)
#define MIN(a,b) ((a)<(b)) ? (a) : (b)
/**
+1 -7
View File
@@ -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,7 +70,7 @@ int main(int argc,
}
d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
atoi(argv[1]),
&apc_all,
NULL,
NULL,
&ahc_echo,
PAGE,