diff options
author | snowdrop | 2006-01-10 11:29:04 +0000 |
---|---|---|
committer | snowdrop | 2006-01-10 11:29:04 +0000 |
commit | 07c7c81772c4b60b7980880847f3b21455a0f3c9 (patch) | |
tree | d7f8ea3508586842124684aa8f0b860f3a3a1c47 /nanohttp | |
parent | e96ea6884919f551111c32de0b63af4dff791f12 (diff) | |
download | csoap-07c7c81772c4b60b7980880847f3b21455a0f3c9.tar.gz csoap-07c7c81772c4b60b7980880847f3b21455a0f3c9.tar.bz2 |
indent with 'indent -nut -bli0 -fca -npcs' command
Diffstat (limited to 'nanohttp')
-rw-r--r-- | nanohttp/nanohttp-client.c | 266 | ||||
-rw-r--r-- | nanohttp/nanohttp-client.h | 51 | ||||
-rw-r--r-- | nanohttp/nanohttp-common.c | 382 | ||||
-rw-r--r-- | nanohttp/nanohttp-common.h | 84 | ||||
-rwxr-xr-x | nanohttp/nanohttp-mime.c | 334 | ||||
-rwxr-xr-x | nanohttp/nanohttp-mime.h | 8 | ||||
-rwxr-xr-x | nanohttp/nanohttp-request.c | 84 | ||||
-rwxr-xr-x | nanohttp/nanohttp-request.h | 6 | ||||
-rwxr-xr-x | nanohttp/nanohttp-response.c | 88 | ||||
-rwxr-xr-x | nanohttp/nanohttp-response.h | 6 | ||||
-rw-r--r-- | nanohttp/nanohttp-server.c | 495 | ||||
-rw-r--r-- | nanohttp/nanohttp-server.h | 49 | ||||
-rw-r--r-- | nanohttp/nanohttp-socket.c | 278 | ||||
-rw-r--r-- | nanohttp/nanohttp-socket.h | 30 | ||||
-rw-r--r-- | nanohttp/nanohttp-ssl.c | 192 | ||||
-rw-r--r-- | nanohttp/nanohttp-ssl.h | 10 | ||||
-rwxr-xr-x | nanohttp/nanohttp-stream.c | 217 | ||||
-rwxr-xr-x | nanohttp/nanohttp-stream.h | 32 |
18 files changed, 1296 insertions, 1316 deletions
diff --git a/nanohttp/nanohttp-client.c b/nanohttp/nanohttp-client.c index b5a3fd0..e61e5af 100644 --- a/nanohttp/nanohttp-client.c +++ b/nanohttp/nanohttp-client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-client.c,v 1.32 2006/01/10 11:21:55 snowdrop Exp $ +* $Id: nanohttp-client.c,v 1.33 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -40,7 +40,7 @@ #if 0 static int -httpc_send_data (httpc_conn_t * conn, const unsigned char *data, size_t size) +httpc_send_data(httpc_conn_t * conn, const unsigned char *data, size_t size) { return -1; } @@ -51,10 +51,10 @@ DESC: Initialize http client connection NOTE: This will be called from soap_client_init_args() ----------------------------------------------------*/ herror_t -httpc_init (int argc, char *argv[]) +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; } @@ -64,9 +64,9 @@ FUNCTION: httpc_destroy DESC: Destroy the http client module ----------------------------------------------------*/ void -httpc_destroy () +httpc_destroy() { - hsocket_module_destroy (); + hsocket_module_destroy(); } @@ -77,12 +77,12 @@ You need to create at least 1 http client connection to communicate via http. ----------------------------------------------------*/ httpc_conn_t * -httpc_new () +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) + if (hsocket_init(&res->sock) != H_OK) { return NULL; } @@ -102,7 +102,7 @@ FUNCTION: httpc_free DESC: Free the given http client object. ----------------------------------------------------*/ void -httpc_free (httpc_conn_t * conn) +httpc_free(httpc_conn_t * conn) { hpair_t *tmp; @@ -114,17 +114,17 @@ httpc_free (httpc_conn_t * conn) { tmp = conn->header; conn->header = conn->header->next; - hpairnode_free (tmp); + hpairnode_free(tmp); } if (conn->out != NULL) { - http_output_stream_free (conn->out); + http_output_stream_free(conn->out); conn->out = NULL; } - hsocket_free (conn->sock); - free (conn); + hsocket_free(conn->sock); + free(conn); } @@ -133,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; - hsocket_close (conn->sock); - httpc_free (conn); + hsocket_close(conn->sock); + httpc_free(conn); } @@ -150,13 +150,13 @@ 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) +httpc_set_header(httpc_conn_t * conn, const char *key, const char *value) { hpair_t *p; if (conn == NULL) { - log_warn1 ("Connection object is NULL"); + log_warn1("Connection object is NULL"); return 0; } p = conn->header; @@ -164,18 +164,18 @@ httpc_set_header (httpc_conn_t * conn, const char *key, const char *value) { if (p->key != NULL) { - if (!strcmp (p->key, key)) + if (!strcmp(p->key, key)) { - free (p->value); - p->value = (char *) malloc (strlen (value) + 1); - strcpy (p->value, value); + 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); + conn->header = hpairnode_new(key, value, conn->header); return 0; } @@ -185,15 +185,15 @@ 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, ...) +_httpc_set_error(httpc_conn_t * conn, int errcode, const char *format, ...) { 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); } /*-------------------------------------------------- @@ -201,17 +201,17 @@ FUNCTION: httpc_header_add_date DESC: Adds the current date to the header. ----------------------------------------------------*/ static void -httpc_header_add_date (httpc_conn_t * conn) +httpc_header_add_date(httpc_conn_t * conn) { 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); + nw = time(NULL); + localtime_r(&nw, &stm); + strftime(buffer, 255, "%a, %d %b %Y %T GMT", &stm); + httpc_set_header(conn, HEADER_DATE, buffer); } @@ -222,7 +222,7 @@ DESC: Sends the current header information stored in conn through conn->sock. ----------------------------------------------------*/ herror_t -httpc_send_header (httpc_conn_t * conn) +httpc_send_header(httpc_conn_t * conn) { hpair_t *p; herror_t status; @@ -233,15 +233,15 @@ httpc_send_header (httpc_conn_t * conn) { if (p->key && p->value) { - sprintf (buffer, "%s: %s\r\n", p->key, p->value); - status = hsocket_send (conn->sock, buffer); + 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"); + status = hsocket_send(conn->sock, "\r\n"); return status; } @@ -291,8 +291,8 @@ 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) +httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, + const char *urlstr) { hurl_t url; @@ -301,26 +301,26 @@ httpc_talk_to_server (hreq_method_t method, httpc_conn_t * conn, if (conn == NULL) { - return herror_new ("httpc_talk_to_server", - GENERAL_INVALID_PARAM, "httpc_conn_t param is 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); + httpc_header_add_date(conn); /* Create url */ - status = hurl_parse (&url, urlstr); + status = hurl_parse(&url, urlstr); if (status != H_OK) { - log_error2 ("Can not parse URL '%s'", SAVE_STR (urlstr)); + 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); + httpc_set_header(conn, HEADER_HOST, url.host); /* Open connection */ - status = hsocket_open (&conn->sock, url.host, url.port); + status = hsocket_open(&conn->sock, url.host, url.port); if (status != H_OK) { return status; @@ -329,10 +329,10 @@ httpc_talk_to_server (hreq_method_t method, httpc_conn_t * conn, /* TODO XXX XXX this is probably not right -- matt */ if (!&conn->sock.ssl) { - status = hsocket_block (conn->sock, conn->block); + status = hsocket_block(conn->sock, conn->block); if (status != H_OK) { - log_error1 ("Cannot make socket non-blocking"); + log_error1("Cannot make socket non-blocking"); return status; } } @@ -342,42 +342,42 @@ httpc_talk_to_server (hreq_method_t method, httpc_conn_t * conn, { /* 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"); + 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"); + 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_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); + 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); + log_error2("Can not send request (status:%d)", status); + hsocket_close(conn->sock); return status; } /* Send Header */ - status = httpc_send_header (conn); + status = httpc_send_header(conn); if (status != H_OK) { - log_error2 ("Can not send header (status:%d)", status); - hsocket_close (conn->sock); + log_error2("Can not send header (status:%d)", status); + hsocket_close(conn->sock); return status; } @@ -390,18 +390,18 @@ 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; - 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; } - status = hresponse_new_from_socket (conn->sock, out); + status = hresponse_new_from_socket(conn->sock, out); if (status != H_OK) { return status; @@ -417,16 +417,16 @@ FUNCTION: httpc_post_begin DESC: Returns H_OK if success ----------------------------------------------------*/ herror_t -httpc_post_begin (httpc_conn_t * conn, const char *url) +httpc_post_begin(httpc_conn_t * conn, const char *url) { herror_t status; - status = httpc_talk_to_server (HTTP_REQUEST_POST, conn, url); + 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); + conn->out = http_output_stream_new(conn->sock, conn->header); return H_OK; } @@ -438,18 +438,18 @@ 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) +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); + status = hresponse_new_from_socket(conn->sock, out); if (status != H_OK) { return status; @@ -612,16 +612,16 @@ hresponse_t* httpc_dime_end(httpc_conn_t *conn) -----------------------------------------------------*/ static void -_httpc_mime_get_boundary (httpc_conn_t * conn, char *dest) +_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) +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]; @@ -633,7 +633,7 @@ httpc_mime_begin (httpc_conn_t * conn, const char *url, start-info= ..; boundary=... */ - sprintf (buffer, "multipart/related;"); + sprintf(buffer, "multipart/related;"); /* using sprintf instead of snprintf because visual c does not support snprintf */ @@ -643,96 +643,93 @@ httpc_mime_begin (httpc_conn_t * conn, const char *url, if (related_type) { - snprintf (temp, 75, " type=\"%s\";", related_type); - strcat (buffer, temp); + snprintf(temp, 75, " type=\"%s\";", related_type); + strcat(buffer, temp); } if (related_start) { - snprintf (temp, 75, " start=\"%s\";", related_start); - strcat (buffer, temp); + 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); + 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); + 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) +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]; /* 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; /* 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); + 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; } herror_t -httpc_mime_end (httpc_conn_t * conn, hresponse_t ** out) +httpc_mime_end(httpc_conn_t * conn, hresponse_t ** out) { 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); + status = hresponse_new_from_socket(conn->sock, out); if (status != H_OK) { return status; @@ -748,51 +745,50 @@ httpc_mime_end (httpc_conn_t * conn, hresponse_t ** out) with next part */ 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) +httpc_mime_send_file(httpc_conn_t * conn, + const char *content_id, + const char *content_type, + const char *transfer_encoding, const char *filename) { herror_t status; - FILE *fd = fopen (filename, "rb"); + FILE *fd = fopen(filename, "rb"); byte_t buffer[MAX_FILE_BUFFER_SIZE]; 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); + status = httpc_mime_next(conn, content_id, content_type, transfer_encoding); if (status != H_OK) { - fclose (fd); + fclose(fd); return status; } - while (!feof (fd)) + while (!feof(fd)) { - size = fread (buffer, 1, MAX_FILE_BUFFER_SIZE, fd); + size = fread(buffer, 1, MAX_FILE_BUFFER_SIZE, fd); if (size == -1) { - fclose (fd); - return herror_new ("httpc_mime_send_file", FILE_ERROR_READ, - "Can not read from file '%s'", filename); + fclose(fd); + 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); + status = http_output_stream_write(conn->out, buffer, size); if (status != H_OK) { - fclose (fd); + fclose(fd); return status; } } } - fclose (fd); - log_verbose1 ("file sent!"); + fclose(fd); + log_verbose1("file sent!"); return H_OK; } diff --git a/nanohttp/nanohttp-client.h b/nanohttp/nanohttp-client.h index 77c8e8e..2b93617 100644 --- a/nanohttp/nanohttp-client.h +++ b/nanohttp/nanohttp-client.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-client.h,v 1.17 2006/01/10 11:21:55 snowdrop Exp $ + * $Id: nanohttp-client.h,v 1.18 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -55,57 +55,56 @@ 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); /* -------------------------------------------------------------- @@ -134,34 +133,34 @@ 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 with next part */ -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); +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); #endif diff --git a/nanohttp/nanohttp-common.c b/nanohttp/nanohttp-common.c index 7d7b141..3dcee6b 100644 --- a/nanohttp/nanohttp-common.c +++ b/nanohttp/nanohttp-common.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-common.c,v 1.21 2006/01/10 11:21:55 snowdrop Exp $ +* $Id: nanohttp-common.c,v 1.22 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -50,24 +50,24 @@ extern int SSLCertLess; /* option stuff */ void -hoption_set (int opt, const char *value) +hoption_set(int opt, const char *value) { if (opt >= MAX_OPTION_SIZE) { - log_warn3 ("Option to high (%d >= %d)", 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) +hoption_get(int opt) { if (opt >= MAX_OPTION_SIZE) { - log_warn3 ("Option to high (%d >= %d)", opt, MAX_OPTION_SIZE); + log_warn3("Option to high (%d >= %d)", opt, MAX_OPTION_SIZE); return ""; } @@ -76,59 +76,59 @@ hoption_get (int opt) void -hoption_init_args (int argc, char *argv[]) +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) + 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) + 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) + 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 } - else if (!strcmp (argv[i], NHTTP_ARG_CERTPASS) && i < argc - 1) + 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 } - else if (!strcmp (argv[i], NHTTP_ARG_CA) && i < argc - 1) + 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 @@ -144,13 +144,13 @@ hoption_init_args (int argc, char *argv[]) /* not thread safe!*/ char * -VisualC_funcname (const char *file, int line) +VisualC_funcname(const char *file, int line) { static char buffer[256]; - int i = strlen (file) - 1; + int i = strlen(file) - 1; while (i > 0 && file[i] != '\\') i--; - sprintf (buffer, "%s:%d", (file[i] != '\\') ? file : (file + i + 1), line); + sprintf(buffer, "%s:%d", (file[i] != '\\') ? file : (file + i + 1), line); return buffer; } @@ -166,23 +166,23 @@ typedef struct _herror_impl_t herror_t -herror_new (const char *func, int errcode, const char *format, ...) +herror_new(const char *func, int errcode, const char *format, ...) { va_list ap; - herror_impl_t *impl = (herror_impl_t *) malloc (sizeof (herror_impl_t)); + 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); + strcpy(impl->func, func); + va_start(ap, format); + vsprintf(impl->message, format, ap); + va_end(ap); return (herror_t) impl; } int -herror_code (herror_t err) +herror_code(herror_t err) { herror_impl_t *impl = (herror_impl_t *) err; if (!err) @@ -191,7 +191,7 @@ herror_code (herror_t err) } char * -herror_func (herror_t err) +herror_func(herror_t err) { herror_impl_t *impl = (herror_impl_t *) err; if (!err) @@ -200,7 +200,7 @@ herror_func (herror_t err) } char * -herror_message (herror_t err) +herror_message(herror_t err) { herror_impl_t *impl = (herror_impl_t *) err; if (!err) @@ -209,12 +209,12 @@ herror_message (herror_t err) } void -herror_release (herror_t err) +herror_release(herror_t err) { herror_impl_t *impl = (herror_impl_t *) err; if (!err) return; - free (impl); + free(impl); } @@ -224,7 +224,7 @@ static char logfile[75] = { '\0' }; static int log_background = 0; log_level_t -log_set_level (log_level_t level) +log_set_level(log_level_t level) { log_level_t old = loglevel; loglevel = level; @@ -233,29 +233,29 @@ log_set_level (log_level_t level) log_level_t -log_get_level () +log_get_level() { return loglevel; } void -log_set_file (const char *filename) +log_set_file(const char *filename) { if (filename) - strncpy (logfile, filename, 75); + strncpy(logfile, filename, 75); else logfile[0] = '\0'; } void -log_set_background (int state) +log_set_background(int state) { log_background = state; } char * -log_get_file () +log_get_file() { if (logfile[0] == '\0') return NULL; @@ -263,8 +263,8 @@ log_get_file () } static void -log_write (log_level_t level, const char *prefix, - const char *func, const char *format, va_list ap) +log_write(log_level_t level, const char *prefix, + const char *func, const char *format, va_list ap) { char buffer[1054]; char buffer2[1054]; @@ -273,84 +273,84 @@ log_write (log_level_t level, const char *prefix, 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); + vsprintf(buffer2, buffer, ap); if (!log_background) { - printf (buffer2); - fflush (stdout); + printf(buffer2); + fflush(stdout); } - if (log_get_file ()) + if (log_get_file()) { - f = fopen (log_get_file (), "a"); + f = fopen(log_get_file(), "a"); if (!f) - f = fopen (log_get_file (), "w"); + f = fopen(log_get_file(), "w"); if (f) { - fprintf (f, buffer2); - fflush (f); - fclose (f); + fprintf(f, buffer2); + fflush(f); + fclose(f); } } } } void -log_verbose (const char *FUNC, const char *format, ...) +log_verbose(const char *FUNC, const char *format, ...) { 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, ...) +log_debug(const char *FUNC, const char *format, ...) { 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, ...) +log_info(const char *FUNC, const char *format, ...) { 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, ...) +log_warn(const char *FUNC, const char *format, ...) { 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, ...) +log_error(const char *FUNC, const char *format, ...) { 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); } @@ -358,7 +358,7 @@ log_error (const char *FUNC, const char *format, ...) FUNCTION: strcmpigcase ------------------------------------------*/ int -strcmpigcase (const char *s1, const char *s2) +strcmpigcase(const char *s1, const char *s2) { int l1, l2, i; @@ -367,14 +367,14 @@ strcmpigcase (const char *s1, const char *s2) if (s1 == NULL || s2 == NULL) return 0; - l1 = strlen (s1); - l2 = strlen (s2); + l1 = strlen(s1); + l2 = strlen(s2); if (l1 != l2) return 0; for (i = 0; i < l1; i++) - if (toupper (s1[i]) != toupper (s2[i])) + if (toupper(s1[i]) != toupper(s2[i])) return 0; return 1; @@ -382,17 +382,17 @@ strcmpigcase (const char *s1, const char *s2) hpair_t * -hpairnode_new (const char *key, const char *value, hpair_t * next) +hpairnode_new(const char *key, const char *value, hpair_t * next) { 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); + pair->key = (char *) malloc(strlen(key) + 1); + strcpy(pair->key, key); } else { @@ -401,8 +401,8 @@ hpairnode_new (const char *key, const char *value, hpair_t * next) if (value != NULL) { - pair->value = (char *) malloc (strlen (value) + 1); - strcpy (pair->value, value); + pair->value = (char *) malloc(strlen(value) + 1); + strcpy(pair->value, value); } else { @@ -415,63 +415,63 @@ hpairnode_new (const char *key, const char *value, hpair_t * next) } hpair_t * -hpairnode_parse (const char *str, const char *delim, hpair_t * next) +hpairnode_parse(const char *str, const char *delim, hpair_t * next) { hpair_t *pair; char *key, *value; int c; - pair = (hpair_t *) malloc (sizeof (hpair_t)); + 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); + 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]); + pair->value = (char *) malloc(strlen(&value[c]) + 1); + strcpy(pair->value, &value[c]); } return pair; } hpair_t * -hpairnode_copy (const hpair_t * src) +hpairnode_copy(const hpair_t * src) { hpair_t *pair; if (src == NULL) return NULL; - pair = hpairnode_new (src->key, src->value, NULL); + pair = hpairnode_new(src->key, src->value, NULL); return pair; } hpair_t * -hpairnode_copy_deep (const hpair_t * src) +hpairnode_copy_deep(const hpair_t * src) { hpair_t *pair, *result, *next; if (src == NULL) return NULL; - result = hpairnode_copy (src); + result = hpairnode_copy(src); next = src->next; pair = result; while (next != NULL) { - pair->next = hpairnode_copy (next); + pair->next = hpairnode_copy(next); pair = pair->next; next = next->next; } @@ -481,75 +481,75 @@ hpairnode_copy_deep (const hpair_t * src) void -hpairnode_dump (hpair_t * pair) +hpairnode_dump(hpair_t * pair) { if (pair == NULL) { - log_verbose1 ("(NULL)[]"); + log_verbose1("(NULL)[]"); return; } - log_verbose5 ("(%p)['%s','%s','%p']", pair, - SAVE_STR (pair->key), SAVE_STR (pair->value), pair->next); + log_verbose5("(%p)['%s','%s','%p']", pair, + SAVE_STR(pair->key), SAVE_STR(pair->value), pair->next); } void -hpairnode_dump_deep (hpair_t * pair) +hpairnode_dump_deep(hpair_t * pair) { hpair_t *p; p = pair; - log_verbose1 ("-- BEGIN dump hpairnode_t --"); + log_verbose1("-- BEGIN dump hpairnode_t --"); while (p != NULL) { - hpairnode_dump (p); + 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) +hpairnode_free(hpair_t * pair) { 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) +hpairnode_free_deep(hpair_t * pair) { hpair_t *tmp; while (pair != NULL) { tmp = pair->next; - hpairnode_free (pair); + hpairnode_free(pair); pair = tmp; } } char * -hpairnode_get_ignore_case (hpair_t * pair, const char *key) +hpairnode_get_ignore_case(hpair_t * pair, const char *key) { if (key == NULL) { - log_error1 ("key is NULL"); + log_error1("key is NULL"); return NULL; } while (pair != NULL) { if (pair->key != NULL) { - if (strcmpigcase (pair->key, key)) + if (strcmpigcase(pair->key, key)) { return pair->value; } @@ -561,18 +561,18 @@ hpairnode_get_ignore_case (hpair_t * pair, const char *key) } char * -hpairnode_get (hpair_t * pair, const char *key) +hpairnode_get(hpair_t * pair, const char *key) { if (key == NULL) { - log_error1 ("key is NULL"); + log_error1("key is NULL"); return NULL; } while (pair != NULL) { if (pair->key != NULL) { - if (!strcmp (pair->key, key)) + if (!strcmp(pair->key, key)) { return pair->value; } @@ -584,23 +584,23 @@ hpairnode_get (hpair_t * pair, const char *key) } static void -hurl_dump (const hurl_t * url) +hurl_dump(const hurl_t * url) { if (url == NULL) { - log_error1 ("url is 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); + 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) +hurl_parse(hurl_t * url, const char *urlstr) { int iprotocol; int ihost; @@ -611,7 +611,7 @@ hurl_parse (hurl_t * url, const char *urlstr) char protocol[1024]; iprotocol = 0; - len = strlen (urlstr); + len = strlen(urlstr); /* find protocol */ while (urlstr[iprotocol] != ':' && urlstr[iprotocol] != '\0') @@ -621,19 +621,19 @@ hurl_parse (hurl_t * url, const char *urlstr) if (iprotocol == 0) { - log_error1 ("no protocol"); - return herror_new ("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol"); + 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"); + 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"); + log_error1("no protocol"); + return herror_new("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol"); } /* find host */ ihost = iprotocol + 3; @@ -645,8 +645,8 @@ hurl_parse (hurl_t * url, const char *urlstr) if (ihost == iprotocol + 1) { - log_error1 ("no host"); - return herror_new ("hurl_parse", URL_ERROR_NO_HOST, "No host"); + log_error1("no host"); + return herror_new("hurl_parse", URL_ERROR_NO_HOST, "No host"); } /* find port */ iport = ihost; @@ -662,29 +662,29 @@ hurl_parse (hurl_t * url, const char *urlstr) } /* find protocol */ - strncpy (protocol, urlstr, iprotocol); + strncpy(protocol, urlstr, iprotocol); protocol[iprotocol] = '\0'; - if (strcmpigcase (protocol, "http")) + if (strcmpigcase(protocol, "http")) url->protocol = PROTOCOL_HTTP; - else if (strcmpigcase (protocol, "https")) + else if (strcmpigcase(protocol, "https")) url->protocol = PROTOCOL_HTTPS; - else if (strcmpigcase (protocol, "ftp")) + else if (strcmpigcase(protocol, "ftp")) url->protocol = PROTOCOL_FTP; else - return herror_new ("hurl_parse", - URL_ERROR_UNKNOWN_PROTOCOL, "Unknown protocol '%s'", - protocol); + 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); + 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); + strncpy(tmp, &urlstr[ihost + 1], size); + url->port = atoi(tmp); } else { @@ -702,12 +702,12 @@ hurl_parse (hurl_t * url, const char *urlstr) } } - len = strlen (urlstr); + 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); + strncpy(url->context, &urlstr[iport], size); url->context[size] = '\0'; } else @@ -715,7 +715,7 @@ hurl_parse (hurl_t * url, const char *urlstr) url->context[0] = '\0'; } - hurl_dump (url); + hurl_dump(url); return H_OK; } @@ -724,7 +724,7 @@ hurl_parse (hurl_t * url, const char *urlstr) /* Content-type stuff */ content_type_t * -content_type_new (const char *content_type_str) +content_type_new(const char *content_type_str) { hpair_t *pair = NULL, *last = NULL; content_type_t *ct; @@ -735,10 +735,10 @@ content_type_new (const char *content_type_str) /* 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) @@ -784,7 +784,7 @@ content_type_new (const char *content_type_str) { value[c] = '\0'; - pair = hpairnode_new (key, value, NULL); + pair = hpairnode_new(key, value, NULL); if (ct->params == NULL) ct->params = pair; else @@ -810,43 +810,43 @@ content_type_new (const char *content_type_str) void -content_type_free (content_type_t * ct) +content_type_free(content_type_t * ct) { 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_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"); + strcpy(part->transfer_encoding, + transfer_encoding ? transfer_encoding : "binary"); if (content_type) { part->header = - hpairnode_new (HEADER_CONTENT_TYPE, content_type, part->header); + hpairnode_new(HEADER_CONTENT_TYPE, content_type, part->header); } else { @@ -857,26 +857,26 @@ part_new (const char *id, const char *filename, } void -part_free (part_t * part) +part_free(part_t * part) { if (part == NULL) return; if (part->deleteOnExit) { - remove (part->filename); + 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_new() /* should be used internally */ { attachments_t *attachments = - (attachments_t *) malloc (sizeof (attachments_t)); + (attachments_t *) malloc(sizeof(attachments_t)); attachments->parts = NULL; attachments->last = NULL; attachments->root_part = NULL; @@ -885,7 +885,7 @@ attachments_new () /* should be used internally */ } void -attachments_add_part (attachments_t * attachments, part_t * part) +attachments_add_part(attachments_t * attachments, part_t * part) { /* paranoya check */ if (!attachments) @@ -903,7 +903,7 @@ attachments_add_part (attachments_t * attachments, part_t * part) Free a mime message */ void -attachments_free (attachments_t * message) +attachments_free(attachments_t * message) { part_t *tmp, *part; @@ -914,14 +914,14 @@ attachments_free (attachments_t * message) while (part) { tmp = part->next; - part_free (part); + 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); } @@ -929,7 +929,7 @@ 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; @@ -937,16 +937,16 @@ strtok_r (char *s, const char *delim, char **save_ptr) s = *save_ptr; /* Scan leading delimiters. */ - s += strspn (s, delim); + s += strspn(s, delim); if (*s == '\0') return NULL; /* Find the end of the token. */ token = s; - s = strpbrk (token, delim); + s = strpbrk(token, delim); if (s == NULL) /* This token finishes the string. */ - *save_ptr = strchr (token, '\0'); + *save_ptr = strchr(token, '\0'); else { /* Terminate the token and make *SAVE_PTR point past it. */ @@ -958,13 +958,13 @@ strtok_r (char *s, const char *delim, char **save_ptr) /* localtime_r() */ struct tm * -localtime_r (const time_t * const timep, struct tm *p_tm) +localtime_r(const time_t * const timep, struct tm *p_tm) { static struct tm *tmp; - tmp = localtime (timep); + tmp = localtime(timep); if (tmp) { - memcpy (p_tm, tmp, sizeof (struct tm)); + memcpy(p_tm, tmp, sizeof(struct tm)); tmp = p_tm; } return tmp; diff --git a/nanohttp/nanohttp-common.h b/nanohttp/nanohttp-common.h index 36136b5..7a957ce 100644 --- a/nanohttp/nanohttp-common.h +++ b/nanohttp/nanohttp-common.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-common.h,v 1.21 2006/01/10 11:21:55 snowdrop Exp $ + * $Id: nanohttp-common.h,v 1.22 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -152,8 +152,8 @@ Set Sleep function platform depended #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; @@ -182,18 +182,18 @@ typedef enum _hreq_method } 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 @@ -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 @@ -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); @@ -433,10 +433,10 @@ typedef struct _part } 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); @@ -451,7 +451,7 @@ typedef struct _attachments part_t *root_part; } 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 @@ -459,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 @@ -482,15 +482,15 @@ typedef enum log_level -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! */ +char *VisualC_funcname(const char *file, int line); /* not thread safe! */ #define __FUNCTION__ VisualC_funcname(__FILE__, __LINE__) #endif #endif @@ -525,11 +525,11 @@ char *VisualC_funcname (const char *file, int line); /* not thread safe! */ #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, ...); diff --git a/nanohttp/nanohttp-mime.c b/nanohttp/nanohttp-mime.c index 3f17d38..5ca7df9 100755 --- a/nanohttp/nanohttp-mime.c +++ b/nanohttp/nanohttp-mime.c @@ -3,7 +3,7 @@ * | \/ | | | | \/ | | _/ * |_''_| |_| |_''_| |_'/ PARSER * -* $Id: nanohttp-mime.c,v 1.8 2006/01/10 11:21:55 snowdrop Exp $ +* $Id: nanohttp-mime.c,v 1.9 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -69,8 +69,8 @@ typedef enum _MIME_read_status #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 *); /** @@ -88,8 +88,8 @@ typedef struct _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 { @@ -101,19 +101,19 @@ typedef struct _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) +MIME_reader_init(MIME_reader * reader, + MIME_read_function reader_function, void *userdata) { reader->size = 0; reader->marker = -1; @@ -127,7 +127,7 @@ 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_reader_read(MIME_reader * reader, unsigned char *buffer, int size) { MIME_read_status status; int readed_size; @@ -144,14 +144,14 @@ MIME_reader_read (MIME_reader * reader, unsigned char *buffer, int size) { if (reader->marker != 0) { - memcpy (tempBuffer, reader->buffer + reader->marker, - reader->size - reader->marker); - memcpy (reader->buffer, tempBuffer, reader->size - reader->marker); + 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) { - fprintf (stderr, "Marker error"); + fprintf(stderr, "Marker error"); return MIME_READ_ERROR; } reader->marker = 0; @@ -160,9 +160,9 @@ MIME_reader_read (MIME_reader * reader, unsigned char *buffer, int size) 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); + status = reader->read_function(reader->userdata, + reader->buffer + reader->current, + &readed_size); if (status == MIME_READ_OK) { @@ -174,7 +174,7 @@ MIME_reader_read (MIME_reader * reader, unsigned char *buffer, int size) 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; } @@ -182,28 +182,28 @@ MIME_reader_read (MIME_reader * reader, unsigned char *buffer, int size) { /* 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) +MIME_reader_set_marker(MIME_reader * reader) { reader->marker = reader->current; } void -MIME_reader_unset_marker (MIME_reader * reader) +MIME_reader_unset_marker(MIME_reader * reader) { reader->marker = -1; } void -MIME_reader_jump_marker (MIME_reader * reader) +MIME_reader_jump_marker(MIME_reader * reader) { reader->current = reader->marker; } @@ -218,48 +218,48 @@ typedef struct _MIME_buffer void -MIME_buffer_init (MIME_buffer * buffer) +MIME_buffer_init(MIME_buffer * buffer) { buffer->size = 0; } void -MIME_buffer_add (MIME_buffer * buffer, unsigned char ch) +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) +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) +MIME_buffer_is_full(MIME_buffer * buffer) { return buffer->size + 150 >= MIME_PARSER_BUFFER_SIZE; } int -MIME_buffer_is_empty (MIME_buffer * buffer) +MIME_buffer_is_empty(MIME_buffer * buffer) { return buffer->size == 0; } void -MIME_buffer_clear (MIME_buffer * buffer) +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_parse(MIME_read_function reader_function, + void *reader_userdata, + const char *user_boundary, + const MIME_callbacks * callbacks, void *callbacks_userdata) { char boundary[150]; unsigned char ch[153]; @@ -269,30 +269,30 @@ MIME_parse (MIME_read_function reader_function, 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 */ - MIME_reader_set_marker (&reader); + MIME_reader_set_marker(&reader); 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; else if (status == MIME_READ_ERROR) @@ -304,8 +304,8 @@ MIME_parse (MIME_read_function reader_function, 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; else if (status == MIME_READ_ERROR) @@ -319,7 +319,7 @@ MIME_parse (MIME_read_function reader_function, } else { - MIME_reader_jump_marker (&reader); + MIME_reader_jump_marker(&reader); ignore = 1; goto read_byte; } @@ -327,8 +327,8 @@ MIME_parse (MIME_read_function reader_function, } /* 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) @@ -340,8 +340,8 @@ MIME_parse (MIME_read_function reader_function, 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) @@ -352,42 +352,42 @@ MIME_parse (MIME_read_function reader_function, /* 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') */ 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); + MIME_buffer_add_bytes(&buffer, ch, boundary_length + 2); - if (MIME_buffer_is_full (&buffer)) + 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') */ @@ -397,8 +397,8 @@ MIME_parse (MIME_read_function reader_function, 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; @@ -407,21 +407,21 @@ MIME_parse (MIME_read_function reader_function, 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 @@ -432,7 +432,7 @@ MIME_parse (MIME_read_function reader_function, } /* if (ch[0] == '-') */ else { - MIME_reader_jump_marker (&reader); + MIME_reader_jump_marker(&reader); ignore = 1; goto read_byte; } /* else of if (ch[0] == '-') */ @@ -440,7 +440,7 @@ MIME_parse (MIME_read_function reader_function, } /* if (ch[0] == '-') */ else { - MIME_reader_jump_marker (&reader); + MIME_reader_jump_marker(&reader); ignore = 1; goto read_byte; } /* else of if (ch[0] == '-') */ @@ -451,17 +451,17 @@ MIME_parse (MIME_read_function reader_function, 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)) + 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) */ @@ -471,14 +471,14 @@ MIME_parse (MIME_read_function reader_function, MIME_read_status -MIME_filereader_function (void *userdata, unsigned char *dest, int *size) +MIME_filereader_function(void *userdata, unsigned char *dest, int *size) { 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; } @@ -507,21 +507,21 @@ typedef struct _mime_callback_data MIME_read_status -mime_streamreader_function (void *userdata, unsigned char *dest, int *size) +mime_streamreader_function(void *userdata, unsigned char *dest, int *size) { 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); + 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)); + log_error4("[%d] %s():%s ", herror_code(in->err), herror_func(in->err), + herror_message(in->err)); } *size = readed; @@ -539,34 +539,34 @@ mime_streamreader_function (void *userdata, unsigned char *dest, int *size) Start Callback functions */ static void -_mime_parse_begin (void *data) +_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) +_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) +_mime_part_begin(void *data) { char buffer[1054]; 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; @@ -583,38 +583,38 @@ _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) +_mime_part_end(void *data) { mime_callback_data_t *cbdata = (mime_callback_data_t *) data; - log_verbose2 ("End Part (%p)", 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) +_mime_process_header(char *buffer) { int i = 0, c = 0, proc_key, begin = 0; hpair_t *first = NULL, *last = NULL; @@ -631,12 +631,12 @@ _mime_process_header (char *buffer) value[c] = '\0'; if (last) { - last->next = hpairnode_new (key, value, NULL); + 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; @@ -668,7 +668,7 @@ _mime_process_header (char *buffer) static void -_mime_received_bytes (void *data, const unsigned char *bytes, int size) +_mime_received_bytes(void *data, const unsigned char *bytes, int size) { int i = 0; char *id, *type, *location; @@ -738,30 +738,28 @@ _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); + 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); + 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; @@ -786,7 +784,7 @@ _mime_received_bytes (void *data, const unsigned char *bytes, int size) /* 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); } @@ -795,15 +793,15 @@ _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; mime_callback_data_t *cbdata = (mime_callback_data_t *) - malloc (sizeof (mime_callback_data_t)); + malloc(sizeof(mime_callback_data_t)); cbdata->part_id = 100; cbdata->buffer_capacity = 0; @@ -811,9 +809,9 @@ mime_message_parse (http_input_stream_t * in, const char *root_id, 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; @@ -824,33 +822,33 @@ 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; mime_callback_data_t *cbdata = (mime_callback_data_t *) - malloc (sizeof (mime_callback_data_t)); + malloc(sizeof(mime_callback_data_t)); cbdata->part_id = 100; cbdata->buffer_capacity = 0; @@ -858,9 +856,9 @@ mime_message_parse_from_file (FILE * in, const char *root_id, 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; @@ -871,22 +869,22 @@ 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; } } @@ -894,8 +892,8 @@ mime_message_parse_from_file (FILE * in, const char *root_id, herror_t -mime_get_attachments (content_type_t * ctype, http_input_stream_t * in, - attachments_t ** dest) +mime_get_attachments(content_type_t * ctype, http_input_stream_t * in, + attachments_t ** dest) { /* MIME variables */ attachments_t *mimeMessage; @@ -903,44 +901,44 @@ mime_get_attachments (content_type_t * ctype, http_input_stream_t * in, 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"); + 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"); + 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)); + 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"); + 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 */ diff --git a/nanohttp/nanohttp-mime.h b/nanohttp/nanohttp-mime.h index 6b160ab..7ee04b0 100755 --- a/nanohttp/nanohttp-mime.h +++ b/nanohttp/nanohttp-mime.h @@ -3,7 +3,7 @@ * | \/ | | | | \/ | | _/ * |_''_| |_| |_''_| |_'/ PARSER * -* $Id: nanohttp-mime.h,v 1.5 2006/01/10 11:21:55 snowdrop Exp $ +* $Id: nanohttp-mime.h,v 1.6 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -40,9 +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 5f15e93..930c609 100755 --- a/nanohttp/nanohttp-request.c +++ b/nanohttp/nanohttp-request.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-request.c,v 1.7 2006/01/10 11:21:55 snowdrop Exp $ +* $Id: nanohttp-request.c,v 1.8 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -35,9 +35,9 @@ static hrequest_t * -hrequest_new () +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; @@ -51,7 +51,7 @@ hrequest_new () } static hrequest_t * -_hrequest_parse_header (char *data) +_hrequest_parse_header(char *data) { hrequest_t *req; hpair_t *hpair = NULL, *qpair = NULL, *tmppair = NULL; @@ -67,12 +67,12 @@ _hrequest_parse_header (char *data) char *opt_value; int firstline = 1; - req = hrequest_new (); + req = hrequest_new(); tmp = data; for (;;) { - result = (char *) strtok_r (tmp, "\r\n", &saveptr); + result = (char *) strtok_r(tmp, "\r\n", &saveptr); tmp = saveptr; if (result == NULL) @@ -84,19 +84,19 @@ _hrequest_parse_header (char *data) tmp2 = result; /* parse [GET|POST] [PATH] [SPEC] */ - key = (char *) strtok_r (tmp2, " ", &saveptr2); + key = (char *) strtok_r(tmp2, " ", &saveptr2); /* save method (get or post) */ tmp2 = saveptr2; if (key != NULL) { - if (!strcmp (key, "POST")) + 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); + key = (char *) strtok_r(tmp2, " ", &saveptr2); /* save version */ tmp2 = saveptr2; @@ -104,7 +104,7 @@ _hrequest_parse_header (char *data) { /* req->spec = (char *) malloc(strlen(tmp2) + 1); strcpy(req->spec, tmp2); */ - if (!strcmp (tmp2, "HTTP/1.0")) + if (!strcmp(tmp2, "HTTP/1.0")) req->version = HTTP_1_0; else req->version = HTTP_1_1; @@ -117,23 +117,23 @@ _hrequest_parse_header (char *data) if (key != NULL) { tmp2 = key; - key = (char *) strtok_r (tmp2, "?", &saveptr2); + 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); + strncpy(req->path, key, REQUEST_MAX_PATH_SIZE); /* parse options */ for (;;) { - key = (char *) strtok_r (tmp2, "&", &saveptr2); + key = (char *) strtok_r(tmp2, "&", &saveptr2); tmp2 = saveptr2; if (key == NULL) break; - opt_key = (char *) strtok_r (key, "=", &saveptr3); + opt_key = (char *) strtok_r(key, "=", &saveptr3); opt_value = saveptr3; if (opt_value == NULL) @@ -142,7 +142,7 @@ _hrequest_parse_header (char *data) /* create option pair */ if (opt_key != NULL) { - tmppair = (hpair_t *) malloc (sizeof (hpair_t)); + tmppair = (hpair_t *) malloc(sizeof(hpair_t)); if (req->query == NULL) { @@ -156,11 +156,11 @@ _hrequest_parse_header (char *data) /* fill hpairnode_t struct */ qpair->next = NULL; - qpair->key = (char *) malloc (strlen (opt_key) + 1); - qpair->value = (char *) malloc (strlen (opt_value) + 1); + 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); + strcpy(qpair->key, opt_key); + strcpy(qpair->value, opt_value); } } @@ -175,7 +175,7 @@ _hrequest_parse_header (char *data) /* create pair */ /* tmppair = (hpair_t *) malloc(sizeof(hpair_t));*/ - tmppair = hpairnode_parse (result, ":", NULL); + tmppair = hpairnode_parse(result, ":", NULL); if (req->header == NULL) { @@ -197,39 +197,39 @@ _hrequest_parse_header (char *data) } /* 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; } void -hrequest_free (hrequest_t * req) +hrequest_free(hrequest_t * req) { 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); + 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; @@ -237,16 +237,16 @@ hrequest_new_from_socket (hsocket_t sock, hrequest_t ** out) 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) { - 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) + if (herror_code(status) != HSOCKET_SSL_CLOSE) { - log_error1 ("Socket read error"); + log_error1("Socket read error"); } return status; } @@ -255,36 +255,36 @@ hrequest_new_from_socket (hsocket_t sock, hrequest_t ** out) if (i > 3) { - if (!strcmp (&(buffer[i - 1]), "\n\n") || - !strcmp (&(buffer[i - 2]), "\n\r\n")) + 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"))) + !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); + hrequest_free(req); return status; } else { req->attachments = mimeMessage; req->in = - http_input_stream_new_from_file (mimeMessage->root_part->filename); + http_input_stream_new_from_file(mimeMessage->root_part->filename); } } diff --git a/nanohttp/nanohttp-request.h b/nanohttp/nanohttp-request.h index e7b7b3e..537cf08 100755 --- a/nanohttp/nanohttp-request.h +++ b/nanohttp/nanohttp-request.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-request.h,v 1.3 2006/01/10 11:21:55 snowdrop Exp $ + * $Id: nanohttp-request.h,v 1.4 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -45,7 +45,7 @@ typedef struct hrequest char root_part_id[150]; } 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 ee9e762..2253640 100755 --- a/nanohttp/nanohttp-response.c +++ b/nanohttp/nanohttp-response.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-response.c,v 1.6 2006/01/10 11:21:55 snowdrop Exp $ +* $Id: nanohttp-response.c,v 1.7 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -32,12 +32,12 @@ #endif static hresponse_t * -hresponse_new () +hresponse_new() { hresponse_t *res; /* create response object */ - res = (hresponse_t *) malloc (sizeof (hresponse_t)); + res = (hresponse_t *) malloc(sizeof(hresponse_t)); res->version = HTTP_1_1; res->errcode = -1; res->desc[0] = '\0'; @@ -49,58 +49,58 @@ hresponse_new () } static hresponse_t * -_hresponse_parse_header (const char *buffer) +_hresponse_parse_header(const char *buffer) { hresponse_t *res; char *s1, *s2, *str; /* create response object */ - res = hresponse_new (); + res = hresponse_new(); /* *** parse spec *** */ /* [HTTP/1.1 | 1.2] [CODE] [DESC] */ /* stage 1: HTTP spec */ - str = (char *) strtok_r ((char *) buffer, " ", &s2); + str = (char *) strtok_r((char *) buffer, " ", &s2); s1 = s2; if (str == NULL) { - log_error1 ("Parse error reading HTTP spec"); + log_error1("Parse error reading HTTP spec"); return NULL; } - if (!strcmp (str, "HTTP/1.0")) + 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); + str = (char *) strtok_r(s1, " ", &s2); s1 = s2; if (str == NULL) { - log_error1 ("Parse error reading HTTP code"); + log_error1("Parse error reading HTTP code"); return NULL; } - res->errcode = atoi (str); + res->errcode = atoi(str); /* stage 3: description text */ - str = (char *) strtok_r (s1, "\r\n", &s2); + str = (char *) strtok_r(s1, "\r\n", &s2); s1 = s2; if (str == NULL) { - log_error1 ("Parse error reading HTTP description"); + 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'; + strncpy(res->desc, str, RESPONSE_MAX_DESC_SIZE); + res->desc[strlen(str)] = '\0'; /* *** parse header *** */ /* [key]: [value] */ for (;;) { - str = strtok_r (s1, "\n", &s2); + str = strtok_r(s1, "\n", &s2); s1 = s2; /* check if header ends without body */ @@ -109,18 +109,18 @@ _hresponse_parse_header (const char *buffer) return res; } /* check also for end of header */ - if (!strcmp (str, "\r")) + if (!strcmp(str, "\r")) { break; } - str[strlen (str) - 1] = '\0'; - res->header = hpairnode_parse (str, ":", res->header); + 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; @@ -128,7 +128,7 @@ _hresponse_parse_header (const char *buffer) 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; @@ -140,10 +140,10 @@ read_header: /* for errorcode: 100 (continue) */ /* Read header */ 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"); + log_error1("Socket read error"); return status; } @@ -151,52 +151,52 @@ read_header: /* for errorcode: 100 (continue) */ if (i > 3) { - if (!strcmp (&(buffer[i - 1]), "\n\n") || - !strcmp (&(buffer[i - 2]), "\n\r\n")) + 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); + 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"))) + !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); + hresponse_free(res); return status; } else { res->attachments = mimeMessage; - http_input_stream_free (res->in); + http_input_stream_free(res->in); res->in = - http_input_stream_new_from_file (mimeMessage->root_part->filename); + http_input_stream_new_from_file(mimeMessage->root_part->filename); if (!res->in) { /* TODO (#1#): Handle error */ @@ -215,21 +215,21 @@ read_header: /* for errorcode: 100 (continue) */ void -hresponse_free (hresponse_t * res) +hresponse_free(hresponse_t * res) { 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 70e5348..c91c56b 100755 --- a/nanohttp/nanohttp-response.h +++ b/nanohttp/nanohttp-response.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-response.h,v 1.3 2006/01/10 11:21:55 snowdrop Exp $ + * $Id: nanohttp-response.h,v 1.4 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -43,8 +43,8 @@ typedef struct hresponse char root_part_id[150]; } 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); diff --git a/nanohttp/nanohttp-server.c b/nanohttp/nanohttp-server.c index 102c62a..d09d5df 100644 --- a/nanohttp/nanohttp-server.c +++ b/nanohttp/nanohttp-server.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-server.c,v 1.38 2006/01/10 11:21:55 snowdrop Exp $ +* $Id: nanohttp-server.c,v 1.39 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -96,7 +96,7 @@ static conndata_t *_httpd_connection; #ifdef WIN32 -static void WSAReaper (void *x); +static void WSAReaper(void *x); #else sigset_t thrsigset; #endif @@ -113,66 +113,66 @@ extern SSL_CTX *SSLctx; * ----------------------------------------------------- */ herror_t -httpd_init (int argc, char *argv[]) +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; /* write argument information */ - log_verbose1 ("Arguments:"); + log_verbose1("Arguments:"); for (i = 0; i < argc; i++) - log_verbose3 ("argv[%i] = '%s'", i, SAVE_STR (argv[i])); + log_verbose3("argv[%i] = '%s'", i, SAVE_STR(argv[i])); /* initialize from arguments */ for (i = 0; i < argc; i++) { - if (!strcmp (argv[i], NHTTPD_ARG_PORT) && i < argc - 1) + if (!strcmp(argv[i], NHTTPD_ARG_PORT) && i < argc - 1) { - _httpd_port = atoi (argv[i + 1]); + _httpd_port = atoi(argv[i + 1]); } - else if (!strcmp (argv[i], NHTTPD_ARG_TERMSIG) && i < argc - 1) + else if (!strcmp(argv[i], NHTTPD_ARG_TERMSIG) && i < argc - 1) { - _httpd_terminate_signal = atoi (argv[i + 1]); + _httpd_terminate_signal = atoi(argv[i + 1]); } - else if (!strcmp (argv[i], NHTTPD_ARG_MAXCONN) && i < argc - 1) + else if (!strcmp(argv[i], NHTTPD_ARG_MAXCONN) && i < argc - 1) { - _httpd_max_connections = atoi (argv[i + 1]); + _httpd_max_connections = atoi(argv[i + 1]); } } - log_verbose2 ("socket bind to port '%d'", _httpd_port); + log_verbose2("socket bind to port '%d'", _httpd_port); /* init built-in services */ /* httpd_register("/httpd/list", service_list); */ - _httpd_connection = calloc (_httpd_max_connections, sizeof (conndata_t)); + _httpd_connection = calloc(_httpd_max_connections, sizeof(conndata_t)); for (i = 0; i < _httpd_max_connections; i++) { - memset ((char *) &_httpd_connection[i], 0, sizeof (_httpd_connection[i])); + memset((char *) &_httpd_connection[i], 0, sizeof(_httpd_connection[i])); } #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"); - } */ + THREAD_BEGIN_ERROR, "_beginthread() failed while starting WSAReaper"); } + */ #endif /* create socket */ - status = hsocket_init (&_httpd_socket); + status = hsocket_init(&_httpd_socket); if (status != H_OK) { return status; } - status = hsocket_bind (&_httpd_socket, _httpd_port); + status = hsocket_bind(&_httpd_socket, _httpd_port); return status; } @@ -184,16 +184,16 @@ httpd_init (int argc, char *argv[]) */ int -httpd_register (const char *ctx, httpd_service func) +httpd_register(const char *ctx, httpd_service func) { hservice_t *service; - service = (hservice_t *) malloc (sizeof (hservice_t)); + service = (hservice_t *) malloc(sizeof(hservice_t)); service->next = NULL; service->func = func; - strcpy (service->ctx, ctx); + strcpy(service->ctx, ctx); - log_verbose3 ("register service:t(%p):%s", service, SAVE_STR (ctx)); + log_verbose3("register service:t(%p):%s", service, SAVE_STR(ctx)); if (_httpd_services_head == NULL) { _httpd_services_head = _httpd_services_tail = service; @@ -214,7 +214,7 @@ httpd_register (const char *ctx, httpd_service func) * ----------------------------------------------------- */ hservice_t * -httpd_services () +httpd_services() { return _httpd_services_head; } @@ -225,9 +225,9 @@ httpd_services () * ----------------------------------------------------- */ static void -hservice_free (hservice_t * service) +hservice_free(hservice_t * service) { - free (service); + free(service); } /* @@ -236,13 +236,13 @@ hservice_free (hservice_t * service) * ----------------------------------------------------- */ static hservice_t * -httpd_find_service (const char *ctx) +httpd_find_service(const char *ctx) { hservice_t *cur = _httpd_services_head; while (cur != NULL) { - if (!strcmp (cur->ctx, ctx)) + if (!strcmp(cur->ctx, ctx)) { return cur; } @@ -259,9 +259,9 @@ httpd_find_service (const char *ctx) * ----------------------------------------------------- */ void -httpd_response_set_content_type (httpd_conn_t * res, const char *content_type) +httpd_response_set_content_type(httpd_conn_t * res, const char *content_type) { - strncpy (res->content_type, content_type, 25); + strncpy(res->content_type, content_type, 25); } @@ -271,7 +271,7 @@ httpd_response_set_content_type (httpd_conn_t * res, const char *content_type) * ----------------------------------------------------- */ herror_t -httpd_send_header (httpd_conn_t * res, int code, const char *text) +httpd_send_header(httpd_conn_t * res, int code, const char *text) { struct tm stm; time_t nw; @@ -281,14 +281,14 @@ httpd_send_header (httpd_conn_t * res, int code, const char *text) herror_t status; /* set status code */ - sprintf (header, "HTTP/1.1 %d %s\r\n", code, text); + sprintf(header, "HTTP/1.1 %d %s\r\n", code, text); /* set date */ - nw = time (NULL); - localtime_r (&nw, &stm); - strftime (buffer, 255, "Date: %a, %d %b %Y %T GMT", &stm); - strcat (header, buffer); - strcat (header, "\r\n"); + nw = time(NULL); + localtime_r(&nw, &stm); + strftime(buffer, 255, "Date: %a, %d %b %Y %T GMT", &stm); + strcat(header, buffer); + strcat(header, "\r\n"); /* set content-type */ /* @@ -298,7 +298,7 @@ httpd_send_header (httpd_conn_t * res, int code, const char *text) */ /* set server name */ - strcat (header, "Server: Nano HTTPD library\r\n"); + strcat(header, "Server: Nano HTTPD library\r\n"); /* set _httpd_connection status */ // strcat (header, "Connection: close\r\n"); @@ -307,40 +307,40 @@ httpd_send_header (httpd_conn_t * res, int code, const char *text) cur = res->header; while (cur != NULL) { - sprintf (buffer, "%s: %s\r\n", cur->key, cur->value); - strcat (header, buffer); + sprintf(buffer, "%s: %s\r\n", cur->key, cur->value); + strcat(header, buffer); cur = cur->next; } /* set end of header */ - strcat (header, "\r\n"); + strcat(header, "\r\n"); /* send header */ - status = hsocket_nsend (res->sock, header, strlen (header)); + status = hsocket_nsend(res->sock, header, strlen(header)); if (status != H_OK) return status; - res->out = http_output_stream_new (res->sock, res->header); + res->out = http_output_stream_new(res->sock, res->header); return H_OK; } int -httpd_send_internal_error (httpd_conn_t * conn, const char *errmsg) +httpd_send_internal_error(httpd_conn_t * conn, const char *errmsg) { const char *template1 = "<html><body><h3>Error!</h3><hr> Message: '%s' </body></html>\r\n"; char buffer[4064]; char buflen[5]; - sprintf (buffer, template1, errmsg); + sprintf(buffer, template1, errmsg); #ifdef WIN32 #define snprintf(buffer, num, s1, s2) sprintf(buffer, s1,s2) #endif - 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)); + 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)); } /* @@ -349,40 +349,40 @@ httpd_send_internal_error (httpd_conn_t * conn, const char *errmsg) * ----------------------------------------------------- */ static void -httpd_request_print (hrequest_t * req) +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 (" Path : '%s'", req->path); - log_verbose2 (" Spec : '%s'", - (req->version == HTTP_1_0) ? "HTTP/1.0" : "HTTP/1.1"); - log_verbose1 (" Parsed query string :"); + log_verbose1("++++++ Request +++++++++"); + 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_verbose1(" Parsed query string :"); pair = req->query; while (pair != NULL) { - log_verbose3 (" %s = '%s'", pair->key, pair->value); + log_verbose3(" %s = '%s'", pair->key, pair->value); pair = pair->next; } - log_verbose1 (" Parsed header :"); + log_verbose1(" Parsed header :"); pair = req->header; while (pair != NULL) { - log_verbose3 (" %s = '%s'", pair->key, pair->value); + log_verbose3(" %s = '%s'", pair->key, pair->value); pair = pair->next; } - log_verbose1 ("++++++++++++++++++++++++"); + log_verbose1("++++++++++++++++++++++++"); } httpd_conn_t * -httpd_new (hsocket_t sock) +httpd_new(hsocket_t sock) { - httpd_conn_t *conn = (httpd_conn_t *) malloc (sizeof (httpd_conn_t)); + httpd_conn_t *conn = (httpd_conn_t *) malloc(sizeof(httpd_conn_t)); conn->sock = sock; conn->out = NULL; conn->content_type[0] = '\0'; @@ -393,19 +393,19 @@ httpd_new (hsocket_t sock) void -httpd_free (httpd_conn_t * conn) +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) +do_req_timeout(int signum) { /* struct sigaction req_timeout; @@ -415,11 +415,11 @@ do_req_timeout (int signum) */ // XXX this is not real pretty, is there a better way? - log_verbose1 ("Thread timeout."); + log_verbose1("Thread timeout."); #ifdef WIN32 - _endthread (); + _endthread(); #else - pthread_exit (0); + pthread_exit(0); #endif } @@ -430,15 +430,15 @@ do_req_timeout (int signum) */ #ifdef WIN32 static unsigned _stdcall -httpd_session_main (void *data) +httpd_session_main(void *data) #else static void * -httpd_session_main (void *data) +httpd_session_main(void *data) #endif { conndata_t *conn = (conndata_t *) data; const char *msg = "SESSION 1.0\n"; - int len = strlen (msg); + int len = strlen(msg); int done = 0; char buffer[256]; /* temp buffer for recv() */ char header[4064]; /* received header */ @@ -450,54 +450,54 @@ httpd_session_main (void *data) header[0] = '\0'; len = 0; - log_verbose1 ("starting httpd_session_main()"); + log_verbose1("starting httpd_session_main()"); #ifdef HAVE_SSL if (!SSLctx) { - log_verbose1 ("Using HTTP"); + 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); + 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"); + return herror_new("hsocket_accept", SSL_ERROR_INIT, + "Unable to initialize SSL"); } } #endif - conn->atime = time ((time_t) 0); + conn->atime = time((time_t) 0); /* call the service */ /* req = hrequest_new_from_buffer (header);*/ do { - log_verbose1 ("starting HTTP request"); - rconn = httpd_new (conn->sock); + 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 (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); + httpd_send_internal_error(rconn, herror_message(status) /* "Request + parse + error!" */ ); + herror_release(status); } done = 1; } else { char *conn_str = - hpairnode_get_ignore_case (req->header, HEADER_CONNECTION); + 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) + if (conn_str && strncasecmp(conn_str, "close", 5) == 0) { done = 1; } @@ -505,31 +505,31 @@ httpd_session_main (void *data) { done = req->version == HTTP_1_0 ? 1 : 0; } - httpd_request_print (req); + httpd_request_print(req); - service = httpd_find_service (req->path); + service = httpd_find_service(req->path); if (service != NULL) { - log_verbose2 ("service '%s' found", req->path); + log_verbose2("service '%s' found", req->path); if (service->func != NULL) { - service->func (rconn, req); + 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); + sprintf(buffer, + "service '%s' not registered properly (func == NULL)", + req->path); + log_verbose1(buffer); + httpd_send_internal_error(rconn, buffer); } } else { - sprintf (buffer, "service '%s' not found", req->path); - log_verbose1 (buffer); - httpd_send_internal_error (rconn, buffer); + sprintf(buffer, "service '%s' not found", req->path); + log_verbose1(buffer); + httpd_send_internal_error(rconn, buffer); } /* httpd_response_free(res); */ @@ -538,29 +538,29 @@ httpd_session_main (void *data) } 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 (); + CloseHandle((HANDLE) conn->tid); + _endthread(); return 0; #else - pthread_exit (NULL); + pthread_exit(NULL); return service; #endif } int -httpd_set_header (httpd_conn_t * conn, const char *key, const char *value) +httpd_set_header(httpd_conn_t * conn, const char *key, const char *value) { hpair_t *p; if (conn == NULL) { - log_warn1 ("Connection object is NULL"); + log_warn1("Connection object is NULL"); return 0; } p = conn->header; @@ -568,28 +568,28 @@ httpd_set_header (httpd_conn_t * conn, const char *key, const char *value) { if (p->key != NULL) { - if (!strcmp (p->key, key)) + if (!strcmp(p->key, key)) { - free (p->value); - p->value = (char *) malloc (strlen (value) + 1); - strcpy (p->value, value); + 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); + conn->header = hpairnode_new(key, value, conn->header); return 0; } void -httpd_set_headers (httpd_conn_t * conn, hpair_t * header) +httpd_set_headers(httpd_conn_t * conn, hpair_t * header) { while (header) { - httpd_set_header (conn, header->key, header->value); + httpd_set_header(conn, header->key, header->value); header = header->next; } } @@ -601,7 +601,7 @@ httpd_set_headers (httpd_conn_t * conn, hpair_t * header) */ #ifdef WIN32 BOOL WINAPI -httpd_term (DWORD sig) +httpd_term(DWORD sig) { // log_debug2 ("Got signal %d", sig); if (sig == _httpd_terminate_signal) @@ -612,9 +612,9 @@ httpd_term (DWORD sig) #else void -httpd_term (int sig) +httpd_term(int sig) { - log_debug2 ("Got signal %d", sig); + log_debug2("Got signal %d", sig); if (sig == _httpd_terminate_signal) _httpd_run = 0; } @@ -628,18 +628,18 @@ httpd_term (int sig) * ----------------------------------------------------- */ static void -_httpd_register_signal_handler () +_httpd_register_signal_handler() { - log_verbose2 ("registering termination signal handler (SIGNAL:%d)", - _httpd_terminate_signal); + log_verbose2("registering termination signal handler (SIGNAL:%d)", + _httpd_terminate_signal); #ifdef WIN32 - if (SetConsoleCtrlHandler ((PHANDLER_ROUTINE) httpd_term, TRUE) == FALSE) + if (SetConsoleCtrlHandler((PHANDLER_ROUTINE) httpd_term, TRUE) == FALSE) { - log_error1 ("Unable to install console event handler!"); + log_error1("Unable to install console event handler!"); } #else - signal (_httpd_terminate_signal, httpd_term); + signal(_httpd_terminate_signal, httpd_term); #endif } @@ -650,7 +650,7 @@ _httpd_register_signal_handler () FUNCTION: _httpd_wait_for_empty_conn ----------------------------------------------------*/ static conndata_t * -_httpd_wait_for_empty_conn () +_httpd_wait_for_empty_conn() { int i; for (i = 0;; i++) @@ -660,7 +660,7 @@ _httpd_wait_for_empty_conn () if (i >= _httpd_max_connections) { - system_sleep (1); + system_sleep(1); i = 0; } else if (_httpd_connection[i].sock.sock == 0) @@ -678,24 +678,23 @@ _httpd_wait_for_empty_conn () * ----------------------------------------------------- */ static void -_httpd_start_thread (conndata_t * conn) +_httpd_start_thread(conndata_t * conn) { int err; #ifdef WIN32 conn->tid = - (HANDLE) _beginthreadex (NULL, 65535, httpd_session_main, conn, 0, &err); + (HANDLE) _beginthreadex(NULL, 65535, httpd_session_main, conn, 0, &err); #else - pthread_attr_init (&(conn->attr)); + pthread_attr_init(&(conn->attr)); #ifdef PTHREAD_CREATE_DETACHED - pthread_attr_setdetachstate (&(conn->attr), PTHREAD_CREATE_DETACHED); + pthread_attr_setdetachstate(&(conn->attr), PTHREAD_CREATE_DETACHED); #endif - pthread_sigmask (SIG_BLOCK, &thrsigset, NULL); - err = pthread_create (&(conn->tid), &(conn->attr), httpd_session_main, - conn); + pthread_sigmask(SIG_BLOCK, &thrsigset, NULL); + err = pthread_create(&(conn->tid), &(conn->attr), httpd_session_main, conn); if (err) { - log_error2 ("Error creating thread: ('%d')", err); + log_error2("Error creating thread: ('%d')", err); } #endif } @@ -709,7 +708,7 @@ _httpd_start_thread (conndata_t * conn) */ herror_t -httpd_run () +httpd_run() { herror_t err; conndata_t *conn; @@ -717,34 +716,34 @@ httpd_run () struct timeval timeout; - log_verbose1 ("starting run routine"); + log_verbose1("starting run routine"); timeout.tv_sec = 1; timeout.tv_usec = 0; #ifdef WIN32 #else - sigemptyset (&thrsigset); - sigaddset (&thrsigset, SIGALRM); + sigemptyset(&thrsigset); + sigaddset(&thrsigset, SIGALRM); #endif /* listen to port */ - err = hsocket_listen (_httpd_socket); + 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); + log_verbose2("listening to port '%d'", _httpd_port); /* register signal handler */ - _httpd_register_signal_handler (); + _httpd_register_signal_handler(); /* make the socket non blocking */ - err = hsocket_block (_httpd_socket, 0); + 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; } @@ -752,7 +751,7 @@ httpd_run () while (_httpd_run) { /* Get an empty connection struct */ - conn = _httpd_wait_for_empty_conn (); + conn = _httpd_wait_for_empty_conn(); if (!_httpd_run) break; @@ -766,11 +765,11 @@ httpd_run () timeout.tv_usec = 0; /* zero and set file descriptior */ - FD_ZERO (&fds); - FD_SET (_httpd_socket.sock, &fds); + FD_ZERO(&fds); + FD_SET(_httpd_socket.sock, &fds); /* select socket descriptor */ - switch (select (_httpd_socket.sock + 1, &fds, NULL, NULL, &timeout)) + switch (select(_httpd_socket.sock + 1, &fds, NULL, NULL, &timeout)) { case 0: /* descriptor is not ready */ @@ -782,7 +781,7 @@ httpd_run () /* no nothing */ break; } - if (FD_ISSET (_httpd_socket.sock, &fds)) + if (FD_ISSET(_httpd_socket.sock, &fds)) { break; } @@ -793,49 +792,49 @@ httpd_run () break; /* Accept a socket */ - err = hsocket_accept (_httpd_socket, &(conn->sock)); - if (err != H_OK && herror_code (err) == SSL_ERROR_INIT) + err = hsocket_accept(_httpd_socket, &(conn->sock)); + if (err != H_OK && herror_code(err) == SSL_ERROR_INIT) { - hsocket_close (conn->sock); + 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) { - log_error2 ("Can not accept socket: %s", herror_message (err)); + log_error2("Can not accept socket: %s", herror_message(err)); return err; /* this is hard core! */ } /* Now start a thread */ - _httpd_start_thread (conn); + _httpd_start_thread(conn); } - free (_httpd_connection); + free(_httpd_connection); return 0; } void -httpd_destroy () +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) +WSAReaper(void *x) { short int connections; short int i; @@ -846,34 +845,32 @@ WSAReaper (void *x) for (;;) { connections = 0; - ctime = time ((time_t) 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); + 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) + 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); + 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])); + TerminateThread(_httpd_connection[i].tid, (DWORD) & rc); + CloseHandle(_httpd_connection[i].tid); + memset((char *) &_httpd_connection[i], 0, sizeof(_httpd_connection[i])); } - Sleep (100); + Sleep(100); } return; } @@ -881,8 +878,8 @@ WSAReaper (void *x) #endif unsigned char * -httpd_get_postdata (httpd_conn_t * conn, hrequest_t * req, long *received, - long max) +httpd_get_postdata(httpd_conn_t * conn, hrequest_t * req, long *received, + long max) { char *content_length_str; long content_length = 0; @@ -892,15 +889,15 @@ httpd_get_postdata (httpd_conn_t * conn, hrequest_t * req, long *received, { content_length_str = - hpairnode_get_ignore_case (req->header, HEADER_CONTENT_LENGTH); + hpairnode_get_ignore_case(req->header, HEADER_CONTENT_LENGTH); if (content_length_str != NULL) - content_length = atol (content_length_str); + content_length = atol(content_length_str); } else { - log_warn1 ("Not a POST method"); + log_warn1("Not a POST method"); return NULL; } @@ -910,23 +907,23 @@ httpd_get_postdata (httpd_conn_t * conn, hrequest_t * req, long *received, if (content_length == 0) { *received = 0; - postdata = (char *) malloc (1); + postdata = (char *) malloc(1); postdata[0] = '\0'; return postdata; } - postdata = (unsigned char *) malloc (content_length + 1); + postdata = (unsigned char *) malloc(content_length + 1); if (postdata == NULL) { - log_error1 ("Not enough memory"); + 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'; return postdata; } - free (postdata); + free(postdata); return NULL; } @@ -938,10 +935,10 @@ httpd_get_postdata (httpd_conn_t * conn, hrequest_t * req, long *received, */ static void -_httpd_mime_get_boundary (httpd_conn_t * conn, char *dest) +_httpd_mime_get_boundary(httpd_conn_t * conn, char *dest) { - sprintf (dest, "---=.Part_NH_%p", conn); - log_verbose2 ("boundary= \"%s\"", dest); + sprintf(dest, "---=.Part_NH_%p", conn); + log_verbose2("boundary= \"%s\"", dest); } @@ -950,10 +947,10 @@ _httpd_mime_get_boundary (httpd_conn_t * conn, char *dest) Returns: H_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) +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) { char buffer[300]; char temp[250]; @@ -968,33 +965,33 @@ httpd_mime_send_header (httpd_conn_t * conn, using sprintf instead of snprintf because visual c does not support snprintf */ - sprintf (buffer, "multipart/related;"); + sprintf(buffer, "multipart/related;"); if (related_type) { - snprintf (temp, 75, " type=\"%s\";", related_type); - strcat (buffer, temp); + snprintf(temp, 75, " type=\"%s\";", related_type); + strcat(buffer, temp); } if (related_start) { - snprintf (temp, 250, " start=\"%s\";", related_start); - strcat (buffer, temp); + snprintf(temp, 250, " start=\"%s\";", related_start); + strcat(buffer, temp); } if (related_start_info) { - snprintf (temp, 250, " start-info=\"%s\";", related_start_info); - strcat (buffer, temp); + snprintf(temp, 250, " start-info=\"%s\";", related_start_info); + strcat(buffer, temp); } - _httpd_mime_get_boundary (conn, boundary); - snprintf (temp, 250, " boundary=\"%s\"", boundary); - strcat (buffer, temp); + _httpd_mime_get_boundary(conn, boundary); + snprintf(temp, 250, " boundary=\"%s\"", boundary); + strcat(buffer, temp); - httpd_set_header (conn, HEADER_CONTENT_TYPE, buffer); + httpd_set_header(conn, HEADER_CONTENT_TYPE, buffer); - return httpd_send_header (conn, code, text); + return httpd_send_header(conn, code, text); } @@ -1003,37 +1000,35 @@ httpd_mime_send_header (httpd_conn_t * conn, with next part */ herror_t -httpd_mime_next (httpd_conn_t * conn, - const char *content_id, - const char *content_type, const char *transfer_encoding) +httpd_mime_next(httpd_conn_t * conn, + const char *content_id, + const char *content_type, const char *transfer_encoding) { herror_t status; char buffer[512]; char boundary[75]; /* Get the boundary string */ - _httpd_mime_get_boundary (conn, boundary); - sprintf (buffer, "\r\n--%s\r\n", boundary); + _httpd_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; /* Send Content header */ - sprintf (buffer, "%s: %s\r\n%s: %s\r\n%s: %s\r\n\r\n", - HEADER_CONTENT_TYPE, content_type ? content_type : "text/plain", - HEADER_CONTENT_TRANSFER_ENCODING, - transfer_encoding ? transfer_encoding : "binary", - HEADER_CONTENT_ID, - content_id ? content_id : "<content-id-not-set>"); + sprintf(buffer, "%s: %s\r\n%s: %s\r\n%s: %s\r\n\r\n", + HEADER_CONTENT_TYPE, content_type ? content_type : "text/plain", + HEADER_CONTENT_TRANSFER_ENCODING, + transfer_encoding ? transfer_encoding : "binary", + HEADER_CONTENT_ID, + content_id ? content_id : "<content-id-not-set>"); - 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; } @@ -1043,47 +1038,46 @@ httpd_mime_next (httpd_conn_t * conn, 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) +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 status; - FILE *fd = fopen (filename, "rb"); + FILE *fd = fopen(filename, "rb"); byte_t buffer[MAX_FILE_BUFFER_SIZE]; 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); + status = httpd_mime_next(conn, content_id, content_type, transfer_encoding); if (status != H_OK) { - fclose (fd); + fclose(fd); return status; } - while (!feof (fd)) + while (!feof(fd)) { - size = fread (buffer, 1, MAX_FILE_BUFFER_SIZE, fd); + size = fread(buffer, 1, MAX_FILE_BUFFER_SIZE, fd); if (size == -1) { - fclose (fd); - return herror_new ("httpd_mime_send_file", FILE_ERROR_READ, - "Can not read from file '%d'", filename); + fclose(fd); + 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); + status = http_output_stream_write(conn->out, buffer, size); if (status != H_OK) { - fclose (fd); + fclose(fd); return status; } } - fclose (fd); + fclose(fd); return H_OK; } @@ -1092,26 +1086,25 @@ httpd_mime_send_file (httpd_conn_t * conn, Returns: H_OK or error flag */ herror_t -httpd_mime_end (httpd_conn_t * conn) +httpd_mime_end(httpd_conn_t * conn) { herror_t status; char buffer[512]; char boundary[75]; /* Get the boundary string */ - _httpd_mime_get_boundary (conn, boundary); - sprintf (buffer, "\r\n--%s--\r\n\r\n", boundary); + _httpd_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); return status; } diff --git a/nanohttp/nanohttp-server.h b/nanohttp/nanohttp-server.h index 101a4b7..383b7be 100644 --- a/nanohttp/nanohttp-server.h +++ b/nanohttp/nanohttp-server.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-server.h,v 1.10 2006/01/10 11:21:55 snowdrop Exp $ + * $Id: nanohttp-server.h,v 1.11 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -62,18 +62,17 @@ typedef struct tag_hservice /* 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 +89,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 ee69561..018f42d 100644 --- a/nanohttp/nanohttp-socket.c +++ b/nanohttp/nanohttp-socket.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-socket.c,v 1.42 2006/01/10 11:21:55 snowdrop Exp $ +* $Id: nanohttp-socket.c,v 1.43 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -91,11 +91,11 @@ NOTE: This will be called from httpd_init() for server and from httpc_init() for client ----------------------------------------------------*/ herror_t -hsocket_module_init () +hsocket_module_init() { #ifdef WIN32 struct WSAData info; - WSAStartup (MAKEWORD (2, 2), &info); + WSAStartup(MAKEWORD(2, 2), &info); #else /* */ /* nothing to init for unix sockets */ @@ -107,10 +107,10 @@ hsocket_module_init () FUNCTION: hsocket_module_destroy ----------------------------------------------------*/ void -hsocket_module_destroy () +hsocket_module_destroy() { #ifdef WIN32 - WSACleanup (); + WSACleanup(); #else /* */ /* nothing to destroy for unix sockets */ @@ -121,21 +121,21 @@ 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); + 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"); + return herror_new("hsocket_init", HSOCKET_ERROR_CONNECT, + "Unable to initialize SSL CTX"); } } #endif @@ -146,7 +146,7 @@ hsocket_init (hsocket_t * sock) FUNCTION: hsocket_free ----------------------------------------------------*/ void -hsocket_free (hsocket_t sock) +hsocket_free(hsocket_t sock) { /* nothing to free for unix sockets */ } @@ -155,49 +155,48 @@ hsocket_free (hsocket_t sock) FUNCTION: hsocket_open ----------------------------------------------------*/ herror_t -hsocket_open (hsocket_t * dsock, const char *hostname, int port) +hsocket_open(hsocket_t * dsock, const char *hostname, int port) { hsocket_t sock; char *ip; struct sockaddr_in address; struct hostent *host; - sock.sock = socket (AF_INET, SOCK_STREAM, 0); + 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); + 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); + 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) { #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); + log_verbose1("Using HTTPS"); + dsock->ssl = init_ssl(SSLctx, sock.sock, SSL_CLIENT); } #endif return H_OK; @@ -207,35 +206,34 @@ hsocket_open (hsocket_t * dsock, const char *hostname, int port) FUNCTION: hsocket_bind ----------------------------------------------------*/ herror_t -hsocket_bind (hsocket_t * dsock, int port) +hsocket_bind(hsocket_t * dsock, int port) { hsocket_t sock; struct sockaddr_in addr; int opt = 1; /* create socket */ - sock.sock = socket (AF_INET, SOCK_STREAM, 0); + 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); + log_error3("Can not create socket: '%s'", "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)) == + 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); + log_error3("Can not bind: '%s'", "Socket error: %d", errno); + return herror_new("hsocket_bind", HSOCKET_ERROR_BIND, "Socket error: %d", + errno); } dsock->sock = sock.sock; return H_OK; @@ -245,26 +243,26 @@ hsocket_bind (hsocket_t * dsock, int port) FUNCTION: hsocket_accept ----------------------------------------------------------*/ herror_t -hsocket_accept (hsocket_t sock, hsocket_t * dest) +hsocket_accept(hsocket_t sock, hsocket_t * dest) { socklen_t asize; hsocket_t sockfd; 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); + asize = sizeof(struct sockaddr_in); #ifdef WIN32 while (1) { - sockfd.sock = accept (sock.sock, (struct sockaddr *) &addr, &asize); + sockfd.sock = accept(sock.sock, (struct sockaddr *) &addr, &asize); if (sockfd.sock == INVALID_SOCKET) { - if (WSAGetLastError () != WSAEWOULDBLOCK) - return herror_new ("hsocket_accept", HSOCKET_ERROR_ACCEPT, - "Socket error: %d", errno); + if (WSAGetLastError() != WSAEWOULDBLOCK) + return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT, + "Socket error: %d", errno); } else { @@ -273,17 +271,17 @@ hsocket_accept (hsocket_t sock, hsocket_t * dest) } #else /* TODO (#1#): why not a loop like in win32? */ - sockfd.sock = accept (sock.sock, (struct sockaddr *) &addr, &asize); + 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); + return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT, + "Socket error: %d", errno); } #endif /* TODO (#1#): Write to access.log file */ - log_verbose3 ("accept new socket (%d) from '%s'", sockfd.sock, - SAVE_STR (((char *) inet_ntoa (addr.sin_addr)))); + log_verbose3("accept new socket (%d) from '%s'", sockfd.sock, + SAVE_STR(((char *) inet_ntoa(addr.sin_addr)))); dest->sock = sockfd.sock; return H_OK; @@ -293,17 +291,17 @@ hsocket_accept (hsocket_t sock, hsocket_t * dest) FUNCTION: hsocket_listen ----------------------------------------------------*/ herror_t -hsocket_listen (hsocket_t sock) +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) + 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); + log_error3("Can not listen: '%s'", "Socket error: %d", errno); + return herror_new("hsocket_listen", HSOCKET_ERROR_LISTEN, + "Socket error: %d", errno); } return H_OK; @@ -311,7 +309,7 @@ hsocket_listen (hsocket_t sock) #if 0 static void -_hsocket_wait_until_receive (hsocket_t sock) +_hsocket_wait_until_receive(hsocket_t sock) { fd_set fds; struct timeval timeout; @@ -322,13 +320,13 @@ _hsocket_wait_until_receive (hsocket_t sock) while (1) { - log_verbose1 ("waiting until receive mode"); + log_verbose1("waiting until receive mode"); /* zero and set file descriptior */ - FD_ZERO (&fds); - FD_SET (sock, &fds); + 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 */ @@ -340,7 +338,7 @@ _hsocket_wait_until_receive (hsocket_t sock) /* no nothing */ break; } - if (FD_ISSET (sock, &fds)) + if (FD_ISSET(sock, &fds)) { break; } @@ -352,11 +350,11 @@ _hsocket_wait_until_receive (hsocket_t sock) FUNCTION: hsocket_close ----------------------------------------------------*/ void -hsocket_close (hsocket_t sock) +hsocket_close(hsocket_t sock) { char junk[10]; /* _hsocket_wait_until_receive(sock);*/ - log_verbose2 ("closing socket %d...", sock.sock); + log_verbose2("closing socket %d...", sock.sock); /* struct linger _linger; hsocket_block(sock,1); @@ -370,51 +368,51 @@ hsocket_close (hsocket_t sock) /* shutdown(sock,SD_RECEIVE); */ - shutdown (sock.sock, SD_SEND); - while (recv (sock.sock, junk, sizeof (junk), 0) > 0) + shutdown(sock.sock, SD_SEND); + while (recv(sock.sock, junk, sizeof(junk), 0) > 0) { }; - closesocket (sock.sock); + 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) + 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); + close(sock.sock); } else { #endif - shutdown (sock.sock, SHUT_RDWR); - while (recv (sock.sock, junk, sizeof (junk), 0) > 0) + shutdown(sock.sock, SHUT_RDWR); + while (recv(sock.sock, junk, sizeof(junk), 0) > 0) { }; - close (sock.sock); + close(sock.sock); #ifdef HAVE_SSL } #endif #endif - log_verbose1 ("socket closed"); + log_verbose1("socket closed"); } #if 0 static int -_test_send_to_file (const char *filename, const byte_t * bytes, int n) +_test_send_to_file(const char *filename, const byte_t * bytes, int n) { int size; - FILE *f = fopen (filename, "ab"); + FILE *f = fopen(filename, "ab"); if (!f) - f = fopen (filename, "wb"); - size = fwrite (bytes, 1, n, f); - fclose (f); + f = fopen(filename, "wb"); + size = fwrite(bytes, 1, n, f); + fclose(f); return size; } #endif @@ -422,19 +420,19 @@ _test_send_to_file (const char *filename, const byte_t * bytes, int n) 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; - 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 ); @@ -444,36 +442,36 @@ hsocket_nsend (hsocket_t sock, const byte_t * bytes, int n) #ifdef HAVE_SSL if (sock.ssl) { - size = SSL_write (sock.ssl, bytes + total, n); + 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); + log_verbose2("Sent %d", size); /* size = _test_send_to_file(filename, bytes, n); */ #ifdef WIN32 if (size == INVALID_SOCKET) - if (WSAGetLastError () == WSAEWOULDBLOCK) + if (WSAGetLastError() == WSAEWOULDBLOCK) continue; else - return herror_new ("hsocket_nsend", HSOCKET_ERROR_SEND, - "Socket error: %d", errno); + return herror_new("hsocket_nsend", HSOCKET_ERROR_SEND, + "Socket error: %d", errno); #else if (size == -1) { #ifdef HAVE_SSL if (sock.ssl) { - log_error1 ("Send error"); - log_ssl_error (sock.ssl, size); + 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; @@ -488,17 +486,17 @@ hsocket_nsend (hsocket_t sock, const byte_t * bytes, int n) FUNCTION: hsocket_send ----------------------------------------------------*/ herror_t -hsocket_send (hsocket_t sock, const char *str) +hsocket_send(hsocket_t sock, const char *str) { - return hsocket_nsend (sock, str, strlen (str)); + return hsocket_nsend(sock, str, strlen(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; @@ -517,41 +515,41 @@ hsocket_read (hsocket_t sock, byte_t * buffer, int total, int force, struct timeval timeout; int i = 0; fd_set fds; - FD_ZERO (&fds); - FD_SET (sock.sock, &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); + 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); + status = SSL_read(sock.ssl, &buffer[totalRead], total - totalRead); if (status < 1) { - int ret = select (sock.sock + 1, &fds, NULL, NULL, &timeout); + 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); + 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"); + 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); + 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)); @@ -559,7 +557,7 @@ hsocket_read (hsocket_t sock, byte_t * buffer, int total, int force, // } 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 @@ -567,21 +565,21 @@ hsocket_read (hsocket_t sock, byte_t * buffer, int total, int force, #else // HAVE_SSL { #endif // HAVE_SSL - status = recv (sock.sock, &buffer[totalRead], total - totalRead, 0); + status = recv(sock.sock, &buffer[totalRead], total - totalRead, 0); #ifdef WIN32 if (status == INVALID_SOCKET) { - wsa_error = WSAGetLastError (); + 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); + log_error2("WSAGetLastError()=%d", wsa_error); + return herror_new("hsocket_read", HSOCKET_ERROR_RECEIVE, + "Socket error: %d", errno); } } } @@ -601,20 +599,20 @@ hsocket_read (hsocket_t sock, byte_t * buffer, int total, int force, { // 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) + 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"); + 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"); + 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); + return herror_new("hsocket_read", HSOCKET_ERROR_RECEIVE, + "Socket error: %d", errno); #endif if (!force) @@ -643,15 +641,15 @@ hsocket_read (hsocket_t sock, byte_t * buffer, int total, int force, 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 @@ -659,13 +657,13 @@ hsocket_block (hsocket_t sock, int block) */ iMode = (block == 0) ? 1 : 0; /* Non block mode */ - if (ioctlsocket (sock.sock, FIONBIO, (u_long FAR *) & iMode) == + if (ioctlsocket(sock.sock, FIONBIO, (u_long FAR *) & iMode) == INVALID_SOCKET) { - int err = WSAGetLastError (); - log_error2 ("ioctlsocket error %d", err); - return herror_new ("hsocket_block", HSOCKET_ERROR_IOCTL, - "Socket error: %d", err); + int err = WSAGetLastError(); + log_error2("ioctlsocket 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 */ diff --git a/nanohttp/nanohttp-socket.h b/nanohttp/nanohttp-socket.h index 68f6c5b..b75a637 100644 --- a/nanohttp/nanohttp-socket.h +++ b/nanohttp/nanohttp-socket.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-socket.h,v 1.19 2006/01/10 11:21:55 snowdrop Exp $ + * $Id: nanohttp-socket.h,v 1.20 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -64,14 +64,14 @@ typedef struct hsocket_t @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(); /** @@ -82,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); /** @@ -90,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); /** @@ -106,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); /** @@ -114,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); /** @@ -130,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); /** @@ -144,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); /** @@ -158,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); /** @@ -172,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); /** @@ -185,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); /** @@ -204,8 +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. @@ -216,7 +216,7 @@ herror_t hsocket_read (hsocket_t sock, byte_t * buffer, int size, int force, <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 38b3595..e1195c7 100644 --- a/nanohttp/nanohttp-ssl.c +++ b/nanohttp/nanohttp-ssl.c @@ -70,53 +70,53 @@ static char *pass; */ void -superseed () +superseed() { int buf[256], i; - srand (time (NULL)); + srand(time(NULL)); for (i = 0; i < 256; i++) { - buf[i] = rand (); + buf[i] = rand(); } - RAND_seed ((unsigned char *) buf, sizeof (buf)); + RAND_seed((unsigned char *) buf, sizeof(buf)); } static int -pw_cb (char *buf, int num, int rwflag, void *userdata) +pw_cb(char *buf, int num, int rwflag, void *userdata) { - if (num < (int) strlen (pass) + 1) + 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) +verify_sn(X509 * cert, int who, int nid, char *str) { char name[256]; char buf[256]; - memset (name, '\0', 256); - memset (buf, '\0', 256); + memset(name, '\0', 256); + memset(buf, '\0', 256); if (who == CERT_SUBJECT) { - X509_NAME_oneline (X509_get_subject_name (cert), name, 256); + X509_NAME_oneline(X509_get_subject_name(cert), name, 256); } else { - X509_NAME_oneline (X509_get_issuer_name (cert), name, 256); + X509_NAME_oneline(X509_get_issuer_name(cert), name, 256); } buf[0] = '/'; - strcat (buf, OBJ_nid2sn (nid)); - strcat (buf, "="); - strcat (buf, str); + strcat(buf, OBJ_nid2sn(nid)); + strcat(buf, "="); + strcat(buf, str); - if (strstr (name, buf)) + if (strstr(name, buf)) { return 1; } @@ -127,11 +127,11 @@ verify_sn (X509 * cert, int who, int nid, char *str) } static int -verify_cb (int prev_ok, X509_STORE_CTX * ctx) +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"); @@ -142,12 +142,12 @@ verify_cb (int prev_ok, X509_STORE_CTX * ctx) unresolved reference while compiling */ if (depth == 0) { - return user_verify (cert); + return user_verify(cert); } else { #endif - log_verbose1 ("Cert ok (prev)"); + log_verbose1("Cert ok (prev)"); return prev_ok; #ifdef NOUSER_VERIFY } @@ -156,18 +156,18 @@ verify_cb (int prev_ok, X509_STORE_CTX * ctx) #ifdef NOUSER_VERIFY int -user_verify (X509 * cert) +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."); + log_verbose1("Validating certificate."); return 1; } #endif SSL_CTX * -initialize_ctx (char *keyfile, char *password, char *calist) +initialize_ctx(char *keyfile, char *password, char *calist) { SSL_CTX *ctx = NULL; @@ -176,139 +176,139 @@ initialize_ctx (char *keyfile, char *password, char *calist) /* Global system initialization */ - log_verbose1 ("Initializing library"); - SSL_library_init (); - SSL_load_error_strings (); - ERR_load_crypto_strings (); - OpenSSL_add_ssl_algorithms (); + 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 ()); + ctx = SSL_CTX_new(SSLv23_method()); if (ctx == NULL) { - log_error1 ("Cannot create SSL context"); + log_error1("Cannot create SSL context"); return NULL; } - log_verbose1 ("SSL context created ok"); + 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))) + if (!(SSL_CTX_use_certificate_file(ctx, keyfile, SSL_FILETYPE_PEM))) { - log_error2 ("Couldn't read certificate file: %s", keyfile); - SSL_CTX_free (ctx); + log_error2("Couldn't read certificate file: %s", keyfile); + SSL_CTX_free(ctx); return ctx = NULL; } - log_verbose1 ("Certificate file read ok"); + log_verbose1("Certificate file read ok"); pass = password; - SSL_CTX_set_default_passwd_cb (ctx, pw_cb); + SSL_CTX_set_default_passwd_cb(ctx, pw_cb); - if (!(SSL_CTX_use_PrivateKey_file (ctx, keyfile, SSL_FILETYPE_PEM))) + if (!(SSL_CTX_use_PrivateKey_file(ctx, keyfile, SSL_FILETYPE_PEM))) { - log_error2 ("Couldn't read key file: %s", keyfile); - SSL_CTX_free (ctx); + log_error2("Couldn't read key file: %s", keyfile); + SSL_CTX_free(ctx); return ctx = NULL; } - log_verbose1 ("Keyfile read ok"); + log_verbose1("Keyfile read ok"); } if (calist != NULL) { /* Load the CAs we trust */ - if (!(SSL_CTX_load_verify_locations (ctx, calist, NULL))) + if (!(SSL_CTX_load_verify_locations(ctx, calist, NULL))) { - log_error2 ("Couldn't read CA list: %s", calist); - SSL_CTX_free (ctx); + 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 (); + superseed(); return ctx; } void -log_ssl_error (SSL * ssl, int ret) +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"); + log_error1("SSL handshake was not successful, contolled shutdown"); } else if (ret == -1) { - log_error1 ("SSL handshake was not successful, fatal error at protocol"); + log_error1("SSL handshake was not successful, fatal error at protocol"); } - errqueue = SSL_get_error (ssl, ret); + errqueue = SSL_get_error(ssl, ret); switch (errqueue) { case SSL_ERROR_NONE: - strcat (errorbuf, "None"); + strcat(errorbuf, "None"); break; case SSL_ERROR_ZERO_RETURN: - strcat (errorbuf, "Zero return"); + strcat(errorbuf, "Zero return"); break; case SSL_ERROR_WANT_READ: - strcat (errorbuf, "Want read"); + strcat(errorbuf, "Want read"); break; case SSL_ERROR_WANT_WRITE: - strcat (errorbuf, "Want write"); + strcat(errorbuf, "Want write"); break; case SSL_ERROR_WANT_X509_LOOKUP: - strcat (errorbuf, "Want x509 lookup"); + strcat(errorbuf, "Want x509 lookup"); break; case SSL_ERROR_SYSCALL: - strcat (errorbuf, "Syscall:"); + strcat(errorbuf, "Syscall:"); if (ret == 0) { - strcat (errorbuf, "Protocol violation"); + strcat(errorbuf, "Protocol violation"); } else if (ret == -1) { - strcat (errorbuf, "BIO reported an I/O error"); + strcat(errorbuf, "BIO reported an I/O error"); } else { - strcat (errorbuf, "Unknown syscall error"); + strcat(errorbuf, "Unknown syscall error"); } /* if */ break; case SSL_ERROR_SSL: - strcat (errorbuf, "SSL library"); - while (errqueue = ERR_get_error ()) + strcat(errorbuf, "SSL library"); + while (errqueue = ERR_get_error()) { - log_error2 ("SSL %s", ERR_error_string (errqueue, NULL)); + log_error2("SSL %s", ERR_error_string(errqueue, NULL)); } break; } /* switch code */ - log_error1 (errorbuf); + log_error1(errorbuf); } SSL * -init_ssl (SSL_CTX * ctx, int sock, int type) +init_ssl(SSL_CTX * ctx, int sock, int type) { int ret; int status; @@ -322,63 +322,63 @@ init_ssl (SSL_CTX * ctx, int sock, int type) #endif #endif - log_verbose1 ("Starting SSL Initialization"); + log_verbose1("Starting SSL Initialization"); - ssl = SSL_new (ctx); + ssl = SSL_new(ctx); if (ssl == NULL) { - log_error1 ("Cannot create new ssl object"); + 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); + log_error1("Setting up BIO with socket"); + rbio = BIO_new_socket(sock, BIO_NOCLOSE); if (rbio == NULL) { - log_error1 ("BIO_new_socket failed"); + log_error1("BIO_new_socket failed"); return NULL; } - SSL_set_bio (ssl, rbio, rbio); + SSL_set_bio(ssl, rbio, rbio); #else - sbio = BIO_new_socket (sock, BIO_NOCLOSE); + sbio = BIO_new_socket(sock, BIO_NOCLOSE); if (sbio == NULL) { - log_error1 ("BIO_new_socket failed"); + log_error1("BIO_new_socket failed"); return NULL; } - SSL_set_bio (ssl, sbio, sbio); + SSL_set_bio(ssl, sbio, sbio); #endif #endif - SSL_set_fd (ssl, sock); + 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); + 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); + log_error1("SSL accept error"); + log_ssl_error(ssl, ret); + SSL_free(ssl); return ssl = NULL; } /* if error */ } else { /* client */ - ret = SSL_connect (ssl); + ret = SSL_connect(ssl); if (ret <= 0) { - log_error1 ("SSL connect error"); - log_ssl_error (ssl, ret); - SSL_free (ssl); + 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. @@ -388,21 +388,21 @@ init_ssl (SSL_CTX * ctx, int sock, int type) did not verify"); SSL_free(ssl); return ssl = NULL; } */ } - log_verbose1 ("Completed SSL Initialization"); + log_verbose1("Completed SSL Initialization"); return ssl; } void -ssl_cleanup (SSL * ssl) +ssl_cleanup(SSL * ssl) { /* does nothing to context */ if (ssl != NULL) { - SSL_shutdown (ssl); + SSL_shutdown(ssl); // SSL_clear(ssl); - SSL_free (ssl); + SSL_free(ssl); ssl = NULL; } } diff --git a/nanohttp/nanohttp-ssl.h b/nanohttp/nanohttp-ssl.h index 449c78c..f4ec7ef 100644 --- a/nanohttp/nanohttp-ssl.h +++ b/nanohttp/nanohttp-ssl.h @@ -63,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 @@ -71,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 @@ -83,20 +83,20 @@ 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 diff --git a/nanohttp/nanohttp-stream.c b/nanohttp/nanohttp-stream.c index 7700867..c8ba216 100755 --- a/nanohttp/nanohttp-stream.c +++ b/nanohttp/nanohttp-stream.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-stream.c,v 1.8 2006/01/10 11:21:55 snowdrop Exp $ +* $Id: nanohttp-stream.c,v 1.9 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -30,7 +30,7 @@ #include <utils/alloc.h> #endif void -_log_str (char *fn, char *str, int size) +_log_str(char *fn, char *str, int size) { /* FILE *f = fopen(fn, "ab"); if (!f) f=fopen(fn,"wb"); @@ -49,19 +49,19 @@ HTTP INPUT STREAM */ static int -_http_stream_is_content_length (hpair_t * header) +_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) +_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)) + if (!strcmp(chunked, TRANSFER_ENCODING_CHUNKED)) { return 1; } @@ -74,7 +74,7 @@ _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_new(hsocket_t sock, hpair_t * header) { http_input_stream_t *result; char *content_length; @@ -82,26 +82,25 @@ http_input_stream_new (hsocket_t sock, hpair_t * header) /* Paranoya check */ /* 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; } /* 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 +108,7 @@ 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; @@ -123,20 +122,20 @@ http_input_stream_new (hsocket_t sock, hpair_t * header) and for debugging. */ http_input_stream_t * -http_input_stream_new_from_file (const char *filename) +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) 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; } @@ -144,47 +143,47 @@ http_input_stream_new_from_file (const char *filename) Free input stream */ void -http_input_stream_free (http_input_stream_t * stream) +http_input_stream_free(http_input_stream_t * stream) { if (stream->type == HTTP_TRANSFER_FILE && stream->fd) { - fclose (stream->fd); + fclose(stream->fd); if (stream->deleteOnExit) - log_info2 ("Removing '%s'", stream->filename); + log_info2("Removing '%s'", stream->filename); /* remove(stream->filename); */ } - free (stream); + free(stream); } static int -_http_input_stream_is_content_length_ready (http_input_stream_t * stream) +_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) +_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) +_http_input_stream_is_connection_closed_ready(http_input_stream_t * stream) { return !stream->connection_closed; } static int -_http_input_stream_is_file_ready (http_input_stream_t * stream) +_http_input_stream_is_file_ready(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) +_http_input_stream_content_length_read(http_input_stream_t * stream, + byte_t * dest, int size) { herror_t status; int read; @@ -194,7 +193,7 @@ _http_input_stream_content_length_read (http_input_stream_t * stream, size = stream->content_length - stream->received; /* read from socket */ - status = hsocket_read (stream->sock, dest, size, 1, &read); + status = hsocket_read(stream->sock, dest, size, 1, &read); if (status != H_OK) { stream->err = status; @@ -206,7 +205,7 @@ _http_input_stream_content_length_read (http_input_stream_t * stream, } static int -_http_input_stream_chunked_read_chunk_size (http_input_stream_t * stream) +_http_input_stream_chunked_read_chunk_size(http_input_stream_t * stream) { char chunk[25]; int status, i = 0; @@ -215,19 +214,19 @@ _http_input_stream_chunked_read_chunk_size (http_input_stream_t * stream) while (1) { - err = hsocket_read (stream->sock, &(chunk[i]), 1, 1, &status); + 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!"); + 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)); + log_error4("[%d] %s(): %s ", herror_code(err), herror_func(err), + herror_message(err)); stream->err = err; return -1; @@ -240,7 +239,7 @@ _http_input_stream_chunked_read_chunk_size (http_input_stream_t * stream) else if (chunk[i] == '\n') { chunk[i] = '\0'; /* double check */ - chunk_size = strtol (chunk, (char **) NULL, 16); /* hex to dec */ + chunk_size = strtol(chunk, (char **) NULL, 16); /* hex to dec */ /* log_verbose3("chunk_size: '%s' as dec: '%d'", chunk, chunk_size); */ return chunk_size; @@ -249,8 +248,8 @@ _http_input_stream_chunked_read_chunk_size (http_input_stream_t * stream) if (i == 24) { stream->err = - herror_new ("_http_input_stream_chunked_read_chunk_size", - STREAM_ERROR_NO_CHUNK_SIZE, "reached max line == %d", i); + herror_new("_http_input_stream_chunked_read_chunk_size", + STREAM_ERROR_NO_CHUNK_SIZE, "reached max line == %d", i); return -1; } else @@ -259,14 +258,14 @@ _http_input_stream_chunked_read_chunk_size (http_input_stream_t * stream) /* 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); + 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) +_http_input_stream_chunked_read(http_input_stream_t * stream, byte_t * dest, + int size) { int status, counter; int remain, read = 0; @@ -284,7 +283,7 @@ _http_input_stream_chunked_read (http_input_stream_t * stream, byte_t * dest, counter = 100; /* maximum for stop infinity */ while (1) { - err = hsocket_read (stream->sock, &ch, 1, 1, &status); + err = hsocket_read(stream->sock, &ch, 1, 1, &status); if (err != H_OK) { @@ -298,9 +297,9 @@ _http_input_stream_chunked_read (http_input_stream_t * stream, byte_t * dest, } if (counter-- == 0) { - stream->err = herror_new ("_http_input_stream_chunked_read", - STREAM_ERROR_WRONG_CHUNK_SIZE, - "Wrong chunk-size"); + stream->err = herror_new("_http_input_stream_chunked_read", + STREAM_ERROR_WRONG_CHUNK_SIZE, + "Wrong chunk-size"); return -1; } } @@ -309,8 +308,7 @@ _http_input_stream_chunked_read (http_input_stream_t * stream, byte_t * dest, 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) @@ -329,7 +327,7 @@ _http_input_stream_chunked_read (http_input_stream_t * stream, byte_t * dest, if (remain < size) { /* read from socket */ - err = hsocket_read (stream->sock, &(dest[read]), remain, 1, &status); + err = hsocket_read(stream->sock, &(dest[read]), remain, 1, &status); if (err != H_OK) { stream->err = err; @@ -337,23 +335,23 @@ _http_input_stream_chunked_read (http_input_stream_t * stream, byte_t * dest, } 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); + 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); + 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); + 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) @@ -373,14 +371,14 @@ _http_input_stream_chunked_read (http_input_stream_t * stream, byte_t * dest, static int -_http_input_stream_connection_closed_read (http_input_stream_t * stream, - byte_t * dest, int size) +_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); + err = hsocket_read(stream->sock, dest, size, 0, &status); if (err != H_OK) { stream->err = err; @@ -391,21 +389,21 @@ _http_input_stream_connection_closed_read (http_input_stream_t * stream, 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) +_http_input_stream_file_read(http_input_stream_t * stream, byte_t * dest, + int size) { int readed; - readed = fread (dest, 1, size, stream->fd); + 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"); + stream->err = herror_new("_http_input_stream_file_read", + HSOCKET_ERROR_RECEIVE, "fread() returned -1"); return -1; } @@ -416,7 +414,7 @@ _http_input_stream_file_read (http_input_stream_t * stream, byte_t * dest, Returns the actual status of the stream. */ int -http_input_stream_is_ready (http_input_stream_t * stream) +http_input_stream_is_ready(http_input_stream_t * stream) { /* paranoya check */ if (stream == NULL) @@ -428,13 +426,13 @@ 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); + return _http_input_stream_is_content_length_ready(stream); case HTTP_TRANSFER_CHUNKED: - return _http_input_stream_is_chunked_ready (stream); + return _http_input_stream_is_chunked_ready(stream); case HTTP_TRANSFER_CONNECTION_CLOSE: - return _http_input_stream_is_connection_closed_ready (stream); + return _http_input_stream_is_connection_closed_ready(stream); case HTTP_TRANSFER_FILE: - return _http_input_stream_is_file_ready (stream); + return _http_input_stream_is_file_ready(stream); default: return 0; } @@ -446,7 +444,7 @@ http_input_stream_is_ready (http_input_stream_t * stream) <0 on error */ int -http_input_stream_read (http_input_stream_t * stream, byte_t * dest, int size) +http_input_stream_read(http_input_stream_t * stream, byte_t * dest, int size) { int readed = 0; /* paranoya check */ @@ -461,21 +459,21 @@ http_input_stream_read (http_input_stream_t * stream, byte_t * dest, int size) switch (stream->type) { case HTTP_TRANSFER_CONTENT_LENGTH: - readed = _http_input_stream_content_length_read (stream, dest, size); + readed = _http_input_stream_content_length_read(stream, dest, size); break; case HTTP_TRANSFER_CHUNKED: - readed = _http_input_stream_chunked_read (stream, dest, size); + 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); + readed = _http_input_stream_connection_closed_read(stream, dest, size); break; case HTTP_TRANSFER_FILE: - readed = _http_input_stream_file_read (stream, dest, size); + 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); + stream->err = herror_new("http_input_stream_read", + STREAM_ERROR_INVALID_TYPE, + "%d is invalid stream type", stream->type); return -1; } @@ -497,7 +495,7 @@ 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_new(hsocket_t sock, hpair_t * header) { http_output_stream_t *result; char *content_length; @@ -507,31 +505,30 @@ http_output_stream_new (hsocket_t sock, hpair_t * header) 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); + 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; } @@ -542,9 +539,9 @@ http_output_stream_new (hsocket_t sock, hpair_t * header) Free output stream */ void -http_output_stream_free (http_output_stream_t * stream) +http_output_stream_free(http_output_stream_t * stream) { - free (stream); + free(stream); } /** @@ -552,31 +549,31 @@ http_output_stream_free (http_output_stream_t * stream) Returns socket error flags or H_OK. */ herror_t -http_output_stream_write (http_output_stream_t * stream, - const byte_t * bytes, int size) +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; } 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; } 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; } @@ -589,21 +586,21 @@ http_output_stream_write (http_output_stream_t * stream, Returns socket error flags or H_OK. */ herror_t -http_output_stream_write_string (http_output_stream_t * stream, - const char *str) +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) +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; } diff --git a/nanohttp/nanohttp-stream.h b/nanohttp/nanohttp-stream.h index 2ef12a2..20ced17 100755 --- a/nanohttp/nanohttp-stream.h +++ b/nanohttp/nanohttp-stream.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-stream.h,v 1.6 2006/01/10 11:21:55 snowdrop Exp $ + * $Id: nanohttp-stream.h,v 1.7 2006/01/10 11:29:05 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -25,7 +25,7 @@ #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: @@ -134,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); /** @@ -150,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); /** @@ -159,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); /** @@ -169,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); /** @@ -193,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); /* @@ -216,8 +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, @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,6 +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 |