diff options
Diffstat (limited to 'nanohttp')
-rw-r--r-- | nanohttp/nanohttp-client.c | 607 | ||||
-rw-r--r-- | nanohttp/nanohttp-client.h | 70 | ||||
-rw-r--r-- | nanohttp/nanohttp-common.c | 1115 | ||||
-rw-r--r-- | nanohttp/nanohttp-common.h | 139 | ||||
-rwxr-xr-x | nanohttp/nanohttp-mime.c | 621 | ||||
-rwxr-xr-x | nanohttp/nanohttp-mime.h | 19 | ||||
-rwxr-xr-x | nanohttp/nanohttp-request.c | 389 | ||||
-rwxr-xr-x | nanohttp/nanohttp-request.h | 18 | ||||
-rwxr-xr-x | nanohttp/nanohttp-response.c | 267 | ||||
-rwxr-xr-x | nanohttp/nanohttp-response.h | 18 | ||||
-rw-r--r-- | nanohttp/nanohttp-server.c | 344 | ||||
-rw-r--r-- | nanohttp/nanohttp-server.h | 60 | ||||
-rw-r--r-- | nanohttp/nanohttp-socket.c | 438 | ||||
-rw-r--r-- | nanohttp/nanohttp-socket.h | 68 | ||||
-rw-r--r-- | nanohttp/nanohttp-ssl.c | 509 | ||||
-rw-r--r-- | nanohttp/nanohttp-ssl.h | 20 | ||||
-rwxr-xr-x | nanohttp/nanohttp-stream.c | 453 | ||||
-rwxr-xr-x | nanohttp/nanohttp-stream.h | 56 |
18 files changed, 2725 insertions, 2486 deletions
diff --git a/nanohttp/nanohttp-client.c b/nanohttp/nanohttp-client.c index 02d6c3c..b5a3fd0 100644 --- a/nanohttp/nanohttp-client.c +++ b/nanohttp/nanohttp-client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-client.c,v 1.31 2006/01/06 15:16:03 mrcsys Exp $ +* $Id: nanohttp-client.c,v 1.32 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -39,8 +39,8 @@ #endif #if 0 -static -int httpc_send_data(httpc_conn_t *conn, const unsigned char* data, size_t size) +static int +httpc_send_data (httpc_conn_t * conn, const unsigned char *data, size_t size) { return -1; } @@ -50,11 +50,11 @@ FUNCTION: httpc_init DESC: Initialize http client connection NOTE: This will be called from soap_client_init_args() ----------------------------------------------------*/ -herror_t -httpc_init(int argc, char *argv[]) +herror_t +httpc_init (int argc, char *argv[]) { - hoption_init_args(argc, argv); - hsocket_module_init(); + hoption_init_args (argc, argv); + hsocket_module_init (); return H_OK; } @@ -63,9 +63,10 @@ httpc_init(int argc, char *argv[]) FUNCTION: httpc_destroy DESC: Destroy the http client module ----------------------------------------------------*/ -void httpc_destroy() +void +httpc_destroy () { - hsocket_module_destroy(); + hsocket_module_destroy (); } @@ -75,23 +76,24 @@ DESC: Creates a new http client connection object You need to create at least 1 http client connection to communicate via http. ----------------------------------------------------*/ -httpc_conn_t * -httpc_new() +httpc_conn_t * +httpc_new () { static int counter = 10000; - httpc_conn_t *res = (httpc_conn_t *) malloc(sizeof(httpc_conn_t)); + httpc_conn_t *res = (httpc_conn_t *) malloc (sizeof (httpc_conn_t)); - if(hsocket_init(&res->sock)!= H_OK){ - return NULL; - } - res->header = NULL; - res->version = HTTP_1_1; - res->out = NULL; - res->_dime_package_nr = 0; - res->_dime_sent_bytes = 0; - res->id = counter++; - res->block = 0; - return res; + if (hsocket_init (&res->sock) != H_OK) + { + return NULL; + } + res->header = NULL; + res->version = HTTP_1_1; + res->out = NULL; + res->_dime_package_nr = 0; + res->_dime_sent_bytes = 0; + res->id = counter++; + res->block = 0; + return res; } @@ -99,30 +101,30 @@ httpc_new() FUNCTION: httpc_free DESC: Free the given http client object. ----------------------------------------------------*/ -void -httpc_free(httpc_conn_t * conn) +void +httpc_free (httpc_conn_t * conn) { - hpair_t *tmp; + hpair_t *tmp; - if (conn == NULL) - return; + if (conn == NULL) + return; - while (conn->header != NULL) + while (conn->header != NULL) { tmp = conn->header; conn->header = conn->header->next; - hpairnode_free(tmp); + hpairnode_free (tmp); } - if (conn->out != NULL) - { - http_output_stream_free(conn->out); - conn->out = NULL; - } + if (conn->out != NULL) + { + http_output_stream_free (conn->out); + conn->out = NULL; + } - hsocket_free(conn->sock); - free(conn); + hsocket_free (conn->sock); + free (conn); } @@ -131,13 +133,13 @@ httpc_free(httpc_conn_t * conn) DESC: Close and free the given http client object. ----------------------------------------------------*/ void -httpc_close_free(httpc_conn_t * conn) +httpc_close_free (httpc_conn_t * conn) { - if (conn == NULL) - return; + if (conn == NULL) + return; - hsocket_close(conn->sock); - httpc_free(conn); + hsocket_close (conn->sock); + httpc_free (conn); } @@ -147,30 +149,34 @@ DESC: Adds a new (key, value) pair to the header or modifies the old pair if this function will finds another pair with the same 'key' value. ----------------------------------------------------*/ -int -httpc_set_header(httpc_conn_t * conn, const char *key, const char *value) +int +httpc_set_header (httpc_conn_t * conn, const char *key, const char *value) { - hpair_t *p; + hpair_t *p; - if (conn == NULL) { - log_warn1("Connection object is NULL"); - return 0; - } - p = conn->header; - while (p != NULL) { - if (p->key != NULL) { - if (!strcmp(p->key, key)) { - free(p->value); - p->value = (char *) malloc(strlen(value) + 1); - strcpy(p->value, value); - return 1; - } - } - p = p->next; - } + if (conn == NULL) + { + log_warn1 ("Connection object is NULL"); + return 0; + } + p = conn->header; + while (p != NULL) + { + if (p->key != NULL) + { + if (!strcmp (p->key, key)) + { + free (p->value); + p->value = (char *) malloc (strlen (value) + 1); + strcpy (p->value, value); + return 1; + } + } + p = p->next; + } - conn->header = hpairnode_new(key, value, conn->header); - return 0; + conn->header = hpairnode_new (key, value, conn->header); + return 0; } @@ -178,36 +184,34 @@ httpc_set_header(httpc_conn_t * conn, const char *key, const char *value) FUNCTION: httpc_header_add_date DESC: Adds the current date to the header. ----------------------------------------------------*/ -static -void _httpc_set_error(httpc_conn_t *conn, int errcode, - const char *format, ...) +static void +_httpc_set_error (httpc_conn_t * conn, int errcode, const char *format, ...) { - va_list ap; + va_list ap; conn->errcode = errcode; - va_start(ap, format); - vsprintf(conn->errmsg, format, ap); - va_end(ap); + va_start (ap, format); + vsprintf (conn->errmsg, format, ap); + va_end (ap); } /*-------------------------------------------------- FUNCTION: httpc_header_add_date DESC: Adds the current date to the header. ----------------------------------------------------*/ -static -void -httpc_header_add_date(httpc_conn_t * conn) +static void +httpc_header_add_date (httpc_conn_t * conn) { - char buffer[255]; - time_t nw; - struct tm stm; + char buffer[255]; + time_t nw; + struct tm stm; - /* Set date */ - nw = time(NULL); - localtime_r(&nw, &stm); - strftime(buffer, 255, "%a, %d %b %Y %T GMT", &stm); - httpc_set_header(conn, HEADER_DATE, buffer); + /* Set date */ + nw = time (NULL); + localtime_r (&nw, &stm); + strftime (buffer, 255, "%a, %d %b %Y %T GMT", &stm); + httpc_set_header (conn, HEADER_DATE, buffer); } @@ -217,26 +221,28 @@ FUNCTION: httpc_send_header DESC: Sends the current header information stored in conn through conn->sock. ----------------------------------------------------*/ -herror_t -httpc_send_header(httpc_conn_t * conn) +herror_t +httpc_send_header (httpc_conn_t * conn) { - hpair_t *p; - herror_t status; - char buffer[1024]; - - p = conn->header; - while (p != NULL) { - if (p->key && p->value) { - sprintf(buffer, "%s: %s\r\n", p->key, p->value); - status = hsocket_send(conn->sock, buffer); - if (status != H_OK) - return status; - } - p = p->next; - } + hpair_t *p; + herror_t status; + char buffer[1024]; - status = hsocket_send(conn->sock, "\r\n"); - return status; + p = conn->header; + while (p != NULL) + { + if (p->key && p->value) + { + sprintf (buffer, "%s: %s\r\n", p->key, p->value); + status = hsocket_send (conn->sock, buffer); + if (status != H_OK) + return status; + } + p = p->next; + } + + status = hsocket_send (conn->sock, "\r\n"); + return status; } /*-------------------------------------------------- @@ -284,90 +290,98 @@ can also be NULL. If success, this function will return 0. >0 otherwise. ----------------------------------------------------*/ -static -herror_t -httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, - const char *urlstr) +static herror_t +httpc_talk_to_server (hreq_method_t method, httpc_conn_t * conn, + const char *urlstr) { - hurl_t url; - char buffer[4096]; - herror_t status; + hurl_t url; + char buffer[4096]; + herror_t status; - if (conn == NULL) { - return herror_new( - "httpc_talk_to_server", - GENERAL_INVALID_PARAM, - "httpc_conn_t param is NULL"); - } - /* Build request header */ - httpc_header_add_date(conn); + if (conn == NULL) + { + return herror_new ("httpc_talk_to_server", + GENERAL_INVALID_PARAM, "httpc_conn_t param is NULL"); + } + /* Build request header */ + httpc_header_add_date (conn); - /* Create url */ - status = hurl_parse(&url, urlstr); - if (status != H_OK) { - log_error2("Can not parse URL '%s'", SAVE_STR(urlstr)); - return status; - } + /* Create url */ + status = hurl_parse (&url, urlstr); + if (status != H_OK) + { + log_error2 ("Can not parse URL '%s'", SAVE_STR (urlstr)); + return status; + } /* TODO (#1#): Check for HTTP protocol in URL */ - /* Set hostname */ - httpc_set_header(conn, HEADER_HOST, url.host); + /* Set hostname */ + httpc_set_header (conn, HEADER_HOST, url.host); - /* Open connection */ - status = hsocket_open(&conn->sock, url.host, url.port); - if (status != H_OK) { - return status; - } + /* Open connection */ + status = hsocket_open (&conn->sock, url.host, url.port); + if (status != H_OK) + { + return status; + } #ifdef HAVE_SSL - /* TODO XXX XXX this is probably not right -- matt */ - if(!&conn->sock.ssl){ - status = hsocket_block(conn->sock, conn->block); - if (status != H_OK) { - log_error1("Cannot make socket non-blocking"); - return status; - } + /* TODO XXX XXX this is probably not right -- matt */ + if (!&conn->sock.ssl) + { + status = hsocket_block (conn->sock, conn->block); + if (status != H_OK) + { + log_error1 ("Cannot make socket non-blocking"); + return status; } + } #endif - /* check method */ - if (method == HTTP_REQUEST_GET) { - - /* Set GET Header */ - sprintf(buffer, "GET %s HTTP/%s\r\n", - (url.context[0] != '\0') ? url.context : ("/"), - (conn->version == HTTP_1_0)?"1.0":"1.1"); - - } else if (method == HTTP_REQUEST_POST) { - - /* Set POST Header */ - sprintf(buffer, "POST %s HTTP/%s\r\n", - (url.context[0] != '\0') ? url.context : ("/"), - (conn->version == HTTP_1_0)?"1.0":"1.1"); - } else { - - log_error1("Unknown method type!"); - return herror_new( - "httpc_talk_to_server", - GENERAL_INVALID_PARAM, - "hreq_method_t must be HTTP_REQUEST_GET or HTTP_REQUEST_POST"); - } + /* check method */ + if (method == HTTP_REQUEST_GET) + { - log_verbose1("Sending header..."); - status = hsocket_send(conn->sock, buffer); - if (status != H_OK) { - log_error2("Can not send request (status:%d)", status); - hsocket_close(conn->sock); - return status; - } - /* Send Header */ - status = httpc_send_header(conn); - if (status != H_OK) { - log_error2("Can not send header (status:%d)", status); - hsocket_close(conn->sock); - return status; - } + /* Set GET Header */ + sprintf (buffer, "GET %s HTTP/%s\r\n", + (url.context[0] != '\0') ? url.context : ("/"), + (conn->version == HTTP_1_0) ? "1.0" : "1.1"); + + } + else if (method == HTTP_REQUEST_POST) + { + + /* Set POST Header */ + sprintf (buffer, "POST %s HTTP/%s\r\n", + (url.context[0] != '\0') ? url.context : ("/"), + (conn->version == HTTP_1_0) ? "1.0" : "1.1"); + } + else + { + + log_error1 ("Unknown method type!"); + return herror_new ("httpc_talk_to_server", + GENERAL_INVALID_PARAM, + "hreq_method_t must be HTTP_REQUEST_GET or HTTP_REQUEST_POST"); + } + + log_verbose1 ("Sending header..."); + status = hsocket_send (conn->sock, buffer); + if (status != H_OK) + { + log_error2 ("Can not send request (status:%d)", status); + hsocket_close (conn->sock); + return status; + } + /* Send Header */ + status = httpc_send_header (conn); + if (status != H_OK) + { + log_error2 ("Can not send header (status:%d)", status); + hsocket_close (conn->sock); + return status; + } - return H_OK; + return H_OK; } @@ -376,25 +390,25 @@ FUNCTION: httpc_get DESC: ----------------------------------------------------*/ herror_t -httpc_get(httpc_conn_t *conn, hresponse_t** out, const char *urlstr) +httpc_get (httpc_conn_t * conn, hresponse_t ** out, const char *urlstr) { - herror_t status; + herror_t status; - status = httpc_talk_to_server(HTTP_REQUEST_GET, conn, urlstr); + status = httpc_talk_to_server (HTTP_REQUEST_GET, conn, urlstr); - if (status != H_OK) - { - return status; - } + if (status != H_OK) + { + return status; + } - status = hresponse_new_from_socket(conn->sock, out); - if (status != H_OK) - { - return status; - } - + status = hresponse_new_from_socket (conn->sock, out); + if (status != H_OK) + { + return status; + } - return H_OK; + + return H_OK; } @@ -402,16 +416,17 @@ httpc_get(httpc_conn_t *conn, hresponse_t** out, const char *urlstr) FUNCTION: httpc_post_begin DESC: Returns H_OK if success ----------------------------------------------------*/ -herror_t httpc_post_begin(httpc_conn_t *conn, const char *url) +herror_t +httpc_post_begin (httpc_conn_t * conn, const char *url) { - - herror_t status; - status = httpc_talk_to_server(HTTP_REQUEST_POST, conn, url); - if (status != H_OK) - return status; + herror_t status; - conn->out = http_output_stream_new(conn->sock, conn->header); + status = httpc_talk_to_server (HTTP_REQUEST_POST, conn, url); + if (status != H_OK) + return status; + + conn->out = http_output_stream_new (conn->sock, conn->header); return H_OK; } @@ -422,24 +437,25 @@ FUNCTION: httpc_post_begin DESC: End a "POST" method and receive the response. You MUST call httpc_post_end() before! ----------------------------------------------------*/ -herror_t httpc_post_end(httpc_conn_t *conn, hresponse_t** out) +herror_t +httpc_post_end (httpc_conn_t * conn, hresponse_t ** out) { herror_t status; - status = http_output_stream_flush(conn->out); + status = http_output_stream_flush (conn->out); if (status != H_OK) { return status; } - status = hresponse_new_from_socket(conn->sock, out); - if (status != H_OK) - { - return status; - } + status = hresponse_new_from_socket (conn->sock, out); + if (status != H_OK) + { + return status; + } - return H_OK; + return H_OK; } @@ -595,128 +611,134 @@ hresponse_t* httpc_dime_end(httpc_conn_t *conn) MIME support functions httpc_mime_* function set -----------------------------------------------------*/ -static -void _httpc_mime_get_boundary(httpc_conn_t *conn, char *dest) +static void +_httpc_mime_get_boundary (httpc_conn_t * conn, char *dest) { - sprintf(dest, "---=.Part_NH_%d", conn->id); - log_verbose2("boundary= \"%s\"", dest); + sprintf (dest, "---=.Part_NH_%d", conn->id); + log_verbose2 ("boundary= \"%s\"", dest); } -herror_t httpc_mime_begin(httpc_conn_t *conn, const char *url, - const char* related_start, - const char* related_start_info, - const char* related_type) +herror_t +httpc_mime_begin (httpc_conn_t * conn, const char *url, + const char *related_start, + const char *related_start_info, const char *related_type) { - herror_t status; - char buffer[300]; - char temp[75]; - char boundary[75]; - - /* - Set Content-type - Set multipart/related parameter - type=..; start=.. ; start-info= ..; boundary=... - - */ - sprintf(buffer, "multipart/related;"); - /* - using sprintf instead of snprintf because visual c does not support snprintf - */ + herror_t status; + char buffer[300]; + char temp[75]; + char boundary[75]; + + /* + Set Content-type Set multipart/related parameter type=..; start=.. ; + start-info= ..; boundary=... + + */ + sprintf (buffer, "multipart/related;"); + /* + using sprintf instead of snprintf because visual c does not support + snprintf */ #ifdef WIN32 #define snprintf(buffer, num, s1, s2) sprintf(buffer, s1,s2) #endif - if (related_type) { - snprintf(temp, 75, " type=\"%s\";", related_type); - strcat(buffer, temp); + if (related_type) + { + snprintf (temp, 75, " type=\"%s\";", related_type); + strcat (buffer, temp); } - - if (related_start) { - snprintf(temp, 75, " start=\"%s\";", related_start); - strcat(buffer, temp); + + if (related_start) + { + snprintf (temp, 75, " start=\"%s\";", related_start); + strcat (buffer, temp); } - if (related_start_info) { - snprintf(temp, 75, " start-info=\"%s\";", related_start_info); - strcat(buffer, temp); + if (related_start_info) + { + snprintf (temp, 75, " start-info=\"%s\";", related_start_info); + strcat (buffer, temp); } - _httpc_mime_get_boundary(conn, boundary); - snprintf(temp, 75, " boundary=\"%s\"", boundary); - strcat(buffer, temp); + _httpc_mime_get_boundary (conn, boundary); + snprintf (temp, 75, " boundary=\"%s\"", boundary); + strcat (buffer, temp); - httpc_set_header(conn, HEADER_CONTENT_TYPE, buffer); + httpc_set_header (conn, HEADER_CONTENT_TYPE, buffer); - status = httpc_post_begin(conn, url); - return status; + status = httpc_post_begin (conn, url); + return status; } -herror_t httpc_mime_next(httpc_conn_t *conn, - const char* content_id, - const char* content_type, - const char* transfer_encoding) +herror_t +httpc_mime_next (httpc_conn_t * conn, + const char *content_id, + const char *content_type, const char *transfer_encoding) { - herror_t status; - char buffer[512]; - char boundary[75]; + herror_t status; + char buffer[512]; + char boundary[75]; /* Get the boundary string */ - _httpc_mime_get_boundary(conn, boundary); - sprintf(buffer, "\r\n--%s\r\n", boundary); + _httpc_mime_get_boundary (conn, boundary); + sprintf (buffer, "\r\n--%s\r\n", boundary); /* Send boundary */ - status = http_output_stream_write(conn->out, - (const byte_t*)buffer, strlen(buffer)); + status = http_output_stream_write (conn->out, + (const byte_t *) buffer, + strlen (buffer)); - if (status != H_OK) - return status; + if (status != H_OK) + return status; - /* Send Content header */ - sprintf(buffer, "%s: %s\r\n%s: %s\r\n%s: %s\r\n\r\n", - HEADER_CONTENT_TYPE, content_type, - HEADER_CONTENT_TRANSFER_ENCODING, transfer_encoding, - HEADER_CONTENT_ID, content_id); + /* Send Content header */ + sprintf (buffer, "%s: %s\r\n%s: %s\r\n%s: %s\r\n\r\n", + HEADER_CONTENT_TYPE, content_type, + HEADER_CONTENT_TRANSFER_ENCODING, transfer_encoding, + HEADER_CONTENT_ID, content_id); - status = http_output_stream_write(conn->out, - (const byte_t*)buffer, strlen(buffer)); + status = http_output_stream_write (conn->out, + (const byte_t *) buffer, + strlen (buffer)); - return status; + return status; } -herror_t httpc_mime_end(httpc_conn_t *conn, hresponse_t** out) +herror_t +httpc_mime_end (httpc_conn_t * conn, hresponse_t ** out) { - herror_t status; - char buffer[512]; - char boundary[75]; + herror_t status; + char buffer[512]; + char boundary[75]; /* Get the boundary string */ - _httpc_mime_get_boundary(conn, boundary); - sprintf(buffer, "\r\n--%s--\r\n\r\n", boundary); + _httpc_mime_get_boundary (conn, boundary); + sprintf (buffer, "\r\n--%s--\r\n\r\n", boundary); /* Send boundary */ - status = http_output_stream_write(conn->out, - (const byte_t*)buffer, strlen(buffer)); + status = http_output_stream_write (conn->out, + (const byte_t *) buffer, + strlen (buffer)); if (status != H_OK) return status; /* Flush put stream */ - status = http_output_stream_flush(conn->out); + status = http_output_stream_flush (conn->out); if (status != H_OK) { return status; } - status = hresponse_new_from_socket(conn->sock, out); - if (status != H_OK) - { - return status; - } - - return H_OK; + status = hresponse_new_from_socket (conn->sock, out); + if (status != H_OK) + { + return status; + } + + return H_OK; } @@ -737,11 +759,11 @@ httpc_mime_send_file (httpc_conn_t * conn, size_t size; if (fd == NULL) - return herror_new("httpc_mime_send_file", FILE_ERROR_OPEN, - "Can not open file '%s'", filename); + return herror_new ("httpc_mime_send_file", FILE_ERROR_OPEN, + "Can not open file '%s'", filename); status = - httpc_mime_next(conn, content_id, content_type, transfer_encoding); + httpc_mime_next (conn, content_id, content_type, transfer_encoding); if (status != H_OK) { fclose (fd); @@ -754,24 +776,23 @@ httpc_mime_send_file (httpc_conn_t * conn, if (size == -1) { fclose (fd); - return herror_new("httpc_mime_send_file", FILE_ERROR_READ, - "Can not read from file '%s'", filename); + return herror_new ("httpc_mime_send_file", FILE_ERROR_READ, + "Can not read from file '%s'", filename); } - if (size>0) - { - /*DEBUG: fwrite(buffer, 1, size, stdout);*/ - status = http_output_stream_write (conn->out, buffer, size); - if (status != H_OK) { - fclose (fd); - return status; - } - } + if (size > 0) + { + /* DEBUG: fwrite(buffer, 1, size, stdout); */ + status = http_output_stream_write (conn->out, buffer, size); + if (status != H_OK) + { + fclose (fd); + return status; + } + } } fclose (fd); - log_verbose1("file sent!"); + log_verbose1 ("file sent!"); return H_OK; } - - diff --git a/nanohttp/nanohttp-client.h b/nanohttp/nanohttp-client.h index 395b29a..77c8e8e 100644 --- a/nanohttp/nanohttp-client.h +++ b/nanohttp/nanohttp-client.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-client.h,v 1.16 2005/12/19 14:18:26 snowdrop Exp $ + * $Id: nanohttp-client.h,v 1.17 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -21,8 +21,8 @@ * * Email: ferhatayaz@yahoo.com ******************************************************************/ -#ifndef NANO_HTTP_CLIENT_H -#define NANO_HTTP_CLIENT_H +#ifndef NANO_HTTP_CLIENT_H +#define NANO_HTTP_CLIENT_H #include <nanohttp/nanohttp-common.h> @@ -36,19 +36,16 @@ typedef struct httpc_conn hpair_t *header; hurl_t url; http_version_t version; - /* - -1 : last dime package - 0 : no dime connection - >0 : dime package number - */ + /* + -1 : last dime package 0 : no dime connection >0 : dime package number */ int _dime_package_nr; - long _dime_sent_bytes; + long _dime_sent_bytes; int errcode; char errmsg[150]; http_output_stream_t *out; - int id; /* uniq id */ + int id; /* uniq id */ int block; -}httpc_conn_t; +} httpc_conn_t; /* -------------------------------------------------------------- @@ -58,56 +55,57 @@ typedef struct httpc_conn /** initialize the httpc_* module */ -herror_t httpc_init(int argc, char *argv[]); - +herror_t httpc_init (int argc, char *argv[]); + /** Destroy the httpc_* module */ -void httpc_destroy(); +void httpc_destroy (); /** Creates a new connection */ -httpc_conn_t* httpc_new(); +httpc_conn_t *httpc_new (); /** Close and release a connection */ -void httpc_close_free(httpc_conn_t* conn); +void httpc_close_free (httpc_conn_t * conn); /** Release a connection (use httpc_close_free() instead) */ -void httpc_free(httpc_conn_t* conn); +void httpc_free (httpc_conn_t * conn); /** * Close and release a connection */ -void httpc_close_free(httpc_conn_t* conn); +void httpc_close_free (httpc_conn_t * conn); /** Set header element (key,value) pair. */ -int httpc_set_header(httpc_conn_t *conn, const char* key, const char* value); +int httpc_set_header (httpc_conn_t * conn, const char *key, + const char *value); /** Invoke a "GET" method request and receive the response */ herror_t -httpc_get(httpc_conn_t *conn, hresponse_t** out, const char *urlstr); +httpc_get (httpc_conn_t * conn, hresponse_t ** out, const char *urlstr); /** Start a "POST" method request Returns: HSOCKET_OK or error flag */ -herror_t httpc_post_begin(httpc_conn_t *conn, const char *url); +herror_t httpc_post_begin (httpc_conn_t * conn, const char *url); /** End a "POST" method and receive the response. You MUST call httpc_post_end() before! */ -herror_t httpc_post_end(httpc_conn_t *conn, hresponse_t **out); +herror_t httpc_post_end (httpc_conn_t * conn, hresponse_t ** out); /* -------------------------------------------------------------- @@ -136,24 +134,24 @@ hresponse_t* httpc_dime_end(httpc_conn_t *conn); Begin MIME multipart/related POST request Returns: HSOCKET_OK or error flag */ -herror_t httpc_mime_begin(httpc_conn_t *conn, const char *url, - const char* related_start, - const char* related_start_info, - const char* related_type); +herror_t httpc_mime_begin (httpc_conn_t * conn, const char *url, + const char *related_start, + const char *related_start_info, + const char *related_type); /** Send boundary and part header and continue with next part */ -herror_t httpc_mime_next(httpc_conn_t *conn, - const char* content_id, - const char* content_type, - const char* transfer_encoding); +herror_t httpc_mime_next (httpc_conn_t * conn, + const char *content_id, + const char *content_type, + const char *transfer_encoding); /** Finish MIME request and get the response */ -herror_t httpc_mime_end(httpc_conn_t *conn, hresponse_t** out); +herror_t httpc_mime_end (httpc_conn_t * conn, hresponse_t ** out); /** Send boundary and part header and continue @@ -161,11 +159,9 @@ herror_t httpc_mime_end(httpc_conn_t *conn, hresponse_t** out); */ herror_t httpc_mime_send_file (httpc_conn_t * conn, - const char *content_id, - const char *content_type, - const char *transfer_encoding, - const char *filename); + const char *content_id, + const char *content_type, + const char *transfer_encoding, + const char *filename); #endif - - diff --git a/nanohttp/nanohttp-common.c b/nanohttp/nanohttp-common.c index aa1ab45..7d7b141 100644 --- a/nanohttp/nanohttp-common.c +++ b/nanohttp/nanohttp-common.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-common.c,v 1.20 2006/01/06 16:16:10 snowdrop Exp $ +* $Id: nanohttp-common.c,v 1.21 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -39,7 +39,7 @@ #define MAX_OPTION_SIZE 50 #define MAX_OPTION_VALUE_SIZE 150 -static char _hoption_table[MAX_OPTION_SIZE][MAX_OPTION_VALUE_SIZE]; +static char _hoption_table[MAX_OPTION_SIZE][MAX_OPTION_VALUE_SIZE]; #ifdef HAVE_SSL extern char *SSLCert; @@ -49,49 +49,56 @@ extern int SSLCertLess; #endif /* option stuff */ -void hoption_set(int opt, const char* value) +void +hoption_set (int opt, const char *value) { - if (opt >= MAX_OPTION_SIZE) { - log_warn3("Option to high (%d >= %d)", opt, MAX_OPTION_SIZE); - return; - } + if (opt >= MAX_OPTION_SIZE) + { + log_warn3 ("Option to high (%d >= %d)", opt, MAX_OPTION_SIZE); + return; + } - strncpy(_hoption_table[opt], value, MAX_OPTION_VALUE_SIZE); + strncpy (_hoption_table[opt], value, MAX_OPTION_VALUE_SIZE); } -char *hoption_get(int opt) +char * +hoption_get (int opt) { - if (opt >= MAX_OPTION_SIZE) { - log_warn3("Option to high (%d >= %d)", opt, MAX_OPTION_SIZE); - return ""; - } + if (opt >= MAX_OPTION_SIZE) + { + log_warn3 ("Option to high (%d >= %d)", opt, MAX_OPTION_SIZE); + return ""; + } - return _hoption_table[opt]; + return _hoption_table[opt]; } -void hoption_init_args(int argc, char* argv[]) +void +hoption_init_args (int argc, char *argv[]) { int i; - hoption_set(HOPTION_TMP_DIR, "."); /* default value */ + hoption_set (HOPTION_TMP_DIR, "."); /* default value */ /* initialize from arguments */ for (i = 0; i < argc; i++) { if (!strcmp (argv[i], NHTTP_ARG_TMPDIR) && i < argc - 1) { - hoption_set(HOPTION_TMP_DIR, argv[i+1]); + hoption_set (HOPTION_TMP_DIR, argv[i + 1]); } else if (!strcmp (argv[i], NHTTP_ARG_LOGFILE) && i < argc - 1) { - log_set_file(argv[i+1]); + log_set_file (argv[i + 1]); } else if (!strcmp (argv[i], NHTTP_ARG_CERT) && i < argc - 1) { #ifndef HAVE_SSL - fprintf(stderr,"WARNING: csoap compiled without '--with-ssl' flag. Parameter '%s' is disabled", NHTTP_ARG_CERT); + fprintf (stderr, + "WARNING: csoap compiled without '--with-ssl' flag. Parameter '%s' is disabled", + NHTTP_ARG_CERT); #else SSLCert = argv[i + 1]; #endif @@ -99,7 +106,9 @@ void hoption_init_args(int argc, char* argv[]) else if (!strcmp (argv[i], NHTTP_ARG_CERTPASS) && i < argc - 1) { #ifndef HAVE_SSL - fprintf(stderr,"WARNING: csoap compiled without '--with-ssl' flag. Parameter '%s' is disabled", NHTTP_ARG_CERTPASS); + fprintf (stderr, + "WARNING: csoap compiled without '--with-ssl' flag. Parameter '%s' is disabled", + NHTTP_ARG_CERTPASS); #else SSLPass = argv[i + 1]; #endif @@ -107,15 +116,19 @@ void hoption_init_args(int argc, char* argv[]) else if (!strcmp (argv[i], NHTTP_ARG_CA) && i < argc - 1) { #ifndef HAVE_SSL - fprintf(stderr,"WARNING: csoap compiled without '--with-ssl' flag. Parameter '%s' is disabled", NHTTP_ARG_CA); + fprintf (stderr, + "WARNING: csoap compiled without '--with-ssl' flag. Parameter '%s' is disabled", + NHTTP_ARG_CA); #else SSLCA = argv[i + 1]; #endif } - else if (!strcmp (argv[i], NHTTP_ARG_HTTPS) ) + else if (!strcmp (argv[i], NHTTP_ARG_HTTPS)) { #ifndef HAVE_SSL - fprintf(stderr,"WARNING: csoap compiled without '--with-ssl' flag. Parameter '%s' is disabled", NHTTP_ARG_HTTPS); + fprintf (stderr, + "WARNING: csoap compiled without '--with-ssl' flag. Parameter '%s' is disabled", + NHTTP_ARG_HTTPS); #else SSLCertLess = 1; #endif @@ -127,613 +140,667 @@ void hoption_init_args(int argc, char* argv[]) #ifdef WIN32 -#ifndef __MINGW32__ +#ifndef __MINGW32__ /* not thread safe!*/ -char *VisualC_funcname(const char* file, int line) +char * +VisualC_funcname (const char *file, int line) { - static char buffer[256]; - int i = strlen(file)-1; - while (i>0 && file[i]!='\\')i--; - sprintf(buffer, "%s:%d", (file[i]!='\\')?file:(file+i+1), line); - return buffer; -} + static char buffer[256]; + int i = strlen (file) - 1; + while (i > 0 && file[i] != '\\') + i--; + sprintf (buffer, "%s:%d", (file[i] != '\\') ? file : (file + i + 1), line); + return buffer; +} #endif #endif typedef struct _herror_impl_t { - int errcode; - char message[250]; - char func[100]; -}herror_impl_t; + int errcode; + char message[250]; + char func[100]; +} herror_impl_t; -herror_t herror_new(const char* func, int errcode, const char* format, ...) +herror_t +herror_new (const char *func, int errcode, const char *format, ...) { - va_list ap; + va_list ap; - herror_impl_t *impl = (herror_impl_t*)malloc(sizeof(herror_impl_t)); - impl->errcode = errcode; - strcpy(impl->func, func); - va_start(ap, format); - vsprintf(impl->message, format, ap); - va_end(ap); + herror_impl_t *impl = (herror_impl_t *) malloc (sizeof (herror_impl_t)); + impl->errcode = errcode; + strcpy (impl->func, func); + va_start (ap, format); + vsprintf (impl->message, format, ap); + va_end (ap); - return (herror_t)impl; + return (herror_t) impl; } -int herror_code(herror_t err) +int +herror_code (herror_t err) { - herror_impl_t* impl = (herror_impl_t*)err; - if (!err) return H_OK; - return impl->errcode; + herror_impl_t *impl = (herror_impl_t *) err; + if (!err) + return H_OK; + return impl->errcode; } -char* herror_func(herror_t err) +char * +herror_func (herror_t err) { - herror_impl_t* impl = (herror_impl_t*)err; - if (!err) return ""; - return impl->func; + herror_impl_t *impl = (herror_impl_t *) err; + if (!err) + return ""; + return impl->func; } -char* herror_message(herror_t err) +char * +herror_message (herror_t err) { - herror_impl_t* impl = (herror_impl_t*)err; - if (!err) return ""; - return impl->message; + herror_impl_t *impl = (herror_impl_t *) err; + if (!err) + return ""; + return impl->message; } -void herror_release(herror_t err) +void +herror_release (herror_t err) { - herror_impl_t* impl = (herror_impl_t*)err; - if (!err) return; - free(impl); + herror_impl_t *impl = (herror_impl_t *) err; + if (!err) + return; + free (impl); } static log_level_t loglevel = HLOG_DEBUG; -static char logfile[75] = {'\0'}; -static int log_background=0; +static char logfile[75] = { '\0' }; +static int log_background = 0; -log_level_t -log_set_level(log_level_t level) +log_level_t +log_set_level (log_level_t level) { - log_level_t old = loglevel; - loglevel = level; - return old; + log_level_t old = loglevel; + loglevel = level; + return old; } -log_level_t -log_get_level() +log_level_t +log_get_level () { - return loglevel; + return loglevel; } -void log_set_file(const char *filename) +void +log_set_file (const char *filename) { - if (filename) - strncpy(logfile, filename, 75); - else - logfile[0] = '\0'; + if (filename) + strncpy (logfile, filename, 75); + else + logfile[0] = '\0'; } -void log_set_background(int state) +void +log_set_background (int state) { - log_background=state; + log_background = state; } -char *log_get_file() +char * +log_get_file () { - if (logfile[0] == '\0') return NULL; - return logfile; + if (logfile[0] == '\0') + return NULL; + return logfile; } -static -void -log_write(log_level_t level, const char *prefix, - const char *func, const char *format, va_list ap) +static void +log_write (log_level_t level, const char *prefix, + const char *func, const char *format, va_list ap) { - char buffer[1054]; - char buffer2[1054]; - FILE *f; + char buffer[1054]; + char buffer2[1054]; + FILE *f; - if (level < loglevel) - return; + if (level < loglevel) + return; - if(!log_background || log_get_file()){ + if (!log_background || log_get_file ()) + { #ifdef WIN32 - sprintf(buffer, "*%s*: [%s] %s\n", - prefix, func, format); + sprintf (buffer, "*%s*: [%s] %s\n", prefix, func, format); #else - sprintf(buffer, "*%s*:(%d) [%s] %s\n", - prefix, pthread_self(),func, format); + sprintf (buffer, "*%s*:(%d) [%s] %s\n", + prefix, pthread_self (), func, format); #endif - vsprintf(buffer2, buffer, ap); - if(!log_background){ - printf(buffer2); - fflush(stdout); - } + vsprintf (buffer2, buffer, ap); + if (!log_background) + { + printf (buffer2); + fflush (stdout); + } - if (log_get_file()) { - f = fopen(log_get_file(), "a"); - if (!f) f = fopen(log_get_file(), "w"); - if (f) { - fprintf(f, buffer2); - fflush(f); - fclose(f); - } - } - } + if (log_get_file ()) + { + f = fopen (log_get_file (), "a"); + if (!f) + f = fopen (log_get_file (), "w"); + if (f) + { + fprintf (f, buffer2); + fflush (f); + fclose (f); + } + } + } } -void -log_verbose(const char *FUNC, const char *format,...) +void +log_verbose (const char *FUNC, const char *format, ...) { - va_list ap; + va_list ap; - va_start(ap, format); - log_write(HLOG_VERBOSE, "VERBOSE", FUNC, format, ap); - va_end(ap); + va_start (ap, format); + log_write (HLOG_VERBOSE, "VERBOSE", FUNC, format, ap); + va_end (ap); } -void -log_debug(const char *FUNC, const char *format,...) +void +log_debug (const char *FUNC, const char *format, ...) { - va_list ap; + va_list ap; - va_start(ap, format); - log_write(HLOG_DEBUG, "DEBUG", FUNC, format, ap); - va_end(ap); + va_start (ap, format); + log_write (HLOG_DEBUG, "DEBUG", FUNC, format, ap); + va_end (ap); } -void -log_info(const char *FUNC, const char *format,...) +void +log_info (const char *FUNC, const char *format, ...) { - va_list ap; + va_list ap; - va_start(ap, format); - log_write(HLOG_INFO, "INFO", FUNC, format, ap); - va_end(ap); + va_start (ap, format); + log_write (HLOG_INFO, "INFO", FUNC, format, ap); + va_end (ap); } -void -log_warn(const char *FUNC, const char *format,...) +void +log_warn (const char *FUNC, const char *format, ...) { - va_list ap; + va_list ap; - va_start(ap, format); - log_write(HLOG_WARN, "WARN", FUNC, format, ap); - va_end(ap); + va_start (ap, format); + log_write (HLOG_WARN, "WARN", FUNC, format, ap); + va_end (ap); } -void -log_error(const char *FUNC, const char *format,...) +void +log_error (const char *FUNC, const char *format, ...) { - va_list ap; + va_list ap; - va_start(ap, format); - log_write(HLOG_ERROR, "ERROR", FUNC, format, ap); - va_end(ap); + va_start (ap, format); + log_write (HLOG_ERROR, "ERROR", FUNC, format, ap); + va_end (ap); } /* ----------------------------------------- FUNCTION: strcmpigcase ------------------------------------------*/ -int -strcmpigcase(const char *s1, const char *s2) +int +strcmpigcase (const char *s1, const char *s2) { - int l1, l2, i; + int l1, l2, i; - if (s1 == NULL && s2 == NULL) - return 1; - if (s1 == NULL || s2 == NULL) - return 0; + if (s1 == NULL && s2 == NULL) + return 1; + if (s1 == NULL || s2 == NULL) + return 0; - l1 = strlen(s1); - l2 = strlen(s2); + l1 = strlen (s1); + l2 = strlen (s2); - if (l1 != l2) - return 0; + if (l1 != l2) + return 0; - for (i = 0; i < l1; i++) - if (toupper(s1[i]) != toupper(s2[i])) - return 0; + for (i = 0; i < l1; i++) + if (toupper (s1[i]) != toupper (s2[i])) + return 0; - return 1; + return 1; } -hpair_t * -hpairnode_new(const char *key, const char *value, hpair_t * next) +hpair_t * +hpairnode_new (const char *key, const char *value, hpair_t * next) { - hpair_t *pair; + hpair_t *pair; - log_verbose3("new pair ('%s','%s')", SAVE_STR(key), SAVE_STR(value)); - pair = (hpair_t *) malloc(sizeof(hpair_t)); + log_verbose3 ("new pair ('%s','%s')", SAVE_STR (key), SAVE_STR (value)); + pair = (hpair_t *) malloc (sizeof (hpair_t)); - if (key != NULL) { - pair->key = (char *) malloc(strlen(key) + 1); - strcpy(pair->key, key); - } else { - pair->key = NULL; - } + if (key != NULL) + { + pair->key = (char *) malloc (strlen (key) + 1); + strcpy (pair->key, key); + } + else + { + pair->key = NULL; + } - if (value != NULL) { - pair->value = (char *) malloc(strlen(value) + 1); - strcpy(pair->value, value); - } else { - pair->value = NULL; - } + if (value != NULL) + { + pair->value = (char *) malloc (strlen (value) + 1); + strcpy (pair->value, value); + } + else + { + pair->value = NULL; + } - pair->next = next; + pair->next = next; - return pair; + return pair; } -hpair_t * -hpairnode_parse(const char *str, const char *delim, hpair_t * next) +hpair_t * +hpairnode_parse (const char *str, const char *delim, hpair_t * next) { - hpair_t *pair; - char *key, *value; - int c; + hpair_t *pair; + char *key, *value; + int c; - pair = (hpair_t *) malloc(sizeof(hpair_t)); - pair->key = ""; - pair->value = ""; - pair->next = next; + pair = (hpair_t *) malloc (sizeof (hpair_t)); + pair->key = ""; + pair->value = ""; + pair->next = next; - key = strtok_r((char *) str, delim, &value); + key = strtok_r ((char *) str, delim, &value); - if (key != NULL) { - pair->key = (char *) malloc(strlen(key) + 1); - strcpy(pair->key, key); - } - if (value != NULL) { - for (c = 0; value[c] == ' '; c++); /* skip white space */ - pair->value = (char *) malloc(strlen(&value[c]) + 1); - strcpy(pair->value, &value[c]); - } - return pair; + if (key != NULL) + { + pair->key = (char *) malloc (strlen (key) + 1); + strcpy (pair->key, key); + } + if (value != NULL) + { + for (c = 0; value[c] == ' '; c++); /* skip white space */ + pair->value = (char *) malloc (strlen (&value[c]) + 1); + strcpy (pair->value, &value[c]); + } + return pair; } -hpair_t * -hpairnode_copy(const hpair_t * src) +hpair_t * +hpairnode_copy (const hpair_t * src) { - hpair_t *pair; + hpair_t *pair; - if (src == NULL) - return NULL; + if (src == NULL) + return NULL; - pair = hpairnode_new(src->key, src->value, NULL); - return pair; + pair = hpairnode_new (src->key, src->value, NULL); + return pair; } -hpair_t * -hpairnode_copy_deep(const hpair_t * src) +hpair_t * +hpairnode_copy_deep (const hpair_t * src) { - hpair_t *pair, *result, *next; + hpair_t *pair, *result, *next; - if (src == NULL) - return NULL; + if (src == NULL) + return NULL; - result = hpairnode_copy(src); + result = hpairnode_copy (src); - next = src->next; - pair = result; + next = src->next; + pair = result; - while (next != NULL) { - pair->next = hpairnode_copy(next); - pair = pair->next; - next = next->next; - } + while (next != NULL) + { + pair->next = hpairnode_copy (next); + pair = pair->next; + next = next->next; + } - return result; + return result; } -void -hpairnode_dump(hpair_t * pair) +void +hpairnode_dump (hpair_t * pair) { - if (pair == NULL) { - log_verbose1("(NULL)[]"); - return; - } - log_verbose5("(%p)['%s','%s','%p']", pair, - SAVE_STR(pair->key), SAVE_STR(pair->value), - pair->next); + if (pair == NULL) + { + log_verbose1 ("(NULL)[]"); + return; + } + log_verbose5 ("(%p)['%s','%s','%p']", pair, + SAVE_STR (pair->key), SAVE_STR (pair->value), pair->next); } -void -hpairnode_dump_deep(hpair_t * pair) +void +hpairnode_dump_deep (hpair_t * pair) { - hpair_t *p; - p = pair; + hpair_t *p; + p = pair; - log_verbose1("-- BEGIN dump hpairnode_t --"); + log_verbose1 ("-- BEGIN dump hpairnode_t --"); - while (p != NULL) { - hpairnode_dump(p); - p = p->next; - } + while (p != NULL) + { + hpairnode_dump (p); + p = p->next; + } - log_verbose1("-- END dump hpairnode_t --\n"); + log_verbose1 ("-- END dump hpairnode_t --\n"); } -void -hpairnode_free(hpair_t * pair) +void +hpairnode_free (hpair_t * pair) { - if (pair == NULL) - return; + if (pair == NULL) + return; - free(pair->key); - free(pair->value); + free (pair->key); + free (pair->value); - free(pair); + free (pair); } -void -hpairnode_free_deep(hpair_t * pair) +void +hpairnode_free_deep (hpair_t * pair) { - hpair_t *tmp; + hpair_t *tmp; + + while (pair != NULL) + { + tmp = pair->next; + hpairnode_free (pair); + pair = tmp; + } +} + +char * +hpairnode_get_ignore_case (hpair_t * pair, const char *key) +{ + if (key == NULL) + { + log_error1 ("key is NULL"); + return NULL; + } + while (pair != NULL) + { + if (pair->key != NULL) + { + if (strcmpigcase (pair->key, key)) + { + return pair->value; + } + } + pair = pair->next; + } + + return NULL; +} + +char * +hpairnode_get (hpair_t * pair, const char *key) +{ + if (key == NULL) + { + log_error1 ("key is NULL"); + return NULL; + } + while (pair != NULL) + { + if (pair->key != NULL) + { + if (!strcmp (pair->key, key)) + { + return pair->value; + } + } + pair = pair->next; + } - while (pair != NULL) { - tmp = pair->next; - hpairnode_free(pair); - pair = tmp; - } + return NULL; } -char * -hpairnode_get_ignore_case(hpair_t * pair, const char *key) +static void +hurl_dump (const hurl_t * url) { - if (key == NULL) { - log_error1("key is NULL"); - return NULL; - } - while (pair != NULL) { - if (pair->key != NULL) { - if (strcmpigcase(pair->key, key)) { - return pair->value; - } - } - pair = pair->next; - } - return NULL; + if (url == NULL) + { + log_error1 ("url is NULL!"); + return; + } + log_verbose2 ("PROTOCOL : %d", url->protocol); + log_verbose2 (" HOST : %s", url->host); + log_verbose2 (" PORT : %d", url->port); + log_verbose2 (" CONTEXT : %s", url->context); } -char * -hpairnode_get(hpair_t * pair, const char *key) + +herror_t +hurl_parse (hurl_t * url, const char *urlstr) { - if (key == NULL) { - log_error1("key is NULL"); - return NULL; - } - while (pair != NULL) { - if (pair->key != NULL) { - if (!strcmp(pair->key, key)) { - return pair->value; - } - } - pair = pair->next; - } - - return NULL; -} - -static -void -hurl_dump(const hurl_t * url) -{ - - if (url == NULL) { - log_error1("url is NULL!"); - return; - } - log_verbose2("PROTOCOL : %d", url->protocol); - log_verbose2(" HOST : %s", url->host); - log_verbose2(" PORT : %d", url->port); - log_verbose2(" CONTEXT : %s", url->context); -} - - -herror_t hurl_parse(hurl_t* url, const char *urlstr) -{ - int iprotocol; - int ihost; - int iport; - int len; - int size; - char tmp[8]; - char protocol[1024]; - - iprotocol = 0; - len = strlen(urlstr); - - /* find protocol */ - while (urlstr[iprotocol] != ':' && urlstr[iprotocol] != '\0') { - iprotocol++; - } - - if (iprotocol == 0) { - log_error1("no protocol"); - return herror_new("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol"); - } - if (iprotocol + 3 >= len) { - log_error1("no host"); - return herror_new("hurl_parse", URL_ERROR_NO_HOST, "No host"); - } - if (urlstr[iprotocol] != ':' - && urlstr[iprotocol + 1] != '/' - && urlstr[iprotocol + 2] != '/') { - log_error1("no protocol"); - return herror_new("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol"); - } - /* find host */ - ihost = iprotocol + 3; - while (urlstr[ihost] != ':' - && urlstr[ihost] != '/' - && urlstr[ihost] != '\0') { - ihost++; - } - - if (ihost == iprotocol + 1) { - log_error1("no host"); - return herror_new("hurl_parse", URL_ERROR_NO_HOST, "No host"); - } - /* find port */ - iport = ihost; - if (ihost + 1 < len) { - if (urlstr[ihost] == ':') { - while (urlstr[iport] != '/' && urlstr[iport] != '\0') { - iport++; - } - } - } - - /* find protocol */ - strncpy(protocol, urlstr, iprotocol); - protocol[iprotocol] = '\0'; - if (strcmpigcase(protocol, "http")) - url->protocol = PROTOCOL_HTTP; - else if (strcmpigcase(protocol, "https")) - url->protocol = PROTOCOL_HTTPS; - else if (strcmpigcase(protocol, "ftp")) - url->protocol = PROTOCOL_FTP; - else return herror_new("hurl_parse", - URL_ERROR_UNKNOWN_PROTOCOL, "Unknown protocol '%s'", protocol); - - /* TODO (#1#): add max of size and URL_MAX_HOST_SIZE */ - size = ihost - iprotocol - 3; - strncpy(url->host, &urlstr[iprotocol + 3], size); - url->host[size] = '\0'; - - if (iport > ihost) { - size = iport - ihost; - strncpy(tmp, &urlstr[ihost + 1], size); - url->port = atoi(tmp); - } else { - switch (url->protocol) { - case PROTOCOL_HTTP: - url->port = URL_DEFAULT_PORT_HTTP; - break; - case PROTOCOL_HTTPS: - url->port = URL_DEFAULT_PORT_HTTPS; - break; - case PROTOCOL_FTP: - url->port = URL_DEFAULT_PORT_FTP; - break; - } - } - - len = strlen(urlstr); - if (len > iport) { - /* TODO (#1#): find max of size and URL_MAX_CONTEXT_SIZE */ - size = len - iport; - strncpy(url->context, &urlstr[iport], size); - url->context[size] = '\0'; - } else { - url->context[0] = '\0'; - } - - hurl_dump(url); - - return H_OK; + int iprotocol; + int ihost; + int iport; + int len; + int size; + char tmp[8]; + char protocol[1024]; + + iprotocol = 0; + len = strlen (urlstr); + + /* find protocol */ + while (urlstr[iprotocol] != ':' && urlstr[iprotocol] != '\0') + { + iprotocol++; + } + + if (iprotocol == 0) + { + log_error1 ("no protocol"); + return herror_new ("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol"); + } + if (iprotocol + 3 >= len) + { + log_error1 ("no host"); + return herror_new ("hurl_parse", URL_ERROR_NO_HOST, "No host"); + } + if (urlstr[iprotocol] != ':' + && urlstr[iprotocol + 1] != '/' && urlstr[iprotocol + 2] != '/') + { + log_error1 ("no protocol"); + return herror_new ("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol"); + } + /* find host */ + ihost = iprotocol + 3; + while (urlstr[ihost] != ':' + && urlstr[ihost] != '/' && urlstr[ihost] != '\0') + { + ihost++; + } + + if (ihost == iprotocol + 1) + { + log_error1 ("no host"); + return herror_new ("hurl_parse", URL_ERROR_NO_HOST, "No host"); + } + /* find port */ + iport = ihost; + if (ihost + 1 < len) + { + if (urlstr[ihost] == ':') + { + while (urlstr[iport] != '/' && urlstr[iport] != '\0') + { + iport++; + } + } + } + + /* find protocol */ + strncpy (protocol, urlstr, iprotocol); + protocol[iprotocol] = '\0'; + if (strcmpigcase (protocol, "http")) + url->protocol = PROTOCOL_HTTP; + else if (strcmpigcase (protocol, "https")) + url->protocol = PROTOCOL_HTTPS; + else if (strcmpigcase (protocol, "ftp")) + url->protocol = PROTOCOL_FTP; + else + return herror_new ("hurl_parse", + URL_ERROR_UNKNOWN_PROTOCOL, "Unknown protocol '%s'", + protocol); + + /* TODO (#1#): add max of size and URL_MAX_HOST_SIZE */ + size = ihost - iprotocol - 3; + strncpy (url->host, &urlstr[iprotocol + 3], size); + url->host[size] = '\0'; + + if (iport > ihost) + { + size = iport - ihost; + strncpy (tmp, &urlstr[ihost + 1], size); + url->port = atoi (tmp); + } + else + { + switch (url->protocol) + { + case PROTOCOL_HTTP: + url->port = URL_DEFAULT_PORT_HTTP; + break; + case PROTOCOL_HTTPS: + url->port = URL_DEFAULT_PORT_HTTPS; + break; + case PROTOCOL_FTP: + url->port = URL_DEFAULT_PORT_FTP; + break; + } + } + + len = strlen (urlstr); + if (len > iport) + { + /* TODO (#1#): find max of size and URL_MAX_CONTEXT_SIZE */ + size = len - iport; + strncpy (url->context, &urlstr[iport], size); + url->context[size] = '\0'; + } + else + { + url->context[0] = '\0'; + } + + hurl_dump (url); + + return H_OK; } /* Content-type stuff */ -content_type_t *content_type_new(const char* content_type_str) +content_type_t * +content_type_new (const char *content_type_str) { hpair_t *pair = NULL, *last = NULL; - content_type_t * ct; + content_type_t *ct; char ch, key[256], value[256]; - int inQuote = 0, i=0, c=0, begin=0, len; - int mode = 0; - /* 0: searching ';' - 1: process key - 2: process value - */ - + int inQuote = 0, i = 0, c = 0, begin = 0, len; + int mode = 0; + /* 0: searching ';' 1: process key 2: process value */ + /* Create object */ - ct = (content_type_t*)malloc(sizeof(content_type_t)); + ct = (content_type_t *) malloc (sizeof (content_type_t)); ct->params = NULL; - len = strlen(content_type_str); + len = strlen (content_type_str); while (i <= len) { if (i != len) ch = content_type_str[i++]; - else + else { - ch = ' '; i++; + ch = ' '; + i++; } switch (mode) { - case 0: - - if (ch == ';') - { - ct->type[c] = '\0'; - c = 0; - mode = 1; - } - else if (ch != ' ' && ch != '\t' && ch != '\r') - ct->type[c++] = ch; - break; - - case 1: - - if (ch == '=') - { - key[c] = '\0'; - c = 0; - mode = 2; - } - else if (ch != ' ' && ch != '\t' && ch != '\r') - key[c++] = ch; - break; - - case 2: - - if (ch != ' ') begin = 1; - - if ((ch == ' ' || ch == ';') && !inQuote && begin) - { - value[c] = '\0'; - - pair = hpairnode_new(key, value, NULL); - if (ct->params == NULL) - ct->params = pair; - else - last->next = pair; - last = pair; - - c = 0; - begin = 0; - mode = 1; - } - else if (ch == '"') - inQuote = !inQuote; - else if (begin && ch != '\r') - value[c++] = ch; - - break; + case 0: + + if (ch == ';') + { + ct->type[c] = '\0'; + c = 0; + mode = 1; + } + else if (ch != ' ' && ch != '\t' && ch != '\r') + ct->type[c++] = ch; + break; + + case 1: + + if (ch == '=') + { + key[c] = '\0'; + c = 0; + mode = 2; + } + else if (ch != ' ' && ch != '\t' && ch != '\r') + key[c++] = ch; + break; + + case 2: + + if (ch != ' ') + begin = 1; + + if ((ch == ' ' || ch == ';') && !inQuote && begin) + { + value[c] = '\0'; + + pair = hpairnode_new (key, value, NULL); + if (ct->params == NULL) + ct->params = pair; + else + last->next = pair; + last = pair; + + c = 0; + begin = 0; + mode = 1; + } + else if (ch == '"') + inQuote = !inQuote; + else if (begin && ch != '\r') + value[c++] = ch; + + break; } } @@ -742,61 +809,74 @@ content_type_t *content_type_new(const char* content_type_str) } -void content_type_free(content_type_t *ct) +void +content_type_free (content_type_t * ct) { - if (!ct) return; + if (!ct) + return; - hpairnode_free_deep(ct->params); - free(ct); + hpairnode_free_deep (ct->params); + free (ct); } -part_t *part_new(const char *id, const char* filename, - const char* content_type, const char* transfer_encoding, part_t *next) +part_t * +part_new (const char *id, const char *filename, + const char *content_type, const char *transfer_encoding, + part_t * next) { - part_t *part = (part_t*)malloc(sizeof(part_t)); + part_t *part = (part_t *) malloc (sizeof (part_t)); part->header = NULL; part->next = next; part->deleteOnExit = 0; - strcpy(part->id, id); - strcpy(part->filename, filename); + strcpy (part->id, id); + strcpy (part->filename, filename); if (content_type) - strcpy(part->content_type, content_type); + strcpy (part->content_type, content_type); else part->content_type[0] = '\0'; - part->header = hpairnode_new(HEADER_CONTENT_ID, id, part->header); + part->header = hpairnode_new (HEADER_CONTENT_ID, id, part->header); /* TODO (#1#): encoding is always binary. implement also others! */ /* part->header = hpairnode_new(HEADER_CONTENT_TRANSFER_ENCODING, "binary", part->header);*/ - - strcpy(part->transfer_encoding, transfer_encoding?transfer_encoding:"binary"); - if (content_type) { - part->header = hpairnode_new(HEADER_CONTENT_TYPE, content_type, part->header); - } else { + strcpy (part->transfer_encoding, + transfer_encoding ? transfer_encoding : "binary"); + + if (content_type) + { + part->header = + hpairnode_new (HEADER_CONTENT_TYPE, content_type, part->header); + } + else + { /* TODO (#1#): get content-type from mime type list */ } return part; } -void part_free(part_t *part) +void +part_free (part_t * part) { if (part == NULL) return; - if (part->deleteOnExit) { - remove(part->filename); + if (part->deleteOnExit) + { + remove (part->filename); } - hpairnode_free_deep(part->header); + hpairnode_free_deep (part->header); - free(part); + free (part); } -attachments_t *attachments_new() /* should be used internally */ +attachments_t * +attachments_new () /* should be used internally */ { - attachments_t *attachments= (attachments_t*)malloc(sizeof(attachments_t)); + attachments_t *attachments = + (attachments_t *) malloc (sizeof (attachments_t)); attachments->parts = NULL; attachments->last = NULL; attachments->root_part = NULL; @@ -804,41 +884,44 @@ attachments_t *attachments_new() /* should be used internally */ return attachments; } -void attachments_add_part(attachments_t *attachments, part_t *part) +void +attachments_add_part (attachments_t * attachments, part_t * part) { /* paranoya check */ - if (!attachments) + if (!attachments) return; - if (attachments->last) + if (attachments->last) attachments->last->next = part; else attachments->parts = part; - + attachments->last = part; } /* Free a mime message */ -void attachments_free(attachments_t *message) +void +attachments_free (attachments_t * message) { part_t *tmp, *part; - if (message == NULL) + if (message == NULL) return; part = message->parts; - while (part) { + while (part) + { tmp = part->next; - part_free(part); - part= tmp; + part_free (part); + part = tmp; } if (message->root_part) - part_free(message->root_part); + part_free (message->root_part); /* TODO (#1#): HERE IS A BUG!!!! */ - free(message); + free (message); } @@ -846,30 +929,31 @@ void attachments_free(attachments_t *message) /* strtok_r() */ char * -strtok_r(char *s, const char *delim, char **save_ptr) +strtok_r (char *s, const char *delim, char **save_ptr) { - char *token; + char *token; - if (s == NULL) - s = *save_ptr; + if (s == NULL) + s = *save_ptr; - /* Scan leading delimiters. */ - s += strspn(s, delim); - if (*s == '\0') - return NULL; + /* Scan leading delimiters. */ + s += strspn (s, delim); + if (*s == '\0') + return NULL; - /* Find the end of the token. */ - token = s; - s = strpbrk(token, delim); - if (s == NULL) - /* This token finishes the string. */ - *save_ptr = strchr(token, '\0'); - else { - /* Terminate the token and make *SAVE_PTR point past it. */ - *s = '\0'; - *save_ptr = s + 1; - } - return token; + /* Find the end of the token. */ + token = s; + s = strpbrk (token, delim); + if (s == NULL) + /* This token finishes the string. */ + *save_ptr = strchr (token, '\0'); + else + { + /* Terminate the token and make *SAVE_PTR point past it. */ + *s = '\0'; + *save_ptr = s + 1; + } + return token; } /* localtime_r() */ @@ -887,4 +971,3 @@ localtime_r (const time_t * const timep, struct tm *p_tm) } #endif - diff --git a/nanohttp/nanohttp-common.h b/nanohttp/nanohttp-common.h index f23d165..36136b5 100644 --- a/nanohttp/nanohttp-common.h +++ b/nanohttp/nanohttp-common.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-common.h,v 1.20 2005/12/19 14:06:16 snowdrop Exp $ + * $Id: nanohttp-common.h,v 1.21 2006/01/10 11:21:55 snowdrop 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 NANO_HTTP_COMMON_H +#define NANO_HTTP_COMMON_H #include <stdlib.h> #include <time.h> @@ -85,7 +85,7 @@ /* TODO (#1#): find proper ports */ #define URL_DEFAULT_PORT_HTTP 80 #define URL_DEFAULT_PORT_HTTPS 81 -#define URL_DEFAULT_PORT_FTP 120 +#define URL_DEFAULT_PORT_FTP 120 /* Success flag */ #define H_OK 0 @@ -123,7 +123,7 @@ #define MIME_ERROR_NO_BOUNDARY_PARAM 1301 #define MIME_ERROR_NO_START_PARAM 1302 #define MIME_ERROR_PARSE_ERROR 1303 -#define MIME_ERROR_NO_ROOT_PART 1304 +#define MIME_ERROR_NO_ROOT_PART 1304 #define MIME_ERROR_NOT_MIME_MESSAGE 1305 @@ -145,19 +145,19 @@ Set Sleep function platform depended */ #ifdef WIN32 - #define system_sleep(seconds) Sleep(seconds*1000); +#define system_sleep(seconds) Sleep(seconds*1000); #else - #define system_sleep(seconds) sleep(seconds); +#define system_sleep(seconds) sleep(seconds); #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); +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 unsigned char byte_t; -typedef void* herror_t; +typedef void *herror_t; @@ -165,35 +165,35 @@ typedef void* herror_t; Indicates the version of the used HTTP protocol. */ -typedef enum _http_version { - HTTP_1_0, - HTTP_1_1 /* default */ -}http_version_t; +typedef enum _http_version +{ + HTTP_1_0, + HTTP_1_1 /* default */ +} http_version_t; /** Indicates the used method */ -typedef enum _hreq_method +typedef enum _hreq_method { HTTP_REQUEST_POST, HTTP_REQUEST_GET -}hreq_method_t ; +} hreq_method_t; -herror_t herror_new(const char* func, - int errcode, const char* format, ...); -int herror_code(herror_t err); -char* herror_func(herror_t err); -char* herror_message(herror_t err); -void herror_release(herror_t err); +herror_t herror_new (const char *func, int errcode, const char *format, ...); +int herror_code (herror_t err); +char *herror_func (herror_t err); +char *herror_message (herror_t err); +void herror_release (herror_t err); /* string function to compare strings ignoring case Returns 1 if s1 equals s2 and 0 otherwise. */ -int strcmpigcase(const char *s1, const char *s2); +int strcmpigcase (const char *s1, const char *s2); @@ -221,7 +221,7 @@ struct hpair @returns A newly crated hpair_t object. Use hpair_free() or hpair_free_deep() to free the pair. */ -hpair_t *hpairnode_new(const char* key, const char* value, hpair_t* next); +hpair_t *hpairnode_new (const char *key, const char *value, hpair_t * next); /** @@ -239,7 +239,7 @@ hpair_t *hpairnode_new(const char* key, const char* value, hpair_t* next); @returns A newly crated hpair_t object. Use hpair_free() or hpair_free_deep() to free the pair. */ -hpair_t *hpairnode_parse(const char *str, const char *delim, hpair_t * next); +hpair_t *hpairnode_parse (const char *str, const char *delim, hpair_t * next); /** @@ -247,7 +247,7 @@ hpair_t *hpairnode_parse(const char *str, const char *delim, hpair_t * next); @param pair the pair to free */ -void hpairnode_free(hpair_t *pair); +void hpairnode_free (hpair_t * pair); /** @@ -257,7 +257,7 @@ void hpairnode_free(hpair_t *pair); @param pair the pair to start to free the linked list */ -void hpairnode_free_deep(hpair_t * pair); +void hpairnode_free_deep (hpair_t * pair); /** @@ -270,7 +270,7 @@ void hpairnode_free_deep(hpair_t * pair); return the value (do not free this string) or NULL if no pair was found with the key 'key'. */ -char *hpairnode_get(hpair_t *pair, const char* key); +char *hpairnode_get (hpair_t * pair, const char *key); /** @@ -284,7 +284,7 @@ char *hpairnode_get(hpair_t *pair, const char* key); return the value (do not free this string) or NULL if no pair was found with the key 'key'. */ -char *hpairnode_get_ignore_case(hpair_t *pair, const char* key); +char *hpairnode_get_ignore_case (hpair_t * pair, const char *key); /** @@ -300,7 +300,7 @@ char *hpairnode_get_ignore_case(hpair_t *pair, const char* key); @see hpairnode_copy_deep */ -hpair_t* hpairnode_copy(const hpair_t *src); +hpair_t *hpairnode_copy (const hpair_t * src); /** @@ -312,11 +312,11 @@ hpair_t* hpairnode_copy(const hpair_t *src); @see hpairnode_copy */ -hpair_t* hpairnode_copy_deep(const hpair_t *src); +hpair_t *hpairnode_copy_deep (const hpair_t * src); /* Debug functions */ -void hpairnode_dump_deep(hpair_t *pair); -void hpairnode_dump(hpair_t *pair); +void hpairnode_dump_deep (hpair_t * pair); +void hpairnode_dump (hpair_t * pair); /** The protocol types in enumeration @@ -330,7 +330,7 @@ typedef enum _hprotocol PROTOCOL_HTTP, PROTOCOL_HTTPS, PROTOCOL_FTP -}hprotocol_t; +} hprotocol_t; @@ -355,7 +355,7 @@ typedef struct _hurl URL_HTTP_DEFAULT_PORT URL_HTTPS_DEFAULT_PORT URL_FTP_DEFAULT_PORT - */ + */ int port; /** The hostname */ @@ -363,7 +363,7 @@ typedef struct _hurl /** The string after the hostname. */ char context[URL_MAX_CONTEXT_SIZE]; -}hurl_t; +} hurl_t; /** @@ -378,7 +378,7 @@ typedef struct _hurl URL_ERROR_NO_PROTOCOL URL_ERROR_NO_HOST */ -herror_t hurl_parse(hurl_t *obj, const char* url); +herror_t hurl_parse (hurl_t * obj, const char *url); /* Object representation of the content-type field @@ -392,7 +392,7 @@ typedef struct _content_type { char type[128]; hpair_t *params; -}content_type_t; +} content_type_t; /** @@ -407,13 +407,13 @@ typedef struct _content_type @see content_type_free */ -content_type_t *content_type_new(const char* content_type_str); +content_type_t *content_type_new (const char *content_type_str); /** Frees the given content_type_t object */ -void content_type_free(content_type_t *ct); +void content_type_free (content_type_t * ct); @@ -429,13 +429,14 @@ typedef struct _part char transfer_encoding[128]; char filename[250]; struct _part *next; - int deleteOnExit; /* default is 0 */ -}part_t; + int deleteOnExit; /* default is 0 */ +} part_t; -part_t *part_new(const char *id, const char* filename, - const char* content_type, const char* transfer_encoding, part_t *next); -void part_free(part_t *part); +part_t *part_new (const char *id, const char *filename, + const char *content_type, const char *transfer_encoding, + part_t * next); +void part_free (part_t * part); @@ -448,9 +449,9 @@ typedef struct _attachments part_t *parts; part_t *last; part_t *root_part; -}attachments_t; +} attachments_t; -attachments_t *attachments_new(); /* should be used internally */ +attachments_t *attachments_new (); /* should be used internally */ /* Free a attachment. Create attachments with MIME @@ -458,15 +459,15 @@ attachments_t *attachments_new(); /* should be used internally */ @see mime_get_attachments */ -void attachments_free(attachments_t *message); -void attachments_add_part(attachments_t *attachments, part_t *part); +void attachments_free (attachments_t * message); +void attachments_add_part (attachments_t * attachments, part_t * part); /* tmp directory for multipart/related stuff */ #define HOPTION_TMP_DIR 2 -void hoption_init_args(int argc, char* argv[]); -void hoption_set(int opt, const char* value); -char *hoption_get(int opt); +void hoption_init_args (int argc, char *argv[]); +void hoption_set (int opt, const char *value); +char *hoption_get (int opt); /* logging stuff */ typedef enum log_level @@ -477,21 +478,21 @@ typedef enum log_level HLOG_WARN, HLOG_ERROR, HLOG_FATAL -}log_level_t; +} log_level_t; -log_level_t log_set_level(log_level_t level); -log_level_t log_get_level(); +log_level_t log_set_level (log_level_t level); +log_level_t log_get_level (); -void log_set_file(const char *filename); -char *log_get_file(); +void log_set_file (const char *filename); +char *log_get_file (); #ifdef WIN32 - #if defined(_MSC_VER) && _MSC_VER <= 1200 - char *VisualC_funcname(const char* file, int line); /* not thread safe!*/ - #define __FUNCTION__ VisualC_funcname(__FILE__, __LINE__) - #endif +#if defined(_MSC_VER) && _MSC_VER <= 1200 +char *VisualC_funcname (const char *file, int line); /* not thread safe! */ +#define __FUNCTION__ VisualC_funcname(__FILE__, __LINE__) +#endif #endif #define log_verbose1(a1) log_verbose(__FUNCTION__, a1) @@ -524,18 +525,12 @@ char *log_get_file(); #define log_error4(a1,a2,a3,a4) log_error(__FUNCTION__, a1,a2,a3,a4) #define log_error5(a1,a2,a3,a4,a5) log_error(__FUNCTION__, a1,a2,a3,a4,a5) -void log_verbose(const char* FUNC, const char *format, ...); -void log_debug(const char* FUNC, const char *format, ...); -void log_info(const char* FUNC, const char *format, ...); -void log_warn(const char* FUNC, const char *format, ...); -void log_error(const char* FUNC, const char *format, ...); +void log_verbose (const char *FUNC, const char *format, ...); +void log_debug (const char *FUNC, const char *format, ...); +void log_info (const char *FUNC, const char *format, ...); +void log_warn (const char *FUNC, const char *format, ...); +void log_error (const char *FUNC, const char *format, ...); #endif - - - - - - diff --git a/nanohttp/nanohttp-mime.c b/nanohttp/nanohttp-mime.c index f4ff487..3f17d38 100755 --- a/nanohttp/nanohttp-mime.c +++ b/nanohttp/nanohttp-mime.c @@ -3,7 +3,7 @@ * | \/ | | | | \/ | | _/ * |_''_| |_| |_''_| |_'/ PARSER * -* $Id: nanohttp-mime.c,v 1.7 2005/05/27 19:28:15 snowdrop Exp $ +* $Id: nanohttp-mime.c,v 1.8 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -45,32 +45,32 @@ Buffered Reader. A helper object to read bytes from a source /* ------------------------------------------------------------------ MIME Parser ------------------------------------------------------------------*/ -typedef void (*MIME_part_begin) (void*); -typedef void (*MIME_part_end) (void*); -typedef void (*MIME_parse_begin) (void*); -typedef void (*MIME_parse_end) (void*); -typedef void (*MIME_ERROR_bytes) (void*, - const unsigned char*, int); - -typedef enum _MIME_parser_status +typedef void (*MIME_part_begin) (void *); +typedef void (*MIME_part_end) (void *); +typedef void (*MIME_parse_begin) (void *); +typedef void (*MIME_parse_end) (void *); +typedef void (*MIME_ERROR_bytes) (void *, const unsigned char *, int); + +typedef enum _MIME_parser_status { MIME_PARSER_INCOMPLETE_MESSAGE, MIME_PARSER_READ_ERROR, MIME_PARSER_OK -}MIME_parser_status; +} MIME_parser_status; typedef enum _MIME_read_status { MIME_READ_OK, MIME_READ_EOF, MIME_READ_ERROR -}MIME_read_status; +} MIME_read_status; #define MIME_READER_MAX_BUFFER_SIZE 1054 #define MIME_PARSER_BUFFER_SIZE 1054 -typedef MIME_read_status (*MIME_read_function) (void*, unsigned char*, int*); +typedef MIME_read_status (*MIME_read_function) (void *, unsigned char *, + int *); /** @@ -85,36 +85,35 @@ typedef struct _MIME_reader MIME_read_function read_function; char buffer[MIME_READER_MAX_BUFFER_SIZE]; void *userdata; -}MIME_reader; +} MIME_reader; -MIME_read_status MIME_filereader_function(void* userdata, - unsigned char* dest, int* size); +MIME_read_status MIME_filereader_function (void *userdata, + unsigned char *dest, int *size); typedef struct _MIME_callbacks { - MIME_part_begin part_begin_cb; - MIME_part_end part_end_cb; - MIME_parse_begin parse_begin_cb; - MIME_parse_end parse_end_cb; + MIME_part_begin part_begin_cb; + MIME_part_end part_end_cb; + MIME_parse_begin parse_begin_cb; + MIME_parse_end parse_end_cb; MIME_ERROR_bytes received_bytes_cb; -}MIME_callbacks; +} MIME_callbacks; -MIME_parser_status MIME_parse( - MIME_read_function reader_function, - void *reader_userdata, - const char* user_boundary, - const MIME_callbacks* callbacks, - void *callbacks_userdata -); +MIME_parser_status MIME_parse (MIME_read_function reader_function, + void *reader_userdata, + const char *user_boundary, + const MIME_callbacks * callbacks, + void *callbacks_userdata); /** Initialize a reader */ -void MIME_reader_init(MIME_reader *reader, - MIME_read_function reader_function,void *userdata) +void +MIME_reader_init (MIME_reader * reader, + MIME_read_function reader_function, void *userdata) { reader->size = 0; reader->marker = -1; @@ -127,8 +126,8 @@ void MIME_reader_init(MIME_reader *reader, /** Read data from a reader source. */ -MIME_read_status MIME_reader_read(MIME_reader *reader, - unsigned char *buffer, int size) +MIME_read_status +MIME_reader_read (MIME_reader * reader, unsigned char *buffer, int size) { MIME_read_status status; int readed_size; @@ -145,64 +144,66 @@ MIME_read_status MIME_reader_read(MIME_reader *reader, { if (reader->marker != 0) { - memcpy(tempBuffer, reader->buffer + reader->marker, - reader->size - reader->marker); - memcpy(reader->buffer, tempBuffer, + memcpy (tempBuffer, reader->buffer + reader->marker, reader->size - reader->marker); + memcpy (reader->buffer, tempBuffer, reader->size - reader->marker); reader->current = reader->size - reader->marker; } - else if (reader->current == MIME_READER_MAX_BUFFER_SIZE-1) + else if (reader->current == MIME_READER_MAX_BUFFER_SIZE - 1) { - fprintf(stderr, "Marker error"); - return MIME_READ_ERROR; + fprintf (stderr, "Marker error"); + return MIME_READ_ERROR; } reader->marker = 0; } else reader->current = 0; - - readed_size = MIME_READER_MAX_BUFFER_SIZE - reader->current -1; - status = reader->read_function(reader->userdata, - reader->buffer + reader->current, &readed_size); + + readed_size = MIME_READER_MAX_BUFFER_SIZE - reader->current - 1; + status = reader->read_function (reader->userdata, + reader->buffer + reader->current, + &readed_size); if (status == MIME_READ_OK) { reader->size = readed_size + reader->current; - } + } else return status; } if (size <= reader->size - reader->current) { - memcpy(buffer, reader->buffer + reader->current, size); + memcpy (buffer, reader->buffer + reader->current, size); reader->current += size; return MIME_READ_OK; - } + } else { /* Fill rest data */ rest_size = reader->size - reader->current; - memcpy(buffer, reader->buffer + reader->current, rest_size); + memcpy (buffer, reader->buffer + reader->current, rest_size); reader->current = reader->size; - return MIME_reader_read(reader, buffer + rest_size, - size - rest_size); + return MIME_reader_read (reader, buffer + rest_size, size - rest_size); } } -void MIME_reader_set_marker(MIME_reader *reader) +void +MIME_reader_set_marker (MIME_reader * reader) { reader->marker = reader->current; } -void MIME_reader_unset_marker(MIME_reader *reader) +void +MIME_reader_unset_marker (MIME_reader * reader) { reader->marker = -1; } -void MIME_reader_jump_marker(MIME_reader *reader) +void +MIME_reader_jump_marker (MIME_reader * reader) { reader->current = reader->marker; } @@ -213,85 +214,89 @@ typedef struct _MIME_buffer { unsigned char data[MIME_PARSER_BUFFER_SIZE]; int size; -}MIME_buffer; +} MIME_buffer; -void MIME_buffer_init(MIME_buffer *buffer) +void +MIME_buffer_init (MIME_buffer * buffer) { buffer->size = 0; } -void MIME_buffer_add(MIME_buffer *buffer, unsigned char ch) +void +MIME_buffer_add (MIME_buffer * buffer, unsigned char ch) { buffer->data[buffer->size++] = ch; } -void MIME_buffer_add_bytes(MIME_buffer *buffer, unsigned char *bytes, int size) +void +MIME_buffer_add_bytes (MIME_buffer * buffer, unsigned char *bytes, int size) { - memcpy(buffer->data, bytes, size); + memcpy (buffer->data, bytes, size); buffer->size += size; } -int MIME_buffer_is_full(MIME_buffer *buffer) +int +MIME_buffer_is_full (MIME_buffer * buffer) { return buffer->size + 150 >= MIME_PARSER_BUFFER_SIZE; } -int MIME_buffer_is_empty(MIME_buffer *buffer) +int +MIME_buffer_is_empty (MIME_buffer * buffer) { return buffer->size == 0; } -void MIME_buffer_clear(MIME_buffer *buffer) +void +MIME_buffer_clear (MIME_buffer * buffer) { buffer->size = 0; } -MIME_parser_status MIME_parse( - MIME_read_function reader_function, - void *reader_userdata, - const char* user_boundary, - const MIME_callbacks* callbacks, - void *callbacks_userdata -) +MIME_parser_status +MIME_parse (MIME_read_function reader_function, + void *reader_userdata, + const char *user_boundary, + const MIME_callbacks * callbacks, void *callbacks_userdata) { - char boundary[150]; + char boundary[150]; unsigned char ch[153]; - int boundary_length, n, ignore = 0; + int boundary_length, n, ignore = 0; MIME_reader reader; MIME_buffer buffer; MIME_read_status status; /* Init reader */ - MIME_reader_init(&reader, reader_function, reader_userdata); + MIME_reader_init (&reader, reader_function, reader_userdata); /* Init buffer */ - MIME_buffer_init(&buffer); + MIME_buffer_init (&buffer); /* Set boundary related stuff */ - sprintf(boundary, "\n--%s", user_boundary); - boundary_length = strlen(boundary); + sprintf (boundary, "\n--%s", user_boundary); + boundary_length = strlen (boundary); /* Call parse begin callback */ - callbacks->parse_begin_cb(callbacks_userdata); + callbacks->parse_begin_cb (callbacks_userdata); while (1) { -set_marker: + set_marker: /* Set marker */ - MIME_reader_set_marker(&reader); + MIME_reader_set_marker (&reader); -read_byte: + read_byte: /* Read 1 byte */ - status = MIME_reader_read(&reader, ch, 1); - _log_str("buffer.log", ch,1); + status = MIME_reader_read (&reader, ch, 1); + _log_str ("buffer.log", ch, 1); if (status == MIME_READ_EOF) - return MIME_PARSER_INCOMPLETE_MESSAGE; + return MIME_PARSER_INCOMPLETE_MESSAGE; else if (status == MIME_READ_ERROR) - return MIME_PARSER_READ_ERROR; + return MIME_PARSER_READ_ERROR; if (ch[0] == '\r' && !ignore) { @@ -299,181 +304,181 @@ read_byte: while (n < boundary_length) { /* Read 1 byte */ - status = MIME_reader_read(&reader, ch, 1); - _log_str("buffer.log", ch,1); + status = MIME_reader_read (&reader, ch, 1); + _log_str ("buffer.log", ch, 1); if (status == MIME_READ_EOF) - return MIME_PARSER_INCOMPLETE_MESSAGE; + return MIME_PARSER_INCOMPLETE_MESSAGE; else if (status == MIME_READ_ERROR) - return MIME_PARSER_READ_ERROR; - - /* Check if byte is in boundary */ - if (ch[0] == boundary[n]) + return MIME_PARSER_READ_ERROR; + + /* Check if byte is in boundary */ + if (ch[0] == boundary[n]) { n = n + 1; - continue; + continue; } else { - MIME_reader_jump_marker(&reader); + MIME_reader_jump_marker (&reader); ignore = 1; goto read_byte; } - - } /* while n < boundary_length*/ + + } /* while n < boundary_length */ /* Read 1 byte */ - status = MIME_reader_read(&reader, ch, 1); - _log_str("buffer.log", ch,1); + status = MIME_reader_read (&reader, ch, 1); + _log_str ("buffer.log", ch, 1); if (status == MIME_READ_EOF) - return MIME_PARSER_INCOMPLETE_MESSAGE; + return MIME_PARSER_INCOMPLETE_MESSAGE; else if (status == MIME_READ_ERROR) - return MIME_PARSER_READ_ERROR; - + return MIME_PARSER_READ_ERROR; + /* Show if byte is '\r' */ if (ch[0] == '\r') { /* Read 1 byte */ - status = MIME_reader_read(&reader, ch, 1); - _log_str("buffer.log", ch,1); + status = MIME_reader_read (&reader, ch, 1); + _log_str ("buffer.log", ch, 1); if (status == MIME_READ_EOF) - return MIME_PARSER_INCOMPLETE_MESSAGE; + return MIME_PARSER_INCOMPLETE_MESSAGE; else if (status == MIME_READ_ERROR) - return MIME_PARSER_READ_ERROR; + return MIME_PARSER_READ_ERROR; - /* Check if byte is '\n' */ + /* Check if byte is '\n' */ if (ch[0] == '\n') { - if (!MIME_buffer_is_empty(&buffer)) + if (!MIME_buffer_is_empty (&buffer)) { /* Invoke callback */ - callbacks->received_bytes_cb( - callbacks_userdata, buffer.data, buffer.size); + callbacks->received_bytes_cb (callbacks_userdata, buffer.data, + buffer.size); /* Empty the buffer */ - MIME_buffer_clear(&buffer); - + MIME_buffer_clear (&buffer); + /* Invoke End Part */ - callbacks->part_end_cb(callbacks_userdata); + callbacks->part_end_cb (callbacks_userdata); } - + /* Invoke start new Part */ - callbacks->part_begin_cb(callbacks_userdata); + callbacks->part_begin_cb (callbacks_userdata); goto set_marker; - } /* if (ch[0] == '\n') */ + } /* if (ch[0] == '\n') */ else { /* Jump to marker and read bytes */ - MIME_reader_jump_marker(&reader); - MIME_reader_read(&reader, ch, boundary_length+2); - _log_str("buffer.log", ch,1); + MIME_reader_jump_marker (&reader); + MIME_reader_read (&reader, ch, boundary_length + 2); + _log_str ("buffer.log", ch, 1); - MIME_buffer_add_bytes(&buffer, ch, boundary_length+2); - - if (MIME_buffer_is_full(&buffer)) + MIME_buffer_add_bytes (&buffer, ch, boundary_length + 2); + + if (MIME_buffer_is_full (&buffer)) { /* Invoke callback */ - callbacks->received_bytes_cb( - callbacks_userdata, buffer.data, buffer.size); - + callbacks->received_bytes_cb (callbacks_userdata, buffer.data, + buffer.size); + /* Empty the buffer */ - MIME_buffer_clear(&buffer); + MIME_buffer_clear (&buffer); } - } /* else of if (ch[0] == '\n') */ + } /* else of if (ch[0] == '\n') */ - } /* if (ch[0] == '\r') */ + } /* if (ch[0] == '\r') */ else { if (ch[0] == '-') { /* Read 1 byte */ - status = MIME_reader_read(&reader, ch, 1); - _log_str("buffer.log", ch,1); + status = MIME_reader_read (&reader, ch, 1); + _log_str ("buffer.log", ch, 1); if (status == MIME_READ_EOF) - return MIME_PARSER_INCOMPLETE_MESSAGE; + return MIME_PARSER_INCOMPLETE_MESSAGE; else if (status == MIME_READ_ERROR) - return MIME_PARSER_READ_ERROR; + return MIME_PARSER_READ_ERROR; if (ch[0] == '-') { - if (!MIME_buffer_is_empty(&buffer)) + if (!MIME_buffer_is_empty (&buffer)) { /* Invoke callback */ - callbacks->received_bytes_cb( - callbacks_userdata, buffer.data, buffer.size); - + callbacks->received_bytes_cb (callbacks_userdata, buffer.data, + buffer.size); + /* Empty the buffer */ - MIME_buffer_clear(&buffer); + MIME_buffer_clear (&buffer); /* Invoke End Part */ - callbacks->part_end_cb(callbacks_userdata); + callbacks->part_end_cb (callbacks_userdata); } - + /* Invoke start new Part */ - callbacks->parse_end_cb(callbacks_userdata); + callbacks->parse_end_cb (callbacks_userdata); + + /* Finish parsing */ + /* TODO (#1#): We assume that after -- comes \r\n This is not + always correct */ - /* Finish parsing*/ - /* TODO (#1#): We assume that after -- comes \r\n - This is not always correct */ - return MIME_PARSER_OK; - } /* if (ch[0] == '-') */ + } /* if (ch[0] == '-') */ else { - MIME_reader_jump_marker(&reader); + MIME_reader_jump_marker (&reader); ignore = 1; goto read_byte; - } /* else of if (ch[0] == '-') */ - - } /* if (ch[0] == '-') */ + } /* else of if (ch[0] == '-') */ + + } /* if (ch[0] == '-') */ else { - MIME_reader_jump_marker(&reader); + MIME_reader_jump_marker (&reader); ignore = 1; goto read_byte; - } /* else of if (ch[0] == '-') */ + } /* else of if (ch[0] == '-') */ - } /* else of if (ch[0] == '\r') */ + } /* else of if (ch[0] == '\r') */ - } /* if ch[0] == '\r' && !ignore */ + } /* if ch[0] == '\r' && !ignore */ else { ignore = 0; - MIME_buffer_add(&buffer, ch[0]); + MIME_buffer_add (&buffer, ch[0]); - /* Chec if buffer is full*/ - if (MIME_buffer_is_full(&buffer)) + /* Chec if buffer is full */ + if (MIME_buffer_is_full (&buffer)) { /* Invoke callback */ - callbacks->received_bytes_cb( - callbacks_userdata, buffer.data, buffer.size); - + callbacks->received_bytes_cb (callbacks_userdata, buffer.data, + buffer.size); + /* Empty the buffer */ - MIME_buffer_clear(&buffer); + MIME_buffer_clear (&buffer); } - } /* else of if ch[0] == '\r' && !ignore */ - } /* while (1) */ + } /* else of if ch[0] == '\r' && !ignore */ + } /* while (1) */ } -MIME_read_status MIME_filereader_function(void* userdata, - unsigned char* dest, int* size) +MIME_read_status +MIME_filereader_function (void *userdata, unsigned char *dest, int *size) { - FILE *f = (FILE*)userdata; + FILE *f = (FILE *) userdata; - if (feof(f)) + if (feof (f)) return MIME_READ_EOF; - *size = fread(dest, 1, *size, f); + *size = fread (dest, 1, *size, f); return MIME_READ_OK; } @@ -487,44 +492,45 @@ MIME_read_status MIME_filereader_function(void* userdata, */ typedef struct _mime_callback_data { - int part_id; + int part_id; attachments_t *message; - part_t *current_part; - int buffer_capacity; - char header[4064]; - char root_id[256]; - int header_index; - int header_search; - FILE *current_fd; - char root_dir[512]; -}mime_callback_data_t; + part_t *current_part; + int buffer_capacity; + char header[4064]; + char root_id[256]; + int header_index; + int header_search; + FILE *current_fd; + char root_dir[512]; +} mime_callback_data_t; -MIME_read_status mime_streamreader_function(void* userdata, - unsigned char* dest, int* size) +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; + int readed = 0; + http_input_stream_t *in = (http_input_stream_t *) userdata; - if (!http_input_stream_is_ready(in)) + if (!http_input_stream_is_ready (in)) return MIME_READ_EOF; - readed = http_input_stream_read(in, dest, *size); - /* - log_info1("http_input_stream_read() returned 0"); - */ - if (readed == -1) { - log_error4("[%d] %s():%s ", herror_code(in->err), herror_func(in->err), herror_message(in->err)); + readed = http_input_stream_read (in, dest, *size); + /* + log_info1("http_input_stream_read() returned 0"); */ + if (readed == -1) + { + log_error4 ("[%d] %s():%s ", herror_code (in->err), herror_func (in->err), + herror_message (in->err)); } *size = readed; - if (*size!=-1) { - /* - _log_str("reader.log", dest, *size); - */ - return MIME_READ_OK; - } + if (*size != -1) + { + /* + _log_str("reader.log", dest, *size); */ + return MIME_READ_OK; + } return MIME_READ_ERROR; } @@ -532,35 +538,35 @@ MIME_read_status mime_streamreader_function(void* userdata, /* Start Callback functions */ -static -void _mime_parse_begin(void *data) +static void +_mime_parse_begin (void *data) { /* Nothing to do mime_callback_data_t *cbdata = (mime_callback_data_t)data; */ - log_verbose2("Begin parse (%p)", data); + log_verbose2 ("Begin parse (%p)", data); } -static -void _mime_parse_end(void *data) +static void +_mime_parse_end (void *data) { /* Nothing to do mime_callback_data_t *cbdata = (mime_callback_data_t)data; */ - log_verbose2("End parse (%p)", data); + log_verbose2 ("End parse (%p)", data); } -static -void _mime_part_begin(void *data) +static void +_mime_part_begin (void *data) { char buffer[1054]; - mime_callback_data_t *cbdata = (mime_callback_data_t*)data; + mime_callback_data_t *cbdata = (mime_callback_data_t *) data; part_t *part; - log_verbose2("Begin Part (%p)", data); - part = (part_t *)malloc(sizeof(part_t)); + log_verbose2 ("Begin Part (%p)", data); + part = (part_t *) malloc (sizeof (part_t)); part->next = NULL; @@ -577,40 +583,40 @@ void _mime_part_begin(void *data) cbdata->header_search = 0; #ifdef WIN32 - sprintf(buffer, "%s\\mime_%p_%d.part", cbdata->root_dir, - cbdata, cbdata->part_id++); + sprintf (buffer, "%s\\mime_%p_%d.part", cbdata->root_dir, + cbdata, cbdata->part_id++); #else - sprintf(buffer, "%s/mime_%p_%d.part", cbdata->root_dir, - cbdata, cbdata->part_id++); + sprintf (buffer, "%s/mime_%p_%d.part", cbdata->root_dir, + cbdata, cbdata->part_id++); #endif /* log_info2("Creating FILE ('%s') deleteOnExit=1", buffer);*/ part->deleteOnExit = 1; - cbdata->current_fd = fopen(buffer, "wb"); + cbdata->current_fd = fopen (buffer, "wb"); if (cbdata->current_fd) - strcpy(cbdata->current_part->filename, buffer); + strcpy (cbdata->current_part->filename, buffer); else - log_error2("Can not open file for write '%s'", buffer); + log_error2 ("Can not open file for write '%s'", buffer); } -static -void _mime_part_end(void *data) +static void +_mime_part_end (void *data) { - mime_callback_data_t *cbdata = (mime_callback_data_t*)data; - log_verbose2("End Part (%p)", data); + mime_callback_data_t *cbdata = (mime_callback_data_t *) data; + log_verbose2 ("End Part (%p)", data); if (cbdata->current_fd) { - fclose(cbdata->current_fd); + fclose (cbdata->current_fd); cbdata->current_fd = NULL; } } -static -hpair_t *_mime_process_header(char *buffer) +static hpair_t * +_mime_process_header (char *buffer) { - int i=0, c=0, proc_key, begin=0; + int i = 0, c = 0, proc_key, begin = 0; hpair_t *first = NULL, *last = NULL; char key[1054], value[1054]; @@ -620,22 +626,22 @@ hpair_t *_mime_process_header(char *buffer) while (buffer[i] != '\0') { - if (buffer[i] == '\r' && buffer[i+1] == '\n') + if (buffer[i] == '\r' && buffer[i + 1] == '\n') { value[c] = '\0'; if (last) { - last->next = hpairnode_new(key, value, NULL); - last = last->next; + last->next = hpairnode_new (key, value, NULL); + last = last->next; } else { - first = last = hpairnode_new(key, value, NULL); + first = last = hpairnode_new (key, value, NULL); } proc_key = 1; c = 0; i++; - } + } else if (buffer[i] == ':') { key[c] = '\0'; @@ -643,7 +649,7 @@ hpair_t *_mime_process_header(char *buffer) begin = 0; proc_key = 0; } - else + else { if (proc_key) key[c++] = buffer[i]; @@ -661,19 +667,23 @@ hpair_t *_mime_process_header(char *buffer) } -static -void _mime_received_bytes(void *data, const unsigned char* bytes, int size) +static void +_mime_received_bytes (void *data, const unsigned char *bytes, int size) { - int i=0; + int i = 0; char *id, *type, *location; - mime_callback_data_t *cbdata = (mime_callback_data_t*)data; + mime_callback_data_t *cbdata = (mime_callback_data_t *) data; - if (!cbdata ) { - log_error1("MIME transport error Called <received bytes> without initializing\n"); + if (!cbdata) + { + log_error1 + ("MIME transport error Called <received bytes> without initializing\n"); return; } - if (!cbdata->current_part) { - log_error1("MIME transport error Called <received bytes> without initializing\n"); + if (!cbdata->current_part) + { + log_error1 + ("MIME transport error Called <received bytes> without initializing\n"); return; } /* log_verbose4("Received %d bytes (%p), header_search = %d", @@ -682,7 +692,7 @@ void _mime_received_bytes(void *data, const unsigned char* bytes, int size) if (cbdata->header_search < 4) { /* Find \r\n\r\n in bytes */ - for (i=0;i<size;i++) + for (i = 0; i < size; i++) { if (cbdata->header_search == 0) { @@ -728,25 +738,30 @@ void _mime_received_bytes(void *data, const unsigned char* bytes, int size) cbdata->header[cbdata->header_index++] = '\n'; cbdata->header[cbdata->header_index++] = '\0'; cbdata->header_search = 4; - cbdata->current_part->header = _mime_process_header(cbdata->header); - hpairnode_dump_deep(cbdata->current_part->header); + cbdata->current_part->header = + _mime_process_header (cbdata->header); + hpairnode_dump_deep (cbdata->current_part->header); /* set id */ - id = hpairnode_get(cbdata->current_part->header, HEADER_CONTENT_ID); + id = + hpairnode_get (cbdata->current_part->header, HEADER_CONTENT_ID); if (id != NULL) { - strcpy(cbdata->current_part->id, id); - if (!strcmp(id, cbdata->root_id)) + strcpy (cbdata->current_part->id, id); + if (!strcmp (id, cbdata->root_id)) cbdata->message->root_part = cbdata->current_part; } - location = hpairnode_get(cbdata->current_part->header, HEADER_CONTENT_LOCATION); + location = + hpairnode_get (cbdata->current_part->header, + HEADER_CONTENT_LOCATION); if (location != NULL) { - strcpy(cbdata->current_part->location, location); + strcpy (cbdata->current_part->location, location); } - type = hpairnode_get(cbdata->current_part->header, HEADER_CONTENT_TYPE); + type = + hpairnode_get (cbdata->current_part->header, HEADER_CONTENT_TYPE); if (type != NULL) { - strcpy(cbdata->current_part->content_type, type); + strcpy (cbdata->current_part->content_type, type); } i++; break; @@ -761,17 +776,17 @@ void _mime_received_bytes(void *data, const unsigned char* bytes, int size) } } /* TODO (#1#): Check for cbdata->header overflow */ - - } /* for (i=0;i<size;i++) */ - } /* if (cbdata->header_search < 4) */ - if (i >= size-1) + } /* for (i=0;i<size;i++) */ + } /* if (cbdata->header_search < 4) */ + + if (i >= size - 1) return; - /* Write remaining bytes into the file or buffer (if root) - (buffer is disabled in this version) */ + /* Write remaining bytes into the file or buffer (if root) (buffer is + disabled in this version) */ if (cbdata->current_fd) - fwrite(&(bytes[i]), 1, size - i, cbdata->current_fd); + fwrite (&(bytes[i]), 1, size - i, cbdata->current_fd); } @@ -780,25 +795,25 @@ void _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, - const char* boundary, const char* dest_dir) +mime_message_parse (http_input_stream_t * in, const char *root_id, + const char *boundary, const char *dest_dir) { MIME_parser_status status; MIME_callbacks callbacks; - attachments_t* message; + attachments_t *message; - mime_callback_data_t *cbdata = (mime_callback_data_t*) - malloc(sizeof(mime_callback_data_t)); + mime_callback_data_t *cbdata = (mime_callback_data_t *) + malloc (sizeof (mime_callback_data_t)); cbdata->part_id = 100; cbdata->buffer_capacity = 0; - cbdata-> current_fd = NULL; + cbdata->current_fd = NULL; cbdata->current_part = NULL; cbdata->header_index = 0; cbdata->header_search = 0; - strcpy(cbdata->root_id, root_id); - strcpy(cbdata->root_dir, dest_dir); - message = (attachments_t*)malloc(sizeof(attachments_t)); + strcpy (cbdata->root_id, root_id); + strcpy (cbdata->root_dir, dest_dir); + message = (attachments_t *) malloc (sizeof (attachments_t)); cbdata->message = message; cbdata->message->parts = NULL; cbdata->message->root_part = NULL; @@ -809,42 +824,43 @@ mime_message_parse(http_input_stream_t *in, const char* root_id, callbacks.part_end_cb = _mime_part_end; callbacks.received_bytes_cb = _mime_received_bytes; - status = MIME_parse(mime_streamreader_function, - in, boundary, &callbacks, cbdata); + status = MIME_parse (mime_streamreader_function, + in, boundary, &callbacks, cbdata); if (status == MIME_PARSER_OK) { - free(cbdata); + free (cbdata); return message; } else { - log_error2("MIME parser error '%s'!", - status == MIME_PARSER_READ_ERROR ? "read error" : "Incomplete message"); + log_error2 ("MIME parser error '%s'!", + status == + MIME_PARSER_READ_ERROR ? "read error" : "Incomplete message"); return NULL; } } attachments_t * -mime_message_parse_from_file(FILE *in, const char* root_id, - const char* boundary, const char* dest_dir) +mime_message_parse_from_file (FILE * in, const char *root_id, + const char *boundary, const char *dest_dir) { MIME_parser_status status; MIME_callbacks callbacks; - attachments_t* message; + attachments_t *message; - mime_callback_data_t *cbdata = (mime_callback_data_t*) - malloc(sizeof(mime_callback_data_t)); + mime_callback_data_t *cbdata = (mime_callback_data_t *) + malloc (sizeof (mime_callback_data_t)); cbdata->part_id = 100; cbdata->buffer_capacity = 0; - cbdata-> current_fd = NULL; + cbdata->current_fd = NULL; cbdata->current_part = NULL; cbdata->header_index = 0; cbdata->header_search = 0; - strcpy(cbdata->root_id, root_id); - strcpy(cbdata->root_dir, dest_dir); - message = (attachments_t*)malloc(sizeof(attachments_t)); + strcpy (cbdata->root_id, root_id); + strcpy (cbdata->root_dir, dest_dir); + message = (attachments_t *) malloc (sizeof (attachments_t)); cbdata->message = message; cbdata->message->parts = NULL; cbdata->message->root_part = NULL; @@ -855,77 +871,82 @@ mime_message_parse_from_file(FILE *in, const char* root_id, callbacks.part_end_cb = _mime_part_end; callbacks.received_bytes_cb = _mime_received_bytes; - status = MIME_parse(MIME_filereader_function, - in, boundary, &callbacks, cbdata); + status = MIME_parse (MIME_filereader_function, + in, boundary, &callbacks, cbdata); if (status == MIME_PARSER_OK) { - free(cbdata); + free (cbdata); return message; } else { /* TODO (#1#): Free objects */ - log_error2("MIME parser error '%s'!", - status == MIME_PARSER_READ_ERROR ? "general error" : "Incomplete message"); + log_error2 ("MIME parser error '%s'!", + status == + MIME_PARSER_READ_ERROR ? "general error" : + "Incomplete message"); return NULL; } } -herror_t mime_get_attachments(content_type_t *ctype, http_input_stream_t *in, attachments_t **dest) +herror_t +mime_get_attachments (content_type_t * ctype, http_input_stream_t * in, + attachments_t ** dest) { /* MIME variables */ - attachments_t *mimeMessage; - part_t *part, *tmp_part=NULL; - char *boundary, *root_id; + attachments_t *mimeMessage; + part_t *part, *tmp_part = NULL; + char *boundary, *root_id; /* Check for MIME message */ - if (!(ctype && - !strcmp(ctype->type, "multipart/related"))) - return herror_new("mime_get_attachments", MIME_ERROR_NOT_MIME_MESSAGE, - "Not a MIME message '%s'", ctype->type); + if (!(ctype && !strcmp (ctype->type, "multipart/related"))) + return herror_new ("mime_get_attachments", MIME_ERROR_NOT_MIME_MESSAGE, + "Not a MIME message '%s'", ctype->type); - boundary = hpairnode_get(ctype->params, "boundary"); - root_id = hpairnode_get(ctype->params, "start"); + boundary = hpairnode_get (ctype->params, "boundary"); + root_id = hpairnode_get (ctype->params, "start"); if (boundary == NULL) { - /* TODO (#1#): Handle Error in http form */ - log_error1("'boundary' not set for multipart/related"); - return herror_new("mime_get_attachments", MIME_ERROR_NO_BOUNDARY_PARAM, - "'boundary' not set for multipart/related"); + /* TODO (#1#): Handle Error in http form */ + log_error1 ("'boundary' not set for multipart/related"); + return herror_new ("mime_get_attachments", MIME_ERROR_NO_BOUNDARY_PARAM, + "'boundary' not set for multipart/related"); } if (root_id == NULL) { - /* TODO (#1#): Handle Error in http form */ - log_error1("'start' not set for multipart/related"); - return herror_new("mime_get_attachments", MIME_ERROR_NO_START_PARAM, - "'start' not set for multipart/related"); + /* TODO (#1#): Handle Error in http form */ + log_error1 ("'start' not set for multipart/related"); + return herror_new ("mime_get_attachments", MIME_ERROR_NO_START_PARAM, + "'start' not set for multipart/related"); } - mimeMessage = mime_message_parse(in, root_id, boundary, hoption_get(HOPTION_TMP_DIR)); + mimeMessage = + mime_message_parse (in, root_id, boundary, hoption_get (HOPTION_TMP_DIR)); if (mimeMessage == NULL) { - /* TODO (#1#): Handle Error in http form */ - log_error1("MIME Parse Error"); - return herror_new("mime_get_attachments", MIME_ERROR_PARSE_ERROR, - "MIME Parse Error"); + /* TODO (#1#): Handle Error in http form */ + log_error1 ("MIME Parse Error"); + return herror_new ("mime_get_attachments", MIME_ERROR_PARSE_ERROR, + "MIME Parse Error"); } /* Find root */ if (!mimeMessage->root_part) { - attachments_free(mimeMessage); - return herror_new("mime_get_attachments", MIME_ERROR_NO_ROOT_PART, - "No root part found!"); + attachments_free (mimeMessage); + return herror_new ("mime_get_attachments", MIME_ERROR_NO_ROOT_PART, + "No root part found!"); } /* delete root_part from list */ part = mimeMessage->parts; - while (part) { + while (part) + { if (part == mimeMessage->root_part) { if (tmp_part) @@ -941,5 +962,3 @@ herror_t mime_get_attachments(content_type_t *ctype, http_input_stream_t *in, at *dest = mimeMessage; return H_OK; } - - diff --git a/nanohttp/nanohttp-mime.h b/nanohttp/nanohttp-mime.h index be2abfa..6b160ab 100755 --- a/nanohttp/nanohttp-mime.h +++ b/nanohttp/nanohttp-mime.h @@ -3,7 +3,7 @@ * | \/ | | | | \/ | | _/ * |_''_| |_| |_''_| |_'/ PARSER * -* $Id: nanohttp-mime.h,v 1.4 2004/11/02 23:09:26 snowdrop Exp $ +* $Id: nanohttp-mime.h,v 1.5 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -40,20 +40,9 @@ -herror_t mime_get_attachments(content_type_t *ctype, - http_input_stream_t *in, attachments_t **dest); +herror_t mime_get_attachments (content_type_t * ctype, + http_input_stream_t * in, + attachments_t ** dest); #endif - - - - - - - - - - - - diff --git a/nanohttp/nanohttp-request.c b/nanohttp/nanohttp-request.c index 9f4fb8b..5f15e93 100755 --- a/nanohttp/nanohttp-request.c +++ b/nanohttp/nanohttp-request.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-request.c,v 1.6 2005/12/19 14:06:16 snowdrop Exp $ +* $Id: nanohttp-request.c,v 1.7 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -34,253 +34,260 @@ /* request stuff */ -static -hrequest_t *hrequest_new() +static hrequest_t * +hrequest_new () { - hrequest_t * req = (hrequest_t *) malloc(sizeof(hrequest_t)); + hrequest_t *req = (hrequest_t *) malloc (sizeof (hrequest_t)); - req->method = HTTP_REQUEST_GET; - req->version = HTTP_1_1; - req->query = NULL; - req->header = NULL; - req->in = NULL; - req->attachments = NULL; - req->content_type = NULL; + req->method = HTTP_REQUEST_GET; + req->version = HTTP_1_1; + req->query = NULL; + req->header = NULL; + req->in = NULL; + req->attachments = NULL; + req->content_type = NULL; - return req; + return req; } -static -hrequest_t * -_hrequest_parse_header(char *data) +static hrequest_t * +_hrequest_parse_header (char *data) { - hrequest_t *req; - hpair_t *hpair = NULL, *qpair = NULL, *tmppair = NULL; - - char *tmp; - char *tmp2; - char *saveptr; - char *saveptr2; - char *saveptr3; - char *result; - char *key; - char *opt_key; - char *opt_value; - int firstline = 1; - - req = hrequest_new(); - tmp = data; - - for (;;) { - result = (char *) strtok_r(tmp, "\r\n", &saveptr); - tmp = saveptr; - - if (result == NULL) - break; - - if (firstline) { - firstline = 0; - tmp2 = result; - - /* parse [GET|POST] [PATH] [SPEC] */ - key = (char *) strtok_r(tmp2, " ", &saveptr2); - - /* save method (get or post) */ - tmp2 = saveptr2; - if (key != NULL) { - if (!strcmp(key, "POST")) - req->method = HTTP_REQUEST_POST; - else - req->method = HTTP_REQUEST_GET; - } - /* below is key the path and tmp2 the spec */ - key = (char *) strtok_r(tmp2, " ", &saveptr2); - - /* save version */ - tmp2 = saveptr2; - if (tmp2 != NULL) { - /*req->spec = (char *) malloc(strlen(tmp2) + 1); - strcpy(req->spec, tmp2); - */ - if (!strcmp(tmp2, "HTTP/1.0")) - req-> version = HTTP_1_0; - else - req-> version = HTTP_1_1; - } - /* - * parse and save path+query parse: - * /path/of/target?key1=value1&key2=value2... - */ - - if (key != NULL) { - tmp2 = key; - key = (char *) strtok_r(tmp2, "?", &saveptr2); - tmp2 = saveptr2; - - /* save path */ - /*req->path = (char *) malloc(strlen(key) + 1);*/ - strncpy(req->path, key, REQUEST_MAX_PATH_SIZE); - - /* parse options */ - for (;;) { - key = (char *) strtok_r(tmp2, "&", &saveptr2); - tmp2 = saveptr2; - - if (key == NULL) - break; - - opt_key = (char *) strtok_r(key, "=", &saveptr3); - opt_value = saveptr3; - - if (opt_value == NULL) - opt_value = ""; - - /* create option pair */ - if (opt_key != NULL) { - tmppair = (hpair_t *) malloc(sizeof(hpair_t)); - - if (req->query == NULL) { - req->query = qpair = tmppair; - } else { - qpair->next = tmppair; - qpair = tmppair; - } - - /* fill hpairnode_t struct */ - qpair->next = NULL; - qpair->key = (char *) malloc(strlen(opt_key) + 1); - qpair->value = (char *) malloc(strlen(opt_value) + 1); - - strcpy(qpair->key, opt_key); - strcpy(qpair->value, opt_value); - - } - } - } - } else { - - /* parse "key: value" */ - /* tmp2 = result; - key = (char *) strtok_r(tmp2, ": ", &saveptr2); - value = saveptr2;*/ - - /* create pair */ + hrequest_t *req; + hpair_t *hpair = NULL, *qpair = NULL, *tmppair = NULL; + + char *tmp; + char *tmp2; + char *saveptr; + char *saveptr2; + char *saveptr3; + char *result; + char *key; + char *opt_key; + char *opt_value; + int firstline = 1; + + req = hrequest_new (); + tmp = data; + + for (;;) + { + result = (char *) strtok_r (tmp, "\r\n", &saveptr); + tmp = saveptr; + + if (result == NULL) + break; + + if (firstline) + { + firstline = 0; + tmp2 = result; + + /* parse [GET|POST] [PATH] [SPEC] */ + key = (char *) strtok_r (tmp2, " ", &saveptr2); + + /* save method (get or post) */ + tmp2 = saveptr2; + if (key != NULL) + { + if (!strcmp (key, "POST")) + req->method = HTTP_REQUEST_POST; + else + req->method = HTTP_REQUEST_GET; + } + /* below is key the path and tmp2 the spec */ + key = (char *) strtok_r (tmp2, " ", &saveptr2); + + /* save version */ + tmp2 = saveptr2; + if (tmp2 != NULL) + { + /* req->spec = (char *) malloc(strlen(tmp2) + 1); strcpy(req->spec, + tmp2); */ + if (!strcmp (tmp2, "HTTP/1.0")) + req->version = HTTP_1_0; + else + req->version = HTTP_1_1; + } + /* + * parse and save path+query parse: + * /path/of/target?key1=value1&key2=value2... + */ + + if (key != NULL) + { + tmp2 = key; + key = (char *) strtok_r (tmp2, "?", &saveptr2); + tmp2 = saveptr2; + + /* save path */ + /* req->path = (char *) malloc(strlen(key) + 1); */ + strncpy (req->path, key, REQUEST_MAX_PATH_SIZE); + + /* parse options */ + for (;;) + { + key = (char *) strtok_r (tmp2, "&", &saveptr2); + tmp2 = saveptr2; + + if (key == NULL) + break; + + opt_key = (char *) strtok_r (key, "=", &saveptr3); + opt_value = saveptr3; + + if (opt_value == NULL) + opt_value = ""; + + /* create option pair */ + if (opt_key != NULL) + { + tmppair = (hpair_t *) malloc (sizeof (hpair_t)); + + if (req->query == NULL) + { + req->query = qpair = tmppair; + } + else + { + qpair->next = tmppair; + qpair = tmppair; + } + + /* fill hpairnode_t struct */ + qpair->next = NULL; + qpair->key = (char *) malloc (strlen (opt_key) + 1); + qpair->value = (char *) malloc (strlen (opt_value) + 1); + + strcpy (qpair->key, opt_key); + strcpy (qpair->value, 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) { - req->header = hpair = tmppair; - } else { - 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); - */ - } - } - + tmppair = hpairnode_parse (result, ":", NULL); + + if (req->header == NULL) + { + req->header = hpair = tmppair; + } + else + { + 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); */ + } + } + /* Check Content-type */ - tmp = hpairnode_get(req->header, HEADER_CONTENT_TYPE); + tmp = hpairnode_get (req->header, HEADER_CONTENT_TYPE); if (tmp != NULL) - req->content_type = content_type_new(tmp); + req->content_type = content_type_new (tmp); - return req; + return req; } -void -hrequest_free(hrequest_t * req) +void +hrequest_free (hrequest_t * req) { - if (req == NULL) - return; + if (req == NULL) + return; - hpairnode_free_deep(req->header); - hpairnode_free_deep(req->query); + hpairnode_free_deep (req->header); + hpairnode_free_deep (req->query); - if (req->in) - http_input_stream_free(req->in); + if (req->in) + http_input_stream_free (req->in); if (req->content_type) - content_type_free(req->content_type); + content_type_free (req->content_type); if (req->attachments) - attachments_free(req->attachments); + attachments_free (req->attachments); - free(req); + free (req); } herror_t -hrequest_new_from_socket(hsocket_t sock, hrequest_t **out) +hrequest_new_from_socket (hsocket_t sock, hrequest_t ** out) { - int i=0, readed; - herror_t status; - hrequest_t *req; - char buffer[MAX_HEADER_SIZE+1]; - attachments_t *mimeMessage; + int i = 0, readed; + herror_t status; + hrequest_t *req; + char buffer[MAX_HEADER_SIZE + 1]; + attachments_t *mimeMessage; - memset(buffer, 0, MAX_HEADER_SIZE); + memset (buffer, 0, MAX_HEADER_SIZE); /* Read header */ - while (i<MAX_HEADER_SIZE) + while (i < MAX_HEADER_SIZE) { - status = hsocket_read(sock, &(buffer[i]), 1, 1, &readed); + status = hsocket_read (sock, &(buffer[i]), 1, 1, &readed); if (status != H_OK) { - if(herror_code(status) != HSOCKET_SSL_CLOSE){ - log_error1("Socket read error"); - } - return status; + if (herror_code (status) != HSOCKET_SSL_CLOSE) + { + log_error1 ("Socket read error"); + } + return status; } - buffer[i+1] = '\0'; /* for strmp */ + buffer[i + 1] = '\0'; /* for strmp */ if (i > 3) { - if (!strcmp(&(buffer[i-1]), "\n\n") || - !strcmp(&(buffer[i-2]), "\n\r\n")) - break; + if (!strcmp (&(buffer[i - 1]), "\n\n") || + !strcmp (&(buffer[i - 2]), "\n\r\n")) + break; } i++; } /* Create response */ - req = _hrequest_parse_header(buffer); + req = _hrequest_parse_header (buffer); /* Create input stream */ - req->in = http_input_stream_new(sock, req->header); + req->in = http_input_stream_new (sock, req->header); /* Check for MIME message */ - if ((req->content_type && - !strcmp(req->content_type->type, "multipart/related"))) + if ((req->content_type && + !strcmp (req->content_type->type, "multipart/related"))) { - status = mime_get_attachments(req->content_type, req->in, &mimeMessage); + status = mime_get_attachments (req->content_type, req->in, &mimeMessage); if (status != H_OK) { /* TODO (#1#): Handle error */ - hrequest_free(req); - return status; + hrequest_free (req); + return status; } else { req->attachments = mimeMessage; - req->in = http_input_stream_new_from_file(mimeMessage->root_part->filename); + req->in = + http_input_stream_new_from_file (mimeMessage->root_part->filename); } } - + *out = req; return H_OK; } - - - - diff --git a/nanohttp/nanohttp-request.h b/nanohttp/nanohttp-request.h index 8989247..e7b7b3e 100755 --- a/nanohttp/nanohttp-request.h +++ b/nanohttp/nanohttp-request.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-request.h,v 1.2 2004/10/28 10:30:46 snowdrop Exp $ + * $Id: nanohttp-request.h,v 1.3 2006/01/10 11:21:55 snowdrop 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_REQUEST_H -#define NANO_HTTP_REQUEST_H +#ifndef NANO_HTTP_REQUEST_H +#define NANO_HTTP_REQUEST_H #include <nanohttp/nanohttp-stream.h> #include <nanohttp/nanohttp-mime.h> @@ -43,15 +43,9 @@ typedef struct hrequest content_type_t *content_type; attachments_t *attachments; char root_part_id[150]; -}hrequest_t; +} hrequest_t; -herror_t hrequest_new_from_socket(hsocket_t sock, hrequest_t **out); -void hrequest_free(hrequest_t *req); +herror_t hrequest_new_from_socket (hsocket_t sock, hrequest_t ** out); +void hrequest_free (hrequest_t * req); #endif - - - - - - diff --git a/nanohttp/nanohttp-response.c b/nanohttp/nanohttp-response.c index 1447345..ee9e762 100755 --- a/nanohttp/nanohttp-response.c +++ b/nanohttp/nanohttp-response.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-response.c,v 1.5 2005/05/27 19:28:15 snowdrop Exp $ +* $Id: nanohttp-response.c,v 1.6 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -31,196 +31,205 @@ #include <utils/alloc.h> #endif -static -hresponse_t* -hresponse_new() +static hresponse_t * +hresponse_new () { - hresponse_t *res; - - /* create response object */ - res = (hresponse_t *) malloc(sizeof(hresponse_t)); - res->version = HTTP_1_1; - res->errcode = -1; - res->desc[0] = '\0'; - res->header = NULL; - res->in = NULL; - res->content_type = NULL; - res->attachments = NULL; - return res; + hresponse_t *res; + + /* create response object */ + res = (hresponse_t *) malloc (sizeof (hresponse_t)); + res->version = HTTP_1_1; + res->errcode = -1; + res->desc[0] = '\0'; + res->header = NULL; + res->in = NULL; + res->content_type = NULL; + res->attachments = NULL; + return res; } -static -hresponse_t* -_hresponse_parse_header(const char *buffer) +static hresponse_t * +_hresponse_parse_header (const char *buffer) { - hresponse_t *res; - char *s1, *s2, *str; - - /* create response object */ - res = hresponse_new(); - - /* *** parse spec *** */ - /* [HTTP/1.1 | 1.2] [CODE] [DESC] */ - - /* stage 1: HTTP spec */ - str = (char *) strtok_r((char *) buffer, " ", &s2); - s1 = s2; - if (str == NULL) { - log_error1("Parse error reading HTTP spec"); - return NULL; - } - - if (!strcmp(str, "HTTP/1.0")) - res->version = HTTP_1_0; - else - res->version = HTTP_1_1; - - /* stage 2: http code */ - str = (char *) strtok_r(s1, " ", &s2); - s1 = s2; - if (str == NULL) { - log_error1("Parse error reading HTTP code"); - return NULL; - } - res->errcode = atoi(str); - - /* stage 3: description text */ - str = (char *) strtok_r(s1, "\r\n", &s2); - s1 = s2; - if (str == NULL) { - log_error1("Parse error reading HTTP description"); - return NULL; - } + hresponse_t *res; + char *s1, *s2, *str; + + /* create response object */ + res = hresponse_new (); + + /* *** parse spec *** */ + /* [HTTP/1.1 | 1.2] [CODE] [DESC] */ + + /* stage 1: HTTP spec */ + str = (char *) strtok_r ((char *) buffer, " ", &s2); + s1 = s2; + if (str == NULL) + { + log_error1 ("Parse error reading HTTP spec"); + return NULL; + } + + if (!strcmp (str, "HTTP/1.0")) + res->version = HTTP_1_0; + else + res->version = HTTP_1_1; + + /* stage 2: http code */ + str = (char *) strtok_r (s1, " ", &s2); + s1 = s2; + if (str == NULL) + { + log_error1 ("Parse error reading HTTP code"); + return NULL; + } + res->errcode = atoi (str); + + /* stage 3: description text */ + str = (char *) strtok_r (s1, "\r\n", &s2); + s1 = s2; + if (str == NULL) + { + log_error1 ("Parse error reading HTTP description"); + return NULL; + } /* res->desc = (char *) malloc(strlen(str) + 1);*/ - strncpy(res->desc, str, RESPONSE_MAX_DESC_SIZE); - res->desc[strlen(str)] = '\0'; - - /* *** parse header *** */ - /* [key]: [value] */ - for (;;) { - str = strtok_r(s1, "\n", &s2); - s1 = s2; - - /* check if header ends without body */ - if (str == NULL) { - return res; - } - /* check also for end of header */ - if (!strcmp(str, "\r")) { - break; - } - str[strlen(str) - 1] = '\0'; - res->header = hpairnode_parse(str, ":", res->header); - } + strncpy (res->desc, str, RESPONSE_MAX_DESC_SIZE); + res->desc[strlen (str)] = '\0'; + + /* *** parse header *** */ + /* [key]: [value] */ + for (;;) + { + str = strtok_r (s1, "\n", &s2); + s1 = s2; + + /* check if header ends without body */ + if (str == NULL) + { + return res; + } + /* check also for end of header */ + if (!strcmp (str, "\r")) + { + break; + } + str[strlen (str) - 1] = '\0'; + res->header = hpairnode_parse (str, ":", res->header); + } /* Check Content-type */ - str = hpairnode_get(res->header, HEADER_CONTENT_TYPE); + str = hpairnode_get (res->header, HEADER_CONTENT_TYPE); if (str != NULL) - res->content_type = content_type_new(str); + res->content_type = content_type_new (str); - /* return response object */ - return res; + /* return response object */ + return res; } herror_t -hresponse_new_from_socket(hsocket_t sock, hresponse_t **out) +hresponse_new_from_socket (hsocket_t sock, hresponse_t ** out) { - int i=0, readed; - herror_t status; - hresponse_t *res; - attachments_t *mimeMessage; - char buffer[MAX_HEADER_SIZE+1]; + int i = 0, readed; + herror_t status; + hresponse_t *res; + attachments_t *mimeMessage; + char buffer[MAX_HEADER_SIZE + 1]; -read_header: /* for errorcode: 100 (continue) */ +read_header: /* for errorcode: 100 (continue) */ /* Read header */ - while (i<MAX_HEADER_SIZE) + while (i < MAX_HEADER_SIZE) { - status = hsocket_read(sock, &(buffer[i]), 1, 1, &readed); + status = hsocket_read (sock, &(buffer[i]), 1, 1, &readed); if (status != H_OK) { - log_error1("Socket read error"); - return status; + log_error1 ("Socket read error"); + return status; } - buffer[i+1] = '\0'; /* for strmp */ + buffer[i + 1] = '\0'; /* for strmp */ if (i > 3) { - if (!strcmp(&(buffer[i-1]), "\n\n") || - !strcmp(&(buffer[i-2]), "\n\r\n")) - break; + if (!strcmp (&(buffer[i - 1]), "\n\n") || + !strcmp (&(buffer[i - 2]), "\n\r\n")) + break; } i++; } /* Create response */ - res = _hresponse_parse_header(buffer); + res = _hresponse_parse_header (buffer); if (res == NULL) { - log_error1("Header parse error"); - return herror_new("hresponse_new_from_socket", - GENERAL_HEADER_PARSE_ERROR, "Can not parse response header"); + log_error1 ("Header parse error"); + return herror_new ("hresponse_new_from_socket", + GENERAL_HEADER_PARSE_ERROR, + "Can not parse response header"); } /* Chec for Errorcode: 100 (continue) */ - if (res->errcode == 100) { - hresponse_free(res); - i=0; + if (res->errcode == 100) + { + hresponse_free (res); + i = 0; goto read_header; } /* Create input stream */ - res->in = http_input_stream_new(sock, res->header); + res->in = http_input_stream_new (sock, res->header); /* Check for MIME message */ - if ((res->content_type && - !strcmp(res->content_type->type, "multipart/related"))) + if ((res->content_type && + !strcmp (res->content_type->type, "multipart/related"))) { - status = mime_get_attachments(res->content_type, res->in, &mimeMessage); + status = mime_get_attachments (res->content_type, res->in, &mimeMessage); if (status != H_OK) { /* TODO (#1#): Handle error */ - hresponse_free(res); - return status; + hresponse_free (res); + return status; } else { res->attachments = mimeMessage; - http_input_stream_free(res->in); - res->in = http_input_stream_new_from_file(mimeMessage->root_part->filename); - if (!res->in) { - /* TODO (#1#): Handle error */ - - } else { - /*res->in->deleteOnExit = 1;*/ - } + http_input_stream_free (res->in); + res->in = + http_input_stream_new_from_file (mimeMessage->root_part->filename); + if (!res->in) + { + /* TODO (#1#): Handle error */ + + } + else + { + /* res->in->deleteOnExit = 1; */ + } } } - *out = res; + *out = res; return H_OK; } - -void -hresponse_free(hresponse_t * res) + +void +hresponse_free (hresponse_t * res) { - if (res == NULL) - return; + if (res == NULL) + return; if (res->header) - hpairnode_free_deep(res->header); + hpairnode_free_deep (res->header); if (res->in) - http_input_stream_free(res->in); + http_input_stream_free (res->in); if (res->content_type) - content_type_free(res->content_type); + content_type_free (res->content_type); if (res->attachments) - attachments_free(res->attachments); - free(res); + attachments_free (res->attachments); + free (res); } - diff --git a/nanohttp/nanohttp-response.h b/nanohttp/nanohttp-response.h index 63dd463..70e5348 100755 --- a/nanohttp/nanohttp-response.h +++ b/nanohttp/nanohttp-response.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-response.h,v 1.2 2004/10/28 10:30:46 snowdrop Exp $ + * $Id: nanohttp-response.h,v 1.3 2006/01/10 11:21:55 snowdrop 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_RESPONSE_H -#define NANO_HTTP_RESPONSE_H +#ifndef NANO_HTTP_RESPONSE_H +#define NANO_HTTP_RESPONSE_H #include <nanohttp/nanohttp-stream.h> #include <nanohttp/nanohttp-common.h> @@ -41,17 +41,11 @@ typedef struct hresponse content_type_t *content_type; attachments_t *attachments; char root_part_id[150]; -}hresponse_t; +} hresponse_t; -herror_t hresponse_new_from_socket(hsocket_t sock, hresponse_t **out); -void hresponse_free(hresponse_t *res); +herror_t hresponse_new_from_socket (hsocket_t sock, hresponse_t ** out); +void hresponse_free (hresponse_t * res); #endif - - - - - - diff --git a/nanohttp/nanohttp-server.c b/nanohttp/nanohttp-server.c index 0ff7d08..102c62a 100644 --- a/nanohttp/nanohttp-server.c +++ b/nanohttp/nanohttp-server.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-server.c,v 1.37 2006/01/06 15:16:03 mrcsys Exp $ +* $Id: nanohttp-server.c,v 1.38 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -51,7 +51,7 @@ #else -#include <process.h> +#include <process.h> #endif @@ -62,15 +62,15 @@ typedef struct _conndata { - hsocket_t sock; + hsocket_t sock; #ifdef WIN32 - HANDLE tid; + HANDLE tid; #else - pthread_t tid; - pthread_attr_t attr; + pthread_t tid; + pthread_attr_t attr; #endif - time_t atime; -}conndata_t; + time_t atime; +} conndata_t; /* * ----------------------------------------------------- @@ -96,13 +96,13 @@ static conndata_t *_httpd_connection; #ifdef WIN32 -static void WSAReaper(void *x); +static void WSAReaper (void *x); #else sigset_t thrsigset; #endif #ifdef HAVE_SSL -extern SSL_CTX* SSLctx; +extern SSL_CTX *SSLctx; #endif @@ -118,9 +118,9 @@ httpd_init (int argc, char *argv[]) int i; herror_t status; - hoption_init_args(argc, argv); + hoption_init_args (argc, argv); - status = hsocket_module_init (); + status = hsocket_module_init (); if (status != H_OK) return status; @@ -159,19 +159,17 @@ httpd_init (int argc, char *argv[]) } #ifdef WIN32 - /* - if (_beginthread (WSAReaper, 0, NULL) == -1) - { - log_error1 ("Winsock reaper thread failed to start"); - return herror_new("httpd_init", THREAD_BEGIN_ERROR, - "_beginthread() failed while starting WSAReaper"); - } - */ + /* + if (_beginthread (WSAReaper, 0, NULL) == -1) { log_error1 ("Winsock + reaper thread failed to start"); return herror_new("httpd_init", + THREAD_BEGIN_ERROR, "_beginthread() failed while starting WSAReaper"); + } */ #endif /* create socket */ status = hsocket_init (&_httpd_socket); - if( status != H_OK ){ + if (status != H_OK) + { return status; } status = hsocket_bind (&_httpd_socket, _httpd_port); @@ -226,10 +224,10 @@ httpd_services () * FUNCTION: httpd_services * ----------------------------------------------------- */ -static -void hservice_free(hservice_t *service) +static void +hservice_free (hservice_t * service) { - free(service); + free (service); } /* @@ -293,7 +291,7 @@ httpd_send_header (httpd_conn_t * res, int code, const char *text) strcat (header, "\r\n"); /* set content-type */ - /* + /* * if (res->content_type[0] == '\0') { strcat(header, "Content-Type: * text/html\r\n"); } else { sprintf(buffer, "Content-Type: %s\r\n", * res->content_type); strcat(header, buffer); } @@ -303,7 +301,7 @@ httpd_send_header (httpd_conn_t * res, int code, const char *text) strcat (header, "Server: Nano HTTPD library\r\n"); /* set _httpd_connection status */ - //strcat (header, "Connection: close\r\n"); + // strcat (header, "Connection: close\r\n"); /* add pairs */ cur = res->header; @@ -319,9 +317,9 @@ httpd_send_header (httpd_conn_t * res, int code, const char *text) /* send header */ status = hsocket_nsend (res->sock, header, strlen (header)); - if (status != H_OK) - return status; - + if (status != H_OK) + return status; + res->out = http_output_stream_new (res->sock, res->header); return H_OK; } @@ -339,7 +337,7 @@ httpd_send_internal_error (httpd_conn_t * conn, const char *errmsg) #ifdef WIN32 #define snprintf(buffer, num, s1, s2) sprintf(buffer, s1,s2) #endif - snprintf(buflen, 5, "%d", strlen(buffer)); + snprintf (buflen, 5, "%d", strlen (buffer)); httpd_set_header (conn, HEADER_CONTENT_LENGTH, buflen); httpd_send_header (conn, 500, "INTERNAL"); return hsocket_nsend (conn->sock, buffer, strlen (buffer)); @@ -356,9 +354,11 @@ httpd_request_print (hrequest_t * req) hpair_t *pair; log_verbose1 ("++++++ Request +++++++++"); - log_verbose2 (" Method : '%s'", (req->method == HTTP_REQUEST_POST)?"POST":"GET"); + log_verbose2 (" Method : '%s'", + (req->method == HTTP_REQUEST_POST) ? "POST" : "GET"); log_verbose2 (" Path : '%s'", req->path); - log_verbose2 (" Spec : '%s'", (req->version==HTTP_1_0)?"HTTP/1.0":"HTTP/1.1"); + log_verbose2 (" Spec : '%s'", + (req->version == HTTP_1_0) ? "HTTP/1.0" : "HTTP/1.1"); log_verbose1 (" Parsed query string :"); pair = req->query; @@ -379,7 +379,8 @@ httpd_request_print (hrequest_t * req) } -httpd_conn_t *httpd_new(hsocket_t sock) +httpd_conn_t * +httpd_new (hsocket_t sock) { httpd_conn_t *conn = (httpd_conn_t *) malloc (sizeof (httpd_conn_t)); conn->sock = sock; @@ -391,18 +392,21 @@ httpd_conn_t *httpd_new(hsocket_t sock) } -void httpd_free(httpd_conn_t *conn) +void +httpd_free (httpd_conn_t * conn) { if (conn->out != NULL) - http_output_stream_free(conn->out); + http_output_stream_free (conn->out); if (conn->header != NULL) - hpairnode_free_deep(conn->header); + hpairnode_free_deep (conn->header); - free(conn); + free (conn); } -void do_req_timeout(int signum){ +void +do_req_timeout (int signum) +{ /* struct sigaction req_timeout; memset(&req_timeout, 0, sizeof(&req_timeout)); @@ -410,12 +414,12 @@ void do_req_timeout(int signum){ sigaction(SIGALRM, &req_timeout, NULL); */ - // XXX this is not real pretty, is there a better way? - log_verbose1("Thread timeout."); + // XXX this is not real pretty, is there a better way? + log_verbose1 ("Thread timeout."); #ifdef WIN32 - _endthread (); - #else - pthread_exit(0); + _endthread (); +#else + pthread_exit (0); #endif } @@ -435,7 +439,7 @@ httpd_session_main (void *data) conndata_t *conn = (conndata_t *) data; const char *msg = "SESSION 1.0\n"; int len = strlen (msg); - int done=0; + int done = 0; char buffer[256]; /* temp buffer for recv() */ char header[4064]; /* received header */ hrequest_t *req = NULL; /* only for test */ @@ -448,14 +452,19 @@ httpd_session_main (void *data) log_verbose1 ("starting httpd_session_main()"); #ifdef HAVE_SSL - if( !SSLctx ){ - log_verbose1("Using HTTP"); - } else { - log_verbose1("Using HTTPS"); - conn->sock.ssl = init_ssl(SSLctx, conn->sock.sock, SSL_SERVER); + if (!SSLctx) + { + log_verbose1 ("Using HTTP"); + } + else + { + log_verbose1 ("Using HTTPS"); + conn->sock.ssl = init_ssl (SSLctx, conn->sock.sock, SSL_SERVER); hsocket_block (conn->sock, 0); - if( conn->sock.ssl == NULL ){ - return herror_new("hsocket_accept", SSL_ERROR_INIT, "Unable to initialize SSL"); + if (conn->sock.ssl == NULL) + { + return herror_new ("hsocket_accept", SSL_ERROR_INIT, + "Unable to initialize SSL"); } } #endif @@ -463,69 +472,77 @@ httpd_session_main (void *data) /* call the service */ /* req = hrequest_new_from_buffer (header);*/ - do{ - log_verbose1("starting HTTP request"); - rconn = httpd_new(conn->sock); + do + { + log_verbose1 ("starting HTTP request"); + rconn = httpd_new (conn->sock); - status = hrequest_new_from_socket (conn->sock, &req); + status = hrequest_new_from_socket (conn->sock, &req); - if (status != H_OK) + if (status != H_OK) + { + if (herror_code (status) != HSOCKET_SSL_CLOSE) { - if(herror_code(status) != HSOCKET_SSL_CLOSE){ - httpd_send_internal_error (rconn, herror_message(status)/*"Request parse error!"*/); - herror_release(status); - } - done=1; + httpd_send_internal_error (rconn, herror_message (status) /* "Request + parse + error!" */ ); + herror_release (status); } - else - { - char *conn_str = hpairnode_get_ignore_case (req->header, HEADER_CONNECTION); + done = 1; + } + else + { + char *conn_str = + hpairnode_get_ignore_case (req->header, HEADER_CONNECTION); #ifdef WIN32 #define strncasecmp(s1, s2, num) strncmp(s1, s2, num) #endif - if(conn_str && strncasecmp( conn_str, "close",5 ) == 0){ - done=1; - } - if(!done){ - done = req->version==HTTP_1_0?1:0; - } - httpd_request_print (req); + if (conn_str && strncasecmp (conn_str, "close", 5) == 0) + { + done = 1; + } + if (!done) + { + done = req->version == HTTP_1_0 ? 1 : 0; + } + httpd_request_print (req); - service = httpd_find_service (req->path); - if (service != NULL) + service = httpd_find_service (req->path); + if (service != NULL) + { + log_verbose2 ("service '%s' found", req->path); + if (service->func != NULL) { - log_verbose2 ("service '%s' found", req->path); - if (service->func != NULL) - { - service->func (rconn, req); - } - else - { - sprintf (buffer, - "service '%s' not registered properly (func == NULL)", - req->path); - log_verbose1 (buffer); - httpd_send_internal_error (rconn, buffer); - } + service->func (rconn, req); } else { - sprintf (buffer, "service '%s' not found", req->path); + sprintf (buffer, + "service '%s' not registered properly (func == NULL)", + req->path); log_verbose1 (buffer); httpd_send_internal_error (rconn, buffer); } - - /* httpd_response_free(res); */ - /*hrequest_free (req);*/ } - }while(!done); + else + { + sprintf (buffer, "service '%s' not found", req->path); + log_verbose1 (buffer); + httpd_send_internal_error (rconn, buffer); + } + + /* httpd_response_free(res); */ + /* hrequest_free (req); */ + } + } + while (!done); - hsocket_close(conn->sock); - log_verbose1("Marking connection as available"); + hsocket_close (conn->sock); + log_verbose1 ("Marking connection as available"); conn->sock.sock = 0; - hrequest_free(req); - httpd_free(rconn); + hrequest_free (req); + httpd_free (rconn); #ifdef WIN32 CloseHandle ((HANDLE) conn->tid); _endthread (); @@ -567,10 +584,12 @@ httpd_set_header (httpd_conn_t * conn, const char *key, const char *value) } -void httpd_set_headers(httpd_conn_t *conn, hpair_t *header) +void +httpd_set_headers (httpd_conn_t * conn, hpair_t * header) { - while (header) { - httpd_set_header(conn, header->key, header->value); + while (header) + { + httpd_set_header (conn, header->key, header->value); header = header->next; } } @@ -584,7 +603,7 @@ void httpd_set_headers(httpd_conn_t *conn, hpair_t *header) BOOL WINAPI httpd_term (DWORD sig) { - //log_debug2 ("Got signal %d", sig); + // log_debug2 ("Got signal %d", sig); if (sig == _httpd_terminate_signal) _httpd_run = 0; return TRUE; @@ -671,7 +690,7 @@ _httpd_start_thread (conndata_t * conn) #ifdef PTHREAD_CREATE_DETACHED pthread_attr_setdetachstate (&(conn->attr), PTHREAD_CREATE_DETACHED); #endif - pthread_sigmask(SIG_BLOCK, &thrsigset, NULL); + pthread_sigmask (SIG_BLOCK, &thrsigset, NULL); err = pthread_create (&(conn->tid), &(conn->attr), httpd_session_main, conn); if (err) @@ -704,8 +723,8 @@ httpd_run () timeout.tv_usec = 0; #ifdef WIN32 #else - sigemptyset(&thrsigset); - sigaddset(&thrsigset, SIGALRM); + sigemptyset (&thrsigset); + sigaddset (&thrsigset, SIGALRM); #endif @@ -713,7 +732,7 @@ httpd_run () err = hsocket_listen (_httpd_socket); if (err != H_OK) { - log_error2 ("httpd_run(): '%d'", herror_message(err)); + log_error2 ("httpd_run(): '%d'", herror_message (err)); return err; } log_verbose2 ("listening to port '%d'", _httpd_port); @@ -725,7 +744,7 @@ httpd_run () err = hsocket_block (_httpd_socket, 0); if (err != H_OK) { - log_error2 ("httpd_run(): '%s'", herror_message(err)); + log_error2 ("httpd_run(): '%s'", herror_message (err)); return err; } @@ -775,18 +794,20 @@ httpd_run () /* Accept a socket */ err = hsocket_accept (_httpd_socket, &(conn->sock)); - if (err != H_OK && herror_code(err) == SSL_ERROR_INIT) { - hsocket_close(conn->sock); + if (err != H_OK && herror_code (err) == SSL_ERROR_INIT) + { + hsocket_close (conn->sock); conn->sock.sock = -1; #ifdef HAVE_SSL conn->sock.ssl = NULL; #endif - log_error1(herror_message(err)); + log_error1 (herror_message (err)); continue; - } else if (err != H_OK) + } + else if (err != H_OK) { - log_error2 ("Can not accept socket: %s", herror_message(err)); - return err; /* this is hard core! */ + log_error2 ("Can not accept socket: %s", herror_message (err)); + return err; /* this is hard core! */ } /* Now start a thread */ @@ -796,54 +817,65 @@ httpd_run () return 0; } -void httpd_destroy() +void +httpd_destroy () { hservice_t *tmp, *cur = _httpd_services_head; while (cur != NULL) { tmp = cur->next; - hservice_free(cur); + hservice_free (cur); cur = tmp; } - hsocket_module_destroy(); + hsocket_module_destroy (); } #ifdef WIN32 -static -void WSAReaper(void *x) +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; + 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 @@ -888,7 +920,7 @@ httpd_get_postdata (httpd_conn_t * conn, hrequest_t * req, long *received, log_error1 ("Not enough memory"); return NULL; } - if (http_input_stream_read(req->in, postdata, (int) content_length) > 0) + if (http_input_stream_read (req->in, postdata, (int) content_length) > 0) { *received = content_length; postdata[content_length] = '\0'; @@ -928,14 +960,13 @@ httpd_mime_send_header (httpd_conn_t * conn, char boundary[250]; /* - Set Content-type - Set multipart/related parameter - type=..; start=.. ; start-info= ..; boundary=... + Set Content-type Set multipart/related parameter type=..; start=.. ; + start-info= ..; boundary=... */ - /* - using sprintf instead of snprintf because visual c does not support snprintf - */ + /* + using sprintf instead of snprintf because visual c does not support + snprintf */ sprintf (buffer, "multipart/related;"); @@ -1023,8 +1054,8 @@ httpd_mime_send_file (httpd_conn_t * conn, size_t size; if (fd == NULL) - return herror_new("httpd_mime_send_file", FILE_ERROR_OPEN, - "Can not open file '%d'", filename); + return herror_new ("httpd_mime_send_file", FILE_ERROR_OPEN, + "Can not open file '%d'", filename); status = httpd_mime_next (conn, content_id, content_type, transfer_encoding); @@ -1040,12 +1071,13 @@ httpd_mime_send_file (httpd_conn_t * conn, if (size == -1) { fclose (fd); - return herror_new("httpd_mime_send_file", FILE_ERROR_READ, - "Can not read from file '%d'", filename); + return herror_new ("httpd_mime_send_file", FILE_ERROR_READ, + "Can not read from file '%d'", filename); } status = http_output_stream_write (conn->out, buffer, size); - if (status != H_OK) { + if (status != H_OK) + { fclose (fd); return status; } diff --git a/nanohttp/nanohttp-server.h b/nanohttp/nanohttp-server.h index f0550f8..101a4b7 100644 --- a/nanohttp/nanohttp-server.h +++ b/nanohttp/nanohttp-server.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-server.h,v 1.9 2004/11/02 23:09:27 snowdrop Exp $ + * $Id: nanohttp-server.h,v 1.10 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -21,8 +21,8 @@ * * Email: ferhatayaz@yahoo.com ******************************************************************/ -#ifndef NANO_HTTP_SERVER_H -#define NANO_HTTP_SERVER_H +#ifndef NANO_HTTP_SERVER_H +#define NANO_HTTP_SERVER_H #include <nanohttp/nanohttp-common.h> @@ -38,13 +38,13 @@ typedef struct httpd_conn char content_type[25]; http_output_stream_t *out; hpair_t *header; -}httpd_conn_t; +} httpd_conn_t; /* Service callback */ -typedef void (*httpd_service)(httpd_conn_t*, hrequest_t*); +typedef void (*httpd_service) (httpd_conn_t *, hrequest_t *); /* @@ -55,25 +55,25 @@ typedef struct tag_hservice char ctx[255]; httpd_service func; struct tag_hservice *next; -}hservice_t; +} hservice_t; /* Begin httpd_* function set */ -herror_t httpd_init(int argc, char *argv[]); -int httpd_register(const char* ctx, httpd_service service); -herror_t httpd_run(); -void httpd_destroy(); +herror_t httpd_init (int argc, char *argv[]); +int httpd_register (const char *ctx, httpd_service service); +herror_t httpd_run (); +void httpd_destroy (); -hservice_t *httpd_services(); +hservice_t *httpd_services (); -herror_t httpd_send_header(httpd_conn_t *res, - int code, const char* text); +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); +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); /* unsigned char *httpd_get_postdata(httpd_conn_t *conn, @@ -90,36 +90,36 @@ 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, - const char* related_start, - const char* related_start_info, - const char* related_type, int code, const char* text); +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, + const char *text); /** Send boundary and part header and continue with next part */ -herror_t httpd_mime_next(httpd_conn_t *conn, - const char* content_id, - const char* content_type, - const char* transfer_encoding); +herror_t httpd_mime_next (httpd_conn_t * conn, + const char *content_id, + const char *content_type, + const char *transfer_encoding); /** Send boundary and part header and continue with next part */ -herror_t httpd_mime_send_file(httpd_conn_t *conn, - const char* content_id, - const char* content_type, - const char* transfer_encoding, - const char* filename); +herror_t httpd_mime_send_file (httpd_conn_t * conn, + const char *content_id, + const char *content_type, + const char *transfer_encoding, + const char *filename); /** Finish MIME request Returns: HSOCKET_OK or error flag */ -herror_t httpd_mime_end(httpd_conn_t *conn); +herror_t httpd_mime_end (httpd_conn_t * conn); #endif - diff --git a/nanohttp/nanohttp-socket.c b/nanohttp/nanohttp-socket.c index 684585c..ee69561 100644 --- a/nanohttp/nanohttp-socket.c +++ b/nanohttp/nanohttp-socket.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-socket.c,v 1.41 2006/01/06 16:17:36 snowdrop Exp $ +* $Id: nanohttp-socket.c,v 1.42 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -26,16 +26,16 @@ #include <nanohttp/nanohttp-ssl.h> #ifdef WIN32 - #include "wsockcompat.h" - #include <winsock2.h> - #include <process.h> - - #ifndef __MINGW32__ - typedef int ssize_t; - #endif - +#include "wsockcompat.h" +#include <winsock2.h> +#include <process.h> + +#ifndef __MINGW32__ +typedef int ssize_t; +#endif + #else - #include <fcntl.h> +#include <fcntl.h> #endif #ifdef HAVE_CONFIG_H @@ -78,7 +78,7 @@ #ifdef HAVE_SSL -SSL_CTX* SSLctx = NULL; +SSL_CTX *SSLctx = NULL; char *SSLCert = NULL; char *SSLPass = NULL; char *SSLCA = NULL; @@ -121,19 +121,22 @@ hsocket_module_destroy () FUNCTION: hsocket_init ----------------------------------------------------*/ herror_t -hsocket_init (hsocket_t * sock ) +hsocket_init (hsocket_t * sock) { - log_verbose1("Starting hsocket init"); + log_verbose1 ("Starting hsocket init"); /* just set the descriptor to -1 */ sock->sock = -1; #ifdef HAVE_SSL sock->ssl = NULL; - if(SSLCert || SSLCertLess){ - log_verbose1("calling init ctx"); - SSLctx=initialize_ctx( SSLCert,SSLPass,SSLCA); - if(SSLctx==NULL){ - return herror_new("hsocket_init", HSOCKET_ERROR_CONNECT, "Unable to initialize SSL CTX" ); - } + if (SSLCert || SSLCertLess) + { + log_verbose1 ("calling init ctx"); + SSLctx = initialize_ctx (SSLCert, SSLPass, SSLCA); + if (SSLctx == NULL) + { + return herror_new ("hsocket_init", HSOCKET_ERROR_CONNECT, + "Unable to initialize SSL CTX"); + } } #endif return H_OK; @@ -161,33 +164,40 @@ hsocket_open (hsocket_t * dsock, const char *hostname, int port) sock.sock = socket (AF_INET, SOCK_STREAM, 0); if (sock.sock <= 0) - return herror_new("hsocket_open", HSOCKET_ERROR_CREATE, "Socket error: %d", errno); + return herror_new ("hsocket_open", HSOCKET_ERROR_CREATE, + "Socket error: %d", errno); /* Get host data */ host = gethostbyname (hostname); if (host == NULL) - return herror_new("hsocket_open", HSOCKET_ERROR_GET_HOSTNAME, "Socket error: %d", errno); + return herror_new ("hsocket_open", HSOCKET_ERROR_GET_HOSTNAME, + "Socket error: %d", errno); ip = inet_ntoa (*(struct in_addr *) *host->h_addr_list); address.sin_addr.s_addr = inet_addr (ip); /* set server addresss */ address.sin_family = host->h_addrtype; - address.sin_port = htons((unsigned short)port); + address.sin_port = htons ((unsigned short) port); /* connect to the server */ - if (connect (sock.sock, (struct sockaddr *) &address, sizeof (address)) != 0) - return herror_new("hsocket_open", HSOCKET_ERROR_CONNECT, "Socket error: %d", errno); + if (connect (sock.sock, (struct sockaddr *) &address, sizeof (address)) != + 0) + return herror_new ("hsocket_open", HSOCKET_ERROR_CONNECT, + "Socket error: %d", errno); #ifdef HAVE_SSL - if( !SSLctx ){ + if (!SSLctx) + { #endif - log_verbose1("Using HTTP"); + log_verbose1 ("Using HTTP"); dsock->sock = sock.sock; #ifdef HAVE_SSL - } else { - log_verbose1("Using HTTPS"); - dsock->ssl = init_ssl(SSLctx, sock.sock, SSL_CLIENT); + } + else + { + log_verbose1 ("Using HTTPS"); + dsock->ssl = init_ssl (SSLctx, sock.sock, SSL_CLIENT); } #endif return H_OK; @@ -201,28 +211,31 @@ hsocket_bind (hsocket_t * dsock, int port) { hsocket_t sock; struct sockaddr_in addr; - int opt=1; + int opt = 1; /* create socket */ sock.sock = socket (AF_INET, SOCK_STREAM, 0); if (sock.sock == -1) { log_error3 ("Can not create socket: '%s'", "Socket error: %d", errno); - return herror_new("hsocket_bind", HSOCKET_ERROR_CREATE, "Socket error: %d", errno); + return herror_new ("hsocket_bind", HSOCKET_ERROR_CREATE, + "Socket error: %d", errno); } - setsockopt( sock.sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt) ); + setsockopt (sock.sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt)); /* bind socket */ addr.sin_family = AF_INET; - addr.sin_port = htons ((unsigned short)port); /* short, network byte order */ + addr.sin_port = htons ((unsigned short) port); /* short, network + byte order */ addr.sin_addr.s_addr = INADDR_ANY; - memset (&(addr.sin_zero), '\0', 8); /* zero the rest of the - * struct */ + 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_error3 ("Can not bind: '%s'", "Socket error: %d", errno); - return herror_new("hsocket_bind", HSOCKET_ERROR_BIND, "Socket error: %d", errno); + return herror_new ("hsocket_bind", HSOCKET_ERROR_BIND, "Socket error: %d", + errno); } dsock->sock = sock.sock; return H_OK; @@ -239,28 +252,34 @@ hsocket_accept (hsocket_t sock, hsocket_t * dest) struct sockaddr_in addr; if (sock.sock <= 0) - return herror_new("hsocket_accept", HSOCKET_ERROR_NOT_INITIALIZED, - "Called hsocket_listen() before initializing!"); + return herror_new ("hsocket_accept", HSOCKET_ERROR_NOT_INITIALIZED, + "Called hsocket_listen() before initializing!"); asize = sizeof (struct sockaddr_in); #ifdef WIN32 while (1) { sockfd.sock = accept (sock.sock, (struct sockaddr *) &addr, &asize); - if (sockfd.sock == INVALID_SOCKET) { + if (sockfd.sock == INVALID_SOCKET) + { if (WSAGetLastError () != WSAEWOULDBLOCK) - return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT, "Socket error: %d", errno); - } else { + return herror_new ("hsocket_accept", HSOCKET_ERROR_ACCEPT, + "Socket error: %d", errno); + } + else + { break; } } -#else +#else /* TODO (#1#): why not a loop like in win32? */ sockfd.sock = accept (sock.sock, (struct sockaddr *) &addr, &asize); - if (sockfd.sock == -1) { - return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT, "Socket error: %d", errno); + if (sockfd.sock == -1) + { + return herror_new ("hsocket_accept", HSOCKET_ERROR_ACCEPT, + "Socket error: %d", errno); } -#endif +#endif /* TODO (#1#): Write to access.log file */ log_verbose3 ("accept new socket (%d) from '%s'", sockfd.sock, @@ -277,21 +296,22 @@ herror_t hsocket_listen (hsocket_t sock) { if (sock.sock <= 0) - return herror_new("hsocket_listen", HSOCKET_ERROR_NOT_INITIALIZED, - "Called hsocket_listen() before initializing!"); + return herror_new ("hsocket_listen", HSOCKET_ERROR_NOT_INITIALIZED, + "Called hsocket_listen() before initializing!"); if (listen (sock.sock, 15) == -1) { log_error3 ("Can not listen: '%s'", "Socket error: %d", errno); - return herror_new("hsocket_listen", HSOCKET_ERROR_LISTEN, "Socket error: %d", errno); + return herror_new ("hsocket_listen", HSOCKET_ERROR_LISTEN, + "Socket error: %d", errno); } return H_OK; } #if 0 -static -void _hsocket_wait_until_receive(hsocket_t sock) +static void +_hsocket_wait_until_receive (hsocket_t sock) { fd_set fds; struct timeval timeout; @@ -300,15 +320,15 @@ void _hsocket_wait_until_receive(hsocket_t sock) timeout.tv_sec = 1; timeout.tv_usec = 0; - while (1) + while (1) { log_verbose1 ("waiting until receive mode"); /* zero and set file descriptior */ FD_ZERO (&fds); FD_SET (sock, &fds); - + /* select socket descriptor */ - switch (select (sock + 1, &fds, NULL, NULL, &timeout)) + switch (select (sock + 1, &fds, NULL, NULL, &timeout)) { case 0: /* descriptor is not ready */ @@ -344,31 +364,40 @@ hsocket_close (hsocket_t sock) _linger.l_linger = 30000; setsockopt(sock, SOL_SOCKET, SO_LINGER, (const char*)&_linger, sizeof(struct linger)); */ - + #ifdef WIN32 - /*shutdown(sock,SD_RECEIVE);*/ - + /* shutdown(sock,SD_RECEIVE); */ + + + shutdown (sock.sock, SD_SEND); + while (recv (sock.sock, junk, sizeof (junk), 0) > 0) + { + }; + closesocket (sock.sock); - shutdown(sock.sock, SD_SEND); - while (recv(sock.sock, junk, sizeof(junk), 0)>0) { }; - closesocket(sock.sock); - #else /* XXX m. campbell - It seems like the while loop here needs this */ - fcntl( sock.sock, F_SETFL, O_NONBLOCK); + fcntl (sock.sock, F_SETFL, O_NONBLOCK); #ifdef HAVE_SSL - if( sock.ssl ){ - log_verbose1("Closing SSL"); - ssl_cleanup(sock.ssl); - shutdown(sock.sock, SHUT_RDWR); - while (recv(sock.sock, junk, sizeof(junk), 0)>0) { }; + if (sock.ssl) + { + log_verbose1 ("Closing SSL"); + ssl_cleanup (sock.ssl); + shutdown (sock.sock, SHUT_RDWR); + while (recv (sock.sock, junk, sizeof (junk), 0) > 0) + { + }; close (sock.sock); - }else { + } + else + { #endif - shutdown(sock.sock, SHUT_RDWR); - while (recv(sock.sock, junk, sizeof(junk), 0)>0) { }; - close (sock.sock); + shutdown (sock.sock, SHUT_RDWR); + while (recv (sock.sock, junk, sizeof (junk), 0) > 0) + { + }; + close (sock.sock); #ifdef HAVE_SSL } #endif @@ -377,70 +406,80 @@ hsocket_close (hsocket_t sock) } #if 0 -static int _test_send_to_file(const char* filename, const byte_t* bytes,int n) +static int +_test_send_to_file (const char *filename, const byte_t * bytes, int n) { - int size; - FILE *f = fopen(filename, "ab"); - if (!f) f = fopen(filename, "wb"); - size= fwrite(bytes, 1, n, f); - fclose(f); - return size; + int size; + FILE *f = fopen (filename, "ab"); + if (!f) + f = fopen (filename, "wb"); + size = fwrite (bytes, 1, n, f); + fclose (f); + return size; } #endif /*-------------------------------------------------- FUNCTION: hsocket_send ----------------------------------------------------*/ herror_t -hsocket_nsend (hsocket_t sock, const byte_t *bytes, int n) +hsocket_nsend (hsocket_t sock, const byte_t * bytes, int n) { int size; - int total=0; + int total = 0; - log_verbose1( "Starting to send" ); + log_verbose1 ("Starting to send"); #ifdef HAVE_SSL if (sock.sock <= 0 && !sock.ssl) #else if (sock.sock <= 0) #endif - return herror_new("hsocket_nsend", HSOCKET_ERROR_NOT_INITIALIZED, - "Called hsocket_listen() before initializing!"); + return herror_new ("hsocket_nsend", HSOCKET_ERROR_NOT_INITIALIZED, + "Called hsocket_listen() before initializing!"); - //log_verbose2( "SENDING %s", bytes ); + // log_verbose2( "SENDING %s", bytes ); /* TODO (#1#): check return value and send again until n bytes sent */ while (1) { #ifdef HAVE_SSL - if(sock.ssl){ - size = SSL_write(sock.ssl, bytes + total, n); - } else { + if (sock.ssl) + { + size = SSL_write (sock.ssl, bytes + total, n); + } + else + { #endif - size = send((int) sock.sock, bytes + total, n, 0); + size = send ((int) sock.sock, bytes + total, n, 0); #ifdef HAVE_SSL } #endif - log_verbose2("Sent %d", size ); - /* size = _test_send_to_file(filename, bytes, n);*/ + log_verbose2 ("Sent %d", size); + /* size = _test_send_to_file(filename, bytes, n); */ #ifdef WIN32 if (size == INVALID_SOCKET) - if (WSAGetLastError () == WSAEWOULDBLOCK) - continue; - else - return herror_new("hsocket_nsend", HSOCKET_ERROR_SEND, "Socket error: %d", errno); + if (WSAGetLastError () == WSAEWOULDBLOCK) + continue; + else + return herror_new ("hsocket_nsend", HSOCKET_ERROR_SEND, + "Socket error: %d", errno); #else - if (size == -1){ + if (size == -1) + { #ifdef HAVE_SSL - if(sock.ssl){ - log_error1("Send error"); - log_ssl_error(sock.ssl, size); - } + if (sock.ssl) + { + log_error1 ("Send error"); + log_ssl_error (sock.ssl, size); + } #endif - return herror_new("hsocket_nsend", HSOCKET_ERROR_SEND, "Socket error: %d", errno); + return herror_new ("hsocket_nsend", HSOCKET_ERROR_SEND, + "Socket error: %d", errno); } #endif n -= size; - total += size; - if (n<=0) break; + total += size; + if (n <= 0) + break; } return H_OK; } @@ -458,7 +497,8 @@ hsocket_send (hsocket_t sock, const char *str) return: -1 is error. read bytes otherwise */ herror_t -hsocket_read (hsocket_t sock, byte_t *buffer, int total, int force, int *received) +hsocket_read (hsocket_t sock, byte_t * buffer, int total, int force, + int *received) { int status; int totalRead; @@ -469,72 +509,81 @@ hsocket_read (hsocket_t sock, byte_t *buffer, int total, int force, int *receive /* log_verbose3("Entering hsocket_read(total=%d,force=%d)", total, force); */ - do { + do + { #ifdef HAVE_SSL - if(sock.ssl){ - struct timeval timeout; - int i=0; - fd_set fds; - FD_ZERO (&fds); - FD_SET (sock.sock, &fds); - timeout.tv_sec = 10; - timeout.tv_usec = 0; + if (sock.ssl) + { + struct timeval timeout; + int i = 0; + fd_set fds; + FD_ZERO (&fds); + FD_SET (sock.sock, &fds); + timeout.tv_sec = 10; + timeout.tv_usec = 0; #ifdef WIN32 #else - fcntl( sock.sock, F_SETFL, O_NONBLOCK); -#endif - // log_verbose1("START READ LOOP"); - //do{ - //log_verbose2("DEBUG A %d",i); - status = SSL_read(sock.ssl, &buffer[totalRead], total - totalRead); - if(status < 1){ - int ret = select (sock.sock + 1, &fds, NULL, NULL, &timeout); - //log_verbose2("DEBUG %d",ret); + fcntl (sock.sock, F_SETFL, O_NONBLOCK); +#endif + // log_verbose1("START READ LOOP"); + // do{ + // log_verbose2("DEBUG A %d",i); + status = SSL_read (sock.ssl, &buffer[totalRead], total - totalRead); + if (status < 1) + { + int ret = select (sock.sock + 1, &fds, NULL, NULL, &timeout); + // log_verbose2("DEBUG %d",ret); #ifdef WIN32 - if (ret == SOCKET_ERROR) - { - wsa_error = WSAGetLastError(); - log_error2("WSAGetLastError()=%d", wsa_error); - return herror_new("hsocket_read", HSOCKET_ERROR_RECEIVE, "Socket error: %d", errno); - - } -#endif - if(ret==0){ - log_verbose1("Socket timeout"); - return herror_new("hsocket_read", HSOCKET_SSL_CLOSE, "Timeout"); - } else { - //log_verbose1("DEBUG C"); - status = SSL_read(sock.ssl, &buffer[totalRead], total - totalRead); - } - //log_verbose3("DEBUG D char: %d status: %d", - // buffer[totalRead], SSL_get_error(sock.ssl, status)); + if (ret == SOCKET_ERROR) + { + wsa_error = WSAGetLastError (); + log_error2 ("WSAGetLastError()=%d", wsa_error); + return herror_new ("hsocket_read", HSOCKET_ERROR_RECEIVE, + "Socket error: %d", errno); + + } +#endif + if (ret == 0) + { + log_verbose1 ("Socket timeout"); + return herror_new ("hsocket_read", HSOCKET_SSL_CLOSE, "Timeout"); + } + else + { + // log_verbose1("DEBUG C"); + status = SSL_read (sock.ssl, &buffer[totalRead], total - totalRead); } - //} while( SSL_get_error(sock.ssl, status) == SSL_ERROR_WANT_READ); + // log_verbose3("DEBUG D char: %d status: %d", + // buffer[totalRead], SSL_get_error(sock.ssl, status)); + } + // } while( SSL_get_error(sock.ssl, status) == SSL_ERROR_WANT_READ); #ifdef WIN32 #else - fcntl( sock.sock, F_SETFL, 0); + fcntl (sock.sock, F_SETFL, 0); #endif - } else { + } + else + { #else // HAVE_SSL { -#endif //HAVE_SSL - status = recv(sock.sock, &buffer[totalRead], total - totalRead, 0); +#endif // HAVE_SSL + status = recv (sock.sock, &buffer[totalRead], total - totalRead, 0); #ifdef WIN32 - if (status == INVALID_SOCKET) + if (status == INVALID_SOCKET) + { + wsa_error = WSAGetLastError (); + switch (wsa_error) { - wsa_error = WSAGetLastError(); - switch (wsa_error) - { - case WSAEWOULDBLOCK: - /* case WSAEALREADY: - case WSAEINPROGRESS: */ - continue; - default: - log_error2("WSAGetLastError()=%d", wsa_error); - return herror_new("hsocket_read", HSOCKET_ERROR_RECEIVE, "Socket error: %d", errno); - } + case WSAEWOULDBLOCK: + /* case WSAEALREADY: case WSAEINPROGRESS: */ + continue; + default: + log_error2 ("WSAGetLastError()=%d", wsa_error); + return herror_new ("hsocket_read", HSOCKET_ERROR_RECEIVE, + "Socket error: %d", errno); } + } } #else } @@ -548,66 +597,75 @@ hsocket_read (hsocket_t sock, byte_t *buffer, int total, int force, int *receive */ #ifdef HAVE_SSL - if( sock.ssl && status < 1 ){ + if (sock.ssl && status < 1) + { - // XXX I'm not sure this err_syscall is right here... - if( SSL_get_shutdown( sock.ssl ) == SSL_RECEIVED_SHUTDOWN || - SSL_get_error(sock.ssl, status) == SSL_ERROR_SYSCALL){ - *received = NULL;; - return herror_new("hsocket_read", HSOCKET_SSL_CLOSE, "SSL Closed"); - } - log_error2("Read error (%d)", status); - log_ssl_error( sock.ssl, status ); - return herror_new("hsocket_read", HSOCKET_ERROR_RECEIVE, "SSL Error"); + // XXX I'm not sure this err_syscall is right here... + if (SSL_get_shutdown (sock.ssl) == SSL_RECEIVED_SHUTDOWN || + SSL_get_error (sock.ssl, status) == SSL_ERROR_SYSCALL) + { + *received = NULL;; + return herror_new ("hsocket_read", HSOCKET_SSL_CLOSE, "SSL Closed"); + } + log_error2 ("Read error (%d)", status); + log_ssl_error (sock.ssl, status); + return herror_new ("hsocket_read", HSOCKET_ERROR_RECEIVE, "SSL Error"); } #endif if (status == -1) - return herror_new("hsocket_read", HSOCKET_ERROR_RECEIVE, "Socket error: %d", errno); -#endif - - if (!force) { - *received = status; - /* - log_verbose3("Leaving !force (received=%d)(status=%d)", *received, status); - */ - return H_OK; + return herror_new ("hsocket_read", HSOCKET_ERROR_RECEIVE, + "Socket error: %d", errno); +#endif + + if (!force) + { + *received = status; + /* + log_verbose3("Leaving !force (received=%d)(status=%d)", *received, + status); */ + return H_OK; } - + totalRead += status; - if (totalRead == total) { - *received = totalRead; - /* - log_verbose4("Leaving totalRead == total (received=%d)(status=%d)(totalRead=%d)", *received, status, totalRead); - */ - return H_OK; + if (totalRead == total) + { + *received = totalRead; + /* + log_verbose4("Leaving totalRead == total + (received=%d)(status=%d)(totalRead=%d)", *received, status, + totalRead); */ + return H_OK; } - } while (1); + } + while (1); } herror_t -hsocket_block(hsocket_t sock, int block) +hsocket_block (hsocket_t sock, int block) { #ifdef WIN32 unsigned long iMode; #endif if (sock.sock <= 0) - return herror_new("hsocket_block", HSOCKET_ERROR_NOT_INITIALIZED, - "Called hsocket_listen() before initializing!"); + return herror_new ("hsocket_block", HSOCKET_ERROR_NOT_INITIALIZED, + "Called hsocket_listen() before initializing!"); #ifdef WIN32 /*#define HSOCKET_BLOCKMODE 0 #define HSOCKET_NONBLOCKMODE 1 */ - iMode = (block==0)?1:0; /* Non block mode */ - if (ioctlsocket (sock.sock, FIONBIO, (u_long FAR *) & iMode) == INVALID_SOCKET) + iMode = (block == 0) ? 1 : 0; /* Non block mode */ + if (ioctlsocket (sock.sock, FIONBIO, (u_long FAR *) & iMode) == + INVALID_SOCKET) { - int err = WSAGetLastError(); + int err = WSAGetLastError (); log_error2 ("ioctlsocket error %d", err); - return herror_new("hsocket_block", HSOCKET_ERROR_IOCTL, "Socket error: %d", err); + return herror_new ("hsocket_block", HSOCKET_ERROR_IOCTL, + "Socket error: %d", err); } #else /* fcntl(sock, F_SETFL, O_NONBLOCK); */ /* TODO (#1#): check for *nix the non blocking sockets */ @@ -615,5 +673,3 @@ hsocket_block(hsocket_t sock, int block) #endif return H_OK; } - - diff --git a/nanohttp/nanohttp-socket.h b/nanohttp/nanohttp-socket.h index 7410690..68f6c5b 100644 --- a/nanohttp/nanohttp-socket.h +++ b/nanohttp/nanohttp-socket.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-socket.h,v 1.18 2006/01/06 15:16:03 mrcsys Exp $ + * $Id: nanohttp-socket.h,v 1.19 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -21,8 +21,8 @@ * * Email: ayaz@jprogrammer.net ******************************************************************/ -#ifndef NANO_HTTP_SOCKET_H -#define NANO_HTTP_SOCKET_H +#ifndef NANO_HTTP_SOCKET_H +#define NANO_HTTP_SOCKET_H #include <nanohttp/nanohttp-common.h> @@ -33,25 +33,27 @@ #endif #ifdef WIN32 - #include <winsock2.h> +#include <winsock2.h> #endif #ifdef WIN32 - typedef struct hsocket_t { -#ifdef HAVE_SSL - SSL *ssl; +typedef struct hsocket_t +{ +#ifdef HAVE_SSL + SSL *ssl; #endif - SOCKET sock; - } hsocket_t; - typedef int socklen_t; + SOCKET sock; +} hsocket_t; +typedef int socklen_t; #else - typedef struct hsocket_t { +typedef struct hsocket_t +{ #ifdef HAVE_SSL - SSL *ssl; + SSL *ssl; #endif - int sock; - } hsocket_t; + int sock; +} hsocket_t; #endif @@ -62,14 +64,14 @@ @returns This function should always return H_OK. */ -herror_t hsocket_module_init(); +herror_t hsocket_module_init (); /** Destroys the socket modul. This should be called after finishing an application. */ -void hsocket_module_destroy(); +void hsocket_module_destroy (); /** @@ -80,7 +82,7 @@ void hsocket_module_destroy(); @returns This function should always return H_OK. */ -herror_t hsocket_init(hsocket_t *sock ); +herror_t hsocket_init (hsocket_t * sock); /** @@ -88,7 +90,7 @@ herror_t hsocket_init(hsocket_t *sock ); @param sock the socket to destroy */ -void hsocket_free(hsocket_t sock); +void hsocket_free (hsocket_t sock); /** @@ -104,7 +106,7 @@ void hsocket_free(hsocket_t sock); <BR>HSOCKET_ERROR_GET_HOSTNAME <BR>HSOCKET_ERROR_CONNECT */ -herror_t hsocket_open(hsocket_t *sock, const char* host, int port); +herror_t hsocket_open (hsocket_t * sock, const char *host, int port); /** @@ -112,7 +114,7 @@ herror_t hsocket_open(hsocket_t *sock, const char* host, int port); @param sock the socket to close */ -void hsocket_close(hsocket_t sock); +void hsocket_close (hsocket_t sock); /** @@ -128,7 +130,7 @@ void hsocket_close(hsocket_t sock); @see hsocket_listen */ -herror_t hsocket_bind(hsocket_t *sock, int port); +herror_t hsocket_bind (hsocket_t * sock, int port); /** @@ -142,7 +144,7 @@ herror_t hsocket_bind(hsocket_t *sock, int port); <BR>HSOCKET_ERROR_NOT_INITIALIZED <BR>HSOCKET_ERROR_LISTEN */ -herror_t hsocket_listen(hsocket_t sock); +herror_t hsocket_listen (hsocket_t sock); /** @@ -156,7 +158,7 @@ herror_t hsocket_listen(hsocket_t sock); <BR>HSOCKET_ERROR_NOT_INITIALIZED <BR>HSOCKET_ERROR_ACCEPT */ -herror_t hsocket_accept(hsocket_t sock, hsocket_t *dest); +herror_t hsocket_accept (hsocket_t sock, hsocket_t * dest); /** @@ -170,7 +172,7 @@ herror_t hsocket_accept(hsocket_t sock, hsocket_t *dest); <BR>HSOCKET_ERROR_NOT_INITIALIZED <BR>HSOCKET_ERROR_SEND */ -herror_t hsocket_nsend(hsocket_t sock, const byte_t* bytes, int size); +herror_t hsocket_nsend (hsocket_t sock, const byte_t * bytes, int size); /** @@ -183,7 +185,7 @@ herror_t hsocket_nsend(hsocket_t sock, const byte_t* bytes, int size); <BR>HSOCKET_ERROR_NOT_INITIALIZED <BR>HSOCKET_ERROR_SEND */ -herror_t hsocket_send(hsocket_t sock, const char* str); +herror_t hsocket_send (hsocket_t sock, const char *str); /** @@ -202,7 +204,8 @@ herror_t hsocket_send(hsocket_t sock, const char* str); the socket. */ -herror_t hsocket_read(hsocket_t sock, byte_t* buffer, int size, int force, int *readed); +herror_t hsocket_read (hsocket_t sock, byte_t * buffer, int size, int force, + int *readed); /** Sets the goven socket to non-blocking socket mode. @@ -213,18 +216,7 @@ herror_t hsocket_read(hsocket_t sock, byte_t* buffer, int size, int force, int * <BR>HSOCKET_ERROR_NOT_INITIALIZED <BR>HSOCKET_ERROR_IOCTL */ -herror_t hsocket_block(hsocket_t sock, int block); +herror_t hsocket_block (hsocket_t sock, int block); #endif - - - - - - - - - - - diff --git a/nanohttp/nanohttp-ssl.c b/nanohttp/nanohttp-ssl.c index 766ac98..38b3595 100644 --- a/nanohttp/nanohttp-ssl.c +++ b/nanohttp/nanohttp-ssl.c @@ -22,7 +22,7 @@ */ /* Enter only if --with-ssl was specified to the configure script */ -#ifdef HAVE_SSL +#ifdef HAVE_SSL #include <sys/types.h> #ifndef WIN32 @@ -60,7 +60,7 @@ typedef unsigned int uint32_t; #define MAXCHUNK 1024 #define HEADER_LEN 5 -char HEADER[HEADER_LEN] = {186, 84, 202, 86, 224}; +char HEADER[HEADER_LEN] = { 186, 84, 202, 86, 224 }; static char *pass; /* @@ -69,303 +69,342 @@ static char *pass; * the SSL random number generator */ -void superseed() +void +superseed () { - int buf[256], i; + int buf[256], i; - srand(time(NULL)); + srand (time (NULL)); - for(i = 0; i < 256; i++) { - buf[i] = rand(); - } RAND_seed( (unsigned char*)buf, sizeof(buf)); + for (i = 0; i < 256; i++) + { + buf[i] = rand (); + } + RAND_seed ((unsigned char *) buf, sizeof (buf)); } -static int pw_cb(char* buf, int num, int rwflag, void *userdata) +static int +pw_cb (char *buf, int num, int rwflag, void *userdata) { - if( num < (int)strlen(pass) + 1) - return(0); + if (num < (int) strlen (pass) + 1) + return (0); - strcpy(buf, pass); - return strlen(pass); + strcpy (buf, pass); + return strlen (pass); } -int verify_sn(X509* cert, int who, int nid, char* str) +int +verify_sn (X509 * cert, int who, int nid, char *str) { - char name[256]; - char buf[256]; - - memset(name, '\0', 256); - memset(buf, '\0', 256); - - if(who == CERT_SUBJECT) { - X509_NAME_oneline(X509_get_subject_name(cert), name, 256); - } else { - X509_NAME_oneline(X509_get_issuer_name(cert), name, 256); - } - - buf[0] = '/'; - strcat(buf, OBJ_nid2sn(nid)); - strcat(buf, "="); - strcat(buf, str); - - if(strstr(name, buf)) { - return 1; - } else { - return 0; - } + char name[256]; + char buf[256]; + + memset (name, '\0', 256); + memset (buf, '\0', 256); + + if (who == CERT_SUBJECT) + { + X509_NAME_oneline (X509_get_subject_name (cert), name, 256); + } + else + { + X509_NAME_oneline (X509_get_issuer_name (cert), name, 256); + } + + buf[0] = '/'; + strcat (buf, OBJ_nid2sn (nid)); + strcat (buf, "="); + strcat (buf, str); + + if (strstr (name, buf)) + { + return 1; + } + else + { + return 0; + } } -static int verify_cb(int prev_ok, X509_STORE_CTX* ctx) +static int +verify_cb (int prev_ok, X509_STORE_CTX * ctx) { - X509* cert = X509_STORE_CTX_get_current_cert(ctx); - int depth = X509_STORE_CTX_get_error_depth(ctx); - int err = X509_STORE_CTX_get_error(ctx); + X509 *cert = X509_STORE_CTX_get_current_cert (ctx); + int depth = X509_STORE_CTX_get_error_depth (ctx); + int err = X509_STORE_CTX_get_error (ctx); /* if( err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ){ log_verbose1("Self signed cert in chain"); return 1; } */ -#ifdef NOUSER_VERIFY /* ifdef's added by Ferhat. because of unresolved reference while compiling */ - if(depth == 0) { - return user_verify(cert); - } else { +#ifdef NOUSER_VERIFY /* ifdef's added by Ferhat. because of + unresolved reference while compiling */ + if (depth == 0) + { + return user_verify (cert); + } + else + { #endif - log_verbose1( "Cert ok (prev)" ); - return prev_ok; + log_verbose1 ("Cert ok (prev)"); + return prev_ok; #ifdef NOUSER_VERIFY - } + } #endif } #ifdef NOUSER_VERIFY -int user_verify(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 +user_verify (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; +} #endif -SSL_CTX *initialize_ctx(char* keyfile, char* password, char* calist) +SSL_CTX * +initialize_ctx (char *keyfile, char *password, char *calist) { - SSL_CTX* ctx = NULL; - - if(password == NULL) password = ""; - - - /* Global system initialization */ - log_verbose1( "Initializing library"); - SSL_library_init(); - SSL_load_error_strings(); - ERR_load_crypto_strings(); - OpenSSL_add_ssl_algorithms(); - - /* Create our context*/ - ctx = SSL_CTX_new( SSLv23_method() ); - - if(ctx == NULL) { - log_error1( "Cannot create SSL context"); - return NULL; - } - log_verbose1( "SSL context created ok"); - - /* Load our keys and certificates*/ - if(keyfile != NULL && password != NULL ){ - - if(!( SSL_CTX_use_certificate_file(ctx, keyfile, SSL_FILETYPE_PEM))){ - log_error2( "Couldn't read certificate file: %s", keyfile); - SSL_CTX_free(ctx); - return ctx = NULL; - } - - log_verbose1("Certificate file read ok"); + SSL_CTX *ctx = NULL; + + if (password == NULL) + password = ""; + + + /* Global system initialization */ + log_verbose1 ("Initializing library"); + SSL_library_init (); + SSL_load_error_strings (); + ERR_load_crypto_strings (); + OpenSSL_add_ssl_algorithms (); + + /* Create our context */ + ctx = SSL_CTX_new (SSLv23_method ()); + + if (ctx == NULL) + { + log_error1 ("Cannot create SSL context"); + return NULL; + } + log_verbose1 ("SSL context created ok"); + + /* Load our keys and certificates */ + if (keyfile != NULL && password != NULL) + { + + if (!(SSL_CTX_use_certificate_file (ctx, keyfile, SSL_FILETYPE_PEM))) + { + log_error2 ("Couldn't read certificate file: %s", keyfile); + SSL_CTX_free (ctx); + return ctx = NULL; + } - pass = password; - SSL_CTX_set_default_passwd_cb(ctx, pw_cb); + log_verbose1 ("Certificate file read ok"); - if( !( SSL_CTX_use_PrivateKey_file(ctx, keyfile, SSL_FILETYPE_PEM) ) ) { - log_error2( "Couldn't read key file: %s", keyfile); - SSL_CTX_free(ctx); - return ctx = NULL; - } + pass = password; + SSL_CTX_set_default_passwd_cb (ctx, pw_cb); - log_verbose1("Keyfile read ok"); + if (!(SSL_CTX_use_PrivateKey_file (ctx, keyfile, SSL_FILETYPE_PEM))) + { + log_error2 ("Couldn't read key file: %s", keyfile); + SSL_CTX_free (ctx); + return ctx = NULL; } - if( calist != NULL) { - /* Load the CAs we trust */ - if( !( SSL_CTX_load_verify_locations(ctx, calist, NULL) ) ) { - log_error2("Couldn't read CA list: %s", calist); - SSL_CTX_free(ctx); - return ctx = NULL; - } + log_verbose1 ("Keyfile read ok"); + } + if (calist != NULL) + { + + /* Load the CAs we trust */ + if (!(SSL_CTX_load_verify_locations (ctx, calist, NULL))) + { + log_error2 ("Couldn't read CA list: %s", calist); + SSL_CTX_free (ctx); + return ctx = NULL; + } - SSL_CTX_set_client_CA_list( ctx, SSL_load_client_CA_file(calist) ); - log_verbose1("Certificate Authority contacted"); + SSL_CTX_set_client_CA_list (ctx, SSL_load_client_CA_file (calist)); + log_verbose1 ("Certificate Authority contacted"); - } - SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, - verify_cb); - log_verbose1("Verify callback registered"); + } + SSL_CTX_set_verify (ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, + verify_cb); + log_verbose1 ("Verify callback registered"); - SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF ); + SSL_CTX_set_session_cache_mode (ctx, SSL_SESS_CACHE_OFF); - /* Load randomness */ - superseed(); + /* Load randomness */ + superseed (); - return ctx; + return ctx; } -void log_ssl_error(SSL* ssl, int ret) +void +log_ssl_error (SSL * ssl, int ret) { - int errqueue; - char errorbuf[256] = "Error: "; - - if(ret == 0) { - log_error1("SSL handshake was not successful, contolled shutdown"); - } else if(ret == -1) { - log_error1("SSL handshake was not successful, fatal error at protocol"); - } - - errqueue = SSL_get_error(ssl, ret); - - switch(errqueue) { - case SSL_ERROR_NONE: - strcat(errorbuf, "None"); - break; - case SSL_ERROR_ZERO_RETURN: - strcat(errorbuf, "Zero return"); - break; - case SSL_ERROR_WANT_READ: - strcat(errorbuf, "Want read"); - break; - case SSL_ERROR_WANT_WRITE: - strcat(errorbuf, "Want write"); - break; - case SSL_ERROR_WANT_X509_LOOKUP: - strcat(errorbuf, "Want x509 lookup"); - break; - case SSL_ERROR_SYSCALL: - strcat(errorbuf, "Syscall:"); - if(ret == 0) { - strcat(errorbuf, "Protocol violation"); - } else if(ret == -1) { - strcat(errorbuf, "BIO reported an I/O error"); - } else { - strcat(errorbuf, "Unknown syscall error"); - } /* if */ - - break; - case SSL_ERROR_SSL: - strcat(errorbuf, "SSL library"); - while(errqueue=ERR_get_error()){ - log_error2("SSL %s", ERR_error_string(errqueue, NULL)); - } - break; - } /* switch code */ - - log_error1( errorbuf); + int errqueue; + char errorbuf[256] = "Error: "; + + if (ret == 0) + { + log_error1 ("SSL handshake was not successful, contolled shutdown"); + } + else if (ret == -1) + { + log_error1 ("SSL handshake was not successful, fatal error at protocol"); + } + + errqueue = SSL_get_error (ssl, ret); + + switch (errqueue) + { + case SSL_ERROR_NONE: + strcat (errorbuf, "None"); + break; + case SSL_ERROR_ZERO_RETURN: + strcat (errorbuf, "Zero return"); + break; + case SSL_ERROR_WANT_READ: + strcat (errorbuf, "Want read"); + break; + case SSL_ERROR_WANT_WRITE: + strcat (errorbuf, "Want write"); + break; + case SSL_ERROR_WANT_X509_LOOKUP: + strcat (errorbuf, "Want x509 lookup"); + break; + case SSL_ERROR_SYSCALL: + strcat (errorbuf, "Syscall:"); + if (ret == 0) + { + strcat (errorbuf, "Protocol violation"); + } + else if (ret == -1) + { + strcat (errorbuf, "BIO reported an I/O error"); + } + else + { + strcat (errorbuf, "Unknown syscall error"); + } /* if */ + + break; + case SSL_ERROR_SSL: + strcat (errorbuf, "SSL library"); + while (errqueue = ERR_get_error ()) + { + log_error2 ("SSL %s", ERR_error_string (errqueue, NULL)); + } + break; + } /* switch code */ + + log_error1 (errorbuf); } -SSL* init_ssl(SSL_CTX* ctx, int sock, int type) +SSL * +init_ssl (SSL_CTX * ctx, int sock, int type) { - int ret; - int status; - SSL* ssl; + int ret; + int status; + SSL *ssl; #if 0 #ifdef WIN32 - BIO* rbio; - BIO* wbio; + BIO *rbio; + BIO *wbio; #else - BIO* sbio; + BIO *sbio; #endif #endif - log_verbose1("Starting SSL Initialization"); - - ssl = SSL_new(ctx); - - if(ssl == NULL) { - log_error1( "Cannot create new ssl object"); - return NULL; - } + log_verbose1 ("Starting SSL Initialization"); + + ssl = SSL_new (ctx); + + if (ssl == NULL) + { + log_error1 ("Cannot create new ssl object"); + return NULL; + } #if 0 #ifdef WIN32 - log_error1("Setting up BIO with socket"); - rbio = BIO_new_socket(sock, BIO_NOCLOSE); - if( rbio == NULL ) { - log_error1( "BIO_new_socket failed"); - return NULL; - } - SSL_set_bio(ssl, rbio, rbio); + log_error1 ("Setting up BIO with socket"); + rbio = BIO_new_socket (sock, BIO_NOCLOSE); + if (rbio == NULL) + { + log_error1 ("BIO_new_socket failed"); + return NULL; + } + SSL_set_bio (ssl, rbio, rbio); #else - sbio = BIO_new_socket(sock, BIO_NOCLOSE); - - if( sbio == NULL ) { - log_error1( "BIO_new_socket failed"); - return NULL; - } - SSL_set_bio(ssl, sbio, sbio); + sbio = BIO_new_socket (sock, BIO_NOCLOSE); + + if (sbio == NULL) + { + log_error1 ("BIO_new_socket failed"); + return NULL; + } + SSL_set_bio (ssl, sbio, sbio); #endif #endif - SSL_set_fd(ssl, sock); - - if(type == SSL_SERVER) { - hsocket_t sock_t; - sock_t.sock = sock; - hsocket_block(sock_t, 1); - ret = SSL_accept(ssl); - hsocket_block(sock_t, 0); - if(ret <= 0) { - log_error1( "SSL accept error"); - log_ssl_error(ssl, ret); - SSL_free(ssl); - return ssl = NULL; - } /* if error */ - } else { /* client */ - ret = SSL_connect(ssl); - if(ret <= 0) { - log_error1( "SSL connect error"); - log_ssl_error(ssl, ret); - SSL_free(ssl); - return ssl = NULL; - } /* if error */ - /* SSL_connect should take care of this for us. - if(SSL_get_peer_certificate(ssl) == NULL) { - log_error1( "No certificate provided"); - SSL_free(ssl); - return ssl = NULL; - } - if(SSL_get_verify_result(ssl) != X509_V_OK) { - log_error1( "Certificate did not verify"); - SSL_free(ssl); - return ssl = NULL; - } - */ - } - - log_verbose1("Completed SSL Initialization"); - return ssl; + SSL_set_fd (ssl, sock); + + if (type == SSL_SERVER) + { + hsocket_t sock_t; + sock_t.sock = sock; + hsocket_block (sock_t, 1); + ret = SSL_accept (ssl); + hsocket_block (sock_t, 0); + if (ret <= 0) + { + log_error1 ("SSL accept error"); + log_ssl_error (ssl, ret); + SSL_free (ssl); + return ssl = NULL; + } /* if error */ + } + else + { /* client */ + ret = SSL_connect (ssl); + if (ret <= 0) + { + log_error1 ("SSL connect error"); + log_ssl_error (ssl, ret); + SSL_free (ssl); + return ssl = NULL; + } /* if error */ + /* SSL_connect should take care of this for us. + if(SSL_get_peer_certificate(ssl) == NULL) { log_error1( "No + certificate provided"); SSL_free(ssl); return ssl = NULL; } + if(SSL_get_verify_result(ssl) != X509_V_OK) { log_error1( "Certificate + did not verify"); SSL_free(ssl); return ssl = NULL; } */ + } + + log_verbose1 ("Completed SSL Initialization"); + return ssl; } -void ssl_cleanup(SSL* ssl) +void +ssl_cleanup (SSL * ssl) { - /* does nothing to context */ + /* does nothing to context */ - if(ssl != NULL) { + if (ssl != NULL) + { - SSL_shutdown(ssl); + SSL_shutdown (ssl); // SSL_clear(ssl); - SSL_free(ssl); - ssl = NULL; - } + SSL_free (ssl); + ssl = NULL; + } } #endif /* end of ifdef HAVE_SSL */ - diff --git a/nanohttp/nanohttp-ssl.h b/nanohttp/nanohttp-ssl.h index c783812..449c78c 100644 --- a/nanohttp/nanohttp-ssl.h +++ b/nanohttp/nanohttp-ssl.h @@ -23,7 +23,7 @@ */ /* Do enter only if --with-ssl was specified by the configure script */ -#ifdef HAVE_SSL +#ifdef HAVE_SSL #ifdef TRU64 #include <arpa/inet.h> @@ -47,9 +47,10 @@ typedef unsigned int uint32_t; #define CERT_SUBJECT 0 #define CERT_ISSUER 1 -typedef struct Con { - SSL* ssl; - int sock; +typedef struct Con +{ + SSL *ssl; + int sock; } Con; /* @@ -62,7 +63,7 @@ typedef struct Con { * Initialize the context */ -SSL_CTX *initialize_ctx(char *keyfile, char *password, char* calist); +SSL_CTX *initialize_ctx (char *keyfile, char *password, char *calist); /* * Quick function for verifying a portion of the cert @@ -70,7 +71,7 @@ SSL_CTX *initialize_ctx(char *keyfile, char *password, char* calist); * returns non-zero if everything went ok */ -int verify_sn(X509* cert, int who, int nid, char* str); +int verify_sn (X509 * cert, int who, int nid, char *str); /* * Called by framework for verify @@ -82,22 +83,21 @@ int verify_sn(X509* cert, int who, int nid, char* str); * This function MUST be implemented by user client/server code somewhere */ -int user_verify(X509* cert); +int user_verify (X509 * cert); /* * Create the ssl socket and return it * pass in the context and an open socket */ -SSL* init_ssl(SSL_CTX* ctx, int sock, int type); +SSL *init_ssl (SSL_CTX * ctx, int sock, int type); /* * Close the ssl connection (socket is still left open) */ -void ssl_cleanup(); +void ssl_cleanup (); #endif #endif /* HAVE_SSL */ - diff --git a/nanohttp/nanohttp-stream.c b/nanohttp/nanohttp-stream.c index 1440e63..7700867 100755 --- a/nanohttp/nanohttp-stream.c +++ b/nanohttp/nanohttp-stream.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-stream.c,v 1.7 2005/05/27 19:28:16 snowdrop Exp $ +* $Id: nanohttp-stream.c,v 1.8 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -29,7 +29,8 @@ #ifdef MEM_DEBUG #include <utils/alloc.h> #endif -void _log_str(char *fn, char *str, int size) +void +_log_str (char *fn, char *str, int size) { /* FILE *f = fopen(fn, "ab"); if (!f) f=fopen(fn,"wb"); @@ -47,24 +48,23 @@ HTTP INPUT STREAM ------------------------------------------------------------------- */ -static -int _http_stream_is_content_length(hpair_t *header) +static int +_http_stream_is_content_length (hpair_t * header) { - return - hpairnode_get_ignore_case(header, HEADER_CONTENT_LENGTH) != NULL; + return hpairnode_get_ignore_case (header, HEADER_CONTENT_LENGTH) != NULL; } -static -int _http_stream_is_chunked(hpair_t *header) +static int +_http_stream_is_chunked (hpair_t * header) { char *chunked; - chunked = hpairnode_get_ignore_case(header, HEADER_TRANSFER_ENCODING); + chunked = hpairnode_get_ignore_case (header, HEADER_TRANSFER_ENCODING); if (chunked != NULL) { - if (!strcmp(chunked, TRANSFER_ENCODING_CHUNKED)) - { - return 1; - } + if (!strcmp (chunked, TRANSFER_ENCODING_CHUNKED)) + { + return 1; + } } return 0; @@ -73,35 +73,35 @@ int _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) +http_input_stream_t * +http_input_stream_new (hsocket_t sock, hpair_t * header) { http_input_stream_t *result; - char *content_length; + char *content_length; /* Paranoya check */ - /*if (header == NULL) - return NULL; - */ + /* if (header == NULL) return NULL; */ /* Create object */ - result = (http_input_stream_t*)malloc(sizeof(http_input_stream_t)); + result = (http_input_stream_t *) malloc (sizeof (http_input_stream_t)); result->sock = sock; result->err = H_OK; /* Find connection type */ - hpairnode_dump_deep(header); + hpairnode_dump_deep (header); /* Check if Content-type */ - if (_http_stream_is_content_length(header)) + if (_http_stream_is_content_length (header)) { - log_verbose1("Stream transfer with 'Content-length'"); - content_length = hpairnode_get_ignore_case(header, HEADER_CONTENT_LENGTH); - result->content_length = atoi(content_length); + log_verbose1 ("Stream transfer with 'Content-length'"); + content_length = + hpairnode_get_ignore_case (header, HEADER_CONTENT_LENGTH); + result->content_length = atoi (content_length); result->received = 0; - result->type = HTTP_TRANSFER_CONTENT_LENGTH; + result->type = HTTP_TRANSFER_CONTENT_LENGTH; } /* Check if Chunked */ - else if (_http_stream_is_chunked(header)) + else if (_http_stream_is_chunked (header)) { - log_verbose1("Stream transfer with 'chunked'"); + log_verbose1 ("Stream transfer with 'chunked'"); result->type = HTTP_TRANSFER_CHUNKED; result->chunk_size = -1; result->received = -1; @@ -109,7 +109,7 @@ http_input_stream_t *http_input_stream_new(hsocket_t sock, hpair_t *header) /* Assume connection close */ else { - log_verbose1("Stream transfer with 'Connection: close'"); + log_verbose1 ("Stream transfer with 'Connection: close'"); result->type = HTTP_TRANSFER_CONNECTION_CLOSE; result->connection_closed = 0; result->received = 0; @@ -122,70 +122,69 @@ http_input_stream_t *http_input_stream_new(hsocket_t sock, hpair_t *header) This function was added for MIME messages and for debugging. */ -http_input_stream_t *http_input_stream_new_from_file(const char* filename) +http_input_stream_t * +http_input_stream_new_from_file (const char *filename) { http_input_stream_t *result; - FILE *fd = fopen(filename, "rb"); + FILE *fd = fopen (filename, "rb"); - if (fd == NULL) + if (fd == NULL) return NULL; /* Create object */ - result = (http_input_stream_t*)malloc(sizeof(http_input_stream_t)); + result = (http_input_stream_t *) malloc (sizeof (http_input_stream_t)); result->type = HTTP_TRANSFER_FILE; result->fd = fd; result->deleteOnExit = 0; - strcpy(result->filename, filename); + strcpy (result->filename, filename); return result; } /** Free input stream */ -void http_input_stream_free(http_input_stream_t *stream) +void +http_input_stream_free (http_input_stream_t * stream) { - if (stream->type == HTTP_TRANSFER_FILE && stream->fd) { - fclose(stream->fd); - if (stream->deleteOnExit) - log_info2("Removing '%s'", stream->filename); - /*remove(stream->filename);*/ - } - - free(stream); + if (stream->type == HTTP_TRANSFER_FILE && stream->fd) + { + fclose (stream->fd); + if (stream->deleteOnExit) + log_info2 ("Removing '%s'", stream->filename); + /* remove(stream->filename); */ + } + + free (stream); } -static -int _http_input_stream_is_content_length_ready( - http_input_stream_t *stream) +static int +_http_input_stream_is_content_length_ready (http_input_stream_t * stream) { return (stream->content_length > stream->received); } -static -int _http_input_stream_is_chunked_ready( - http_input_stream_t *stream) +static int +_http_input_stream_is_chunked_ready (http_input_stream_t * stream) { return stream->chunk_size != 0; } -static -int _http_input_stream_is_connection_closed_ready( - http_input_stream_t *stream) +static int +_http_input_stream_is_connection_closed_ready (http_input_stream_t * stream) { - return !stream->connection_closed; + return !stream->connection_closed; } -static -int _http_input_stream_is_file_ready( - http_input_stream_t *stream) +static int +_http_input_stream_is_file_ready (http_input_stream_t * stream) { - return !feof(stream->fd); + return !feof (stream->fd); } -static -int _http_input_stream_content_length_read( - http_input_stream_t *stream, byte_t *dest, int size) +static int +_http_input_stream_content_length_read (http_input_stream_t * stream, + byte_t * dest, int size) { herror_t status; int read; @@ -195,8 +194,9 @@ int _http_input_stream_content_length_read( size = stream->content_length - stream->received; /* read from socket */ - status = hsocket_read(stream->sock, dest, size, 1, &read); - if (status != H_OK) { + status = hsocket_read (stream->sock, dest, size, 1, &read); + if (status != H_OK) + { stream->err = status; return -1; } @@ -205,106 +205,112 @@ int _http_input_stream_content_length_read( return read; } -static -int _http_input_stream_chunked_read_chunk_size( - http_input_stream_t *stream) +static int +_http_input_stream_chunked_read_chunk_size (http_input_stream_t * stream) { - char chunk[25]; + char chunk[25]; int status, i = 0; int chunk_size; herror_t err; - + while (1) { - err = hsocket_read(stream->sock, &(chunk[i]), 1, 1, &status); - if (status != 1) { - stream->err = herror_new("_http_input_stream_chunked_read_chunk_size", - GENERAL_INVALID_PARAM, "This should never happens!"); + err = hsocket_read (stream->sock, &(chunk[i]), 1, 1, &status); + if (status != 1) + { + stream->err = herror_new ("_http_input_stream_chunked_read_chunk_size", + GENERAL_INVALID_PARAM, + "This should never happens!"); return -1; - } + } + + if (err != H_OK) + { + log_error4 ("[%d] %s(): %s ", herror_code (err), herror_func (err), + herror_message (err)); - if (err != H_OK) { - log_error4("[%d] %s(): %s ", herror_code(err), herror_func(err), herror_message(err)); - stream->err = err; return -1; } if (chunk[i] == '\r' || chunk[i] == ';') { - chunk[i] = '\0'; + chunk[i] = '\0'; } else if (chunk[i] == '\n') { - chunk[i] = '\0'; /* double check*/ - chunk_size = strtol(chunk, (char **) NULL, 16); /* hex to dec */ - /* - log_verbose3("chunk_size: '%s' as dec: '%d'", chunk, chunk_size); - */ - return chunk_size; + chunk[i] = '\0'; /* double check */ + chunk_size = strtol (chunk, (char **) NULL, 16); /* hex to dec */ + /* + log_verbose3("chunk_size: '%s' as dec: '%d'", chunk, chunk_size); */ + return chunk_size; } - - if (i == 24) { - stream->err = herror_new( - "_http_input_stream_chunked_read_chunk_size", STREAM_ERROR_NO_CHUNK_SIZE, - "reached max line == %d", i); - return -1; + + if (i == 24) + { + stream->err = + herror_new ("_http_input_stream_chunked_read_chunk_size", + STREAM_ERROR_NO_CHUNK_SIZE, "reached max line == %d", i); + return -1; } else - i++; + i++; } /* this should never happens */ - stream->err = herror_new( - "_http_input_stream_chunked_read_chunk_size", STREAM_ERROR_NO_CHUNK_SIZE, - "reached max line == %d", i); - return -1; + stream->err = + herror_new ("_http_input_stream_chunked_read_chunk_size", + STREAM_ERROR_NO_CHUNK_SIZE, "reached max line == %d", i); + return -1; } -static -int _http_input_stream_chunked_read( - http_input_stream_t *stream, byte_t *dest, int size) +static int +_http_input_stream_chunked_read (http_input_stream_t * stream, byte_t * dest, + int size) { int status, counter; - int remain, read=0; + int remain, read = 0; char ch; herror_t err; while (size > 0) { - remain = stream->chunk_size - stream->received ; + remain = stream->chunk_size - stream->received; if (remain == 0 && stream->chunk_size != -1) { - /* This is not the first chunk. - so skip new line until chunk size string */ - counter = 100; /* maximum for stop infinity */ - while (1) + /* This is not the first chunk. so skip new line until chunk size + string */ + counter = 100; /* maximum for stop infinity */ + while (1) + { + err = hsocket_read (stream->sock, &ch, 1, 1, &status); + + if (err != H_OK) { - err = hsocket_read(stream->sock, &ch, 1, 1, &status ); - - if (err != H_OK) { - stream->err = err; - return -1; - } - - if (ch == '\n') - { - break; - } - if (counter-- == 0) { - stream->err = herror_new("_http_input_stream_chunked_read", - STREAM_ERROR_WRONG_CHUNK_SIZE,"Wrong chunk-size"); - return -1; - } + stream->err = err; + return -1; } + + if (ch == '\n') + { + break; + } + if (counter-- == 0) + { + stream->err = herror_new ("_http_input_stream_chunked_read", + STREAM_ERROR_WRONG_CHUNK_SIZE, + "Wrong chunk-size"); + return -1; + } + } } if (remain == 0) { /* receive new chunk size */ - stream->chunk_size = - _http_input_stream_chunked_read_chunk_size(stream); + stream->chunk_size = + _http_input_stream_chunked_read_chunk_size (stream); stream->received = 0; if (stream->chunk_size < 0) @@ -323,27 +329,35 @@ int _http_input_stream_chunked_read( if (remain < size) { /* read from socket */ - err = hsocket_read(stream->sock, &(dest[read]), remain, 1, &status); - if (err != H_OK) { + err = hsocket_read (stream->sock, &(dest[read]), remain, 1, &status); + if (err != H_OK) + { stream->err = err; return -1; } - if (status != remain) { - stream->err = herror_new("_http_input_stream_chunked_read", - GENERAL_INVALID_PARAM, "This should never happens(remain=%d)(status=%d)!", remain, status); - return -1; - } + if (status != remain) + { + stream->err = herror_new ("_http_input_stream_chunked_read", + GENERAL_INVALID_PARAM, + "This should never happens(remain=%d)(status=%d)!", + remain, status); + return -1; + } } else { /* read from socket */ - err = hsocket_read(stream->sock, &(dest[read]), size, 1, &status); - if (status != size) { - stream->err = herror_new("_http_input_stream_chunked_read", - GENERAL_INVALID_PARAM, "This should never happens(size=%d)(status=%d)!", size, status); - return -1; - } - if (err != H_OK) { + err = hsocket_read (stream->sock, &(dest[read]), size, 1, &status); + if (status != size) + { + stream->err = herror_new ("_http_input_stream_chunked_read", + GENERAL_INVALID_PARAM, + "This should never happens(size=%d)(status=%d)!", + size, status); + return -1; + } + if (err != H_OK) + { stream->err = err; return -1; } @@ -358,16 +372,17 @@ int _http_input_stream_chunked_read( } -static -int _http_input_stream_connection_closed_read( - http_input_stream_t *stream, byte_t *dest, int size) +static int +_http_input_stream_connection_closed_read (http_input_stream_t * stream, + byte_t * dest, int size) { int status; herror_t err; /* read from socket */ - err = hsocket_read(stream->sock, dest, size, 0, &status ); - if (err != H_OK) { + err = hsocket_read (stream->sock, dest, size, 0, &status); + if (err != H_OK) + { stream->err = err; return -1; } @@ -376,20 +391,21 @@ int _http_input_stream_connection_closed_read( stream->connection_closed = 1; stream->received += status; - _log_str("stream.in", dest, size); + _log_str ("stream.in", dest, size); return status; } -static -int _http_input_stream_file_read( - http_input_stream_t *stream, byte_t *dest, int size) +static int +_http_input_stream_file_read (http_input_stream_t * stream, byte_t * dest, + int size) { int readed; - - readed = fread(dest, 1, size, stream->fd); - if (readed == -1) { - stream->err = herror_new("_http_input_stream_file_read", - HSOCKET_ERROR_RECEIVE, "fread() returned -1"); + + readed = fread (dest, 1, size, stream->fd); + if (readed == -1) + { + stream->err = herror_new ("_http_input_stream_file_read", + HSOCKET_ERROR_RECEIVE, "fread() returned -1"); return -1; } @@ -399,7 +415,8 @@ int _http_input_stream_file_read( /** Returns the actual status of the stream. */ -int http_input_stream_is_ready(http_input_stream_t *stream) +int +http_input_stream_is_ready (http_input_stream_t * stream) { /* paranoya check */ if (stream == NULL) @@ -410,16 +427,16 @@ int http_input_stream_is_ready(http_input_stream_t *stream) switch (stream->type) { - case HTTP_TRANSFER_CONTENT_LENGTH: - return _http_input_stream_is_content_length_ready(stream); - case HTTP_TRANSFER_CHUNKED: - return _http_input_stream_is_chunked_ready(stream); - case HTTP_TRANSFER_CONNECTION_CLOSE: - return _http_input_stream_is_connection_closed_ready(stream); - case HTTP_TRANSFER_FILE: - return _http_input_stream_is_file_ready(stream); - default: - return 0; + case HTTP_TRANSFER_CONTENT_LENGTH: + return _http_input_stream_is_content_length_ready (stream); + case HTTP_TRANSFER_CHUNKED: + return _http_input_stream_is_chunked_ready (stream); + case HTTP_TRANSFER_CONNECTION_CLOSE: + return _http_input_stream_is_connection_closed_ready (stream); + case HTTP_TRANSFER_FILE: + return _http_input_stream_is_file_ready (stream); + default: + return 0; } } @@ -428,36 +445,38 @@ int http_input_stream_is_ready(http_input_stream_t *stream) Returns the actual read bytes <0 on error */ -int http_input_stream_read(http_input_stream_t *stream, - byte_t *dest, int size) +int +http_input_stream_read (http_input_stream_t * stream, byte_t * dest, int size) { - int readed=0; + int readed = 0; /* paranoya check */ - if (stream == NULL) { + if (stream == NULL) + { return -1; } - + /* reset error flag */ stream->err = H_OK; switch (stream->type) { - case HTTP_TRANSFER_CONTENT_LENGTH: - readed=_http_input_stream_content_length_read(stream, dest, size); - break; - case HTTP_TRANSFER_CHUNKED: - readed=_http_input_stream_chunked_read(stream, dest, size); - break; - case HTTP_TRANSFER_CONNECTION_CLOSE: - readed=_http_input_stream_connection_closed_read(stream, dest, size); - break; - case HTTP_TRANSFER_FILE: - readed=_http_input_stream_file_read(stream, dest, size); - break; - default: - stream->err = herror_new("http_input_stream_read", - STREAM_ERROR_INVALID_TYPE, "%d is invalid stream type", stream->type); - return -1; + case HTTP_TRANSFER_CONTENT_LENGTH: + readed = _http_input_stream_content_length_read (stream, dest, size); + break; + case HTTP_TRANSFER_CHUNKED: + readed = _http_input_stream_chunked_read (stream, dest, size); + break; + case HTTP_TRANSFER_CONNECTION_CLOSE: + readed = _http_input_stream_connection_closed_read (stream, dest, size); + break; + case HTTP_TRANSFER_FILE: + readed = _http_input_stream_file_read (stream, dest, size); + break; + default: + stream->err = herror_new ("http_input_stream_read", + STREAM_ERROR_INVALID_TYPE, + "%d is invalid stream type", stream->type); + return -1; } return readed; @@ -477,40 +496,42 @@ 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) +http_output_stream_t * +http_output_stream_new (hsocket_t sock, hpair_t * header) { http_output_stream_t *result; - char *content_length; + char *content_length; /* Paranoya check */ /* if (header == NULL) return NULL; */ /* Create object */ - result = (http_output_stream_t*)malloc(sizeof(http_output_stream_t)); + result = (http_output_stream_t *) malloc (sizeof (http_output_stream_t)); result->sock = sock; result->sent = 0; /* Find connection type */ - + /* Check if Content-type */ - if (_http_stream_is_content_length(header)) + if (_http_stream_is_content_length (header)) { - log_verbose1("Stream transfer with 'Content-length'"); - content_length = hpairnode_get_ignore_case(header, HEADER_CONTENT_LENGTH); - result->content_length = atoi(content_length); - result->type = HTTP_TRANSFER_CONTENT_LENGTH; + log_verbose1 ("Stream transfer with 'Content-length'"); + content_length = + hpairnode_get_ignore_case (header, HEADER_CONTENT_LENGTH); + result->content_length = atoi (content_length); + result->type = HTTP_TRANSFER_CONTENT_LENGTH; } /* Check if Chunked */ - else if (_http_stream_is_chunked(header)) + else if (_http_stream_is_chunked (header)) { - log_verbose1("Stream transfer with 'chunked'"); + log_verbose1 ("Stream transfer with 'chunked'"); result->type = HTTP_TRANSFER_CHUNKED; } /* Assume connection close */ else { - log_verbose1("Stream transfer with 'Connection: close'"); + log_verbose1 ("Stream transfer with 'Connection: close'"); result->type = HTTP_TRANSFER_CONNECTION_CLOSE; } @@ -520,42 +541,44 @@ http_output_stream_t *http_output_stream_new(hsocket_t sock, hpair_t *header) /** Free output stream */ -void http_output_stream_free(http_output_stream_t *stream) +void +http_output_stream_free (http_output_stream_t * stream) { - free(stream); + free (stream); } /** Writes 'size' bytes of 'bytes' into stream. Returns socket error flags or H_OK. */ -herror_t http_output_stream_write(http_output_stream_t *stream, - const byte_t *bytes, int size) +herror_t +http_output_stream_write (http_output_stream_t * stream, + const byte_t * bytes, int size) { herror_t status; char chunked[15]; if (stream->type == HTTP_TRANSFER_CHUNKED) { - sprintf(chunked, "%x\r\n", size); - status = hsocket_send(stream->sock, chunked); + sprintf (chunked, "%x\r\n", size); + status = hsocket_send (stream->sock, chunked); if (status != H_OK) - return status; + return status; } if (size > 0) { - _log_str("stream.out", (char*)bytes, size); - status = hsocket_nsend(stream->sock, bytes, size); + _log_str ("stream.out", (char *) bytes, size); + status = hsocket_nsend (stream->sock, bytes, size); if (status != H_OK) - return status; + return status; } - if (stream->type == HTTP_TRANSFER_CHUNKED) + if (stream->type == HTTP_TRANSFER_CHUNKED) { - status = hsocket_send(stream->sock, "\r\n"); + status = hsocket_send (stream->sock, "\r\n"); if (status != H_OK) - return status; + return status; } return H_OK; @@ -565,29 +588,25 @@ herror_t http_output_stream_write(http_output_stream_t *stream, Writes 'strlen()' bytes of 'str' into stream. Returns socket error flags or H_OK. */ -herror_t http_output_stream_write_string(http_output_stream_t *stream, - const char *str) +herror_t +http_output_stream_write_string (http_output_stream_t * stream, + const char *str) { - return http_output_stream_write(stream, str, strlen(str)); + return http_output_stream_write (stream, str, strlen (str)); } -herror_t http_output_stream_flush(http_output_stream_t *stream) +herror_t +http_output_stream_flush (http_output_stream_t * stream) { herror_t status; if (stream->type == HTTP_TRANSFER_CHUNKED) { - status = hsocket_send(stream->sock, "0\r\n\r\n"); + status = hsocket_send (stream->sock, "0\r\n\r\n"); if (status != H_OK) - return status; + return status; } - return H_OK; + return H_OK; } - - - - - - diff --git a/nanohttp/nanohttp-stream.h b/nanohttp/nanohttp-stream.h index d0a234a..2ef12a2 100755 --- a/nanohttp/nanohttp-stream.h +++ b/nanohttp/nanohttp-stream.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-stream.h,v 1.5 2004/11/02 23:09:27 snowdrop Exp $ + * $Id: nanohttp-stream.h,v 1.6 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -21,11 +21,11 @@ * * Email: ferhatayaz@yahoo.com ******************************************************************/ -#ifndef NANO_HTTP_STREAM_H -#define NANO_HTTP_STREAM_H +#ifndef NANO_HTTP_STREAM_H +#define NANO_HTTP_STREAM_H #include <stdio.h> -void _log_str(char *fn, char *str, int size); +void _log_str (char *fn, char *str, int size); /* HTTP Stream modul: @@ -77,8 +77,7 @@ typedef enum http_transfer_type /** This transfer style will be used by MIME support and for debug purposes.*/ HTTP_TRANSFER_FILE - -}http_transfer_type_t; +} http_transfer_type_t; /** @@ -87,9 +86,9 @@ typedef enum http_transfer_type */ typedef struct http_input_stream { - hsocket_t sock; + hsocket_t sock; herror_t err; - http_transfer_type_t type; + http_transfer_type_t type; int received; int content_length; int chunk_size; @@ -98,8 +97,8 @@ typedef struct http_input_stream /* file handling */ FILE *fd; char filename[255]; - int deleteOnExit; /* default is 0 */ -}http_input_stream_t; + int deleteOnExit; /* default is 0 */ +} http_input_stream_t; /** @@ -109,10 +108,10 @@ typedef struct http_input_stream typedef struct http_output_stream { hsocket_t sock; - http_transfer_type_t type; + http_transfer_type_t type; int content_length; int sent; -}http_output_stream_t; +} http_output_stream_t; /* @@ -135,7 +134,7 @@ typedef struct http_output_stream @see http_input_stream_free */ -http_input_stream_t *http_input_stream_new(hsocket_t sock, hpair_t *header); +http_input_stream_t *http_input_stream_new (hsocket_t sock, hpair_t * header); /** @@ -151,7 +150,7 @@ http_input_stream_t *http_input_stream_new(hsocket_t sock, hpair_t *header); @see http_input_stream_free */ -http_input_stream_t *http_input_stream_new_from_file(const char* filename); +http_input_stream_t *http_input_stream_new_from_file (const char *filename); /** @@ -160,7 +159,7 @@ http_input_stream_t *http_input_stream_new_from_file(const char* filename); @param stream the input stream to free. */ -void http_input_stream_free(http_input_stream_t *stream); +void http_input_stream_free (http_input_stream_t * stream); /** @@ -170,7 +169,7 @@ void http_input_stream_free(http_input_stream_t *stream); @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); +int http_input_stream_is_ready (http_input_stream_t * stream); /** @@ -194,8 +193,8 @@ int http_input_stream_is_ready(http_input_stream_t *stream); @returns the actual readed bytes or -1 on error. */ -int http_input_stream_read(http_input_stream_t *stream, - byte_t *dest, int size); +int http_input_stream_read (http_input_stream_t * stream, + byte_t * dest, int size); /* @@ -217,7 +216,8 @@ int http_input_stream_read(http_input_stream_t *stream, @see http_output_stream_free */ -http_output_stream_t *http_output_stream_new(hsocket_t sock, hpair_t *header); +http_output_stream_t *http_output_stream_new (hsocket_t sock, + hpair_t * header); /** @@ -226,7 +226,7 @@ http_output_stream_t *http_output_stream_new(hsocket_t sock, hpair_t *header); @param stream the stream to free. */ -void http_output_stream_free(http_output_stream_t *stream); +void http_output_stream_free (http_output_stream_t * stream); /** @@ -240,8 +240,8 @@ void http_output_stream_free(http_output_stream_t *stream); <BR>HSOCKET_ERROR_NOT_INITIALIZED <BR>HSOCKET_ERROR_SEND */ -herror_t http_output_stream_write(http_output_stream_t *stream, - const byte_t *bytes, int size); +herror_t http_output_stream_write (http_output_stream_t * stream, + const byte_t * bytes, int size); /** Writes a null terminated string to the stream. @@ -253,8 +253,8 @@ herror_t http_output_stream_write(http_output_stream_t *stream, <BR>HSOCKET_ERROR_NOT_INITIALIZED <BR>HSOCKET_ERROR_SEND */ -herror_t http_output_stream_write_string(http_output_stream_t *stream, - const char *str); +herror_t http_output_stream_write_string (http_output_stream_t * stream, + const char *str); /** @@ -267,12 +267,6 @@ herror_t http_output_stream_write_string(http_output_stream_t *stream, <BR>HSOCKET_ERROR_NOT_INITIALIZED <BR>HSOCKET_ERROR_SEND */ -herror_t http_output_stream_flush(http_output_stream_t *stream); +herror_t http_output_stream_flush (http_output_stream_t * stream); #endif - - - - - - |