diff options
-rw-r--r-- | config.h.in | 6 | ||||
-rw-r--r-- | nanohttp/nanohttp-client.c | 605 | ||||
-rw-r--r-- | nanohttp/nanohttp-common.c | 409 | ||||
-rw-r--r-- | nanohttp/nanohttp-server.c | 341 | ||||
-rw-r--r-- | nanohttp/nanohttp-socket.c | 656 | ||||
-rw-r--r-- | nanohttp/nanohttp-socket.h | 4 | ||||
-rw-r--r-- | xsd2c/tr.c | 5 |
7 files changed, 1069 insertions, 957 deletions
diff --git a/config.h.in b/config.h.in index 0356f7c..2e100bd 100644 --- a/config.h.in +++ b/config.h.in @@ -51,6 +51,9 @@ /* Define to 1 if you have the `vprintf' function. */ #undef HAVE_VPRINTF +/* Name of package */ +#undef PACKAGE + /* Define to the address where bug reports for this package should be sent. */ #undef PACKAGE_BUGREPORT @@ -75,6 +78,9 @@ /* Define to 1 if your <sys/time.h> declares `struct tm'. */ #undef TM_IN_SYS_TIME +/* Version number of package */ +#undef VERSION + /* Define to empty if `const' does not conform to ANSI C. */ #undef const diff --git a/nanohttp/nanohttp-client.c b/nanohttp/nanohttp-client.c index 20a04a1..ee02c59 100644 --- a/nanohttp/nanohttp-client.c +++ b/nanohttp/nanohttp-client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-client.c,v 1.17 2004/08/31 16:34:08 rans Exp $ +* $Id: nanohttp-client.c,v 1.18 2004/09/01 07:58:00 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -18,7 +18,7 @@ * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. -* +* * Email: ayaz@jprogrammer.net ******************************************************************/ #include <nanohttp/nanohttp-client.h> @@ -32,16 +32,17 @@ #include <stdlib.h> -/*--------------------------------------------------
-FUNCTION: httpc_init
-DESC: Initialize http client connection
-NOTE: This will be called from soap_client_init_args()
-----------------------------------------------------*/
-int httpc_init(int argc, char *argv[])
-{
- hsocket_module_init();
- return 0;
-}
+/*-------------------------------------------------- +FUNCTION: httpc_init +DESC: Initialize http client connection +NOTE: This will be called from soap_client_init_args() +----------------------------------------------------*/ +int +httpc_init(int argc, char *argv[]) +{ + hsocket_module_init(); + return 0; +} /*-------------------------------------------------- FUNCTION: httpc_new @@ -49,9 +50,10 @@ DESC: Creates a new http client connection object You need to create at least 1 http client connection to communicate via http. ----------------------------------------------------*/ -httpc_conn_t* httpc_new() +httpc_conn_t * +httpc_new() { - httpc_conn_t* res = (httpc_conn_t*)malloc(sizeof(httpc_conn_t)); + httpc_conn_t *res = (httpc_conn_t *) malloc(sizeof(httpc_conn_t)); hsocket_init(&res->sock); res->header = NULL; @@ -65,14 +67,15 @@ httpc_conn_t* httpc_new() FUNCTION: httpc_free DESC: Free the given http client object. ----------------------------------------------------*/ -void httpc_free(httpc_conn_t* conn) +void +httpc_free(httpc_conn_t * conn) { - hpair_t *tmp; + hpair_t *tmp; if (conn != NULL) { hsocket_free(conn->sock); - while (conn->header != NULL) { + while (conn->header != NULL) { tmp = conn->header; conn->header = conn->header->next; hpairnode_free(tmp); @@ -86,24 +89,24 @@ void httpc_free(httpc_conn_t* conn) /*-------------------------------------------------- FUNCTION: httpc_set_header DESC: Adds a new (key, value) pair to the header -or modifies the old pair if this function will +or modifies the old pair if this function will finds another pair with the same 'key' value. ----------------------------------------------------*/ -int httpc_set_header(httpc_conn_t *conn, const char* key, const char* value) +int +httpc_set_header(httpc_conn_t * conn, const char *key, const char *value) { - hpair_t *p; + hpair_t *p; if (conn == NULL) { log_warn1("Connection object is NULL"); return 0; } - p = conn->header; while (p != NULL) { if (p->key != NULL) { if (!strcmp(p->key, key)) { free(p->value); - p->value = (char*)malloc(strlen(value)+1); + p->value = (char *) malloc(strlen(value) + 1); strcpy(p->value, value); return 1; } @@ -118,20 +121,21 @@ int httpc_set_header(httpc_conn_t *conn, const char* key, const char* value) /*-------------------------------------------------- FUNCTION: httpc_header_add_date -DESC: Adds the current date to the header. +DESC: Adds the current date to the header. ----------------------------------------------------*/ -static -void httpc_header_add_date(httpc_conn_t *conn) +static +void +httpc_header_add_date(httpc_conn_t * conn) { - char buffer[255]; - time_t nw; - struct tm stm; + char buffer[255]; + time_t nw; + struct tm stm; /* Set date */ nw = time(NULL); localtime_r(&nw, &stm); strftime(buffer, 255, "%a, %d %b %y %T GMT", &stm); - httpc_set_header(conn, HEADER_DATE, buffer); + httpc_set_header(conn, HEADER_DATE, buffer); } @@ -141,11 +145,12 @@ FUNCTION: httpc_send_header DESC: Sends the current header information stored in conn through conn->sock. ----------------------------------------------------*/ -int httpc_send_header(httpc_conn_t *conn) +int +httpc_send_header(httpc_conn_t * conn) { - hpair_t *p; - int status; - char buffer[1024]; + hpair_t *p; + int status; + char buffer[1024]; p = conn->header; while (p != NULL) { @@ -163,17 +168,18 @@ int httpc_send_header(httpc_conn_t *conn) } static -hresponse_t *httpc_receive_header(hsocket_t sock) +hresponse_t * +httpc_receive_header(hsocket_t sock) { - hresponse_t *res; - int done; - int i; - int status; - char buffer[HSOCKET_MAX_BUFSIZE]; - char *response; - char *rest; - int rsize; - int restsize; + hresponse_t *res; + int done; + int i; + int status; + char buffer[HSOCKET_MAX_BUFSIZE]; + char *response; + char *rest; + int rsize; + int restsize; /* Receive Response incl. header */ rsize = restsize = 0; @@ -185,41 +191,40 @@ hresponse_t *httpc_receive_header(hsocket_t sock) status = hsocket_read(sock, buffer, HSOCKET_MAX_BUFSIZE, 0); if (status <= 0) { - log_error2("Can not receive response (status:%d)", status); + log_error2("Can not receive response (status:%d)", status); return NULL; - } - - - for (i=0;i<status-2;i++) { + } + for (i = 0; i < status - 2; i++) { - /* log_debug5("%d -> '%c' (%d, %d)", buffer[i], buffer[i], - buffer[i+1], buffer[i+2]); - */ + /* + * log_debug5("%d -> '%c' (%d, %d)", buffer[i], + * buffer[i], buffer[i+1], buffer[i+2]); + */ if (buffer[i] == '\n') { - if (buffer[i+1] == '\n') { + if (buffer[i + 1] == '\n') { - response = (char*)realloc(response, rsize+i+1); + response = (char *) realloc(response, rsize + i + 1); strncpy(&response[rsize], buffer, i); - response[rsize+i] = '\0'; + response[rsize + i] = '\0'; rsize += i; restsize = status - i - 2; - rest = (char*)malloc(restsize+1); - strncpy(rest, &buffer[i+2], restsize); + rest = (char *) malloc(restsize + 1); + strncpy(rest, &buffer[i + 2], restsize); rest[restsize] = '\0'; done = 1; break; - } else if (buffer[i+1] == '\r' && buffer[i+2] == '\n') { + } else if (buffer[i + 1] == '\r' && buffer[i + 2] == '\n') { - response = (char*)realloc(response, rsize+i+1); + response = (char *) realloc(response, rsize + i + 1); strncpy(&response[rsize], buffer, i); - response[rsize+i] = '\0'; + response[rsize + i] = '\0'; rsize += i; restsize = status - i - 3; - rest = (char*)malloc(restsize+1); - strncpy(rest, &buffer[i+3], restsize); + rest = (char *) malloc(restsize + 1); + strncpy(rest, &buffer[i + 3], restsize); rest[restsize] = '\0'; done = 1; break; @@ -236,17 +241,15 @@ hresponse_t *httpc_receive_header(hsocket_t sock) log_error1("Header too long!"); return NULL; } - res = hresponse_new_from_buffer(response); if (res == NULL) { log_error1("Can't create response"); return NULL; } - res->bodysize = restsize; res->body = rest; - if (res->errcode == 100) { /* continue */ + if (res->errcode == 100) { /* continue */ hresponse_free(res); res = httpc_receive_header(sock); } @@ -255,177 +258,173 @@ hresponse_t *httpc_receive_header(hsocket_t sock) static -int httpc_receive_with_connection_closed(httpc_conn_t *conn, - hresponse_t *res, - httpc_response_callback cb, - void *userdata) +int +httpc_receive_with_connection_closed(httpc_conn_t * conn, + hresponse_t * res, + httpc_response_callback cb, + void *userdata) { /* connection closed */ - char *connection_status; - int status; - char buffer[HSOCKET_MAX_BUFSIZE]; - int counter; + char *connection_status; + int status; + char buffer[HSOCKET_MAX_BUFSIZE]; + int counter; counter = 0; /* ================================================= */ - /* Retreive with only "Connection: close" */ + /* Retreive with only "Connection: close" */ /* ================================================= */ - connection_status = + connection_status = hpairnode_get(res->header, HEADER_CONNECTION); - if (connection_status != NULL && - !strcmp(connection_status, "close")) { - - log_debug1("Server communicates with 'Connection: close' !"); - - /* Invoke callback for rest */ - if (res->bodysize > 0) - cb(0, conn, userdata, res->bodysize, res->body); + if (connection_status != NULL && + !strcmp(connection_status, "close")) { + log_debug1("Server communicates with 'Connection: close' !"); - while (1) { + /* Invoke callback for rest */ + if (res->bodysize > 0) + cb(0, conn, userdata, res->bodysize, res->body); - status = hsocket_read(conn->sock, buffer, HSOCKET_MAX_BUFSIZE, 0); - if (status == 0) { /* close connection */ - close(conn->sock); - return 0; - } + while (1) { - if (status < 0) { /* error */ - log_error2("Can nor read from socket (status: %d)", status); - return 11; - } + status = hsocket_read(conn->sock, buffer, HSOCKET_MAX_BUFSIZE, 0); - /* Invoke callback */ - cb(counter++, conn, userdata, status, buffer); + if (status == 0) { /* close connection */ + close(conn->sock); + return 0; } - - return 0; + if (status < 0) { /* error */ + log_error2("Can nor read from socket (status: %d)", status); + return 11; + } + /* Invoke callback */ + cb(counter++, conn, userdata, status, buffer); } - return -1; + return 0; + } + return -1; } static -int httpc_receive_with_chunked_encoding(httpc_conn_t *conn, - hresponse_t *res, - httpc_response_callback cb, - void *userdata) +int +httpc_receive_with_chunked_encoding(httpc_conn_t * conn, + hresponse_t * res, + httpc_response_callback cb, + void *userdata) { /* chunked encoding */ - char *transfer_encoding; - char *chunk_buffer; - int chunk_size; - hbufsocket_t bufsock; - char chunk_size_str[25]; - int chunk_size_cur; - int counter; - char buffer[2]; + char *transfer_encoding; + char *chunk_buffer; + int chunk_size; + hbufsocket_t bufsock; + char chunk_size_str[25]; + int chunk_size_cur; + int counter; + char buffer[2]; counter = 0; /* ================================================= */ - /* Retreive with chunked encoding */ + /* Retreive with chunked encoding */ /* ================================================= */ /* Check if server communicates with chunked encoding */ - transfer_encoding = + transfer_encoding = hpairnode_get(res->header, HEADER_TRANSFER_ENCODING); - if (transfer_encoding != NULL && - !strcmp(transfer_encoding, "chunked")) { - - log_debug1("Server communicates with chunked encoding !"); - - /* initialize buffered socket */ - bufsock.sock = conn->sock; - bufsock.cur = 0; - bufsock.buffer = (char *)res->body; - bufsock.bufsize = res->bodysize; - - chunk_size = 1; - while (chunk_size > 0) { - - /* read chunk size */ - chunk_size_cur = 0; - while (1) { - + if (transfer_encoding != NULL && + !strcmp(transfer_encoding, "chunked")) { - if (hbufsocket_read(&bufsock, &chunk_size_str[chunk_size_cur], 1)) { - log_error1("Can not read from socket"); - return 9; - } + log_debug1("Server communicates with chunked encoding !"); - log_debug2("chunk_size_str[chunk_size_cur] = '%c'", - chunk_size_str[chunk_size_cur]); + /* initialize buffered socket */ + bufsock.sock = conn->sock; + bufsock.cur = 0; + bufsock.buffer = (char *) res->body; + bufsock.bufsize = res->bodysize; - if (chunk_size_str[chunk_size_cur] == '\n') { - chunk_size_str[chunk_size_cur] = '\0'; - break; - } + chunk_size = 1; + while (chunk_size > 0) { - if (chunk_size_str[chunk_size_cur] != '\r' - && chunk_size_str[chunk_size_cur] != ';') { - chunk_size_cur++; - } - - } /* while (1) */ - - chunk_size = strtol(chunk_size_str,(char**)NULL, 16); /* hex to dec */ - log_debug3("chunk_size: '%s' as dec: '%d'", - chunk_size_str, chunk_size); + /* read chunk size */ + chunk_size_cur = 0; + while (1) { - if (chunk_size <= 0) break; - chunk_buffer = (char*)malloc(chunk_size+1); - if (hbufsocket_read(&bufsock, chunk_buffer, chunk_size)) { + if (hbufsocket_read(&bufsock, &chunk_size_str[chunk_size_cur], 1)) { log_error1("Can not read from socket"); return 9; } - cb(counter++, conn, userdata, chunk_size, chunk_buffer); - free(chunk_buffer); + log_debug2("chunk_size_str[chunk_size_cur] = '%c'", + chunk_size_str[chunk_size_cur]); - /* skip new line */ - buffer[0] = 0; /* reset buffer[0] */ - while (buffer[0] != '\n') { - hbufsocket_read(&bufsock, &buffer[0], 1); + if (chunk_size_str[chunk_size_cur] == '\n') { + chunk_size_str[chunk_size_cur] = '\0'; + break; + } + if (chunk_size_str[chunk_size_cur] != '\r' + && chunk_size_str[chunk_size_cur] != ';') { + chunk_size_cur++; } + } /* while (1) */ - } /* while (chunk_size > 0) */ + chunk_size = strtol(chunk_size_str, (char **) NULL, 16); /* hex to dec */ + log_debug3("chunk_size: '%s' as dec: '%d'", + chunk_size_str, chunk_size); - /* rest and response are no longer required */ + if (chunk_size <= 0) + break; - return 0; + chunk_buffer = (char *) malloc(chunk_size + 1); + if (hbufsocket_read(&bufsock, chunk_buffer, chunk_size)) { + log_error1("Can not read from socket"); + return 9; + } + cb(counter++, conn, userdata, chunk_size, chunk_buffer); + free(chunk_buffer); - } /* if transfer_encodig */ + /* skip new line */ + buffer[0] = 0; /* reset buffer[0] */ + while (buffer[0] != '\n') { + hbufsocket_read(&bufsock, &buffer[0], 1); + } - return -1; + } /* while (chunk_size > 0) */ + + /* rest and response are no longer required */ + + return 0; + + } /* if transfer_encodig */ + return -1; } /* -returns -1 if server does not communicate with -content-length; -*/ -static -int httpc_receive_with_content_length(httpc_conn_t *conn, - hresponse_t *res, - httpc_response_callback cb, - void *userdata) + * returns -1 if server does not communicate with content-length; + */ +static +int +httpc_receive_with_content_length(httpc_conn_t * conn, + hresponse_t * res, + httpc_response_callback cb, + void *userdata) { - int counter; - int content_length; - int remain_length; - int recvSize; - char *content_length_str; - char buffer[HSOCKET_MAX_BUFSIZE]; + int counter; + int content_length; + int remain_length; + int recvSize; + char *content_length_str; + char buffer[HSOCKET_MAX_BUFSIZE]; /* ================================================= */ - /* Retreive with content-length */ + /* Retreive with content-length */ /* ================================================= */ /* Check if server communicates with content-length */ - content_length_str = + content_length_str = hpairnode_get_ignore_case(res->header, HEADER_CONTENT_LENGTH); if (content_length_str != NULL) { @@ -434,7 +433,7 @@ int httpc_receive_with_content_length(httpc_conn_t *conn, /* Invoke callback for rest */ if (res->bodysize > 0) - cb(0, conn, userdata, res->bodysize, (char *)res->body); + cb(0, conn, userdata, res->bodysize, (char *) res->body); /* content length */ content_length = atol(content_length_str); @@ -448,7 +447,7 @@ int httpc_receive_with_content_length(httpc_conn_t *conn, recvSize = remain_length; } - if (hsocket_read(conn->sock, buffer, recvSize,1)) { + if (hsocket_read(conn->sock, buffer, recvSize, 1)) { log_error1("Can not read from socket!"); return 9; } else { @@ -457,75 +456,73 @@ int httpc_receive_with_content_length(httpc_conn_t *conn, remain_length -= HSOCKET_MAX_BUFSIZE; - } /* while */ + } /* while */ /* rest and response are no longer required */ return 0; - } /* if content length */ - + } /* if content length */ return -1; } static -int httpc_receive_response(httpc_conn_t *conn, - httpc_response_start_callback start_cb, - httpc_response_callback cb, void *userdata) +int +httpc_receive_response(httpc_conn_t * conn, + httpc_response_start_callback start_cb, + httpc_response_callback cb, void *userdata) { - hresponse_t *res; - int status; + hresponse_t *res; + int status; /* receive header */ log_verbose1("receiving header"); res = httpc_receive_header(conn->sock); - if (res == NULL) return 1; + if (res == NULL) + return 1; log_verbose1("header ok"); /* Invoke callback */ - start_cb(conn, userdata, res->header, res->spec, - res->errcode, res->desc); + start_cb(conn, userdata, res->header, res->spec, + res->errcode, res->desc); /* try to receive with content length */ - status = httpc_receive_with_content_length(conn, res, - cb, userdata); + status = httpc_receive_with_content_length(conn, res, + cb, userdata); if (status != -1) { hresponse_free(res); return status; } - status = httpc_receive_with_chunked_encoding(conn, res, - cb, userdata); + cb, userdata); if (status != -1) { hresponse_free(res); return status; } - status = httpc_receive_with_connection_closed(conn, res, - cb, userdata); + cb, userdata); if (status != -1) { hresponse_free(res); return status; } - log_error1("Unknown server response retreive type!"); return -1; } /*-------------------------------------------------- FUNCTION: httpc_talk_to_server -DESC: This function is the heart of the httpc +DESC: This function is the heart of the httpc module. It will send the request and process the -response. +response. Here the parameters: method: -the request method. This can be HTTP_REQUEST_POST and +the request method. This can be HTTP_REQUEST_POST and HTTP_REQUEST_GET. -conn: +conn: the connection object (created with httpc_new()) urlstr: @@ -542,11 +539,11 @@ a callback function, which will be called everytime when data arrives. content_size: -size of content to send. +size of content to send. (only if method is HTTP_REQUEST_POST) content: -the content data to send. +the content data to send. (only if method is HTTP_REQUEST_POST) userdata: @@ -555,22 +552,22 @@ start_cb and cb callbacks as a parameter. This can also be NULL. -If success, this function will return 0. +If success, this function will return 0. >0 otherwise. ----------------------------------------------------*/ static -int httpc_talk_to_server(hreq_method method, httpc_conn_t *conn, - const char *urlstr) +int +httpc_talk_to_server(hreq_method method, httpc_conn_t * conn, + const char *urlstr) { - hurl_t *url; - char buffer[4096]; - int status; + hurl_t *url; + char buffer[4096]; + int status; if (conn == NULL) { log_error1("Connection object is NULL"); return 1; } - /* Build request header */ httpc_header_add_date(conn); @@ -580,37 +577,33 @@ int httpc_talk_to_server(hreq_method method, httpc_conn_t *conn, log_error2("Can not parse URL '%s'", SAVE_STR(urlstr)); return 2; } - /* Set hostname */ httpc_set_header(conn, HEADER_HOST, url->host); /* Open connection */ status = hsocket_open(&conn->sock, url->host, url->port); if (status != HSOCKET_OK) { - log_error3("Can not open connection to '%s' (status:%d)", - SAVE_STR(url->host), status); + log_error3("Can not open connection to '%s' (status:%d)", + SAVE_STR(url->host), status); return 3; } - - status=hsocket_makenonblock(conn->sock); - if(status!=HSOCKET_OK) - { + status = hsocket_makenonblock(conn->sock); + if (status != HSOCKET_OK) { log_error1("Cannot make socket non-blocking"); return status; } - /* check method */ if (method == HTTP_REQUEST_GET) { /* Set GET Header */ - sprintf(buffer, "GET %s HTTP/1.1\r\n", - (url->context)?url->context:("/")); + sprintf(buffer, "GET %s HTTP/1.1\r\n", + (url->context) ? url->context : ("/")); } else if (method == HTTP_REQUEST_POST) { /* Set POST Header */ - sprintf(buffer, "POST %s HTTP/1.1\r\n", - (url->context)?url->context:("/")); + sprintf(buffer, "POST %s HTTP/1.1\r\n", + (url->context) ? url->context : ("/")); } else { log_error1("Unknown method type!"); @@ -623,7 +616,6 @@ int httpc_talk_to_server(hreq_method method, httpc_conn_t *conn, hsocket_close(conn->sock); return 4; } - /* Send Header */ status = httpc_send_header(conn); if (status != HSOCKET_OK) { @@ -631,7 +623,6 @@ int httpc_talk_to_server(hreq_method method, httpc_conn_t *conn, hsocket_close(conn->sock); return 5; } - return status; } @@ -640,16 +631,17 @@ int httpc_talk_to_server(hreq_method method, httpc_conn_t *conn, /*-------------------------------------------------- FUNCTION: httpc_get_cb DESC: Wraps the httpc_talk_to_server() function -to communicate with GET method. +to communicate with GET method. See the documentation of httpc_talk_to_server() for more information. ----------------------------------------------------*/ -int httpc_get_cb(httpc_conn_t *conn, const char *urlstr, - httpc_response_start_callback start_cb, - httpc_response_callback cb, void *userdata) +int +httpc_get_cb(httpc_conn_t * conn, const char *urlstr, + httpc_response_start_callback start_cb, + httpc_response_callback cb, void *userdata) { - int status; + int status; status = httpc_talk_to_server(HTTP_REQUEST_GET, conn, urlstr); @@ -665,89 +657,90 @@ int httpc_get_cb(httpc_conn_t *conn, const char *urlstr, /*-------------------------------------------------- FUNCTION: httpc_post_cb DESC: Wraps the httpc_talk_to_server() function -to communicate with POST method. +to communicate with POST method. See the documentation of httpc_talk_to_server() for more information. TODO: Add post content rutine ----------------------------------------------------*/ -int httpc_post_cb(httpc_conn_t *conn, const char *urlstr, - httpc_response_start_callback start_cb, - httpc_response_callback cb, int content_size, - char *content, void *userdata) +int +httpc_post_cb(httpc_conn_t * conn, const char *urlstr, + httpc_response_start_callback start_cb, + httpc_response_callback cb, int content_size, + char *content, void *userdata) { - int status; - char buffer[255]; + int status; + char buffer[255]; sprintf(buffer, "%d", content_size); httpc_set_header(conn, HEADER_CONTENT_LENGTH, buffer); status = httpc_talk_to_server(HTTP_REQUEST_POST, conn, urlstr); - if (status != HSOCKET_OK) - return status; + if (status != HSOCKET_OK) + return status; status = hsocket_nsend(conn->sock, content, content_size); if (status != HSOCKET_OK) return status; - status = httpc_receive_response(conn, start_cb, cb, userdata); - return status; + status = httpc_receive_response(conn, start_cb, cb, userdata); + return status; } -/*====================================================== -The following functions are used internally to wrap -the httpc_x_cb (x = get|post) functions. -======================================================*/ +/* + * ====================================================== The following + * functions are used internally to wrap the httpc_x_cb (x = get|post) + * functions. ====================================================== + */ static -void httpc_custom_res_callback(int counter, httpc_conn_t* conn, - void *userdata, int size, char *buffer) +void +httpc_custom_res_callback(int counter, httpc_conn_t * conn, + void *userdata, int size, char *buffer) { - hresponse_t *res; + hresponse_t *res; /* get response object */ - res = (hresponse_t*)userdata; + res = (hresponse_t *) userdata; /* allocate buffersize */ res->bodysize += size; - res->body = (char *)realloc(res->body, res->bodysize + 1); + res->body = (char *) realloc(res->body, res->bodysize + 1); - memcpy(&(res->body[res->bodysize-size]), buffer, size); - res->body[res->bodysize]='\0'; + memcpy(&(res->body[res->bodysize - size]), buffer, size); + res->body[res->bodysize] = '\0'; } -static -void httpc_custom_start_callback(httpc_conn_t *conn, void *userdata, - hpair_t *header, const char *spec, - int errcode, const char *desc) +static +void +httpc_custom_start_callback(httpc_conn_t * conn, void *userdata, + hpair_t * header, const char *spec, + int errcode, const char *desc) { - hresponse_t *res; + hresponse_t *res; /* get response object */ - res = (hresponse_t*)userdata; + res = (hresponse_t *) userdata; /* set spec */ if (spec != NULL) { strcpy(res->spec, spec); } - /* set errcode */ res->errcode = errcode; /* set desc */ if (desc != NULL) { - res->desc = (char*)malloc(strlen(desc)+1); + res->desc = (char *) malloc(strlen(desc) + 1); strcpy(res->desc, desc); } - /* set header */ if (header == NULL) { log_warn1("header is NULL!"); } - res->header = hpairnode_copy_deep(header); } @@ -755,60 +748,61 @@ void httpc_custom_start_callback(httpc_conn_t *conn, void *userdata, /*-------------------------------------------------- FUNCTION: httpc_get ----------------------------------------------------*/ -hresponse_t *httpc_get(httpc_conn_t *conn, const char *url) +hresponse_t * +httpc_get(httpc_conn_t * conn, const char *url) { - int status; - hresponse_t *res; + int status; + hresponse_t *res; res = hresponse_new(); status = httpc_get_cb(conn, url, httpc_custom_start_callback, - httpc_custom_res_callback, res); + httpc_custom_res_callback, res); if (status != 0) { hresponse_free(res); return NULL; } - return res; -} +} /*-------------------------------------------------- FUNCTION: httpc_post ----------------------------------------------------*/ -hresponse_t *httpc_post(httpc_conn_t *conn, const char *url, - int content_size, const char *content) -{ - int status; - hresponse_t *res; +hresponse_t * +httpc_post(httpc_conn_t * conn, const char *url, + int content_size, const char *content) +{ + int status; + hresponse_t *res; res = hresponse_new(); status = httpc_post_cb(conn, url, httpc_custom_start_callback, - httpc_custom_res_callback, content_size, - (char *)content, res); + httpc_custom_res_callback, content_size, + (char *) content, res); if (status != 0) { hresponse_free(res); return NULL; } - return res; -} +} /* -POST Module -*/ + * POST Module + */ /*-------------------------------------------------- FUNCTION: httpc_post_open ----------------------------------------------------*/ -int httpc_post_open(httpc_conn_t *conn, const char *urlstr) +int +httpc_post_open(httpc_conn_t * conn, const char *urlstr) { - int status; + int status; httpc_set_header(conn, HEADER_TRANSFER_ENCODING, "chunked"); @@ -820,21 +814,22 @@ int httpc_post_open(httpc_conn_t *conn, const char *urlstr) /*-------------------------------------------------- FUNCTION: httpc_post_send ----------------------------------------------------*/ -int httpc_post_send(httpc_conn_t *conn, - const char* buffer, - int bufsize) +int +httpc_post_send(httpc_conn_t * conn, + const char *buffer, + int bufsize) { - char hexsize[100]; /* chunk size in hex */ - int status; + char hexsize[100]; /* chunk size in hex */ + int status; - sprintf(hexsize, "%x\r\n", bufsize); + sprintf(hexsize, "%x\r\n", bufsize); status = hsocket_send(conn->sock, hexsize); - if (status != HSOCKET_OK) + if (status != HSOCKET_OK) return status; status = hsocket_nsend(conn->sock, buffer, bufsize); - if (status != HSOCKET_OK) + if (status != HSOCKET_OK) return status; status = hsocket_send(conn->sock, "\r\n"); @@ -845,36 +840,38 @@ int httpc_post_send(httpc_conn_t *conn, /*-------------------------------------------------- FUNCTION: httpc_post_finish_cb ----------------------------------------------------*/ -int httpc_post_finish_cb(httpc_conn_t *conn, - httpc_response_start_callback start_cb, - httpc_response_callback cb, - void *userdata) +int +httpc_post_finish_cb(httpc_conn_t * conn, + httpc_response_start_callback start_cb, + httpc_response_callback cb, + void *userdata) { - int status; + int status; status = hsocket_send(conn->sock, "0\r\n\r\n"); - if (status != HSOCKET_OK) return status; + if (status != HSOCKET_OK) + return status; - status = httpc_receive_response(conn, start_cb, cb, userdata); - return status; + status = httpc_receive_response(conn, start_cb, cb, userdata); + return status; } /*-------------------------------------------------- FUNCTION: httpc_post_finish ----------------------------------------------------*/ -hresponse_t *httpc_post_finish(httpc_conn_t *conn) +hresponse_t * +httpc_post_finish(httpc_conn_t * conn) { - int status; - hresponse_t *res; + int status; + hresponse_t *res; res = hresponse_new(); status = httpc_post_finish_cb(conn, httpc_custom_start_callback, - httpc_custom_res_callback, res); + httpc_custom_res_callback, res); if (status != 0) { hresponse_free(res); return NULL; } - - return res; + return res; } diff --git a/nanohttp/nanohttp-common.c b/nanohttp/nanohttp-common.c index b9dbe62..b96c0bc 100644 --- a/nanohttp/nanohttp-common.c +++ b/nanohttp/nanohttp-common.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-common.c,v 1.10 2004/08/30 13:28:58 snowdrop Exp $ +* $Id: nanohttp-common.c,v 1.11 2004/09/01 07:58:08 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -18,7 +18,7 @@ * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. -* +* * Email: ayaz@jprogrammer.net ******************************************************************/ @@ -30,26 +30,26 @@ #ifdef WIN32 #include <string.h> -static char *strtok_r (char *s, const char *delim, char **save_ptr) +static char * +strtok_r(char *s, const char *delim, char **save_ptr) { - char *token; + char *token; if (s == NULL) 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'); - else - { + *save_ptr = strchr(token, '\0'); + else { /* Terminate the token and make *SAVE_PTR point past it. */ *s = '\0'; *save_ptr = s + 1; @@ -60,73 +60,82 @@ static char *strtok_r (char *s, const char *delim, char **save_ptr) static log_level_t loglevel = HLOG_DEBUG; -log_level_t log_set_level(log_level_t level) +log_level_t +log_set_level(log_level_t level) { - log_level_t old = loglevel; + log_level_t old = loglevel; loglevel = level; return old; } -log_level_t log_get_level() +log_level_t +log_get_level() { return loglevel; } static -void log_write(log_level_t level, const char *prefix, - const char* func, const char *format, va_list ap) +void +log_write(log_level_t level, const char *prefix, + const char *func, const char *format, va_list ap) { - char buffer[1054]; - char buffer2[1054]; + char buffer[1054]; + char buffer2[1054]; - if (level < loglevel) return; + if (level < loglevel) + return; - sprintf(buffer, "*%s*: [%s] %s\n", prefix, func, format); + sprintf(buffer, "*%s*: [%s] %s\n", prefix, func, format); vsprintf(buffer2, buffer, ap); printf(buffer2); - fflush(stdout); + fflush(stdout); } -void log_verbose(const char* FUNC, const char *format, ...) +void +log_verbose(const char *FUNC, const char *format,...) { - va_list ap; + va_list ap; va_start(ap, format); log_write(HLOG_VERBOSE, "VERBOSE", FUNC, format, ap); va_end(ap); } -void log_debug(const char* FUNC, const char *format, ...) +void +log_debug(const char *FUNC, const char *format,...) { - va_list ap; + va_list ap; va_start(ap, format); log_write(HLOG_DEBUG, "DEBUG", FUNC, format, ap); va_end(ap); } -void log_info(const char* FUNC, const char *format, ...) +void +log_info(const char *FUNC, const char *format,...) { - va_list ap; + va_list ap; va_start(ap, format); log_write(HLOG_INFO, "INFO", FUNC, format, ap); va_end(ap); } -void log_warn(const char* FUNC, const char *format, ...) +void +log_warn(const char *FUNC, const char *format,...) { - va_list ap; + va_list ap; va_start(ap, format); log_write(HLOG_WARN, "WARN", FUNC, format, ap); va_end(ap); } -void log_error(const char* FUNC, const char *format, ...) +void +log_error(const char *FUNC, const char *format,...) { - va_list ap; + va_list ap; va_start(ap, format); log_write(HLOG_ERROR, "ERROR", FUNC, format, ap); @@ -134,22 +143,27 @@ void log_error(const char* FUNC, const char *format, ...) } -/* ----------------------------------------- -FUNCTION: strcmpigcase ------------------------------------------- */ -int strcmpigcase(const char *s1, const char *s2) +/* + * ----------------------------------------- FUNCTION: strcmpigcase + * ------------------------------------------ + */ +int +strcmpigcase(const char *s1, const char *s2) { - int l1, l2, i; + int l1, l2, i; - if (s1 == NULL && s2 == NULL) return 1; - if (s1 == NULL || s2 == NULL) return 0; + if (s1 == NULL && s2 == NULL) + return 1; + if (s1 == NULL || s2 == NULL) + return 0; l1 = strlen(s1); l2 = strlen(s2); - if (l1 != l2) return 0; + if (l1 != l2) + return 0; - for (i=0;i<l1;i++) + for (i = 0; i < l1; i++) if (toupper(s1[i]) != toupper(s2[i])) return 0; @@ -157,22 +171,23 @@ int strcmpigcase(const char *s1, const char *s2) } -hpair_t *hpairnode_new(const char* key, const char* value, hpair_t *next) +hpair_t * +hpairnode_new(const char *key, const char *value, hpair_t * next) { - hpair_t *pair; + hpair_t *pair; log_verbose3("new pair ('%s','%s')", SAVE_STR(key), SAVE_STR(value)); - pair = (hpair_t*)malloc(sizeof(hpair_t)); + pair = (hpair_t *) malloc(sizeof(hpair_t)); if (key != NULL) { - pair->key = (char*)malloc(strlen(key)+1); + pair->key = (char *) malloc(strlen(key) + 1); strcpy(pair->key, key); } else { pair->key = NULL; } if (value != NULL) { - pair->value = (char*)malloc(strlen(value)+1); + pair->value = (char *) malloc(strlen(value) + 1); strcpy(pair->value, value); } else { pair->value = NULL; @@ -183,50 +198,53 @@ hpair_t *hpairnode_new(const char* key, const char* value, hpair_t *next) return pair; } -hpair_t *hpairnode_parse(const char *str, const char *delim, hpair_t *next) +hpair_t * +hpairnode_parse(const char *str, const char *delim, hpair_t * next) { - hpair_t *pair; - char *key, *value; - int c; + hpair_t *pair; + char *key, *value; + int c; - pair = (hpair_t*)malloc(sizeof(hpair_t)); + pair = (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); + if (key != NULL) { + pair->key = (char *) malloc(strlen(key) + 1); strcpy(pair->key, key); } - if (value != NULL) { - for (c=0;value[c]==' ';c++); /* skip white space */ - pair->value = (char*)malloc(strlen(&value[c])+1); + for (c = 0; value[c] == ' '; c++); /* skip white space */ + pair->value = (char *) malloc(strlen(&value[c]) + 1); strcpy(pair->value, &value[c]); } - return pair; } -hpair_t* hpairnode_copy(const hpair_t *src) +hpair_t * +hpairnode_copy(const hpair_t * src) { - hpair_t *pair; + hpair_t *pair; - if (src == NULL) return NULL; + if (src == NULL) + return NULL; pair = hpairnode_new(src->key, src->value, NULL); return pair; } -hpair_t* hpairnode_copy_deep(const hpair_t *src) +hpair_t * +hpairnode_copy_deep(const hpair_t * src) { - hpair_t *pair, *result, *next; + hpair_t *pair, *result, *next; - if (src == NULL) return NULL; + if (src == NULL) + return NULL; result = hpairnode_copy(src); @@ -234,7 +252,7 @@ hpair_t* hpairnode_copy_deep(const hpair_t *src) pair = result; while (next != NULL) { - pair->next = hpairnode_copy(next); + pair->next = hpairnode_copy(next); pair = pair->next; next = next->next; } @@ -243,22 +261,23 @@ hpair_t* hpairnode_copy_deep(const hpair_t *src) } -void hpairnode_dump(hpair_t *pair) +void +hpairnode_dump(hpair_t * pair) { if (pair == NULL) { log_verbose1("(NULL)[]"); return; } - - log_verbose5("(%p)['%s','%s','%p']", pair, - SAVE_STR(pair->key), SAVE_STR(pair->value), - pair->next); + log_verbose5("(%p)['%s','%s','%p']", pair, + SAVE_STR(pair->key), SAVE_STR(pair->value), + pair->next); } -void hpairnode_dump_deep(hpair_t *pair) +void +hpairnode_dump_deep(hpair_t * pair) { - hpair_t *p; + hpair_t *p; p = pair; log_verbose1("-- BEGIN dump hpairnode_t --"); @@ -272,9 +291,11 @@ void hpairnode_dump_deep(hpair_t *pair) } -void hpairnode_free(hpair_t *pair) +void +hpairnode_free(hpair_t * pair) { - if (pair == NULL) return; + if (pair == NULL) + return; free(pair->key); free(pair->value); @@ -283,24 +304,25 @@ void hpairnode_free(hpair_t *pair) } -void hpairnode_free_deep(hpair_t *pair) +void +hpairnode_free_deep(hpair_t * pair) { - hpair_t *tmp; + hpair_t *tmp; while (pair != NULL) { tmp = pair->next; hpairnode_free(pair); - pair=tmp; + pair = tmp; } } -char *hpairnode_get_ignore_case(hpair_t *pair, const char* key) +char * +hpairnode_get_ignore_case(hpair_t * pair, const char *key) { if (key == NULL) { log_error1("key is NULL"); return NULL; } - while (pair != NULL) { if (pair->key != NULL) { if (strcmpigcase(pair->key, key)) { @@ -313,13 +335,13 @@ char *hpairnode_get_ignore_case(hpair_t *pair, const char* key) return NULL; } -char *hpairnode_get(hpair_t *pair, const char* key) +char * +hpairnode_get(hpair_t * pair, const char *key) { if (key == NULL) { log_error1("key is NULL"); return NULL; } - while (pair != NULL) { if (pair->key != NULL) { if (!strcmp(pair->key, key)) { @@ -333,14 +355,14 @@ char *hpairnode_get(hpair_t *pair, const char* key) } static -void hurl_dump(const hurl_t *url) +void +hurl_dump(const hurl_t * url) { if (url == NULL) { log_error1("url is NULL!"); - return ; + return; } - log_verbose2("PROTOCOL : %s", SAVE_STR(url->protocol)); log_verbose2(" HOST : %s", SAVE_STR(url->host)); log_verbose2(" PORT : %d", url->port); @@ -348,22 +370,22 @@ void hurl_dump(const hurl_t *url) } -hurl_t* hurl_new(const char* urlstr) +hurl_t * +hurl_new(const char *urlstr) { - int iprotocol; - int ihost; - int iport; - int len; - int size; - hurl_t *url; - char tmp[8]; + int iprotocol; + int ihost; + int iport; + int len; + int size; + hurl_t *url; + char tmp[8]; iprotocol = 0; len = strlen(urlstr); /* find protocol */ - while (urlstr[iprotocol] != ':' && urlstr[iprotocol] != '\0') - { + while (urlstr[iprotocol] != ':' && urlstr[iprotocol] != '\0') { iprotocol++; } @@ -371,26 +393,21 @@ hurl_t* hurl_new(const char* urlstr) log_error1("no protocol"); return NULL; } - if (iprotocol + 3 >= len) { log_error1("no host"); return NULL; } - - if ( urlstr[iprotocol] != ':' - && urlstr[iprotocol+1] != '/' - && urlstr[iprotocol+2] != '/') - { + if (urlstr[iprotocol] != ':' + && urlstr[iprotocol + 1] != '/' + && urlstr[iprotocol + 2] != '/') { log_error1("no protocol"); return NULL; } - /* find host */ ihost = iprotocol + 3; while (urlstr[ihost] != ':' - && urlstr[ihost] != '/' - && urlstr[ihost] != '\0') - { + && urlstr[ihost] != '/' + && urlstr[ihost] != '\0') { ihost++; } @@ -398,44 +415,40 @@ hurl_t* hurl_new(const char* urlstr) log_error1("no host"); return NULL; } - /* find port */ iport = ihost; if (ihost + 1 < len) { if (urlstr[ihost] == ':') { - while (urlstr[iport] != '/' && urlstr[iport] != '\0') { + while (urlstr[iport] != '/' && urlstr[iport] != '\0') { iport++; } } } + url = (hurl_t *) malloc(sizeof(hurl_t)); - url = (hurl_t*)malloc(sizeof(hurl_t)); - - url->protocol = (char*)malloc(sizeof(char)*iprotocol+1); + url->protocol = (char *) malloc(sizeof(char) * iprotocol + 1); strncpy(url->protocol, urlstr, iprotocol); url->protocol[iprotocol] = '\0'; size = ihost - iprotocol - 3; - url->host = (char*)malloc(sizeof(char)*size + 1); - strncpy(url->host, &urlstr[iprotocol+3], size); + url->host = (char *) malloc(sizeof(char) * size + 1); + strncpy(url->host, &urlstr[iprotocol + 3], size); url->host[size] = '\0'; - if (iport > ihost) - { + if (iport > ihost) { size = iport - ihost; - strncpy(tmp, &urlstr[ihost+1], size); + strncpy(tmp, &urlstr[ihost + 1], size); url->port = atoi(tmp); } else { url->port = 80; } len = strlen(urlstr); - if (len > iport ) - { + if (len > iport) { size = len - iport; - url->context = (char*)malloc(sizeof(char)*size+1); + url->context = (char *) malloc(sizeof(char) * size + 1); strncpy(url->context, &urlstr[iport], size); - url->context[size]='\0'; + url->context[size] = '\0'; } else { url->context = NULL; } @@ -446,9 +459,11 @@ hurl_t* hurl_new(const char* urlstr) } -void hurl_free(hurl_t *url) +void +hurl_free(hurl_t * url) { - if (url == NULL) return; + if (url == NULL) + return; free(url->protocol); free(url->host); @@ -460,27 +475,30 @@ void hurl_free(hurl_t *url) /* request stuff */ -/* ----------------------------------------------------- -FUNCTION: hrequest_new_from_buffer ------------------------------------------------------ */ -hrequest_t *hrequest_new_from_buffer(char *data) +/* + * ----------------------------------------------------- FUNCTION: + * hrequest_new_from_buffer + * ----------------------------------------------------- + */ +hrequest_t * +hrequest_new_from_buffer(char *data) { - hrequest_t *req; - hpair_t *hpair = NULL, *qpair = NULL, *tmppair = NULL; - - char *tmp; - char *tmp2; - char *saveptr; - char *saveptr2; - char *saveptr3; - char *result; - char *key; - char *value; - char *opt_key; - char *opt_value; - int firstline = 1; - - req = (hrequest_t*)malloc(sizeof(hrequest_t)); + hrequest_t *req; + hpair_t *hpair = NULL, *qpair = NULL, *tmppair = NULL; + + char *tmp; + char *tmp2; + char *saveptr; + char *saveptr2; + char *saveptr3; + char *result; + char *key; + char *value; + char *opt_key; + char *opt_value; + int firstline = 1; + + req = (hrequest_t *) malloc(sizeof(hrequest_t)); req->method = NULL; req->spec = NULL; @@ -491,58 +509,57 @@ hrequest_t *hrequest_new_from_buffer(char *data) tmp = data; for (;;) { - result = (char*)strtok_r(tmp, "\n", &saveptr); - tmp=saveptr; + result = (char *) strtok_r(tmp, "\n", &saveptr); + tmp = saveptr; - if (result == NULL) + if (result == NULL) break; if (firstline) { - firstline=0; + firstline = 0; 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) { - req->method = (char*)malloc(strlen(key)+1); + req->method = (char *) malloc(strlen(key) + 1); strcpy(req->method, key); } - /* below is key the path and tmp2 the spec */ - key = (char*)strtok_r(tmp2," ", &saveptr2); + key = (char *) strtok_r(tmp2, " ", &saveptr2); /* save spec */ tmp2 = saveptr2; if (tmp2 != NULL) { - req->spec = (char*)malloc(strlen(tmp2)+1); + req->spec = (char *) malloc(strlen(tmp2) + 1); strcpy(req->spec, tmp2); } - - /* parse and save path+query - parse: /path/of/target?key1=value1&key2=value2... - */ + /* + * parse and save path+query parse: + * /path/of/target?key1=value1&key2=value2... + */ 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); + req->path = (char *) malloc(strlen(key) + 1); strcpy(req->path, key); /* 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) @@ -550,7 +567,7 @@ hrequest_t *hrequest_new_from_buffer(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) { req->query = qpair = tmppair; @@ -561,8 +578,8 @@ hrequest_t *hrequest_new_from_buffer(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); @@ -570,16 +587,15 @@ hrequest_t *hrequest_new_from_buffer(char *data) } } } - } else { /* parse "key: value" */ tmp2 = result; - key = (char*)strtok_r(tmp2, ": ", &saveptr2); + key = (char *) strtok_r(tmp2, ": ", &saveptr2); value = saveptr2; /* create pair */ - tmppair = (hpair_t*)malloc(sizeof(hpair_t)); + tmppair = (hpair_t *) malloc(sizeof(hpair_t)); if (req->header == NULL) { req->header = hpair = tmppair; @@ -590,8 +606,8 @@ hrequest_t *hrequest_new_from_buffer(char *data) /* fill pairnode_t struct */ hpair->next = NULL; - hpair->key = (char*)malloc(strlen(key)+1); - hpair->value = (char*)malloc(strlen(value)+1); + hpair->key = (char *) malloc(strlen(key) + 1); + hpair->value = (char *) malloc(strlen(value) + 1); strcpy(hpair->key, key); strcpy(hpair->value, value); @@ -602,9 +618,11 @@ hrequest_t *hrequest_new_from_buffer(char *data) } -void hrequest_free(hrequest_t *req) +void +hrequest_free(hrequest_t * req) { - if (req == NULL) return; + if (req == NULL) + return; free(req->method); free(req->path); @@ -618,15 +636,17 @@ void hrequest_free(hrequest_t *req) /* response stuff */ -/* ------------------------------------------- -FUNCTION: hresponse_new ----------------------------------------------*/ -hresponse_t *hresponse_new() +/* + * ------------------------------------------- FUNCTION: hresponse_new + * --------------------------------------------- + */ +hresponse_t * +hresponse_new() { - hresponse_t *res; + hresponse_t *res; /* create response object */ - res = (hresponse_t*)malloc(sizeof(hresponse_t)); + res = (hresponse_t *) malloc(sizeof(hresponse_t)); res->spec[0] = '\0'; res->errcode = -1; res->desc = NULL; @@ -638,13 +658,15 @@ hresponse_t *hresponse_new() } -/* ------------------------------------------- -FUNCTION: hresponse_new_from_buffer ----------------------------------------------*/ -hresponse_t *hresponse_new_from_buffer(const char* buffer) +/* + * ------------------------------------------- FUNCTION: + * hresponse_new_from_buffer --------------------------------------------- + */ +hresponse_t * +hresponse_new_from_buffer(const char *buffer) { - hresponse_t *res; - char *s1, *s2, *str; + hresponse_t *res; + char *s1, *s2, *str; /* create response object */ res = hresponse_new(); @@ -653,25 +675,31 @@ hresponse_t *hresponse_new_from_buffer(const char* buffer) /* [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"); return NULL; } - + if (str == NULL) { + log_error1("Parse error"); + return NULL; + } strncpy(res->spec, str, 10); /* 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"); return NULL; } - + if (str == NULL) { + log_error1("Parse error"); + return NULL; + } 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"); return NULL; } - - res->desc = (char*)malloc(strlen(str)+1); + if (str == NULL) { + log_error1("Parse error"); + return NULL; + } + res->desc = (char *) malloc(strlen(str) + 1); strcpy(res->desc, str); res->desc[strlen(str)] = '\0'; @@ -685,13 +713,11 @@ hresponse_t *hresponse_new_from_buffer(const char* buffer) if (str == NULL) { return res; } - /* check also for end of header */ if (!strcmp(str, "\r")) { break; } - - str[strlen(str)-1] = '\0'; + str[strlen(str) - 1] = '\0'; res->header = hpairnode_parse(str, ":", res->header); } @@ -703,7 +729,8 @@ hresponse_t *hresponse_new_from_buffer(const char* buffer) } -void hresponse_free(hresponse_t *res) +void +hresponse_free(hresponse_t * res) { - /* not implemented yet!*/ + /* not implemented yet! */ } diff --git a/nanohttp/nanohttp-server.c b/nanohttp/nanohttp-server.c index 807ab58..9b51cd6 100644 --- a/nanohttp/nanohttp-server.c +++ b/nanohttp/nanohttp-server.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-server.c,v 1.14 2004/08/31 16:34:08 rans Exp $ +* $Id: nanohttp-server.c,v 1.15 2004/09/01 07:58:08 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -18,7 +18,7 @@ * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. -* +* * Email: ayaz@jprogrammer.net ******************************************************************/ #include <nanohttp/nanohttp-server.h> @@ -42,45 +42,48 @@ #endif -/* ----------------------------------------------------- -nano httpd internally globals ------------------------------------------------------ */ -static int _httpd_port = 10000; -static int _httpd_max_connections = 20; -static int _httpd_max_idle = 120; +/* + * ----------------------------------------------------- nano httpd + * internally globals ----------------------------------------------------- + */ +static int _httpd_port = 10000; +static int _httpd_max_connections = 20; +static int _httpd_max_idle = 120; static hsocket_t _httpd_socket; static hservice_t *_httpd_services_head = NULL; static hservice_t *_httpd_services_tail = NULL; -static int _httpd_run = 1; -static int _httpd_terminate_signal = SIGTERM; +static int _httpd_run = 1; +static int _httpd_terminate_signal = SIGTERM; static conndata_t *_httpd_connection; #ifdef WIN32 #include <nanohttp/nanohttp-windows.h> #endif -/* ----------------------------------------------------- -FUNCTION: httpd_init
-NOTE: This will be called from soap_server_init_args() ------------------------------------------------------ */ -int httpd_init(int argc, char *argv[]) +/* + * ----------------------------------------------------- FUNCTION: httpd_init + * NOTE: This will be called from soap_server_init_args() + * ----------------------------------------------------- + */ +int +httpd_init(int argc, char *argv[]) { - int i, status; -
status = hsocket_module_init();
- if (status != 0)
- return status;
+ int i, status; + status = hsocket_module_init(); + if (status != 0) + return status; /* write argument information */ - log_verbose1("Arguments:"); - for (i=0;i<argc;i++) + log_verbose1("Arguments:"); + for (i = 0; i < argc; 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) { - _httpd_port = atoi(argv[i+1]); - } else if (!strcmp(argv[i], NHTTPD_ARG_TERMSIG) && i < argc-1) { - _httpd_terminate_signal = atoi(argv[i+1]); + for (i = 0; i < argc; i++) { + if (!strcmp(argv[i], NHTTPD_ARG_PORT) && i < argc - 1) { + _httpd_port = atoi(argv[i + 1]); + } else if (!strcmp(argv[i], NHTTPD_ARG_TERMSIG) && i < argc - 1) { + _httpd_terminate_signal = atoi(argv[i + 1]); } } @@ -88,15 +91,18 @@ int httpd_init(int argc, char *argv[]) /* init built-in services */ /* - httpd_register("/httpd/list", service_list); - */ - _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])); + * httpd_register("/httpd/list", service_list); + */ + _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])); } #ifdef WIN32 - if (_beginthread(WSAReaper, 0, NULL)==-1) { + if (_beginthread(WSAReaper, 0, NULL) == -1) { log_error1("Winsock reaper thread failed to start"); - return(-1); + return (-1); } #endif @@ -107,21 +113,22 @@ int httpd_init(int argc, char *argv[]) return status; } -/* ----------------------------------------------------- -FUNCTION: httpd_register ------------------------------------------------------ */ +/* + * ----------------------------------------------------- FUNCTION: + * httpd_register ----------------------------------------------------- + */ -int httpd_register(const char* ctx, httpd_service func) +int +httpd_register(const char *ctx, httpd_service func) { - hservice_t* service; + 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); - 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; } else { @@ -133,21 +140,25 @@ int httpd_register(const char* ctx, httpd_service func) } -/* ----------------------------------------------------- -FUNCTION: httpd_services ------------------------------------------------------ */ -hservice_t *httpd_services() +/* + * ----------------------------------------------------- FUNCTION: + * httpd_services ----------------------------------------------------- + */ +hservice_t * +httpd_services() { return _httpd_services_head; } -/* ----------------------------------------------------- -FUNCTION: httpd_find_service ------------------------------------------------------ */ -static hservice_t *httpd_find_service(const char* ctx) +/* + * ----------------------------------------------------- FUNCTION: + * httpd_find_service ----------------------------------------------------- + */ +static hservice_t * +httpd_find_service(const char *ctx) { - hservice_t *cur = _httpd_services_head; + hservice_t *cur = _httpd_services_head; while (cur != NULL) { if (!strcmp(cur->ctx, ctx)) { @@ -160,29 +171,33 @@ static hservice_t *httpd_find_service(const char* ctx) } -/* ----------------------------------------------------- -FUNCTION: httpd_response_set_content_type ------------------------------------------------------ */ -void httpd_response_set_content_type(httpd_conn_t *res, - const char* content_type) +/* + * ----------------------------------------------------- FUNCTION: + * httpd_response_set_content_type + * ----------------------------------------------------- + */ +void +httpd_response_set_content_type(httpd_conn_t * res, const char *content_type) { strncpy(res->content_type, content_type, 25); } -/* ----------------------------------------------------- -FUNCTION: httpd_response_send_header ------------------------------------------------------ */ -int httpd_send_header(httpd_conn_t *res, - int code, const char* text, - hpair_t *pair) +/* + * ----------------------------------------------------- FUNCTION: + * httpd_response_send_header + * ----------------------------------------------------- + */ +int +httpd_send_header(httpd_conn_t * res, + int code, const char *text, hpair_t * pair) { - struct tm stm; - time_t nw; - char buffer[255]; - char header[1024]; - hpair_t *cur; - int status; + struct tm stm; + time_t nw; + char buffer[255]; + char header[1024]; + hpair_t *cur; + int status; /* set status code */ sprintf(header, "HTTP/1.1 %d %s\r\n", code, text); @@ -196,13 +211,10 @@ int httpd_send_header(httpd_conn_t *res, /* set content-type */ /* - if (res->content_type[0] == '\0') { - strcat(header, "Content-Type: text/html\r\n"); - } else { - sprintf(buffer, "Content-Type: %s\r\n", res->content_type); - strcat(header, buffer); - } - */ + * if (res->content_type[0] == '\0') { strcat(header, "Content-Type: + * text/html\r\n"); } else { sprintf(buffer, "Content-Type: %s\r\n", + * res->content_type); strcat(header, buffer); } + */ /* set server name */ strcat(header, "Server: Nano HTTPD library\r\n"); @@ -228,23 +240,26 @@ int httpd_send_header(httpd_conn_t *res, } -int httpd_send_internal_error(httpd_conn_t *conn, const char* errmsg) +int +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"; + const char *template1 = + "<html><body><h3>Error!</h3><hr> Message: '%s' </body></html>\r\n"; - char buffer[4064]; + char buffer[4064]; sprintf(buffer, template1, errmsg); httpd_send_header(conn, 500, "INTERNAL", NULL); return send(conn->sock, buffer, strlen(buffer), 0); } -/* ----------------------------------------------------- -FUNCTION: httpd_request_print ------------------------------------------------------ */ -static void httpd_request_print(hrequest_t *req) +/* + * ----------------------------------------------------- FUNCTION: + * httpd_request_print ----------------------------------------------------- + */ +static void +httpd_request_print(hrequest_t * req) { - hpair_t *pair; + hpair_t *pair; log_verbose1("++++++ Request +++++++++"); log_verbose2(" Method : '%s'", req->method); @@ -261,55 +276,60 @@ static void httpd_request_print(hrequest_t *req) } -/* ----------------------------------------------------- -FUNCTION: httpd_session_main ------------------------------------------------------ */ +/* + * ----------------------------------------------------- FUNCTION: + * httpd_session_main ----------------------------------------------------- + */ #ifdef WIN32 -static unsigned _stdcall httpd_session_main(void *data) +static unsigned _stdcall +httpd_session_main(void *data) #else -static void* httpd_session_main(void *data) +static void * +httpd_session_main(void *data) #endif { - conndata_t *conn = (conndata_t*)data; - const char *msg = "SESSION 1.0\n"; - int len = strlen(msg); - char ch[2]; - char buffer[256]; /* temp buffer for recv() */ - char header[4064]; /* received header */ - int total; /* result from recv() */ - int headerreached =0; /* whether reach header "\n\n" */ - hrequest_t* req = NULL; /* only for test */ - httpd_conn_t *rconn; - hservice_t* service = NULL; - long content_length = 0; + conndata_t *conn = (conndata_t *) data; + const char *msg = "SESSION 1.0\n"; + int len = strlen(msg); + char ch[2]; + char buffer[256]; /* temp buffer for recv() */ + char header[4064]; /* received header */ + int total; /* result from recv() */ + int headerreached = 0; /* whether reach header + * "\n\n" */ + hrequest_t *req = NULL; /* only for test */ + httpd_conn_t *rconn; + hservice_t *service = NULL; + long content_length = 0; header[0] = '\0'; len = 0; log_verbose1("starting httpd_session_main()"); - conn->atime=time((time_t)0); + conn->atime = time((time_t) 0); while (len < 4064) { - /*printf("receiving ...\n");*/ + /* printf("receiving ...\n"); */ total = recv(conn->sock, ch, 1, 0); - if (total==0) break; + if (total == 0) + break; header[len] = ch[0]; len++; if (len > 3) { - if (!strncmp(&header[len-4], "\r\n\r\n", 4)) { + if (!strncmp(&header[len - 4], "\r\n\r\n", 4)) { header[len] = '\0'; break; } } } - /* log_verbose2("=== HEADER ===\n%s\n============\n", header);*/ + /* log_verbose2("=== HEADER ===\n%s\n============\n", header); */ /* call the service */ req = hrequest_new_from_buffer(header); httpd_request_print(req); - rconn = (httpd_conn_t*)malloc(sizeof(httpd_conn_t)); + rconn = (httpd_conn_t *) malloc(sizeof(httpd_conn_t)); rconn->sock = conn->sock; rconn->content_type[0] = '\0'; @@ -319,8 +339,8 @@ static void* httpd_session_main(void *data) if (service->func != NULL) { service->func(rconn, req); } else { - sprintf(buffer, - "service '%s' not registered properly (func == NULL)", + sprintf(buffer, + "service '%s' not registered properly (func == NULL)", req->path); log_verbose1(buffer); httpd_send_internal_error(rconn, buffer); @@ -332,70 +352,67 @@ static void* httpd_session_main(void *data) } close(conn->sock); + conn->sock = 0; - /* httpd_response_free(res);*/ + /* httpd_response_free(res); */ hrequest_free(req); #ifdef WIN32 - CloseHandle((HANDLE)conn->tid); + CloseHandle((HANDLE) conn->tid); _endthread(); return 0; #else pthread_exit(NULL); return service; #endif -} +} -/* ----------------------------------------------------- -FUNCTION: httpd_term ------------------------------------------------------ */ -void httpd_term(int sig) +/* + * ----------------------------------------------------- FUNCTION: httpd_term + * ----------------------------------------------------- + */ +void +httpd_term(int sig) { if (sig == _httpd_terminate_signal) _httpd_run = 0; } -/* ----------------------------------------------------- -FUNCTION: httpd_run ------------------------------------------------------ */ +/* + * ----------------------------------------------------- FUNCTION: httpd_run + * ----------------------------------------------------- + */ -int httpd_run() +int +httpd_run() { - int err; - fd_set fds; - struct timeval timeout; + int err; + fd_set fds; + struct timeval timeout; -#ifndef WIN32 -pthread_attr_init(&attr); -#ifdef PTHREAD_CREATE_DETACHED - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); -#endif -#endif - log_verbose1("starting run routine"); timeout.tv_sec = 1; timeout.tv_usec = 0; /* listen to port */ - err = hsocket_listen(_httpd_socket,15); - if (err != HSOCKET_OK) { + err = hsocket_listen(_httpd_socket, 15); + if (err != HSOCKET_OK) { log_error2("httpd_run(): '%d'", err); return err; } - - log_verbose2("registering termination signal handler (SIGNAL:%d)", _httpd_terminate_signal); + log_verbose2("registering termination signal handler (SIGNAL:%d)", + _httpd_terminate_signal); signal(_httpd_terminate_signal, httpd_term); log_verbose2("listening to port '%d'", _httpd_port); - err=hsocket_makenonblock(_httpd_socket); - if (err != HSOCKET_OK) { + err = hsocket_makenonblock(_httpd_socket); + if (err != HSOCKET_OK) { log_error2("httpd_run(): '%d'", err); return err; } - timeout.tv_sec = 1; timeout.tv_usec = 0; @@ -407,22 +424,20 @@ pthread_attr_init(&attr); #ifndef WIN32 select(1, &fds, NULL, NULL, &timeout); #else - if(select(1, &fds, NULL, NULL, &timeout)==SOCKET_ERROR) - { - err=WSAGetLastError(); + if (select(1, &fds, NULL, NULL, &timeout) == SOCKET_ERROR) { + err = WSAGetLastError(); log_error1("select error"); return -1; } #endif - while (_httpd_run && (FD_ISSET(_httpd_socket, &fds))) - + while (_httpd_run && (FD_ISSET(_httpd_socket, &fds))) if (!_httpd_run) break; - if (hsocket_accept(_httpd_socket, httpd_session_main, _httpd_connection, - _httpd_max_connections) != 0) - { + if (hsocket_accept + (_httpd_socket, httpd_session_main, _httpd_connection, + _httpd_max_connections) != 0) { continue; } } @@ -430,16 +445,18 @@ pthread_attr_init(&attr); return 0; } -char *httpd_get_postdata(httpd_conn_t *conn, hrequest_t *req, long *received, long max) +char * +httpd_get_postdata(httpd_conn_t * conn, hrequest_t * req, long *received, + long max) { - char *content_length_str; - long content_length = 0; - long total = 0; - char *postdata = NULL; + char *content_length_str; + long content_length = 0; + long total = 0; + char *postdata = NULL; if (!strcmp(req->method, "POST")) { - content_length_str = + content_length_str = hpairnode_get_ignore_case(req->header, HEADER_CONTENT_LENGTH); if (content_length_str != NULL) @@ -448,32 +465,28 @@ char *httpd_get_postdata(httpd_conn_t *conn, hrequest_t *req, long *received, lo } else { log_warn1("Not a POST method"); return NULL; - } + } - if (content_length > max && max != -1) + if (content_length > max && max != -1) return NULL; if (content_length == 0) { *received = 0; - postdata = (char*)malloc(1); + postdata = (char *) malloc(1); postdata[0] = '\0'; return postdata; } - - postdata = (char*)malloc(content_length+1); + postdata = (char *) malloc(content_length + 1); if (postdata == NULL) { log_error1("Not enough memory"); return NULL; } - - - if (hsocket_read(conn->sock, postdata, - (int)content_length, 1) == HSOCKET_OK) { - *received = content_length; - postdata[content_length] = '\0'; - return postdata; - } - - free (postdata); - return NULL; + if (hsocket_read(conn->sock, postdata, + (int) content_length, 1) == HSOCKET_OK) { + *received = content_length; + postdata[content_length] = '\0'; + return postdata; + } + free(postdata); + return NULL; } diff --git a/nanohttp/nanohttp-socket.c b/nanohttp/nanohttp-socket.c index 844a5ef..a7882b1 100644 --- a/nanohttp/nanohttp-socket.c +++ b/nanohttp/nanohttp-socket.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-socket.c,v 1.13 2004/08/31 16:34:08 rans Exp $ +* $Id: nanohttp-socket.c,v 1.14 2004/09/01 07:58:08 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -18,7 +18,7 @@ * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. -* +* * Email: ayaz@jprogrammer.net ******************************************************************/ #include <nanohttp/nanohttp-socket.h> @@ -35,27 +35,27 @@ typedef int ssize_t; #endif #ifdef HAVE_CONFIG_H -# include "config.h" -#endif +#include "config.h" +#endif #ifdef HAVE_SYS_TYPES_H -# include <sys/types.h> +#include <sys/types.h> #endif #ifdef HAVE_SYS_SOCKET_H -# include <sys/socket.h> +#include <sys/socket.h> #endif #ifdef HAVE_ARPA_INET_H -# include <arpa/inet.h> +#include <arpa/inet.h> #endif #ifdef HAVE_NETDB_H -# include <netdb.h> +#include <netdb.h> #endif #ifdef HAVE_UNISTD_H -# include <unistd.h> +#include <unistd.h> #endif #include <stdio.h> @@ -63,327 +63,383 @@ typedef int ssize_t; /*-------------------------------------------------- FUNCTION: hsocket_module_init -NOTE: This will be called from httpd_init()
- for server and from httpc_init() for client
+NOTE: This will be called from httpd_init() + for server and from httpc_init() for client ----------------------------------------------------*/ -int hsocket_module_init() +int +hsocket_module_init () { -#ifdef WIN32
- // WSACleanup();
- struct WSAData info;
- WSAStartup(MAKEWORD(2,2), &info);
-#else
- /* nothing to init for unix sockets */
-#endif
- return 0; +#ifdef WIN32 + //WSACleanup(); + struct WSAData info; + WSAStartup (MAKEWORD (2, 2), &info); + +#else /* */ + /* nothing to init for unix sockets */ +#endif /* */ + return 0; } /*-------------------------------------------------- -FUNCTION: hsocket_module_destroy
+FUNCTION: hsocket_module_destroy ----------------------------------------------------*/ -void hsocket_module_destroy() +void +hsocket_module_destroy () { -#ifdef WIN32
- WSACleanup();
-#else
- /* nothing to destroy for unix sockets */
-#endif
+#ifdef WIN32 + WSACleanup (); + +#else /* */ + /* nothing to destroy for unix sockets */ +#endif /* */ } /*-------------------------------------------------- FUNCTION: hsocket_init ----------------------------------------------------*/ -int hsocket_init(hsocket_t *sock) +int +hsocket_init (hsocket_t * sock) { - /* just set the descriptor to -1 */ - *sock = -1; - return 0; + /* just set the descriptor to -1 */ + *sock = -1; + return 0; } /*-------------------------------------------------- FUNCTION: hsocket_free ----------------------------------------------------*/ -void hsocket_free(hsocket_t sock) +void +hsocket_free (hsocket_t sock) { - /* nothing to free for unix sockets */ + /* nothing to free for unix sockets */ } /*-------------------------------------------------- FUNCTION: hsocket_open ----------------------------------------------------*/ -int hsocket_open(hsocket_t *dsock, const char* hostname, int port) +int +hsocket_open (hsocket_t * dsock, const char *hostname, int port) { - hsocket_t sock; - char *ip; - struct sockaddr_in address; - struct hostent* host; - - sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock <= 0) return HSOCKET_CAN_NOT_CREATE; - - /* Get host data */ - host = gethostbyname(hostname); - if (host == NULL) return HSOCKET_CAN_NOT_GET_HOSTNAME; - - 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(port); - /* connect to the server */ - if (connect(sock, (struct sockaddr*) &address, sizeof(address)) != 0) - return HSOCKET_CAN_NOT_CONNECT; - - *dsock = sock; - return HSOCKET_OK; + hsocket_t sock; + char *ip; + struct sockaddr_in address; + struct hostent *host; + + sock = socket (AF_INET, SOCK_STREAM, 0); + if (sock <= 0) + return HSOCKET_CAN_NOT_CREATE; + + /* Get host data */ + host = gethostbyname (hostname); + if (host == NULL) + return HSOCKET_CAN_NOT_GET_HOSTNAME; + + 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 (port); + /* connect to the server */ + if (connect (sock, (struct sockaddr *) &address, sizeof (address)) != 0) + return HSOCKET_CAN_NOT_CONNECT; + + *dsock = sock; + return HSOCKET_OK; } /*-------------------------------------------------- FUNCTION: hsocket_bind ----------------------------------------------------*/ -int hsocket_bind(hsocket_t *dsock, int port) +int +hsocket_bind (hsocket_t * dsock, int port) { - hsocket_t sock; - struct sockaddr_in addr; - - /* create socket */ - sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock == -1) { - log_error2("Can not create socket: '%s'", strerror(errno)); - return HSOCKET_CAN_NOT_CREATE; - } - - /* bind socket */ - addr.sin_family = AF_INET; - addr.sin_port = htons(port); /* short, network byte order */ - addr.sin_addr.s_addr = INADDR_ANY; - memset(&(addr.sin_zero), '\0', 8); /* zero the rest of the struct */ - - if (bind(sock, (struct sockaddr *)&addr, - sizeof(struct sockaddr)) == -1) { - log_error2("Can not bind: '%s'", strerror(errno)); - return HSOCKET_CAN_NOT_BIND; - } - - *dsock = sock; + hsocket_t sock; + struct sockaddr_in addr; + + /* create socket */ + sock = socket (AF_INET, SOCK_STREAM, 0); + if (sock == -1) + { + log_error2 ("Can not create socket: '%s'", strerror (errno)); + return HSOCKET_CAN_NOT_CREATE; + } + /* bind socket */ + addr.sin_family = AF_INET; + addr.sin_port = htons (port); /* short, network byte order */ + addr.sin_addr.s_addr = INADDR_ANY; + memset (&(addr.sin_zero), '\0', 8); /* zero the rest of the + * struct */ + + if (bind (sock, (struct sockaddr *) &addr, sizeof (struct sockaddr)) == -1) + { + log_error2 ("Can not bind: '%s'", strerror (errno)); + return HSOCKET_CAN_NOT_BIND; + } + *dsock = sock; #ifdef WIN32 - // setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)true, sizeof(BOOL)); - // setsockopt(sock, SOL_SOCKET, SO_CONDITIONAL_ACCEPT, (char*)true, sizeof(BOOL)); + //setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char *) true, sizeof(BOOL)); + //setsockopt(sock, SOL_SOCKET, SO_CONDITIONAL_ACCEPT, (char *) true, sizeof(BOOL)); #endif - return HSOCKET_OK; + return HSOCKET_OK; } #ifdef WIN32 -int hsocket_accept(hsocket_t sock, unsigned ( __stdcall *func )( void * ), conndata_t *conns, - int max_conn) +int +hsocket_accept (hsocket_t sock, unsigned (__stdcall * func) (void *), + conndata_t * conns, int max_conn) #else -int hsocket_accept(hsocket_t sock, void(*func) (void *), conndata_t *conns, int max_conn) +int +hsocket_accept (hsocket_t sock, void (*func) (void *), conndata_t * conns, + int max_conn) #endif { - int i; - int err; - socklen_t asize; - struct sockaddr_in addr; - - asize = sizeof(struct sockaddr_in); - while(1) + int i; + int err; + socklen_t asize; + struct sockaddr_in addr; + + asize = sizeof (struct sockaddr_in); + while (1) + { + for (i = 0;; i++) { - for (i=0;;i++) { - if (i>=max_conn) { - Sleep(1000); - i=0; - continue; - } - if (conns[i].sock==0) break; - } - conns[i].sock = accept(sock, (struct sockaddr *)&addr, &asize); + if (i >= max_conn) + { +#ifdef WIN32 + Sleep (1000); +#else + log_verbose2 ("max connection i=%d", i); + sleep (1); +#endif + i = 0; + continue; + } + if (conns[i].sock == 0) + break; + } + conns[i].sock = accept (sock, (struct sockaddr *) &addr, &asize); #ifndef WIN32 - if (conns[i].sock == -1) { - conns[i].sock=0; - continue; - } + if (conns[i].sock == -1) + { + conns[i].sock = 0; + continue; + } #else - if (conns[i].sock == INVALID_SOCKET)
- { - if(WSAGetLastError()!=WSAEWOULDBLOCK) - { - log_error1("accept() died... restarting..."); - closesocket(sock); - WSACleanup(); - return INVALID_SOCKET; - } - else - { - conns[i].sock=0; - continue; - } - } + if (conns[i].sock == INVALID_SOCKET) + { + if (WSAGetLastError () != WSAEWOULDBLOCK) + { + log_error1 ("accept() died... restarting..."); + closesocket (sock); + WSACleanup (); + return INVALID_SOCKET; + } + else + { + conns[i].sock = 0; + continue; + } + } #endif - else - { - log_verbose3("accept new socket (%d) from '%s'", conns[i].sock, - SAVE_STR(((char*)inet_ntoa(addr.sin_addr))) ); + else + { + log_verbose3 ("accept new socket (%d) from '%s'", conns[i].sock, + SAVE_STR (((char *) inet_ntoa (addr.sin_addr)))); #ifdef WIN32 - conns[i].tid=(HANDLE)_beginthreadex(NULL, 65535, func, &conns[i], 0, &err); + conns[i].tid = + (HANDLE) _beginthreadex (NULL, 65535, func, &conns[i], 0, &err); #else - err = pthread_create(&(conns[i].tid), &(conns[i].attr), func, &conns[i]); + pthread_attr_init (&(conns[i].attr)); +#ifdef PTHREAD_CREATE_DETACHED + pthread_attr_setdetachstate (&(conns[i].attr), + PTHREAD_CREATE_DETACHED); +#endif + + err = + pthread_create (&(conns[i].tid), &(conns[i].attr), func, + &conns[i]); #endif - if (err) { - log_error2("Error creating thread: ('%d')", err); - } - } + if (err) + { + log_error2 ("Error creating thread: ('%d')", err); + } } - return 0; + } + return 0; } /*-------------------------------------------------- FUNCTION: hsocket_listen ----------------------------------------------------*/ -int hsocket_listen(hsocket_t sock, int n) +int +hsocket_listen (hsocket_t sock, int n) { - if (listen(sock, n) == -1) { - log_error2("Can not listen: '%s'", strerror(errno)); - return HSOCKET_CAN_NOT_LISTEN; - } - return HSOCKET_OK; + if (listen (sock, n) == -1) + { + log_error2 ("Can not listen: '%s'", strerror (errno)); + return HSOCKET_CAN_NOT_LISTEN; + } + return HSOCKET_OK; } /*-------------------------------------------------- FUNCTION: hsocket_close ----------------------------------------------------*/ -void hsocket_close(hsocket_t sock) +void +hsocket_close (hsocket_t sock) { - close(sock); + close (sock); } /*-------------------------------------------------- FUNCTION: hsocket_send ----------------------------------------------------*/ -int hsocket_nsend(hsocket_t sock, const char* buffer, int n) +int +hsocket_nsend (hsocket_t sock, const char *buffer, int n) { - int size; + int size; - size = send((int)sock, buffer, n, 0); - if (size == -1) - return HSOCKET_CAN_NOT_SEND; + size = send ((int) sock, buffer, n, 0); + if (size == -1) + return HSOCKET_CAN_NOT_SEND; - return HSOCKET_OK; + return HSOCKET_OK; } /*-------------------------------------------------- FUNCTION: hsocket_send ----------------------------------------------------*/ -int hsocket_send(hsocket_t sock, const char* buffer) +int +hsocket_send (hsocket_t sock, const char *buffer) { - int size; - size = send((int)sock, buffer, strlen(buffer), 0); - if (size == -1) - return HSOCKET_CAN_NOT_SEND; + int size; + size = send ((int) sock, buffer, strlen (buffer), 0); + if (size == -1) + return HSOCKET_CAN_NOT_SEND; - return HSOCKET_OK; + return HSOCKET_OK; } -int hsocket_read(hsocket_t sock, char* buffer, int total, int force) +int +hsocket_read (hsocket_t sock, char *buffer, int total, int force) { - int status; - int totalRead; + int status; + int totalRead; - totalRead = 0; + totalRead = 0; - do { - status = recv(sock, &buffer[totalRead], total - totalRead, 0); - if (!force) - { + do + { + status = recv (sock, &buffer[totalRead], total - totalRead, 0); + if (!force) + { #ifdef WIN32 - if(WSAGetLastError()!=WSAEWOULDBLOCK) - { - return status; - } + if (WSAGetLastError () != WSAEWOULDBLOCK) + { + return status; + } #else - return status; + return status; #endif - } - if (status > 0) { - totalRead += status; - } else { + } + if (status > 0) + { + totalRead += status; + } + else + { #ifdef WIN32 - if(WSAGetLastError()!=WSAEWOULDBLOCK) - { - return status; - } + if (WSAGetLastError () != WSAEWOULDBLOCK) + { + return status; + } #else - return status; + return status; #endif - } - if (totalRead >= total) - return 0; - } while (1); + } + if (totalRead >= total) + return 0; + } + while (1); } /*-------------------------------------------------- FUNCTION: hsocket_recv ----------------------------------------------------*/ -int hsocket_recv(hsocket_t sock, char** buffer, int *totalSize) +int +hsocket_recv (hsocket_t sock, char **buffer, int *totalSize) { - ssize_t size; - int chunk=1; - char tmp[HSOCKET_MAX_BUFSIZE+1]; - int fsize; - int bufSize; - - if (*totalSize > 0) { - bufSize = *totalSize; - } else { - bufSize = HSOCKET_MAX_BUFSIZE; + ssize_t size; + int chunk = 1; + char tmp[HSOCKET_MAX_BUFSIZE + 1]; + int fsize; + int bufSize; + + if (*totalSize > 0) + { + bufSize = *totalSize; + } + else + { + bufSize = HSOCKET_MAX_BUFSIZE; + } + + *totalSize = 0; + + /* calculate first size for realloc */ + if (*buffer) + { + fsize = strlen (*buffer); + } + else + { + fsize = 0; + } + + do + { + + size = recv (sock, tmp, bufSize, 0); + bufSize = HSOCKET_MAX_BUFSIZE; + + if (size == -1) + { + log_error1 ("Error reading from socket"); + return HSOCKET_CAN_NOT_RECEIVE; } - - *totalSize = 0; - - /* calculate first size for realloc */ - if (*buffer) { - fsize = strlen(*buffer); - } else { - fsize = 0; + if (size == 0) + { + break; + } + *totalSize += size; + if (*buffer) + { + log_verbose2 ("reallocation %d bytes", *totalSize + fsize + 1); + *buffer = (char *) realloc ((char *) *buffer, + (*totalSize) + fsize + + HSOCKET_MAX_BUFSIZE); + strcat (*buffer, tmp); + } + else + { + *buffer = (char *) realloc (NULL, *totalSize + 1); + strcpy (*buffer, tmp); } - do { - - size = recv(sock, tmp, bufSize, 0); - bufSize = HSOCKET_MAX_BUFSIZE; - - if (size == -1) { - log_error1("Error reading from socket"); - return HSOCKET_CAN_NOT_RECEIVE; - } - - if (size == 0) { - break; - } - - *totalSize += size; - if (*buffer) { - log_verbose2("reallocation %d bytes",*totalSize+fsize+1); - *buffer = (char*)realloc((char*)*buffer, - (*totalSize)+fsize+HSOCKET_MAX_BUFSIZE); - strcat(*buffer, tmp); - } else { - *buffer = (char*)realloc(NULL, *totalSize+1); - strcpy(*buffer, tmp); - } - - (*buffer)[*totalSize+fsize] = '\0'; - chunk++; - } while (size > 0); + (*buffer)[*totalSize + fsize] = '\0'; + chunk++; + } + while (size > 0); - return HSOCKET_OK; + return HSOCKET_OK; } @@ -391,33 +447,35 @@ int hsocket_recv(hsocket_t sock, char** buffer, int *totalSize) /*-------------------------------------------------- FUNCTION: hsocket_recv ----------------------------------------------------*/ -int hsocket_recv_cb(hsocket_t sock, - hsocket_recv_callback cb, void *userdata) +int +hsocket_recv_cb (hsocket_t sock, hsocket_recv_callback cb, void *userdata) { - ssize_t size; - char tmp[HSOCKET_MAX_BUFSIZE+1]; - - do { + ssize_t size; + char tmp[HSOCKET_MAX_BUFSIZE + 1]; - size = recv(sock, tmp, HSOCKET_MAX_BUFSIZE, 0); + do + { - if (size == -1) { - log_error1("Error reading from socket"); - return HSOCKET_CAN_NOT_RECEIVE; - } + size = recv (sock, tmp, HSOCKET_MAX_BUFSIZE, 0); - if (size == 0) { - break; - } - - tmp[size]='\0'; - if (!cb(sock, tmp, size, userdata)) { - break; - } - - } while (size > 0); + if (size == -1) + { + log_error1 ("Error reading from socket"); + return HSOCKET_CAN_NOT_RECEIVE; + } + if (size == 0) + { + break; + } + tmp[size] = '\0'; + if (!cb (sock, tmp, size, userdata)) + { + break; + } + } + while (size > 0); - return HSOCKET_OK; + return HSOCKET_OK; } @@ -425,69 +483,79 @@ int hsocket_recv_cb(hsocket_t sock, /*-------------------------------------------------- FUNCTION: hbufsocket_read ----------------------------------------------------*/ -int hbufsocket_read(hbufsocket_t *bufsock, char *buffer, int size) +int +hbufsocket_read (hbufsocket_t * bufsock, char *buffer, int size) { - int status; - int tmpsize; + int status; + int tmpsize; - if (bufsock->bufsize - bufsock->cur >= size) { + if (bufsock->bufsize - bufsock->cur >= size) + { - log_verbose1("no need to read from socket"); - strncpy(buffer, &(bufsock->buffer[bufsock->cur]), size); - bufsock->cur += size; - return HSOCKET_OK; + log_verbose1 ("no need to read from socket"); + strncpy (buffer, &(bufsock->buffer[bufsock->cur]), size); + bufsock->cur += size; + return HSOCKET_OK; - } else { + } + else + { - tmpsize = bufsock->bufsize - bufsock->cur; - log_verbose2("tmpsize = %d", tmpsize); + tmpsize = bufsock->bufsize - bufsock->cur; + log_verbose2 ("tmpsize = %d", tmpsize); - if (tmpsize > 0) - strncpy(buffer, &(bufsock->buffer[bufsock->cur]), tmpsize); + if (tmpsize > 0) + strncpy (buffer, &(bufsock->buffer[bufsock->cur]), tmpsize); - size -= tmpsize; + size -= tmpsize; - free(bufsock->buffer); + free (bufsock->buffer); - status = hsocket_read(bufsock->sock, &buffer[tmpsize], size, 1); - if (status == size) { - bufsock->buffer = (char*)malloc(size+1); - strncpy(bufsock->buffer, &buffer[tmpsize], size); - bufsock->cur = size; - } else { - return status; - } - - return HSOCKET_OK; + status = hsocket_read (bufsock->sock, &buffer[tmpsize], size, 1); + if (status == size) + { + bufsock->buffer = (char *) malloc (size + 1); + strncpy (bufsock->buffer, &buffer[tmpsize], size); + bufsock->cur = size; + } + else + { + return status; } + + return HSOCKET_OK; + } } -int hsocket_makenonblock(hsocket_t sock) +int +hsocket_makenonblock (hsocket_t sock) { #ifdef WIN32 - unsigned long iMode; - iMode = HSOCKET_NONBLOCKMODE; - if(ioctlsocket(sock, FIONBIO, (u_long FAR*) &iMode) == INVALID_SOCKET) - { - log_error1("ioctlsocket error"); - return -1; - } -#else
fcntl(sock, F_SETFL, O_NONBLOCK); + unsigned long iMode; + iMode = HSOCKET_NONBLOCKMODE; + if (ioctlsocket (sock, FIONBIO, (u_long FAR *) & iMode) == INVALID_SOCKET) + { + log_error1 ("ioctlsocket error"); + return -1; + } +#else /* fcntl(sock, F_SETFL, O_NONBLOCK); */ #endif - return HSOCKET_OK; + return HSOCKET_OK; } #ifdef WIN32 -struct tm *localtime_r(const time_t *const timep, struct tm *p_tm) +struct tm * +localtime_r (const time_t * const timep, struct tm *p_tm) { - static struct tm* tmp; - tmp = localtime(timep); - if (tmp) { - memcpy(p_tm, tmp, sizeof(struct tm)); - tmp = p_tm; - } - return tmp; + static struct tm *tmp; + tmp = localtime (timep); + if (tmp) + { + memcpy (p_tm, tmp, sizeof (struct tm)); + tmp = p_tm; + } + return tmp; } -#endif
+#endif /* */ diff --git a/nanohttp/nanohttp-socket.h b/nanohttp/nanohttp-socket.h index da3f62c..cbed20f 100644 --- a/nanohttp/nanohttp-socket.h +++ b/nanohttp/nanohttp-socket.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-socket.h,v 1.8 2004/08/31 16:34:08 rans Exp $ + * $Id: nanohttp-socket.h,v 1.9 2004/09/01 07:58:08 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -104,7 +104,7 @@ typedef struct tag_conndata HANDLE tid; #else pthread_t tid; - pthread_attr_t attr; + pthread_attr_t attr; #endif time_t atime; }conndata_t; @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: tr.c,v 1.2 2004/06/03 20:23:03 snowdrop Exp $ + * $Id: tr.c,v 1.3 2004/09/01 07:58:08 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -56,11 +56,12 @@ static char* _trXSD2C(const char* xsdType, struct XSD2C_TypeRegistry* head); void trInitModule(const char* ns) { + struct XSD2C_TypeRegistry* cur; tr_head = NULL; tr_tail = NULL; trl_head = NULL; trl_tail = NULL; - struct XSD2C_TypeRegistry* cur; + strcpy(trXSDNS, ns); #include "types.map" |