diff options
Diffstat (limited to 'nanohttp')
-rw-r--r-- | nanohttp/nanohttp-admin.c | 17 | ||||
-rw-r--r-- | nanohttp/nanohttp-client.c | 44 | ||||
-rw-r--r-- | nanohttp/nanohttp-client.h | 8 | ||||
-rw-r--r-- | nanohttp/nanohttp-common.h | 106 | ||||
-rw-r--r-- | nanohttp/nanohttp-logging.h | 4 | ||||
-rwxr-xr-x | nanohttp/nanohttp-mime.c | 8 | ||||
-rwxr-xr-x | nanohttp/nanohttp-mime.h | 8 | ||||
-rwxr-xr-x | nanohttp/nanohttp-request.c | 43 | ||||
-rwxr-xr-x | nanohttp/nanohttp-request.h | 48 | ||||
-rwxr-xr-x | nanohttp/nanohttp-response.c | 4 | ||||
-rwxr-xr-x | nanohttp/nanohttp-response.h | 30 | ||||
-rw-r--r-- | nanohttp/nanohttp-server.c | 257 | ||||
-rw-r--r-- | nanohttp/nanohttp-server.h | 98 | ||||
-rw-r--r-- | nanohttp/nanohttp-socket.c | 88 | ||||
-rw-r--r-- | nanohttp/nanohttp-socket.h | 258 | ||||
-rw-r--r-- | nanohttp/nanohttp-ssl.c | 111 | ||||
-rw-r--r-- | nanohttp/nanohttp-ssl.h | 46 | ||||
-rwxr-xr-x | nanohttp/nanohttp-stream.c | 57 | ||||
-rwxr-xr-x | nanohttp/nanohttp-stream.h | 356 |
19 files changed, 720 insertions, 871 deletions
diff --git a/nanohttp/nanohttp-admin.c b/nanohttp/nanohttp-admin.c index 83c2931..746855b 100644 --- a/nanohttp/nanohttp-admin.c +++ b/nanohttp/nanohttp-admin.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-admin.c,v 1.3 2006/11/21 20:59:02 m0gg Exp $ +* $Id: nanohttp-admin.c,v 1.4 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -25,10 +25,6 @@ #include <config.h> #endif -#ifdef HAVE_SYS_TIME_H -#include <sys/time.h> -#endif - #ifdef HAVE_STDIO_H #include <stdio.h> #endif @@ -41,17 +37,12 @@ #include <pthread.h> #endif -#ifdef HAVE_NETINET_IN_H -#include <netinet/in.h> -#endif - #include "nanohttp-common.h" -#include "nanohttp-socket.h" #include "nanohttp-stream.h" #include "nanohttp-request.h" #include "nanohttp-server.h" -#include "nanohttp-admin.h" +#include "nanohttp-admin.h" static void _httpd_admin_send_title(httpd_conn_t *conn, const char *title) @@ -136,7 +127,7 @@ _httpd_admin_list_statistics(httpd_conn_t *conn, const char *service_name) static void -_httpd_admin_handle_get(httpd_conn_t * conn, hrequest_t * req) +_httpd_admin_handle_get(httpd_conn_t * conn, struct hrequest_t *req) { char *param; @@ -165,7 +156,7 @@ _httpd_admin_handle_get(httpd_conn_t * conn, hrequest_t * req) static void -_httpd_admin_entry(httpd_conn_t * conn, hrequest_t * req) +_httpd_admin_entry(httpd_conn_t * conn, struct hrequest_t *req) { if (req->method == HTTP_REQUEST_GET) { diff --git a/nanohttp/nanohttp-client.c b/nanohttp/nanohttp-client.c index a105b82..f8efeaf 100644 --- a/nanohttp/nanohttp-client.c +++ b/nanohttp/nanohttp-client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-client.c,v 1.43 2006/11/21 08:34:34 m0gg Exp $ +* $Id: nanohttp-client.c,v 1.44 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -45,6 +45,10 @@ #include <stdarg.h> #endif +#ifdef HAVE_ERRNO_H +#include <errno.h> +#endif + #ifdef HAVE_STRING_H #include <string.h> #endif @@ -60,6 +64,7 @@ #include "nanohttp-common.h" #include "nanohttp-socket.h" #include "nanohttp-stream.h" +#include "nanohttp-request.h" #include "nanohttp-response.h" #include "nanohttp-base64.h" #include "nanohttp-logging.h" @@ -102,9 +107,19 @@ httpc_new(void) httpc_conn_t *res; if (!(res = (httpc_conn_t *) malloc(sizeof(httpc_conn_t)))) + { + log_error2("malloc failed (%s)", strerror(errno)); + return NULL; + } + + if (!(res->sock = (struct hsocket_t *)malloc(sizeof(struct hsocket_t)))) + { + log_error2("malloc failed (%s)", strerror(errno)); + free(res); return NULL; + } - if ((status = hsocket_init(&res->sock)) != H_OK) + if ((status = hsocket_init(res->sock)) != H_OK) { log_warn2("hsocket_init failed (%s)", herror_message(status)); return NULL; @@ -145,7 +160,8 @@ httpc_free(httpc_conn_t * conn) conn->out = NULL; } - hsocket_free(&(conn->sock)); + hsocket_free(conn->sock); + free(conn->sock); free(conn); return; @@ -161,7 +177,7 @@ httpc_close_free(httpc_conn_t * conn) if (conn == NULL) return; - hsocket_close(&(conn->sock)); + hsocket_close(conn->sock); httpc_free(conn); return; @@ -302,12 +318,12 @@ httpc_send_header(httpc_conn_t * conn) if (walker->key && walker->value) { sprintf(buffer, "%s: %s\r\n", walker->key, walker->value); - if ((status = hsocket_send(&(conn->sock), buffer)) != H_OK) + if ((status = hsocket_send(conn->sock, buffer)) != H_OK) return status; } } - return hsocket_send(&(conn->sock), "\r\n"); + return hsocket_send(conn->sock, "\r\n"); } /*-------------------------------------------------- @@ -386,7 +402,7 @@ httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, ssl = url.protocol == PROTOCOL_HTTPS ? 1 : 0; /* Open connection */ - if ((status = hsocket_open(&conn->sock, url.host, url.port, ssl)) != H_OK) + if ((status = hsocket_open(conn->sock, url.host, url.port, ssl)) != H_OK) return status; switch(method) @@ -413,10 +429,10 @@ httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, } log_verbose1("Sending request..."); - if ((status = hsocket_send(&(conn->sock), buffer)) != H_OK) + if ((status = hsocket_send(conn->sock, buffer)) != H_OK) { log_error2("Cannot send request (%s)", herror_message(status)); - hsocket_close(&(conn->sock)); + hsocket_close(conn->sock); return status; } @@ -424,7 +440,7 @@ httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, if ((status = httpc_send_header(conn)) != H_OK) { log_error2("Cannot send header (%s)", herror_message(status)); - hsocket_close(&(conn->sock)); + hsocket_close(conn->sock); return status; } @@ -443,7 +459,7 @@ httpc_get(httpc_conn_t * conn, hresponse_t ** out, const char *urlstr) if ((status = httpc_talk_to_server(HTTP_REQUEST_GET, conn, urlstr)) != H_OK) return status; - if ((status = hresponse_new_from_socket(&(conn->sock), out)) != H_OK) + if ((status = hresponse_new_from_socket(conn->sock, out)) != H_OK) return status; return H_OK; @@ -462,7 +478,7 @@ httpc_post_begin(httpc_conn_t * conn, const char *url) if ((status = httpc_talk_to_server(HTTP_REQUEST_POST, conn, url)) != H_OK) return status; - conn->out = http_output_stream_new(&(conn->sock), conn->header); + conn->out = http_output_stream_new(conn->sock, conn->header); return H_OK; } @@ -481,7 +497,7 @@ httpc_post_end(httpc_conn_t * conn, hresponse_t ** out) if ((status = http_output_stream_flush(conn->out)) != H_OK) return status; - if ((status = hresponse_new_from_socket(&(conn->sock), out)) != H_OK) + if ((status = hresponse_new_from_socket(conn->sock, out)) != H_OK) return status; return H_OK; @@ -598,7 +614,7 @@ httpc_mime_end(httpc_conn_t * conn, hresponse_t ** out) if ((status = http_output_stream_flush(conn->out)) != H_OK) return status; - if ((status = hresponse_new_from_socket(&(conn->sock), out)) != H_OK) + if ((status = hresponse_new_from_socket(conn->sock, out)) != H_OK) return status; return H_OK; diff --git a/nanohttp/nanohttp-client.h b/nanohttp/nanohttp-client.h index 2ef96a8..7a1cc79 100644 --- a/nanohttp/nanohttp-client.h +++ b/nanohttp/nanohttp-client.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-client.h,v 1.25 2006/11/19 09:40:14 m0gg Exp $ + * $Id: nanohttp-client.h,v 1.26 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -26,7 +26,7 @@ typedef struct httpc_conn { - hsocket_t sock; + struct hsocket_t *sock; hpair_t *header; hurl_t url; http_version_t version; @@ -36,7 +36,7 @@ typedef struct httpc_conn long _dime_sent_bytes; int errcode; char errmsg[150]; - http_output_stream_t *out; + struct http_output_stream_t *out; int id; /* uniq id */ } httpc_conn_t; @@ -61,7 +61,7 @@ extern "C" { * * @see httpc_destroy, herror_t, soap_client_init_args */ -herror_t httpc_init(int argc, char *argv[]); +herror_t httpc_init(int argc, char **argv); /** * diff --git a/nanohttp/nanohttp-common.h b/nanohttp/nanohttp-common.h index bf54a12..cc9f8f8 100644 --- a/nanohttp/nanohttp-common.h +++ b/nanohttp/nanohttp-common.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-common.h,v 1.32 2006/11/19 09:40:14 m0gg Exp $ + * $Id: nanohttp-common.h,v 1.33 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -21,8 +21,8 @@ * * Email: ferhatayaz@yahoo.com ******************************************************************/ -#ifndef NANO_HTTP_COMMON_H -#define NANO_HTTP_COMMON_H +#ifndef __nanohttp_common_h +#define __nanohttp_common_h #define HEADER_CONTENT_ID "Content-Id" #define HEADER_CONTENT_TRANSFER_ENCODING "Content-Transfer-Encoding" @@ -69,75 +69,14 @@ #define HEADER_EXPIRES "Expires" #define HEADER_LAST_MODIFIED "Last-Modified" - /** * - * XXX: move to nanohttp-response.h - * - * Response Header Fields - * - * The response-header fields allow the server to pass additional information - * about the response which cannot be placed in the Status-Line. These header - * fields give information about the server and about further access to the - * resource identified by the Request-URI. (see RFC2616) + * Command line arguments for client and server. * */ -#define HEADER_ACCEPT_RANGES "Accept-Ranges" -#define HEADER_AGE "Age" -#define HEADER_EXTENSION_TAG "ETag" -#define HEADER_LOCATION "Location" -#define HEADER_PROXY_AUTHENTICATE "Proxy-Authenticate" -#define HEADER_RETRY_AFTER "Retry-After" -#define HEADER_SERVER "Server" -#define HEADER_VARY "Vary" -#define HEADER_WWW_AUTHENTICATE "WWW-Authenticate" - -/** - * - * XXX: move to nanohttp-request.h - * - * Request Header Fields - * - * The request-header fields allow the client to pass additional information - * about the request, and about the client itself, to the server. These fields - * act as request modifiers, with semantics equivalent to the parameters on a - * programming language method invocation (see RFC2616). - * - */ -#define HEADER_ACCEPT "Accept" -#define HEADER_CHARSET "Accept-Charset" -#define HEADER_ACCEPT_ENCODING "Accept-Encoding" -#define HEADER_ACCEPT_LANGUAGE "Accept-Language" -#define HEADER_AUTHORIZATION "Authorization" -#define HEADER_EXPECT "Expect" -#define HEADER_FROM "From" -#define HEADER_HOST "Host" -#define HEADER_IF_MATCH "If-Match" -#define HEADER_IF_MODIFIED_SINCE "If-Modified-Since" -#define HEADER_IF_NONE_MATCH "If-None-Match" -#define HEADER_IF_RANGE "If-Range" -#define HEADER_IF_UNMODIFIED_SINCE "If-Unmodified-Since" -#define HEADER_IF_MAX_FORWARDS "Max-Forwards" -#define HEADER_PROXY_AUTHORIZATION "Proxy-Authorization" -#define HEADER_RANGE "Range" -#define HEADER_REFERER "Referer" -#define HEADER_TRANSFER_EXTENSION "TE" -#define HEADER_USER_AGENT "User-Agent" - -/** - * - * nanohttp command line flags - * - */ -#define NHTTPD_ARG_PORT "-NHTTPport" -#define NHTTPD_ARG_TERMSIG "-NHTTPtsig" -#define NHTTPD_ARG_MAXCONN "-NHTTPmaxconn" -#define NHTTPD_ARG_TIMEOUT "-NHTTPtimeout" - #define NHTTP_ARG_CERT "-NHTTPcert" #define NHTTP_ARG_CERTPASS "-NHTTPcertpass" #define NHTTP_ARG_CA "-NHTTPCA" -#define NHTTP_ARG_HTTPS "-NHTTPS" #ifndef SAVE_STR #define SAVE_STR(str) ((str==0)?("(null)"):(str)) @@ -146,27 +85,13 @@ #define BOUNDARY_LENGTH 18 #define MAX_HEADER_SIZE 4256 -#define MAX_SOCKET_BUFFER_SIZE 4256 #define MAX_FILE_BUFFER_SIZE 4256 -#define URL_MAX_HOST_SIZE 120 -#define URL_MAX_CONTEXT_SIZE 1024 - -#define HSOCKET_MAX_BUFSIZE 1024 - #define REQUEST_MAX_PATH_SIZE 1024 #define RESPONSE_MAX_DESC_SIZE 1024 - -/* - DIME common stuff -*/ -#define DIME_VERSION_1 0x08 -#define DIME_FIRST_PACKAGE 0x04 -#define DIME_LAST_PACKAGE 0x02 -#define DIME_CHUNKED 0x01 -#define DIME_TYPE_URI 0x2 - - + +#define URL_MAX_HOST_SIZE 120 +#define URL_MAX_CONTEXT_SIZE 1024 /* TODO (#1#): find proper ports */ #define URL_DEFAULT_PORT_HTTP 80 @@ -234,15 +159,6 @@ #define HSSL_ERROR_SERVER 1760 #define HSSL_ERROR_CONNECT 1770 -/* -Set Sleep function platform depended -*/ -#ifdef WIN32 -#define system_sleep(seconds) Sleep(seconds*1000); -#else -#define system_sleep(seconds) sleep(seconds); -#endif - /** Indicates the version of the used HTTP protocol. @@ -275,12 +191,6 @@ typedef enum _hreq_method extern "C" { #endif -#ifdef WIN32 -#include <string.h> -char *strtok_r(char *s, const char *delim, char **save_ptr); -struct tm *localtime_r(const time_t * const timep, struct tm *p_tm); -#endif - typedef void *herror_t; herror_t herror_new(const char *func, int errcode, const char *format, ...); @@ -543,7 +453,7 @@ typedef struct _attachments part_t *root_part; } attachments_t; -attachments_t *attachments_new(); /* should be used internally */ +attachments_t *attachments_new(void); /* should be used internally */ /* Free a attachment. Create attachments with MIME diff --git a/nanohttp/nanohttp-logging.h b/nanohttp/nanohttp-logging.h index a687717..7bc1076 100644 --- a/nanohttp/nanohttp-logging.h +++ b/nanohttp/nanohttp-logging.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-logging.h,v 1.2 2006/08/28 10:39:52 m0gg Exp $ + * $Id: nanohttp-logging.h,v 1.3 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2006 Ferhat Ayaz @@ -47,7 +47,7 @@ extern log_level_t hlog_set_level(log_level_t level); extern log_level_t hlog_get_level(void); extern void hlog_set_file(const char *filename); -extern char *hlog_get_file(); +extern char *hlog_get_file(void); #ifdef WIN32 #if defined(_MSC_VER) && _MSC_VER <= 1200 diff --git a/nanohttp/nanohttp-mime.c b/nanohttp/nanohttp-mime.c index a853486..1736e3b 100755 --- a/nanohttp/nanohttp-mime.c +++ b/nanohttp/nanohttp-mime.c @@ -3,7 +3,7 @@ * | \/ | | | | \/ | | _/ * |_''_| |_| |_''_| |_'/ PARSER * -* $Id: nanohttp-mime.c,v 1.15 2006/11/21 08:34:34 m0gg Exp $ +* $Id: nanohttp-mime.c,v 1.16 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -518,7 +518,7 @@ MIME_read_status mime_streamreader_function(void *userdata, unsigned char *dest, int *size) { int readed = 0; - http_input_stream_t *in = (http_input_stream_t *) userdata; + struct http_input_stream_t *in = (struct http_input_stream_t *) userdata; if (!http_input_stream_is_ready(in)) return MIME_READ_EOF; @@ -799,7 +799,7 @@ _mime_received_bytes(void *data, const unsigned char *bytes, int size) */ attachments_t * -mime_message_parse(http_input_stream_t * in, const char *root_id, +mime_message_parse(struct http_input_stream_t * in, const char *root_id, const char *boundary, const char *dest_dir) { MIME_parser_status status; @@ -896,7 +896,7 @@ mime_message_parse_from_file(FILE * in, const char *root_id, } herror_t -mime_get_attachments(content_type_t * ctype, http_input_stream_t * in, +mime_get_attachments(content_type_t * ctype, struct http_input_stream_t * in, attachments_t ** dest) { /* MIME variables */ diff --git a/nanohttp/nanohttp-mime.h b/nanohttp/nanohttp-mime.h index 7d718d9..655c7e2 100755 --- a/nanohttp/nanohttp-mime.h +++ b/nanohttp/nanohttp-mime.h @@ -3,7 +3,7 @@ * | \/ | | | | \/ | | _/ * |_''_| |_| |_''_| |_'/ PARSER * -* $Id: nanohttp-mime.h,v 1.9 2006/11/19 09:40:14 m0gg Exp $ +* $Id: nanohttp-mime.h,v 1.10 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -37,11 +37,7 @@ extern "C" { "multipart/related" MIME Message Builder ------------------------------------------------------------------*/ - - -herror_t mime_get_attachments(content_type_t * ctype, - http_input_stream_t * in, - attachments_t ** dest); +extern herror_t mime_get_attachments(content_type_t * ctype, struct http_input_stream_t * in, attachments_t ** dest); #ifdef __cplusplus } diff --git a/nanohttp/nanohttp-request.c b/nanohttp/nanohttp-request.c index 30c79df..0fd1d7e 100755 --- a/nanohttp/nanohttp-request.c +++ b/nanohttp/nanohttp-request.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-request.c,v 1.17 2006/11/21 20:59:02 m0gg Exp $ +* $Id: nanohttp-request.c,v 1.18 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -56,24 +56,24 @@ #include "nanohttp-mime.h" #include "nanohttp-request.h" - -static hrequest_t * +static struct hrequest_t * hrequest_new(void) { - hrequest_t *req; + struct hrequest_t *req; - if (!(req = (hrequest_t *) malloc(sizeof(hrequest_t)))) { - + if (!(req = (struct hrequest_t *) malloc(sizeof(struct hrequest_t)))) + { log_error2("malloc failed (%s)", strerror(errno)); return NULL; } - if (!(req->statistics = (struct request_statistics *)malloc(sizeof(struct request_statistics)))) { - + if (!(req->statistics = (struct request_statistics *)malloc(sizeof(struct request_statistics)))) + { log_error2("malloc failed (%s)", strerror(errno)); free(req); return NULL; } + if (gettimeofday(&(req->statistics->time), NULL) < 0) log_error2("gettimeofday failed (%s)", strerror(errno)); @@ -88,10 +88,10 @@ hrequest_new(void) return req; } -static hrequest_t * +static struct hrequest_t * _hrequest_parse_header(char *data) { - hrequest_t *req; + struct hrequest_t *req; hpair_t *hpair = NULL, *qpair = NULL, *tmppair = NULL; char *tmp; @@ -214,20 +214,12 @@ _hrequest_parse_header(char *data) qpair->next = NULL; qpair->key = strdup(opt_key); qpair->value = strdup(opt_value); - } } } } else { - - /* parse "key: value" */ - /* tmp2 = result; key = (char *) strtok_r(tmp2, ": ", &saveptr2); value - = saveptr2; */ - - /* create pair */ -/* tmppair = (hpair_t *) malloc(sizeof(hpair_t));*/ tmppair = hpairnode_parse(result, ":", NULL); if (req->header == NULL) @@ -239,13 +231,6 @@ _hrequest_parse_header(char *data) hpair->next = tmppair; hpair = tmppair; } - - /* fill pairnode_t struct */ - /* - hpair->next = NULL; hpair->key = (char *) malloc(strlen(key) + 1); - hpair->value = (char *) malloc(strlen(value) + 1); - - strcpy(hpair->key, key); strcpy(hpair->value, value); */ } } @@ -257,9 +242,8 @@ _hrequest_parse_header(char *data) return req; } - void -hrequest_free(hrequest_t * req) +hrequest_free(struct hrequest_t * req) { if (req == NULL) return; @@ -284,13 +268,12 @@ hrequest_free(hrequest_t * req) return; } - herror_t -hrequest_new_from_socket(hsocket_t *sock, hrequest_t ** out) +hrequest_new_from_socket(struct hsocket_t *sock, struct hrequest_t ** out) { int i, readed; herror_t status; - hrequest_t *req; + struct hrequest_t *req; char buffer[MAX_HEADER_SIZE + 1]; attachments_t *mimeMessage; diff --git a/nanohttp/nanohttp-request.h b/nanohttp/nanohttp-request.h index ba1b907..2234e99 100755 --- a/nanohttp/nanohttp-request.h +++ b/nanohttp/nanohttp-request.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-request.h,v 1.9 2006/11/21 20:59:02 m0gg Exp $ + * $Id: nanohttp-request.h,v 1.10 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -21,8 +21,38 @@ * * Email: ferhatayaz@yahoo.com ******************************************************************/ -#ifndef NANO_HTTP_REQUEST_H -#define NANO_HTTP_REQUEST_H +#ifndef __nhttp_request_h +#define __nhttp_request_h + +/** + * + * Request Header Fields + * + * The request-header fields allow the client to pass additional information + * about the request, and about the client itself, to the server. These fields + * act as request modifiers, with semantics equivalent to the parameters on a + * programming language method invocation (see RFC2616). + * + */ +#define HEADER_ACCEPT "Accept" +#define HEADER_CHARSET "Accept-Charset" +#define HEADER_ACCEPT_ENCODING "Accept-Encoding" +#define HEADER_ACCEPT_LANGUAGE "Accept-Language" +#define HEADER_AUTHORIZATION "Authorization" +#define HEADER_EXPECT "Expect" +#define HEADER_FROM "From" +#define HEADER_HOST "Host" +#define HEADER_IF_MATCH "If-Match" +#define HEADER_IF_MODIFIED_SINCE "If-Modified-Since" +#define HEADER_IF_NONE_MATCH "If-None-Match" +#define HEADER_IF_RANGE "If-Range" +#define HEADER_IF_UNMODIFIED_SINCE "If-Unmodified-Since" +#define HEADER_IF_MAX_FORWARDS "Max-Forwards" +#define HEADER_PROXY_AUTHORIZATION "Proxy-Authorization" +#define HEADER_RANGE "Range" +#define HEADER_REFERER "Referer" +#define HEADER_TRANSFER_EXTENSION "TE" +#define HEADER_USER_AGENT "User-Agent" #ifdef __NHTTP_INTERNAL struct request_statistics @@ -36,7 +66,7 @@ struct request_statistics /* request object */ -typedef struct hrequest +struct hrequest_t { hreq_method_t method; http_version_t version; @@ -47,18 +77,20 @@ typedef struct hrequest struct request_statistics *statistics; - http_input_stream_t *in; + struct http_input_stream_t *in; content_type_t *content_type; attachments_t *attachments; char root_part_id[150]; -} hrequest_t; +}; #ifdef __cplusplus extern "C" { #endif -herror_t hrequest_new_from_socket(hsocket_t *sock, hrequest_t ** out); -void hrequest_free(hrequest_t * req); +#ifdef __NHTTP_INTERNAL +herror_t hrequest_new_from_socket(struct hsocket_t *sock, struct hrequest_t **out); +void hrequest_free(struct hrequest_t *req); +#endif #ifdef __cplusplus } diff --git a/nanohttp/nanohttp-response.c b/nanohttp/nanohttp-response.c index db2f897..ad8c4a1 100755 --- a/nanohttp/nanohttp-response.c +++ b/nanohttp/nanohttp-response.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-response.c,v 1.13 2006/11/21 08:34:34 m0gg Exp $ +* $Id: nanohttp-response.c,v 1.14 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -154,7 +154,7 @@ _hresponse_parse_header(const char *buffer) herror_t -hresponse_new_from_socket(hsocket_t *sock, hresponse_t ** out) +hresponse_new_from_socket(struct hsocket_t *sock, hresponse_t ** out) { int i = 0, count; herror_t status; diff --git a/nanohttp/nanohttp-response.h b/nanohttp/nanohttp-response.h index 4dcc452..553d69f 100755 --- a/nanohttp/nanohttp-response.h +++ b/nanohttp/nanohttp-response.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-response.h,v 1.8 2006/11/19 09:40:14 m0gg Exp $ + * $Id: nanohttp-response.h,v 1.9 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -24,6 +24,28 @@ #ifndef __nanohttp_response_h #define __nanohttp_response_h +/** + * + * Response Header Fields + * + * The response-header fields allow the server to pass additional information + * about the response which cannot be placed in the Status-Line. These header + * fields give information about the server and about further access to the + * resource identified by the Request-URI. (see RFC2616) + * + * @see http://www.ietf.org/rfc/rfc2616.txt + * + */ +#define HEADER_ACCEPT_RANGES "Accept-Ranges" +#define HEADER_AGE "Age" +#define HEADER_EXTENSION_TAG "ETag" +#define HEADER_LOCATION "Location" +#define HEADER_PROXY_AUTHENTICATE "Proxy-Authenticate" +#define HEADER_RETRY_AFTER "Retry-After" +#define HEADER_SERVER "Server" +#define HEADER_VARY "Vary" +#define HEADER_WWW_AUTHENTICATE "WWW-Authenticate" + /* response object */ typedef struct hresponse { @@ -33,7 +55,7 @@ typedef struct hresponse hpair_t *header; - http_input_stream_t *in; + struct http_input_stream_t *in; content_type_t *content_type; attachments_t *attachments; char root_part_id[150]; @@ -43,8 +65,10 @@ typedef struct hresponse extern "C" { #endif -herror_t hresponse_new_from_socket(hsocket_t *sock, hresponse_t ** out); +#ifdef __NHTTP_INTERNAL +herror_t hresponse_new_from_socket(struct hsocket_t *sock, hresponse_t ** out); void hresponse_free(hresponse_t * res); +#endif #ifdef __cplusplus } diff --git a/nanohttp/nanohttp-server.c b/nanohttp/nanohttp-server.c index 269e741..45b9348 100644 --- a/nanohttp/nanohttp-server.c +++ b/nanohttp/nanohttp-server.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-server.c,v 1.64 2006/11/21 20:59:03 m0gg Exp $ +* $Id: nanohttp-server.c,v 1.65 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -82,6 +82,7 @@ #include "nanohttp-socket.h" #include "nanohttp-stream.h" #include "nanohttp-request.h" +#include "nanohttp-response.h" #include "nanohttp-server.h" #include "nanohttp-base64.h" #include "nanohttp-ssl.h" @@ -90,29 +91,27 @@ typedef struct _conndata { volatile int flag; - hsocket_t sock; + struct hsocket_t sock; #ifdef WIN32 HANDLE tid; #else pthread_t tid; pthread_attr_t attr; #endif - time_t atime; } conndata_t; #define CONNECTION_FREE 0 #define CONNECTION_IN_USE 1 -/* - * ----------------------------------------------------- - * nano httpd - * internally globals - * ----------------------------------------------------- +/** + * + * nanohttpd internally globals + * */ static volatile int _httpd_run = 1; -static hsocket_t _httpd_socket; +static struct hsocket_t _httpd_socket; static int _httpd_port = 10000; static int _httpd_max_connections = 20; static int _httpd_timeout = 10; @@ -126,7 +125,6 @@ static conndata_t *_httpd_connection; #ifdef WIN32 static DWORD _httpd_terminate_signal = CTRL_C_EVENT; static int _httpd_max_idle = 120; -static void WSAReaper(void *x); HANDLE _httpd_connection_lock; LPCTSTR _httpd_connection_lock_str; #define strncasecmp(s1, s2, num) strncmp(s1, s2, num) @@ -137,6 +135,23 @@ static sigset_t thrsigset; static pthread_mutex_t _httpd_connection_lock; #endif +/** + * + * Set Sleep function platform depended + * + */ +#ifdef WIN32 +static void _sys_sleep(int secs) +{ + Sleep(secs*1000); +} +#else +static inline void _sys_sleep(int secs) +{ + sleep(secs); +} +#endif + static void _httpd_parse_arguments(int argc, char **argv) { @@ -186,38 +201,42 @@ _httpd_connection_slots_init(void) return; } -static void +static herror_t _httpd_register_builtin_services(int argc, char **argv) { herror_t status; if ((status = httpd_admin_init_args(argc, argv)) != H_OK) + { log_error2("httpd_admin_init_args failed (%s)", herror_message(status)); + return status; + } - return; + return H_OK; } -/* - * ----------------------------------------------------- - * FUNCTION: httpd_init - * NOTE: This will be called from soap_server_init_args() - * ----------------------------------------------------- - */ herror_t -httpd_init(int argc, char *argv[]) +httpd_init(int argc, char **argv) { herror_t status; _httpd_parse_arguments(argc, argv); if ((status = hsocket_module_init(argc, argv)) != H_OK) + { + log_error2("hsocket_modeule_init failed (%s)", herror_message(status)); return status; + } log_verbose2("socket bind to port '%d'", _httpd_port); _httpd_connection_slots_init(); - _httpd_register_builtin_services(argc, argv); + if ((status = _httpd_register_builtin_services(argc, argv)) != H_OK) + { + log_error2("_httpd_register_builtin_services failed (%s)", herror_message(status)); + return status; + } if ((status = hsocket_init(&_httpd_socket)) != H_OK) { @@ -225,15 +244,16 @@ httpd_init(int argc, char *argv[]) return status; } - return hsocket_bind(&_httpd_socket, _httpd_port); + if ((status = hsocket_bind(&_httpd_socket, _httpd_port)) != H_OK) + { + log_error2("hsocket_bind failed (%s)", herror_message(status)); + return status; + } + + return H_OK; } -/* - * ----------------------------------------------------- - * FUNCTION: httpd_register - * ----------------------------------------------------- - */ -int +herror_t httpd_register_secure(const char *ctx, httpd_service func, httpd_auth auth) { hservice_t *service; @@ -241,13 +261,13 @@ httpd_register_secure(const char *ctx, httpd_service func, httpd_auth auth) if (!(service = (hservice_t *) malloc(sizeof(hservice_t)))) { log_error2("malloc failed (%s)", strerror(errno)); - return -1; + return herror_new("httpd_register_secure", 0, "malloc failed (%s)", strerror(errno)); } if (!(service->statistics = (struct service_statistics *)malloc(sizeof(struct service_statistics)))) { log_error2("malloc failed (%s)", strerror(errno)); - return -1; + return herror_new("httpd_register_secure", 0, "malloc failed (%s)", strerror(errno)); } memset(service->statistics, 0, sizeof(struct service_statistics)); service->statistics->time.tv_sec = 0; @@ -270,19 +290,19 @@ httpd_register_secure(const char *ctx, httpd_service func, httpd_auth auth) _httpd_services_tail = service; } - return 1; + return H_OK; } -int +herror_t httpd_register(const char *ctx, httpd_service service) { return httpd_register_secure(ctx, service, NULL); } -int +herror_t httpd_register_default_secure(const char *ctx, httpd_service service, httpd_auth auth) { - int ret; + herror_t ret; ret = httpd_register_secure(ctx, service, auth); @@ -292,13 +312,13 @@ httpd_register_default_secure(const char *ctx, httpd_service service, httpd_auth return ret; } -int +herror_t httpd_register_default(const char *ctx, httpd_service service) { return httpd_register_default_secure(ctx, service, NULL); } -int +short httpd_get_port(void) { return _httpd_port; @@ -314,6 +334,8 @@ void httpd_set_timeout(int t) { _httpd_timeout = t; + + return; } const char * @@ -322,9 +344,6 @@ httpd_get_protocol(void) return hssl_enabled() ? "https" : "http"; } -/*-------------------------------------------------- -FUNCTION: httpd_get_conncount -----------------------------------------------------*/ int httpd_get_conncount(void) { @@ -339,22 +358,12 @@ httpd_get_conncount(void) return ret; } -/* - * ----------------------------------------------------- - * FUNCTION: httpd_get_services - * ----------------------------------------------------- - */ hservice_t * httpd_get_services(void) { return _httpd_services_head; } -/* - * ----------------------------------------------------- - * FUNCTION: httpd_services - * ----------------------------------------------------- - */ static void hservice_free(hservice_t * service) { @@ -363,11 +372,6 @@ hservice_free(hservice_t * service) return; } -/* - * ----------------------------------------------------- - * FUNCTION: httpd_find_service - * ----------------------------------------------------- - */ hservice_t * httpd_find_service(const char *context) { @@ -382,12 +386,6 @@ httpd_find_service(const char *context) return _httpd_services_default; } - -/* - * ----------------------------------------------------- - * FUNCTION: httpd_response_set_content_type - * ----------------------------------------------------- - */ void httpd_response_set_content_type(httpd_conn_t * res, const char *content_type) { @@ -396,12 +394,6 @@ httpd_response_set_content_type(httpd_conn_t * res, const char *content_type) return; } - -/* - * ----------------------------------------------------- - * FUNCTION: httpd_response_send_header - * ----------------------------------------------------- - */ herror_t httpd_send_header(httpd_conn_t * res, int code, const char *text) { @@ -452,14 +444,21 @@ httpd_send_header(httpd_conn_t * res, int code, const char *text) return H_OK; } - herror_t httpd_send_internal_error(httpd_conn_t * conn, const char *errmsg) { const char *template1 = - "<html><body><h3>Error!</h3><hr> Message: '%s' </body></html>\r\n"; - - char buffer[4064]; + "<html>" + "<head>" + "</head>" + "<body>" + "<h3>Error!</h3>" + "<hr/>" + "<div>Message: '%s'</div>" + "</body>" + "</html>"; + + char buffer[4096]; char buflen[5]; sprintf(buffer, template1, errmsg); @@ -471,13 +470,8 @@ httpd_send_internal_error(httpd_conn_t * conn, const char *errmsg) return http_output_stream_write_string(conn->out, buffer); } -/* - * ----------------------------------------------------- - * FUNCTION: httpd_request_print - * ----------------------------------------------------- - */ static void -httpd_request_print(hrequest_t * req) +_httpd_request_print(struct hrequest_t * req) { hpair_t *pair; @@ -501,15 +495,13 @@ httpd_request_print(hrequest_t * req) return; } - httpd_conn_t * -httpd_new(hsocket_t * sock) +httpd_new(struct hsocket_t * sock) { httpd_conn_t *conn; if (!(conn = (httpd_conn_t *) malloc(sizeof(httpd_conn_t)))) { - log_error2("malloc failed (%s)", strerror(errno)); return NULL; } @@ -521,7 +513,6 @@ httpd_new(hsocket_t * sock) return conn; } - void httpd_free(httpd_conn_t * conn) { @@ -539,11 +530,9 @@ httpd_free(httpd_conn_t * conn) return; } - static int _httpd_decode_authorization(const char *value, char **user, char **pass) { - unsigned char *tmp, *tmp2; size_t len; @@ -580,7 +569,7 @@ _httpd_decode_authorization(const char *value, char **user, char **pass) } static int -_httpd_authenticate_request(hrequest_t * req, httpd_auth auth) +_httpd_authenticate_request(struct hrequest_t * req, httpd_auth auth) { char *user, *pass; char *authorization; @@ -614,11 +603,6 @@ _httpd_authenticate_request(hrequest_t * req, httpd_auth auth) return ret; } -/* - * ----------------------------------------------------- - * FUNCTION: httpd_session_main - * ----------------------------------------------------- - */ #ifdef WIN32 static unsigned _stdcall httpd_session_main(void *data) @@ -627,7 +611,7 @@ static void * httpd_session_main(void *data) #endif { - hrequest_t *req; /* only for test */ + struct hrequest_t *req; conndata_t *conn; httpd_conn_t *rconn; hservice_t *service; @@ -649,9 +633,6 @@ httpd_session_main(void *data) { log_verbose3("starting HTTP request on socket %d (%p)", conn->sock, conn->sock.sock); - /* XXX: only used in WSAreaper */ - conn->atime = time(NULL); - if ((status = hrequest_new_from_socket(&(conn->sock), &req)) != H_OK) { int code; @@ -660,8 +641,7 @@ httpd_session_main(void *data) { case HSOCKET_ERROR_SSLCLOSE: case HSOCKET_ERROR_RECEIVE: - log_error2("hrequest_new_from_socket failed (%s)", - herror_message(status)); + log_error2("hrequest_new_from_socket failed (%s)", herror_message(status)); break; default: httpd_send_internal_error(rconn, herror_message(status)); @@ -729,11 +709,11 @@ httpd_session_main(void *data) char *template = "<html>" "<head>" - "<title>Unauthorized</title>" - "</head>" - "<body>" - "<h1>Unauthorized request logged</h1>" - "</body>" + "<title>Unauthorized</title>" + "</head>" + "<body>" + "<h1>Unauthorized request logged</h1>" + "</body>" "</html>"; httpd_set_header(rconn, HEADER_WWW_AUTHENTICATE, @@ -810,6 +790,7 @@ httpd_set_headers(httpd_conn_t * conn, hpair_t * header) httpd_set_header(conn, header->key, header->value); header = header->next; } + return; } int @@ -843,11 +824,6 @@ httpd_add_headers(httpd_conn_t * conn, const hpair_t * values) return; } -/* - * ----------------------------------------------------- - * FUNCTION: httpd_term - * ----------------------------------------------------- - */ #ifdef WIN32 BOOL WINAPI httpd_term(DWORD sig) @@ -894,9 +870,6 @@ _httpd_register_signal_handler(void) return; } -/*-------------------------------------------------- -FUNCTION: _httpd_wait_for_empty_conn -----------------------------------------------------*/ static conndata_t * _httpd_wait_for_empty_conn(void) { @@ -907,13 +880,13 @@ _httpd_wait_for_empty_conn(void) #else pthread_mutex_lock(&_httpd_connection_lock); #endif + for (i = 0;; i++) { if (!_httpd_run) { - #ifdef WIN32 - ReleaseMutex(_httpd_connection_lock); + ReleaseMutex(_httpd_connection_lock); #else pthread_mutex_unlock(&_httpd_connection_lock); #endif @@ -922,7 +895,7 @@ _httpd_wait_for_empty_conn(void) if (i >= _httpd_max_connections) { - system_sleep(1); + _sys_sleep(1); i = -1; } else if (_httpd_connection[i].flag == CONNECTION_FREE) @@ -931,20 +904,16 @@ _httpd_wait_for_empty_conn(void) break; } } + #ifdef WIN32 - ReleaseMutex(_httpd_connection_lock); + ReleaseMutex(_httpd_connection_lock); #else - pthread_mutex_unlock(&_httpd_connection_lock); + pthread_mutex_unlock(&_httpd_connection_lock); #endif return &_httpd_connection[i]; } -/* - * ----------------------------------------------------- - * FUNCTION: _httpd_start_thread - * ----------------------------------------------------- - */ static void _httpd_start_thread(conndata_t * conn) { @@ -969,13 +938,6 @@ _httpd_start_thread(conndata_t * conn) return; } - -/* - * ----------------------------------------------------- - * FUNCTION: httpd_run - * ----------------------------------------------------- - */ - herror_t httpd_run(void) { @@ -1074,55 +1036,8 @@ httpd_destroy(void) return; } -#ifdef WIN32 - -static void -WSAReaper(void *x) -{ - short int connections; - short int i; - char junk[10]; - int rc; - time_t ctime; - - for (;;) - { - connections = 0; - ctime = time((time_t) 0); - for (i = 0; i < _httpd_max_connections; i++) - { - if (_httpd_connection[i].tid == 0) - continue; - GetExitCodeThread((HANDLE) _httpd_connection[i].tid, (PDWORD) & rc); - if (rc != STILL_ACTIVE) - continue; - connections++; - if ((ctime - _httpd_connection[i].atime < _httpd_max_idle) || - (_httpd_connection[i].atime == 0)) - continue; - log_verbose3("Reaping socket %u from (runtime ~= %d seconds)", - _httpd_connection[i].sock, - ctime - _httpd_connection[i].atime); - shutdown(_httpd_connection[i].sock.sock, 2); - while (recv(_httpd_connection[i].sock.sock, junk, sizeof(junk), 0) > 0) - { - }; - closesocket(_httpd_connection[i].sock.sock); - _httpd_connection[i].sock.sock = 0; - TerminateThread(_httpd_connection[i].tid, (DWORD) & rc); - CloseHandle(_httpd_connection[i].tid); - memset((char *) &_httpd_connection[i], 0, sizeof(_httpd_connection[i])); - } - Sleep(100); - } - return; -} - -#endif - unsigned char * -httpd_get_postdata(httpd_conn_t * conn, hrequest_t * req, long *received, - long max) +httpd_get_postdata(httpd_conn_t * conn, struct hrequest_t * req, long *received, long max) { char *content_length_str; long content_length = 0; diff --git a/nanohttp/nanohttp-server.h b/nanohttp/nanohttp-server.h index eb3a06b..be48005 100644 --- a/nanohttp/nanohttp-server.h +++ b/nanohttp/nanohttp-server.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-server.h,v 1.23 2006/11/21 20:59:03 m0gg Exp $ + * $Id: nanohttp-server.h,v 1.24 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -24,26 +24,47 @@ #ifndef __nanohttp_server_h #define __nanohttp_server_h +/** + * + * nanohttp command line flags + * + */ +#define NHTTPD_ARG_PORT "-NHTTPport" +#define NHTTPD_ARG_TERMSIG "-NHTTPtsig" +#define NHTTPD_ARG_MAXCONN "-NHTTPmaxconn" +#define NHTTPD_ARG_TIMEOUT "-NHTTPtimeout" typedef struct httpd_conn { - hsocket_t *sock; + struct hsocket_t *sock; char content_type[25]; - http_output_stream_t *out; + struct http_output_stream_t *out; hpair_t *header; } httpd_conn_t; +/** + * + * Service callback function for a nanoHTTP service. + * + */ +typedef void (*httpd_service) (httpd_conn_t *conn, struct hrequest_t *req); -/* - Service callback +/** + * + * Authentication callback function for a nanoHTTP service. + * */ -typedef void (*httpd_service) (httpd_conn_t *, hrequest_t *); -typedef int (*httpd_auth) (hrequest_t * req, const char *user, - const char *password); +typedef int (*httpd_auth) (struct hrequest_t *req, const char *user, const char *pass); #ifdef __NHTTP_INTERNAL -struct service_statistics { +/** + * + * Service statistics per nanoHTTP service. + * + */ +struct service_statistics +{ unsigned long requests; unsigned long bytes_transmitted; unsigned long bytes_received; @@ -52,8 +73,10 @@ struct service_statistics { }; #endif -/* +/** + * * Service representation object + * */ typedef struct tag_hservice { @@ -65,7 +88,6 @@ typedef struct tag_hservice } hservice_t; - #ifdef __cplusplus extern "C" { @@ -75,43 +97,35 @@ extern "C" /* Begin httpd_* function set */ - herror_t httpd_init(int argc, char *argv[]); - void httpd_destroy(void); + extern herror_t httpd_init(int argc, char *argv[]); + extern void httpd_destroy(void); - herror_t httpd_run(void); + extern herror_t httpd_run(void); - int httpd_register(const char *ctx, httpd_service service); - int httpd_register_secure(const char *ctx, httpd_service service, - httpd_auth auth); + extern herror_t httpd_register(const char *ctx, httpd_service service); + extern herror_t httpd_register_secure(const char *ctx, httpd_service service, httpd_auth auth); - int httpd_register_default(const char *ctx, httpd_service service); - int httpd_register_default_secure(const char *ctx, httpd_service service, - httpd_auth auth); + extern herror_t httpd_register_default(const char *ctx, httpd_service service); + extern herror_t httpd_register_default_secure(const char *ctx, httpd_service service, httpd_auth auth); - int httpd_get_port(void); - int httpd_get_timeout(void); - void httpd_set_timeout(int t); + extern short httpd_get_port(void); + extern int httpd_get_timeout(void); + extern void httpd_set_timeout(int t); - const char *httpd_get_protocol(void); - int httpd_get_conncount(void); + extern const char *httpd_get_protocol(void); + extern int httpd_get_conncount(void); - hservice_t *httpd_get_services(void); - hservice_t *httpd_find_service(const char *name); + extern hservice_t *httpd_get_services(void); + extern hservice_t *httpd_find_service(const char *name); - herror_t httpd_send_header(httpd_conn_t * res, int code, const char *text); + extern herror_t httpd_send_header(httpd_conn_t * res, int code, const char *text); - int httpd_set_header(httpd_conn_t * conn, const char *key, - const char *value); - void httpd_set_headers(httpd_conn_t * conn, hpair_t * header); + extern int httpd_set_header(httpd_conn_t * conn, const char *key, const char *value); + extern void httpd_set_headers(httpd_conn_t * conn, hpair_t * header); - int httpd_add_header(httpd_conn_t * conn, const char *key, - const char *value); - void httpd_add_headers(httpd_conn_t * conn, const hpair_t * values); + extern int httpd_add_header(httpd_conn_t * conn, const char *key, const char *value); + extern void httpd_add_headers(httpd_conn_t * conn, const hpair_t * values); -/* -unsigned char *httpd_get_postdata(httpd_conn_t *conn, - hrequest_t *req, long *received, long max); -*/ /* -------------------------------------------------------------- MIME RELATED FUNCTIONS ---------------------------------------------------------------*/ @@ -123,7 +137,7 @@ unsigned char *httpd_get_postdata(httpd_conn_t *conn, Begin MIME multipart/related POST Returns: HSOCKET_OK or error flag */ - herror_t httpd_mime_send_header(httpd_conn_t * conn, + extern herror_t httpd_mime_send_header(httpd_conn_t * conn, const char *related_start, const char *related_start_info, const char *related_type, int code, @@ -133,7 +147,7 @@ unsigned char *httpd_get_postdata(httpd_conn_t *conn, Send boundary and part header and continue with next part */ - herror_t httpd_mime_next(httpd_conn_t * conn, + extern herror_t httpd_mime_next(httpd_conn_t * conn, const char *content_id, const char *content_type, const char *transfer_encoding); @@ -142,7 +156,7 @@ unsigned char *httpd_get_postdata(httpd_conn_t *conn, Send boundary and part header and continue with next part */ - herror_t httpd_mime_send_file(httpd_conn_t * conn, + extern herror_t httpd_mime_send_file(httpd_conn_t * conn, const char *content_id, const char *content_type, const char *transfer_encoding, @@ -152,7 +166,7 @@ unsigned char *httpd_get_postdata(httpd_conn_t *conn, Finish MIME request Returns: HSOCKET_OK or error flag */ - herror_t httpd_mime_end(httpd_conn_t * conn); + extern herror_t httpd_mime_end(httpd_conn_t * conn); #ifdef __cplusplus diff --git a/nanohttp/nanohttp-socket.c b/nanohttp/nanohttp-socket.c index d9e8900..9936379 100644 --- a/nanohttp/nanohttp-socket.c +++ b/nanohttp/nanohttp-socket.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-socket.c,v 1.62 2006/11/19 09:40:14 m0gg Exp $ +* $Id: nanohttp-socket.c,v 1.63 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -79,10 +79,10 @@ typedef int ssize_t; #endif -#include "nanohttp-logging.h" #include "nanohttp-common.h" #include "nanohttp-socket.h" #include "nanohttp-ssl.h" +#include "nanohttp-logging.h" #ifdef WIN32 static void @@ -114,11 +114,6 @@ _hsocket_module_sys_destroy(void) } #endif -/*-------------------------------------------------- -FUNCTION: hsocket_module_init -NOTE: This will be called from httpd_init() - for server and from httpc_init() for client -----------------------------------------------------*/ herror_t hsocket_module_init(int argc, char **argv) { @@ -127,9 +122,6 @@ hsocket_module_init(int argc, char **argv) return hssl_module_init(argc, argv); } -/*-------------------------------------------------- -FUNCTION: hsocket_module_destroy -----------------------------------------------------*/ void hsocket_module_destroy(void) { @@ -138,34 +130,25 @@ hsocket_module_destroy(void) return; } -/*-------------------------------------------------- -FUNCTION: hsocket_init -----------------------------------------------------*/ herror_t -hsocket_init(hsocket_t * sock) +hsocket_init(struct hsocket_t * sock) { - memset(sock, 0, sizeof(hsocket_t)); + memset(sock, 0, sizeof(struct hsocket_t)); sock->sock = HSOCKET_FREE; return H_OK; } -/*-------------------------------------------------- -FUNCTION: hsocket_free -----------------------------------------------------*/ void -hsocket_free(hsocket_t * sock) +hsocket_free(struct hsocket_t * sock) { /* nop */ return; } -/*-------------------------------------------------- -FUNCTION: hsocket_open -----------------------------------------------------*/ herror_t -hsocket_open(hsocket_t * dsock, const char *hostname, int port, int ssl) +hsocket_open(struct hsocket_t * dsock, const char *hostname, int port, int ssl) { struct sockaddr_in address; struct hostent *host; @@ -209,13 +192,10 @@ hsocket_open(hsocket_t * dsock, const char *hostname, int port, int ssl) return H_OK; } -/*-------------------------------------------------- -FUNCTION: hsocket_bind -----------------------------------------------------*/ herror_t -hsocket_bind(hsocket_t * dsock, unsigned short port) +hsocket_bind(struct hsocket_t *dsock, unsigned short port) { - hsocket_t sock; + struct hsocket_t sock; struct sockaddr_in addr; int opt = 1; @@ -234,8 +214,7 @@ hsocket_bind(hsocket_t * dsock, unsigned short port) addr.sin_addr.s_addr = INADDR_ANY; memset(&(addr.sin_zero), '\0', 8); /* zero the rest of the struct */ - if (bind(sock.sock, (struct sockaddr *) &addr, sizeof(struct sockaddr)) == - -1) + if (bind(sock.sock, (struct sockaddr *) &addr, sizeof(struct sockaddr)) == -1) { log_error2("Cannot bind socket (%s)", strerror(errno)); return herror_new("hsocket_bind", HSOCKET_ERROR_BIND, "Socket error (%s)", @@ -247,16 +226,15 @@ hsocket_bind(hsocket_t * dsock, unsigned short port) #ifdef WIN32 static herror_t -_hsocket_sys_accept(hsocket_t * sock, hsocket_t * dest) +_hsocket_sys_accept(struct hsocket_t * sock, struct hsocket_t * dest) { int asize; - hsocket_t sockfd; + struct hsocket_t sockfd; asize = sizeof(struct sockaddr_in); while (1) { - sockfd.sock = - accept(sock->sock, (struct sockaddr *) &(dest->addr), &asize); + sockfd.sock = accept(sock->sock, (struct sockaddr *) &(dest->addr), &asize); if (sockfd.sock == INVALID_SOCKET) { if (WSAGetLastError() != WSAEWOULDBLOCK) @@ -275,14 +253,13 @@ _hsocket_sys_accept(hsocket_t * sock, hsocket_t * dest) } #else static herror_t -_hsocket_sys_accept(hsocket_t * sock, hsocket_t * dest) +_hsocket_sys_accept(struct hsocket_t * sock, struct hsocket_t * dest) { socklen_t len; len = sizeof(struct sockaddr_in); - if ((dest->sock = - accept(sock->sock, (struct sockaddr *) &(dest->addr), &len)) == -1) + if ((dest->sock = accept(sock->sock, (struct sockaddr *) &(dest->addr), &len)) == -1) { log_warn2("accept failed (%s)", strerror(errno)); return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT, @@ -294,11 +271,8 @@ _hsocket_sys_accept(hsocket_t * sock, hsocket_t * dest) } #endif -/*---------------------------------------------------------- -FUNCTION: hsocket_accept -----------------------------------------------------------*/ herror_t -hsocket_accept(hsocket_t * sock, hsocket_t * dest) +hsocket_accept(struct hsocket_t * sock, struct hsocket_t * dest) { herror_t status; @@ -322,11 +296,8 @@ hsocket_accept(hsocket_t * sock, hsocket_t * dest) return H_OK; } -/*-------------------------------------------------- -FUNCTION: hsocket_listen -----------------------------------------------------*/ herror_t -hsocket_listen(hsocket_t * sock) +hsocket_listen(struct hsocket_t * sock) { if (sock->sock < 0) return herror_new("hsocket_listen", HSOCKET_ERROR_NOT_INITIALIZED, @@ -344,7 +315,7 @@ hsocket_listen(hsocket_t * sock) #ifdef WIN32 static void -_hsocket_sys_close(hsocket_t * sock) +_hsocket_sys_close(struct hsocket_t * sock) { char junk[10]; @@ -359,7 +330,7 @@ _hsocket_sys_close(hsocket_t * sock) } #else static inline void -_hsocket_sys_close(hsocket_t * sock) +_hsocket_sys_close(struct hsocket_t * sock) { shutdown(sock->sock, SHUT_RDWR); @@ -370,11 +341,8 @@ _hsocket_sys_close(hsocket_t * sock) } #endif -/*-------------------------------------------------- -FUNCTION: hsocket_close -----------------------------------------------------*/ void -hsocket_close(hsocket_t * sock) +hsocket_close(struct hsocket_t * sock) { log_verbose3("closing socket %p (%d)...", sock, sock->sock); @@ -390,11 +358,8 @@ hsocket_close(hsocket_t * sock) return; } -/*-------------------------------------------------- -FUNCTION: hsocket_send -----------------------------------------------------*/ herror_t -hsocket_nsend(hsocket_t * sock, const unsigned char * bytes, int n) +hsocket_nsend(struct hsocket_t * sock, const unsigned char * bytes, int n) { herror_t status; size_t total = 0; @@ -409,7 +374,6 @@ hsocket_nsend(hsocket_t * sock, const unsigned char * bytes, int n) while (1) { - if ((status = hssl_write(sock, bytes + total, n, &size)) != H_OK) { log_warn2("hssl_write failed (%s)", herror_message(status)); @@ -425,11 +389,8 @@ hsocket_nsend(hsocket_t * sock, const unsigned char * bytes, int n) return H_OK; } -/*-------------------------------------------------- -FUNCTION: hsocket_send -----------------------------------------------------*/ herror_t -hsocket_send(hsocket_t * sock, const char *str) +hsocket_send(struct hsocket_t * sock, const char *str) { return hsocket_nsend(sock, str, strlen(str)); } @@ -457,8 +418,7 @@ hsocket_select_recv(int sock, char *buf, size_t len) } herror_t -hsocket_read(hsocket_t * sock, unsigned char * buffer, int total, int force, - int *received) +hsocket_read(struct hsocket_t * sock, unsigned char * buffer, int total, int force, int *received) { herror_t status; size_t totalRead; @@ -470,9 +430,7 @@ hsocket_read(hsocket_t * sock, unsigned char * buffer, int total, int force, do { - if ((status = - hssl_read(sock, &buffer[totalRead], (size_t) total - totalRead, - &count)) != H_OK) + if ((status = hssl_read(sock, &buffer[totalRead], (size_t) total - totalRead, &count)) != H_OK) { log_warn2("hssl_read failed (%s)", herror_message(status)); return status; diff --git a/nanohttp/nanohttp-socket.h b/nanohttp/nanohttp-socket.h index 9bb88b5..a9e4ef6 100644 --- a/nanohttp/nanohttp-socket.h +++ b/nanohttp/nanohttp-socket.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-socket.h,v 1.30 2006/11/19 09:40:14 m0gg Exp $ + * $Id: nanohttp-socket.h,v 1.31 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -29,7 +29,7 @@ /* Socket definition */ -typedef struct hsocket_t +struct hsocket_t { #ifdef WIN32 SOCKET sock; @@ -40,8 +40,7 @@ typedef struct hsocket_t size_t bytes_transmitted; size_t bytes_received; void *ssl; -} -hsocket_t; /* end of socket definition */ +}; #ifdef __cplusplus extern "C" @@ -49,157 +48,166 @@ extern "C" #endif /** - Initializes the socket modul. This should be called only - once for an application. - - @returns This function should always return H_OK. + * + * Initializes the socket modul. This should be called only once for an + * application. + * + * @returns This function should always return H_OK. + * */ - herror_t hsocket_module_init(int argc, char **argv); +extern herror_t hsocket_module_init(int argc, char **argv); /** - Destroys the socket modul. This should be called after - finishing an application. -*/ - void hsocket_module_destroy(void); - + * + * Destroys the socket modul. This should be called after finishing an + * application. + * + */ +extern void hsocket_module_destroy(void); /** - Initializes a given socket object. This function (or - hsokcet_init_ssl) should - be called for every socket before using it. - - @param sock the destination socket to initialize. - - @see hsocket_init_ssl - @returns This function should always return H_OK. + * + * Initializes a given socket object. This function (or hsokcet_init_ssl) should + * be called for every socket before using it. + * + * @param sock the destination socket to initialize. + * + * @returns This function should always return H_OK. + * + * @see hsocket_init_ssl + * */ - herror_t hsocket_init(hsocket_t * sock); +extern herror_t hsocket_init(struct hsocket_t * sock); /** - Destroys and releases a given socket. - - @param sock the socket to destroy -*/ - void hsocket_free(hsocket_t * sock); - + * + * Destroys and releases a given socket. + * + * @param sock the socket to destroy + * + */ +extern void hsocket_free(struct hsocket_t * sock); /** - Connects to a given host. The hostname can be an IP number - or a humen readable hostname. - - @param sock the destonation socket object to use - @param host hostname - @param port port number to connect to - @param ssl whether to open a SSL connection - - @returns H_OK if success. One of the followings if fails:<P> - <BR>HSOCKET_ERROR_CREATE - <BR>HSOCKET_ERROR_GET_HOSTNAME - <BR>HSOCKET_ERROR_CONNECT + * + * Connects to a given host. The hostname can be an IP number or a humen + * readable hostname. + * + * @param sock the destonation socket object to use + * @param host hostname + * @param port port number to connect to + * @param ssl whether to open a SSL connection + * + * @returns H_OK if success. One of the followings if fails:<P> + * - HSOCKET_ERROR_CREATE + * - HSOCKET_ERROR_GET_HOSTNAME + * - HSOCKET_ERROR_CONNECT + * */ - herror_t hsocket_open(hsocket_t * sock, const char *host, int port, - int ssl); - +extern herror_t hsocket_open(struct hsocket_t *sock, const char *host, int port, int ssl); /** - Close a socket connection. - - @param sock the socket to close -*/ - void hsocket_close(hsocket_t * sock); + * + * Close a socket connection. + * + * @param sock the socket to close + * + */ +extern void hsocket_close(struct hsocket_t *sock); /** - Binds a socket to a given port number. After bind you - can call hsocket_listen() to listen to the port. - - @param sock socket to use. - @param port port number to bind to - - @returns H_OK if success. One of the followings if fails:<P> - <BR>HSOCKET_ERROR_CREATE - <BR>HSOCKET_ERROR_BIND - - @see hsocket_listen + * + * Binds a socket to a given port number. After bind you can call + * hsocket_listen() to listen to the port. + * + * @param sock socket to use. + * @param port port number to bind to + * + * @returns H_OK if success. One of the followings if fails:<P> + * - HSOCKET_ERROR_CREATE + * - HSOCKET_ERROR_BIND + * + * @see hsocket_listen + * */ - herror_t hsocket_bind(hsocket_t * sock, unsigned short port); - +extern herror_t hsocket_bind(struct hsocket_t *sock, unsigned short port); /** - Set the socket to the listen mode. You must bind - the socket to a port with hsocket_bind() before - you can listen to the port. - - @param sock the socket to use - - @returns H_OK if success. One of the followings if fails:<P> - <BR>HSOCKET_ERROR_NOT_INITIALIZED - <BR>HSOCKET_ERROR_LISTEN -*/ - herror_t hsocket_listen(hsocket_t * sock); - + * + * Set the socket to the listen mode. You must bind the socket to a port with + * hsocket_bind() before you can listen to the port. + * + * @param sock the socket to use + * + * @returns H_OK if success. One of the followings if fails:<P> + * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_LISTEN + */ +extern herror_t hsocket_listen(struct hsocket_t *sock); /** - Accepts an incoming socket request. Note that this function - will not return until a socket connection is ready. - - @param sock the socket which listents to a port - @param dest the destination socket which will be created - - @returns H_OK if success. One of the followings if fails:<P> - <BR>HSOCKET_ERROR_NOT_INITIALIZED - <BR>HSOCKET_ERROR_ACCEPT -*/ - herror_t hsocket_accept(hsocket_t *sock, hsocket_t *dest); - + * + * Accepts an incoming socket request. Note that this function + * will not return until a socket connection is ready. + * + * @param sock the socket which listents to a port + * @param dest the destination socket which will be created + * + * @returns H_OK if success. One of the followings if fails:<P> + * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_ACCEPT + */ +extern herror_t hsocket_accept(struct hsocket_t *sock, struct hsocket_t *dest); /** - Sends data throught the socket. - - @param sock the socket to use to send the data - @param bytes bytes to send - @param size size of memory to sent pointed by bytes. - - @returns H_OK if success. One of the followings if fails:<P> - <BR>HSOCKET_ERROR_NOT_INITIALIZED - <BR>HSOCKET_ERROR_SEND -*/ - herror_t hsocket_nsend(hsocket_t * sock, const unsigned char *bytes, int size); + * + * Sends data throught the socket. + * + * @param sock the socket to use to send the data + * @param bytes bytes to send + * @param size size of memory to sent pointed by bytes. + * + * @returns H_OK if success. One of the followings if fails:<P> + * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_SEND + */ +extern herror_t hsocket_nsend(struct hsocket_t *sock, const unsigned char *bytes, int size); /** - Sends a string throught the socket - - @param sock the socket to use to send the data - @param str the null terminated string to sent - - @returns H_OK if success. One of the followings if fails:<P> - <BR>HSOCKET_ERROR_NOT_INITIALIZED - <BR>HSOCKET_ERROR_SEND -*/ - herror_t hsocket_send(hsocket_t * sock, const char *str); + * + * Sends a string throught the socket + * + * @param sock the socket to use to send the data + * @param str the null terminated string to sent + * + * @returns H_OK if success. One of the followings if fails:<P> + * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_SEND + * + */ +extern herror_t hsocket_send(struct hsocket_t *sock, const char *str); +extern int hsocket_select_read(int sock, char *buf, size_t len); - int hsocket_select_read(int sock, char *buf, size_t len); /** - Reads data from the socket. - - @param sock the socket to read data from - @param buffer the buffer to use to save the readed bytes - @param size the maximum size of bytes to read - @param force if force is 1 then hsocket_read() will wait until - maximum size of bytes (size parameter) was readed. Otherwise - this function will not wait and will return with the bytes - quequed on the socket. - - @returns This function will return -1 if an read error was occured. - Otherwise the return value is the size of bytes readed from - the socket. - -*/ - herror_t hsocket_read(hsocket_t * sock, unsigned char *buffer, int size, - int force, int *readed); + * + * Reads data from the socket. + * + * @param sock the socket to read data from + * @param buffer the buffer to use to save the readed bytes + * @param size the maximum size of bytes to read + * @param force if force is 1 then hsocket_read() will wait until maximum size + * of bytes (size parameter) was readed. Otherwise this function will not + * wait and will return with the bytes quequed on the socket. + * + * @returns This function will return -1 if an read error was occured. Otherwise + * the return value is the size of bytes readed from the socket. + * + */ +extern herror_t hsocket_read(struct hsocket_t * sock, unsigned char *buffer, int size, int force, int *readed); #ifdef __cplusplus } diff --git a/nanohttp/nanohttp-ssl.c b/nanohttp/nanohttp-ssl.c index d8e6e3f..8dbc948 100644 --- a/nanohttp/nanohttp-ssl.c +++ b/nanohttp/nanohttp-ssl.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-ssl.c,v 1.29 2006/11/19 09:40:14 m0gg Exp $ +* $Id: nanohttp-ssl.c,v 1.30 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2001-2005 Rochester Institute of Technology @@ -71,9 +71,10 @@ #endif #endif -#include "nanohttp-logging.h" #include "nanohttp-common.h" #include "nanohttp-socket.h" +#include "nanohttp-logging.h" + #include "nanohttp-ssl.h" #ifdef HAVE_SSL @@ -85,9 +86,42 @@ static SSL_CTX *context = NULL; static int enabled = 0; -static int _hssl_dummy_verify_cert(X509 * cert); +static int +_hssl_dummy_verify_cert(X509 * cert) +{ + /* TODO: Make sure that the client is providing a client cert, or that the + Module is providing the Module cert */ + + /* connect to anyone */ + + log_verbose1("Validating certificate."); + return 1; +} + int (*_hssl_verify_cert) (X509 * cert) = _hssl_dummy_verify_cert; +static int +_hssl_cert_verify_callback(int prev_ok, X509_STORE_CTX * ctx) +{ +/* + if ((X509_STORE_CTX_get_error(ctx) = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN)) + { + log_verbose1("Self signed cert in chain"); + return 1; + } +*/ + log_verbose2("Cert depth = %d", X509_STORE_CTX_get_error_depth(ctx)); + if (X509_STORE_CTX_get_error_depth(ctx) == 0) + { + return _hssl_verify_cert(X509_STORE_CTX_get_current_cert(ctx)); + } + else + { + log_verbose1("Cert ok (prev)"); + return prev_ok; + } +} + static void _hssl_superseed(void) { @@ -104,7 +138,6 @@ _hssl_superseed(void) return; } - static char * _hssl_get_error(SSL * ssl, int ret) { @@ -133,7 +166,6 @@ _hssl_get_error(SSL * ssl, int ret) } } - static int _hssl_password_callback(char *buf, int num, int rwflag, void *userdata) { @@ -145,10 +177,10 @@ _hssl_password_callback(char *buf, int num, int rwflag, void *userdata) return 0; strcpy(buf, certpass); + return ret; } - int verify_sn(X509 * cert, int who, int nid, char *str) { @@ -179,70 +211,45 @@ void hssl_set_hssl_verify_cert(int func(X509 * cert)) { _hssl_verify_cert = func; -} - -static int -_hssl_dummy_verify_cert(X509 * cert) -{ - /* TODO: Make sure that the client is providing a client cert, or that the - Module is providing the Module cert */ - - /* connect to anyone */ - - log_verbose1("Validating certificate."); - return 1; -} -static int -_hssl_cert_verify_callback(int prev_ok, X509_STORE_CTX * ctx) -{ -/* - if ((X509_STORE_CTX_get_error(ctx) = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN)) - { - log_verbose1("Self signed cert in chain"); - return 1; - } -*/ - log_verbose2("Cert depth = %d", X509_STORE_CTX_get_error_depth(ctx)); - if (X509_STORE_CTX_get_error_depth(ctx) == 0) - { - return _hssl_verify_cert(X509_STORE_CTX_get_current_cert(ctx)); - } - else - { - log_verbose1("Cert ok (prev)"); - return prev_ok; - } + return; } void hssl_set_certificate(char *c) { certificate = c; + + return; } void hssl_set_certpass(char *c) { certpass = c; + + return; } void hssl_set_ca(char *c) { ca_list = c; + + return; } void hssl_enable(void) { enabled = 1; + + return; } static void _hssl_parse_arguments(int argc, char **argv) { - int i; for (i = 1; i < argc; i++) @@ -259,7 +266,7 @@ _hssl_parse_arguments(int argc, char **argv) { ca_list = argv[i]; } - else if (!strcmp(argv[i - 1], NHTTP_ARG_HTTPS)) + else if (!strcmp(argv[i - 1], NHTTPD_ARG_HTTPS)) { enabled = 1; } @@ -268,7 +275,6 @@ _hssl_parse_arguments(int argc, char **argv) return; } - static void _hssl_library_init(void) { @@ -291,7 +297,6 @@ _hssl_library_init(void) return; } - static herror_t _hssl_server_context_init(void) { @@ -352,7 +357,6 @@ _hssl_server_context_init(void) return H_OK; } - static void _hssl_server_context_destroy(void) { @@ -364,7 +368,6 @@ _hssl_server_context_destroy(void) return; } - herror_t hssl_module_init(int argc, char **argv) { @@ -383,7 +386,6 @@ hssl_module_init(int argc, char **argv) return _hssl_server_context_init(); } - void hssl_module_destroy(void) { @@ -392,16 +394,14 @@ hssl_module_destroy(void) return; } - int hssl_enabled(void) { return enabled; } - herror_t -hssl_client_ssl(hsocket_t * sock) +hssl_client_ssl(struct hsocket_t * sock) { SSL *ssl; int ret; @@ -452,7 +452,7 @@ _hssl_bio_read(BIO * b, char *out, int outl) } herror_t -hssl_server_ssl(hsocket_t * sock) +hssl_server_ssl(struct hsocket_t *sock) { SSL *ssl; int ret; @@ -502,9 +502,8 @@ hssl_server_ssl(hsocket_t * sock) return H_OK; } - void -hssl_cleanup(hsocket_t * sock) +hssl_cleanup(struct hsocket_t * sock) { if (sock->ssl) { @@ -517,7 +516,7 @@ hssl_cleanup(hsocket_t * sock) } herror_t -hssl_read(hsocket_t * sock, char *buf, size_t len, size_t * received) +hssl_read(struct hsocket_t * sock, char *buf, size_t len, size_t * received) { int count; @@ -544,7 +543,7 @@ hssl_read(hsocket_t * sock, char *buf, size_t len, size_t * received) herror_t -hssl_write(hsocket_t * sock, const char *buf, size_t len, size_t * sent) +hssl_write(struct hsocket_t * sock, const char *buf, size_t len, size_t * sent) { int count; @@ -572,7 +571,7 @@ hssl_write(hsocket_t * sock, const char *buf, size_t len, size_t * sent) #else herror_t -hssl_read(hsocket_t * sock, char *buf, size_t len, size_t * received) +hssl_read(struct hsocket_t * sock, char *buf, size_t len, size_t * received) { int count; @@ -587,7 +586,7 @@ hssl_read(hsocket_t * sock, char *buf, size_t len, size_t * received) herror_t -hssl_write(hsocket_t * sock, const char *buf, size_t len, size_t * sent) +hssl_write(struct hsocket_t * sock, const char *buf, size_t len, size_t * sent) { int count; diff --git a/nanohttp/nanohttp-ssl.h b/nanohttp/nanohttp-ssl.h index e81668d..f796356 100644 --- a/nanohttp/nanohttp-ssl.h +++ b/nanohttp/nanohttp-ssl.h @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-ssl.h,v 1.19 2006/11/19 09:40:14 m0gg Exp $ +* $Id: nanohttp-ssl.h,v 1.20 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2001-2005 Rochester Institute of Technology @@ -34,6 +34,13 @@ #include <openssl/ssl.h> #endif +/** + * + * Commandline argument to enabled SSL in the nanoHTTP server. + * + */ +#define NHTTPD_ARG_HTTPS "-NHTTPS" + #ifdef __cplusplus extern "C" { @@ -44,26 +51,26 @@ extern "C" * Initialization and shutdown of the SSL module * */ - herror_t hssl_module_init(int argc, char **argv); - void hssl_module_destroy(void); +extern herror_t hssl_module_init(int argc, char **argv); +extern void hssl_module_destroy(void); - void hssl_set_certificate(char *c); - void hssl_set_certpass(char *c); - void hssl_set_ca(char *c); +extern void hssl_set_certificate(char *c); +extern void hssl_set_certpass(char *c); +extern void hssl_set_ca(char *c); - void hssl_enable(void); +extern void hssl_enable(void); - int hssl_enabled(void); +extern int hssl_enabled(void); /** * * Socket initialization and shutdown * */ - herror_t hssl_client_ssl(hsocket_t * sock); - herror_t hssl_server_ssl(hsocket_t * sock); +extern herror_t hssl_client_ssl(struct hsocket_t * sock); +extern herror_t hssl_server_ssl(struct hsocket_t * sock); - void hssl_cleanup(hsocket_t * sock); +extern void hssl_cleanup(struct hsocket_t * sock); /* * Callback for password checker @@ -77,7 +84,7 @@ extern "C" */ #define CERT_SUBJECT 1 - int verify_sn(X509 * cert, int who, int nid, char *str); +extern int verify_sn(X509 * cert, int who, int nid, char *str); /* * Called by framework for verify @@ -85,7 +92,7 @@ extern "C" /* static int verify_cb(int prev_ok, X509_STORE_CTX* ctx); */ - void hssl_set_user_verify(int func(X509 * cert)); +extern void hssl_set_user_verify(int func(X509 * cert)); #ifdef __cplusplus } @@ -98,6 +105,7 @@ hssl_module_init(int argc, char **argv) { return H_OK; } + static inline void hssl_module_destroy(void) { @@ -111,19 +119,19 @@ hssl_enabled(void) } static inline herror_t -hssl_client_ssl(hsocket_t * sock) +hssl_client_ssl(struct hsocket_t *sock) { return H_OK; } static inline herror_t -hssl_server_ssl(hsocket_t * sock) +hssl_server_ssl(struct hsocket_t *sock) { return H_OK; } static inline void -hssl_cleanup(hsocket_t * sock) +hssl_cleanup(struct hsocket_t *sock) { return; } @@ -135,10 +143,8 @@ extern "C" { #endif - herror_t hssl_read(hsocket_t * sock, char *buf, size_t len, - size_t * received); - herror_t hssl_write(hsocket_t * sock, const char *buf, size_t len, - size_t * sent); +extern herror_t hssl_read(struct hsocket_t * sock, char *buf, size_t len, size_t * received); +extern herror_t hssl_write(struct hsocket_t * sock, const char *buf, size_t len, size_t * sent); #ifdef __cplusplus } diff --git a/nanohttp/nanohttp-stream.c b/nanohttp/nanohttp-stream.c index 27d8d62..09d06b2 100755 --- a/nanohttp/nanohttp-stream.c +++ b/nanohttp/nanohttp-stream.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-stream.c,v 1.14 2006/11/19 09:40:14 m0gg Exp $ +* $Id: nanohttp-stream.c,v 1.15 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -79,16 +79,16 @@ _http_stream_is_chunked(hpair_t * header) /** Creates a new input stream. */ -http_input_stream_t * -http_input_stream_new(hsocket_t *sock, hpair_t * header) +struct http_input_stream_t * +http_input_stream_new(struct hsocket_t *sock, hpair_t * header) { - http_input_stream_t *result; + struct http_input_stream_t *result; char *content_length; /* Paranoya check */ /* if (header == NULL) return NULL; */ /* Create object */ - if (!(result = (http_input_stream_t *) malloc(sizeof(http_input_stream_t)))) + if (!(result = (struct http_input_stream_t *) malloc(sizeof(struct http_input_stream_t)))) { log_error2("malloc failed (%s)", strerror(errno)); return NULL; @@ -132,10 +132,10 @@ http_input_stream_new(hsocket_t *sock, hpair_t * header) This function was added for MIME messages and for debugging. */ -http_input_stream_t * +struct http_input_stream_t * http_input_stream_new_from_file(const char *filename) { - http_input_stream_t *result; + struct http_input_stream_t *result; FILE *fd; if (!(fd = fopen(filename, "rb"))) { @@ -145,7 +145,7 @@ http_input_stream_new_from_file(const char *filename) } /* Create object */ - if (!(result = (http_input_stream_t *) malloc(sizeof(http_input_stream_t)))) + if (!(result = (struct http_input_stream_t *) malloc(sizeof(struct http_input_stream_t)))) { log_error2("malloc failed (%s)", strerror(errno)); fclose(fd); @@ -164,7 +164,7 @@ http_input_stream_new_from_file(const char *filename) Free input stream */ void -http_input_stream_free(http_input_stream_t * stream) +http_input_stream_free(struct http_input_stream_t * stream) { if (stream->type == HTTP_TRANSFER_FILE && stream->fd) { @@ -178,31 +178,31 @@ http_input_stream_free(http_input_stream_t * stream) } static int -_http_input_stream_is_content_length_ready(http_input_stream_t * stream) +_http_input_stream_is_content_length_ready(struct http_input_stream_t * stream) { return (stream->content_length > stream->received); } static int -_http_input_stream_is_chunked_ready(http_input_stream_t * stream) +_http_input_stream_is_chunked_ready(struct http_input_stream_t * stream) { return stream->chunk_size != 0; } static int -_http_input_stream_is_connection_closed_ready(http_input_stream_t * stream) +_http_input_stream_is_connection_closed_ready(struct http_input_stream_t * stream) { return !stream->connection_closed; } static int -_http_input_stream_is_file_ready(http_input_stream_t * stream) +_http_input_stream_is_file_ready(struct http_input_stream_t * stream) { return !feof(stream->fd); } static int -_http_input_stream_content_length_read(http_input_stream_t * stream, unsigned char *dest, int size) +_http_input_stream_content_length_read(struct http_input_stream_t * stream, unsigned char *dest, int size) { herror_t status; int read; @@ -223,7 +223,7 @@ _http_input_stream_content_length_read(http_input_stream_t * stream, unsigned ch } static int -_http_input_stream_chunked_read_chunk_size(http_input_stream_t * stream) +_http_input_stream_chunked_read_chunk_size(struct http_input_stream_t * stream) { char chunk[25]; int status, i = 0; @@ -282,7 +282,7 @@ _http_input_stream_chunked_read_chunk_size(http_input_stream_t * stream) } static int -_http_input_stream_chunked_read(http_input_stream_t * stream, unsigned char *dest, +_http_input_stream_chunked_read(struct http_input_stream_t * stream, unsigned char *dest, int size) { int status, counter; @@ -386,8 +386,7 @@ _http_input_stream_chunked_read(http_input_stream_t * stream, unsigned char *des static int -_http_input_stream_connection_closed_read(http_input_stream_t * stream, - unsigned char *dest, int size) +_http_input_stream_connection_closed_read(struct http_input_stream_t * stream, unsigned char *dest, int size) { int status; herror_t err; @@ -407,7 +406,7 @@ _http_input_stream_connection_closed_read(http_input_stream_t * stream, } static int -_http_input_stream_file_read(http_input_stream_t * stream, unsigned char *dest, int size) +_http_input_stream_file_read(struct http_input_stream_t * stream, unsigned char *dest, int size) { size_t len; @@ -425,7 +424,7 @@ _http_input_stream_file_read(http_input_stream_t * stream, unsigned char *dest, Returns the actual status of the stream. */ int -http_input_stream_is_ready(http_input_stream_t * stream) +http_input_stream_is_ready(struct http_input_stream_t * stream) { /* paranoia check */ if (stream == NULL) @@ -455,7 +454,7 @@ http_input_stream_is_ready(http_input_stream_t * stream) <0 on error */ int -http_input_stream_read(http_input_stream_t * stream, unsigned char *dest, int size) +http_input_stream_read(struct http_input_stream_t * stream, unsigned char *dest, int size) { int len = 0; /* paranoia check */ @@ -505,10 +504,10 @@ HTTP OUTPUT STREAM /** Creates a new output stream. Transfer code will be found from header. */ -http_output_stream_t * -http_output_stream_new(hsocket_t *sock, hpair_t * header) +struct http_output_stream_t * +http_output_stream_new(struct hsocket_t *sock, hpair_t * header) { - http_output_stream_t *result; + struct http_output_stream_t *result; char *content_length; /* Paranoya check */ @@ -516,7 +515,7 @@ http_output_stream_new(hsocket_t *sock, hpair_t * header) return NULL; */ /* Create object */ - if (!(result = (http_output_stream_t *) malloc(sizeof(http_output_stream_t)))) + if (!(result = (struct http_output_stream_t *) malloc(sizeof(struct http_output_stream_t)))) { log_error2("malloc failed (%s)", strerror(errno)); return NULL; @@ -555,7 +554,7 @@ http_output_stream_new(hsocket_t *sock, hpair_t * header) Free output stream */ void -http_output_stream_free(http_output_stream_t * stream) +http_output_stream_free(struct http_output_stream_t * stream) { free(stream); @@ -567,7 +566,7 @@ http_output_stream_free(http_output_stream_t * stream) Returns socket error flags or H_OK. */ herror_t -http_output_stream_write(http_output_stream_t * stream, +http_output_stream_write(struct http_output_stream_t * stream, const unsigned char *bytes, int size) { herror_t status; @@ -600,7 +599,7 @@ http_output_stream_write(http_output_stream_t * stream, Returns socket error flags or H_OK. */ herror_t -http_output_stream_write_string(http_output_stream_t * stream, +http_output_stream_write_string(struct http_output_stream_t * stream, const char *str) { return http_output_stream_write(stream, str, strlen(str)); @@ -608,7 +607,7 @@ http_output_stream_write_string(http_output_stream_t * stream, herror_t -http_output_stream_flush(http_output_stream_t * stream) +http_output_stream_flush(struct http_output_stream_t * stream) { herror_t status; diff --git a/nanohttp/nanohttp-stream.h b/nanohttp/nanohttp-stream.h index 88e783e..cb37deb 100755 --- a/nanohttp/nanohttp-stream.h +++ b/nanohttp/nanohttp-stream.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-stream.h,v 1.11 2006/11/19 09:40:14 m0gg Exp $ + * $Id: nanohttp-stream.h,v 1.12 2006/11/23 15:27:33 m0gg Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -24,35 +24,38 @@ #ifndef __nanohttp_stream_h #define __nanohttp_stream_h -/* - HTTP Stream modul: - - nanohttp supports 2 different streams: - - 1. http_input_stream_t - 2. http_output_stream_t - - These are not regular streams. They will care about - transfer styles while sending/receiving data. - - Supported transfer styles are - - o Content-length - o Chunked encoding - o Connection: "close" - - A stream will set its transfer style from the header - information, which must be given while creating a stream. - - A stream will start sending/receiving data "after" - sending/receiving header information. (After <CF><CF>)" - -*/ +/** + * + * HTTP Stream modul: + * + * nanohttp supports 2 different streams: + * + * 1. http_input_stream_t + * 2. http_output_stream_t + * + * These are not regular streams. They will care about transfer styles while + * sending/receiving data. + * + * Supported transfer styles are + * + * - Content-length + * - Chunked encoding + * - Connection: "close" + * + * A stream will set its transfer style from the header information, which must + * be given while creating a stream. + * + * A stream will start sending/receiving data "after" sending/receiving header + * information. (After <CF><CF>)" + * + */ /** - Transfer types supported while - sending/receiving data. -*/ + * + * Transfer types supported while + * sending/receiving data. + * + */ typedef enum http_transfer_type { /** The stream cares about Content-length */ @@ -61,23 +64,23 @@ typedef enum http_transfer_type /** The stream sends/receives chunked data */ HTTP_TRANSFER_CHUNKED, - /** The stream sends/receives data until - connection is closed */ + /** The stream sends/receives data until connection is closed */ HTTP_TRANSFER_CONNECTION_CLOSE, - /** This transfer style will be used by MIME support - and for debug purposes.*/ + /** This transfer style will be used by MIME support and for debug purposes.*/ HTTP_TRANSFER_FILE } http_transfer_type_t; /** - HTTP INPUT STREAM. Receives data from a socket/file - and cares about the transfer style. -*/ -typedef struct http_input_stream + * + * HTTP INPUT STREAM. Receives data from a socket/file + * and cares about the transfer style. + * + */ +struct http_input_stream_t { - hsocket_t *sock; + struct hsocket_t *sock; herror_t err; http_transfer_type_t type; int received; @@ -89,180 +92,175 @@ typedef struct http_input_stream FILE *fd; char filename[255]; int deleteOnExit; /* default is 0 */ -} http_input_stream_t; - +}; /** - HTTP OUTPUT STREAM. Sends data to a socket - and cares about the transfer style. -*/ -typedef struct http_output_stream + * + * HTTP OUTPUT STREAM. Sends data to a socket + * and cares about the transfer style. + * + */ +struct http_output_stream_t { - hsocket_t *sock; + struct hsocket_t *sock; http_transfer_type_t type; int content_length; int sent; -} http_output_stream_t; +}; #ifdef __cplusplus extern "C" { #endif -/* --------------------------------------------------------------- - HTTP INPUT STREAM --------------------------------------------------------------- -*/ - /** - Creates a new input stream. The transfer style will be - choosen from the given header. - - @param sock the socket to receive data from - @param header the http header. This must be received before - creating a http_input_stream_t. - - @returns a newly created http_input_stream_t object. If no - transfer style was found in the header, - HTTP_TRANSFER_CONNECTION_CLOSE will be used as default. - - @see http_input_stream_free -*/ -http_input_stream_t *http_input_stream_new(hsocket_t *sock, hpair_t *header); - + * + * Creates a new input stream. The transfer style will be + * choosen from the given header. + * + * @param sock the socket to receive data from + * @param header the http header. This must be received before + * creating a http_input_stream_t. + * + * @returns a newly created http_input_stream_t object. If no transfer style was + * found in the header, HTTP_TRANSFER_CONNECTION_CLOSE will be used as + * default. + * + * @see http_input_stream_free + * + */ +extern struct http_input_stream_t *http_input_stream_new(struct hsocket_t *sock, hpair_t *header); /** - Creates a new input stream from file. - This function was added for MIME messages - and for debugging. The transfer style is always - HTTP_TRANSFER_FILE. - - @param filename the name of the file to open and read. - - @returns The return value is a http_input_stream_t object - if the file exists and could be opened. NULL otherwise. - - @see http_input_stream_free -*/ -http_input_stream_t *http_input_stream_new_from_file(const char *filename); - + * + * Creates a new input stream from file. This function was added for MIME + * messages and for debugging. The transfer style is always HTTP_TRANSFER_FILE. + * + * @param filename the name of the file to open and read. + * + * @returns The return value is a http_input_stream_t object if the file exists + * and could be opened. NULL otherwise. + * + * @see http_input_stream_free + * + */ +extern struct http_input_stream_t *http_input_stream_new_from_file(const char *filename); /** - Free input stream. Note that the socket will not be closed - by this functions. - - @param stream the input stream to free. -*/ -void http_input_stream_free(http_input_stream_t * stream); - + * + * Free input stream. Note that the socket will not be closed by this functions. + * + * @param stream the input stream to free. + * + */ +extern void http_input_stream_free(struct http_input_stream_t * stream); /** - Returns the actual status of the stream. - - @param stream the stream to check its status - @returns <br>1 if there are still data to read. - <br>0 if no more data exists. -*/ -int http_input_stream_is_ready(http_input_stream_t * stream); - + * + * Returns the actual status of the stream. + * + * @param stream the stream to check its status + * + * @returns <br>1 if there are still data to read. + * <br>0 if no more data exists. + */ +extern int http_input_stream_is_ready(struct http_input_stream_t * stream); /** - Tries to read 'size' bytes from the stream. Check always - with http_input_stream_is_ready() if there are some data - to read. If it returns 0, no more data is available on - stream. - <P> - On error this function will return -1. In this case the - "err" field of stream will be set to the actual error. - This can be one of the followings: <P> - - <BR>STREAM_ERROR_NO_CHUNK_SIZE - <BR>STREAM_ERROR_WRONG_CHUNK_SIZE - <BR>STREAM_ERROR_INVALID_TYPE - <BR>HSOCKET_ERROR_RECEIVE - - @param stream the stream to read data from - @param dest destination memory to store readed bytes - @param size maximum size of 'dest' (size to read) - - @returns the actual readed bytes or -1 on error. -*/ -int http_input_stream_read(http_input_stream_t * stream, - unsigned char* dest, int size); - - -/* --------------------------------------------------------------- - HTTP OUTPUT STREAM --------------------------------------------------------------- -*/ + * + * Tries to read 'size' bytes from the stream. Check always with + * http_input_stream_is_ready() if there are some data to read. If it returns 0, + * no more data is available on stream. + * + * On error this function will return -1. In this case the "err" field of stream + * will be set to the actual error. This can be one of the followings: + * + * - STREAM_ERROR_NO_CHUNK_SIZE + * - STREAM_ERROR_WRONG_CHUNK_SIZE + * - STREAM_ERROR_INVALID_TYPE + * - HSOCKET_ERROR_RECEIVE + * + * @param stream the stream to read data from + * @param dest destination memory to store readed bytes + * @param size maximum size of 'dest' (size to read) + * + * @returns the actual readed bytes or -1 on error. + * + */ +extern int http_input_stream_read(struct http_input_stream_t * stream, unsigned char *dest, int size); /** - Creates a new output stream. Transfer style will be found - from the given header. - - @param sock the socket to to send data to - @param header the header which must be sent before - - @returns a http_output_stream_t object. If no proper transfer - style was found in the header, HTTP_TRANSFER_CONNECTION_CLOSE - will be used as default. - - @see http_output_stream_free -*/ -http_output_stream_t *http_output_stream_new(hsocket_t *sock, - hpair_t * header); + * + * Creates a new output stream. Transfer style will be found from the given + * header. + * + * @param sock the socket to to send data to + * @param header the header which must be sent before + * + * @returns a http_output_stream_t object. If no proper transfer style was found + * in the header, HTTP_TRANSFER_CONNECTION_CLOSE will be used as default. + * + * @see http_output_stream_free + * + */ +extern struct http_output_stream_t *http_output_stream_new(struct hsocket_t *sock, hpair_t * header); /** - Free output stream. Note that this functions will not - close any socket connections. - - @param stream the stream to free. -*/ -void http_output_stream_free(http_output_stream_t * stream); - + * + * Free output stream. Note that this functions will not close any socket + * connections. + * + * @param stream the stream to free. + * + */ +extern void http_output_stream_free(struct http_output_stream_t * stream); /** - Writes 'size' bytes of 'bytes' into stream. - - @param stream the stream to use to send data - @param bytes bytes to send - @param size size of bytes to send - - @returns H_OK if success. One of the followings otherwise - <BR>HSOCKET_ERROR_NOT_INITIALIZED - <BR>HSOCKET_ERROR_SEND -*/ -herror_t http_output_stream_write(http_output_stream_t * stream, - const unsigned char* bytes, int size); + * + * Writes 'size' bytes of 'bytes' into stream. + * + * @param stream the stream to use to send data + * @param bytes bytes to send + * @param size size of bytes to send + * + * @returns H_OK if success. One of the followings otherwise + * + * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_SEND + * + */ +extern herror_t http_output_stream_write(struct http_output_stream_t *stream, const unsigned char* bytes, int size); /** - Writes a null terminated string to the stream. - - @param stream the stream to use to send data - @param str a null terminated string to send - - @returns H_OK if success. One of the followings otherwise - <BR>HSOCKET_ERROR_NOT_INITIALIZED - <BR>HSOCKET_ERROR_SEND -*/ -herror_t http_output_stream_write_string(http_output_stream_t * stream, - const char *str); - + * + * Writes a null terminated string to the stream. + * + * @param stream the stream to use to send data + * @param str a null terminated string to send + * + * @returns H_OK if success. One of the followings otherwise + * + * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_SEND + * + */ +extern herror_t http_output_stream_write_string(struct http_output_stream_t *stream, const char *str); /** - Sends finish flags if nesseccary (like in chunked transport). - Call always this function before closing the connections. - - @param stream the stream to send post data. - - @returns H_OK if success. One of the followings otherwise - <BR>HSOCKET_ERROR_NOT_INITIALIZED - <BR>HSOCKET_ERROR_SEND -*/ -herror_t http_output_stream_flush(http_output_stream_t * stream); + * + * Sends finish flags if nesseccary (like in chunked transport). + * Call always this function before closing the connections. + * + * @param stream the stream to send post data. + * + * @returns H_OK if success. One of the followings otherwise + * + * - HSOCKET_ERROR_NOT_INITIALIZED + * - HSOCKET_ERROR_SEND + * + */ +extern herror_t http_output_stream_flush(struct http_output_stream_t *stream); #ifdef __cplusplus } |