From 6457c46897d6e0c63476bf4ba4ca14b4844fac0d Mon Sep 17 00:00:00 2001 From: m0gg Date: Thu, 23 Nov 2006 15:27:32 +0000 Subject: Code cleanup --- nanohttp/nanohttp-server.c | 257 +++++++++++++++------------------------------ 1 file changed, 86 insertions(+), 171 deletions(-) (limited to 'nanohttp/nanohttp-server.c') 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 = - "