From c24e5b5135d745098ea6e5c4664a88ada6b99225 Mon Sep 17 00:00:00 2001 From: snowdrop Date: Tue, 10 Jan 2006 11:21:55 +0000 Subject: indent with 'indent -nut -bli0 -fca' command --- examples/csoap/echoattachments-client.c | 227 ++++--- examples/csoap/echoattachments-server.c | 64 +- examples/csoap/simpleclient.c | 61 +- examples/csoap/simpleserver.c | 64 +- examples/csoap/soapclient.c | 301 +++++---- libcsoap/soap-client.c | 353 +++++----- libcsoap/soap-client.h | 17 +- libcsoap/soap-ctx.c | 152 +++-- libcsoap/soap-ctx.h | 19 +- libcsoap/soap-env.c | 191 +++--- libcsoap/soap-env.h | 87 ++- libcsoap/soap-fault.c | 96 +-- libcsoap/soap-fault.h | 14 +- libcsoap/soap-router.c | 118 ++-- libcsoap/soap-router.h | 20 +- libcsoap/soap-server.c | 589 ++++++++-------- libcsoap/soap-server.h | 12 +- libcsoap/soap-service.c | 94 +-- libcsoap/soap-service.h | 20 +- libcsoap/soap-xml.c | 162 +++-- libcsoap/soap-xml.h | 19 +- nanohttp/nanohttp-client.c | 607 +++++++++-------- nanohttp/nanohttp-client.h | 70 +- nanohttp/nanohttp-common.c | 1115 +++++++++++++++++-------------- nanohttp/nanohttp-common.h | 139 ++-- nanohttp/nanohttp-mime.c | 621 ++++++++--------- nanohttp/nanohttp-mime.h | 19 +- nanohttp/nanohttp-request.c | 389 +++++------ nanohttp/nanohttp-request.h | 18 +- nanohttp/nanohttp-response.c | 267 ++++---- nanohttp/nanohttp-response.h | 18 +- nanohttp/nanohttp-server.c | 344 +++++----- nanohttp/nanohttp-server.h | 60 +- nanohttp/nanohttp-socket.c | 438 ++++++------ nanohttp/nanohttp-socket.h | 68 +- nanohttp/nanohttp-ssl.c | 509 +++++++------- nanohttp/nanohttp-ssl.h | 20 +- nanohttp/nanohttp-stream.c | 453 +++++++------ nanohttp/nanohttp-stream.h | 56 +- 39 files changed, 4163 insertions(+), 3728 deletions(-) diff --git a/examples/csoap/echoattachments-client.c b/examples/csoap/echoattachments-client.c index 0d8e9dc..353e1d2 100755 --- a/examples/csoap/echoattachments-client.c +++ b/examples/csoap/echoattachments-client.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: echoattachments-client.c,v 1.8 2004/11/02 23:09:19 snowdrop Exp $ + * $Id: echoattachments-client.c,v 1.9 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: CSOAP examples project * Copyright (C) 2003-2004 Ferhat Ayaz @@ -31,66 +31,79 @@ static const char *url = "http://localhost:10000/echoattachments"; static const char *method = "echo"; -void compareFiles(const char* received, const char *sent) +void +compareFiles (const char *received, const char *sent) { FILE *f1, *f2; char c1, c2; - long s1,s2; + long s1, s2; - printf("Opening received file to compare: '%s'\n", received); - f1 = fopen(received, "r"); - if (!f1) { - fprintf(stderr, "Can not open '%s'\n", received); + printf ("Opening received file to compare: '%s'\n", received); + f1 = fopen (received, "r"); + if (!f1) + { + fprintf (stderr, "Can not open '%s'\n", received); return; } - printf("Opening sent file to compare: '%s'\n", sent); - f2 = fopen(sent, "r"); - if (!f2) { - fprintf(stderr, "Can not open '%s'\n", sent); - fclose(f1); + printf ("Opening sent file to compare: '%s'\n", sent); + f2 = fopen (sent, "r"); + if (!f2) + { + fprintf (stderr, "Can not open '%s'\n", sent); + fclose (f1); return; } - fseek(f1, 0, SEEK_END); - fseek(f2, 0, SEEK_END); + fseek (f1, 0, SEEK_END); + fseek (f2, 0, SEEK_END); - s1 = ftell(f1); - s2 = ftell(f2); + s1 = ftell (f1); + s2 = ftell (f2); - fseek(f1, 0, SEEK_SET); - fseek(f2, 0, SEEK_SET); + fseek (f1, 0, SEEK_SET); + fseek (f2, 0, SEEK_SET); - if (s1 > s2) { + if (s1 > s2) + { - printf("ERROR: files are not equal! Received file is bigger\n"); - fclose(f1); fclose(f2); - return; + printf ("ERROR: files are not equal! Received file is bigger\n"); + fclose (f1); + fclose (f2); + return; - } else if (s2 > s1) { + } + else if (s2 > s1) + { - printf("ERROR: files are not equal! Sent file is bigger\n"); - fclose(f1); fclose(f2); - return; + printf ("ERROR: files are not equal! Sent file is bigger\n"); + fclose (f1); + fclose (f2); + return; } - while (feof(f1)) { - - c1= fgetc(f1); - c2= fgetc(f2); - if (c1 != c2){ - printf("ERROR: files are not equal! Byte compare failed\n"); - fclose(f1); fclose(f2); + while (feof (f1)) + { + + c1 = fgetc (f1); + c2 = fgetc (f2); + if (c1 != c2) + { + printf ("ERROR: files are not equal! Byte compare failed\n"); + fclose (f1); + fclose (f2); break; } } - - printf("OK: files are equal!\n"); - fclose(f1); fclose(f2); - + + printf ("OK: files are equal!\n"); + fclose (f1); + fclose (f2); + } -int main(int argc, char *argv[]) +int +main (int argc, char *argv[]) { SoapCtx *ctx, *ctx2; char href[MAX_HREF_SIZE]; @@ -98,86 +111,88 @@ int main(int argc, char *argv[]) herror_t err; - if (argc < 2) { - fprintf(stderr, "usage: %s [url]\n", argv[0]); - exit(1); + if (argc < 2) + { + fprintf (stderr, "usage: %s [url]\n", argv[0]); + exit (1); + } + + /* + Initialize soap client */ + err = soap_client_init_args (argc, argv); + if (err != H_OK) + { + log_error4 ("[%d] %s():%s ", herror_code (err), herror_func (err), + herror_message (err)); + herror_release (err); + return 1; } - /* - Initialize soap client - */ - err = soap_client_init_args(argc, argv); - if (err != H_OK) { - log_error4("[%d] %s():%s ", herror_code(err), herror_func(err), herror_message(err)); - herror_release(err); - return 1; + /* + Create a context object */ + err = soap_ctx_new_with_method (urn, method, &ctx); + if (err != H_OK) + { + log_error4 ("[%d] %s():%s ", herror_code (err), herror_func (err), + herror_message (err)); + herror_release (err); + return 1; } - /* - Create a context object - */ - err = soap_ctx_new_with_method(urn, method, &ctx); - if (err != H_OK) { - log_error4("[%d] %s():%s ", herror_code(err), herror_func(err), herror_message(err)); - herror_release(err); - return 1; + /* + Add file to the context */ + err = soap_ctx_add_file (ctx, argv[1], "application/octet-stream", href); + if (err != H_OK) + { + log_error4 ("%s():%s [%d]", herror_func (err), herror_message (err), + herror_code (err)); + herror_release (err); + return 1; } - - /* - Add file to the context - */ - err = soap_ctx_add_file(ctx, argv[1], "application/octet-stream", href); - if (err != H_OK) { - log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); - herror_release(err); - return 1; - } - - /* - Add file reference to the envelope - */ - soap_env_add_attachment(ctx->env,"source", href); - - /* - Send soap request to the server - */ - printf("sending request ...\n"); + + /* + Add file reference to the envelope */ + soap_env_add_attachment (ctx->env, "source", href); + + /* + Send soap request to the server */ + printf ("sending request ...\n"); if (argc > 2) - err = soap_client_invoke(ctx, &ctx2, argv[2], ""); + err = soap_client_invoke (ctx, &ctx2, argv[2], ""); else - err = soap_client_invoke(ctx, &ctx2, url, ""); - - if (err != H_OK) { - log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); - herror_release(err); - return 1; + err = soap_client_invoke (ctx, &ctx2, url, ""); + + if (err != H_OK) + { + log_error4 ("%s():%s [%d]", herror_func (err), herror_message (err), + herror_code (err)); + herror_release (err); + return 1; } - /* - Handle response (just print to the screen) - */ - fault = soap_env_get_fault(ctx2->env); - if (fault) { - soap_xml_doc_print(ctx2->env->root->doc); - } else if (ctx2->attachments) { - compareFiles(ctx2->attachments->parts->filename, argv[1]); - } else { - printf("No attachments!"); - soap_xml_doc_print(ctx2->env->root->doc); + /* + Handle response (just print to the screen) */ + fault = soap_env_get_fault (ctx2->env); + if (fault) + { + soap_xml_doc_print (ctx2->env->root->doc); + } + else if (ctx2->attachments) + { + compareFiles (ctx2->attachments->parts->filename, argv[1]); + } + else + { + printf ("No attachments!"); + soap_xml_doc_print (ctx2->env->root->doc); } - /* - Clean up - */ - soap_ctx_free(ctx2); - soap_ctx_free(ctx); + /* + Clean up */ + soap_ctx_free (ctx2); + soap_ctx_free (ctx); - soap_client_destroy(); + soap_client_destroy (); return 0; } - - - - - diff --git a/examples/csoap/echoattachments-server.c b/examples/csoap/echoattachments-server.c index 01299b4..151d020 100755 --- a/examples/csoap/echoattachments-server.c +++ b/examples/csoap/echoattachments-server.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: echoattachments-server.c,v 1.5 2004/11/02 23:09:19 snowdrop Exp $ + * $Id: echoattachments-server.c,v 1.6 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: CSOAP examples project * Copyright (C) 2003-2004 Ferhat Ayaz @@ -31,55 +31,59 @@ static const char *method = "echo"; -herror_t echo_attachments(SoapCtx *req, SoapCtx* res) +herror_t +echo_attachments (SoapCtx * req, SoapCtx * res) { herror_t err; part_t *part; char href[MAX_HREF_SIZE]; - err = soap_env_new_with_response(req->env, &res->env); - if (err != H_OK) { - return err; + err = soap_env_new_with_response (req->env, &res->env); + if (err != H_OK) + { + return err; } - if (req->attachments) + if (req->attachments) { - for (part = req->attachments->parts; part != NULL; part = part->next) - { - soap_ctx_add_file(res, part->filename, part->content_type, href); - soap_env_add_attachment(res->env, "echoFile", href); + for (part = req->attachments->parts; part != NULL; part = part->next) + { + soap_ctx_add_file (res, part->filename, part->content_type, href); + soap_env_add_attachment (res->env, "echoFile", href); } } - + return H_OK; } -int main(int argc, char *argv[]) +int +main (int argc, char *argv[]) { - herror_t err; + herror_t err; SoapRouter *router; - - log_set_level(HLOG_VERBOSE); - - err = soap_server_init_args(argc, argv); - if (err != H_OK) { - log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); - herror_release(err); - return 1; + + log_set_level (HLOG_VERBOSE); + + err = soap_server_init_args (argc, argv); + if (err != H_OK) + { + log_error4 ("%s():%s [%d]", herror_func (err), herror_message (err), + herror_code (err)); + herror_release (err); + return 1; } - - router = soap_router_new(); - soap_router_register_service(router, echo_attachments, method, urn); - soap_server_register_router(router, url); - log_info1("send SIGTERM to shutdown"); - soap_server_run(); + router = soap_router_new (); + soap_router_register_service (router, echo_attachments, method, urn); + soap_server_register_router (router, url); - log_info1("shutting down\n"); - soap_server_destroy(); + log_info1 ("send SIGTERM to shutdown"); + soap_server_run (); + + log_info1 ("shutting down\n"); + soap_server_destroy (); return 0; } - diff --git a/examples/csoap/simpleclient.c b/examples/csoap/simpleclient.c index 29c4c1e..4e0e843 100644 --- a/examples/csoap/simpleclient.c +++ b/examples/csoap/simpleclient.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: simpleclient.c,v 1.9 2004/11/02 23:09:19 snowdrop Exp $ + * $Id: simpleclient.c,v 1.10 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: CSOAP examples project * Copyright (C) 2003-2004 Ferhat Ayaz @@ -29,44 +29,51 @@ static const char *urn = "urn:examples"; static const char *method = "sayHello"; -int main(int argc, char *argv[]) +int +main (int argc, char *argv[]) { SoapCtx *ctx, *ctx2; herror_t err; - - /*log_set_level(HLOG_VERBOSE);*/ - err = soap_client_init_args(argc, argv); - if (err != H_OK) { - log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); - herror_release(err); - return 1; + + /* log_set_level(HLOG_VERBOSE); */ + err = soap_client_init_args (argc, argv); + if (err != H_OK) + { + log_error4 ("%s():%s [%d]", herror_func (err), herror_message (err), + herror_code (err)); + herror_release (err); + return 1; } - err = soap_ctx_new_with_method(urn, method, &ctx); - if (err != H_OK) { - log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); - herror_release(err); - return 1; + err = soap_ctx_new_with_method (urn, method, &ctx); + if (err != H_OK) + { + log_error4 ("%s():%s [%d]", herror_func (err), herror_message (err), + herror_code (err)); + herror_release (err); + return 1; } - soap_env_add_item(ctx->env, "xsd:string", "name", "Jonny B. Good"); - + soap_env_add_item (ctx->env, "xsd:string", "name", "Jonny B. Good"); + if (argc > 1) - err = soap_client_invoke(ctx, &ctx2, argv[1], ""); + err = soap_client_invoke (ctx, &ctx2, argv[1], ""); else - err = soap_client_invoke(ctx, &ctx2, url, ""); + err = soap_client_invoke (ctx, &ctx2, url, ""); - if (err != H_OK) { - log_error4("[%d] %s(): %s ", herror_code(err), herror_func(err), herror_message(err)); - herror_release(err); - soap_ctx_free(ctx); - return 1; + if (err != H_OK) + { + log_error4 ("[%d] %s(): %s ", herror_code (err), herror_func (err), + herror_message (err)); + herror_release (err); + soap_ctx_free (ctx); + return 1; } - soap_xml_doc_print(ctx2->env->root->doc); - soap_ctx_free(ctx2); - soap_ctx_free(ctx); + soap_xml_doc_print (ctx2->env->root->doc); + soap_ctx_free (ctx2); + soap_ctx_free (ctx); - soap_client_destroy(); + soap_client_destroy (); return 0; } diff --git a/examples/csoap/simpleserver.c b/examples/csoap/simpleserver.c index 90c62db..b0d37f9 100644 --- a/examples/csoap/simpleserver.c +++ b/examples/csoap/simpleserver.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: simpleserver.c,v 1.14 2005/07/22 13:27:26 snowdrop Exp $ + * $Id: simpleserver.c,v 1.15 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: CSOAP examples project * Copyright (C) 2003-2004 Ferhat Ayaz @@ -29,7 +29,8 @@ static const char *urn = "urn:examples"; static const char *method = "sayHello"; -herror_t say_hello(SoapCtx *req, SoapCtx* res) +herror_t +say_hello (SoapCtx * req, SoapCtx * res) { herror_t err; @@ -37,49 +38,54 @@ herror_t say_hello(SoapCtx *req, SoapCtx* res) xmlNodePtr method, node; - err = soap_env_new_with_response(req->env, &res->env); - if (err != H_OK) { - return err; + err = soap_env_new_with_response (req->env, &res->env); + if (err != H_OK) + { + return err; } - method = soap_env_get_method(req->env); - node = soap_xml_get_children(method); + method = soap_env_get_method (req->env); + node = soap_xml_get_children (method); - while (node) { - name = (char*)xmlNodeListGetString(node->doc, node->xmlChildrenNode, 1); - soap_env_add_itemf(res->env,"xsd:string", "echo", "Hello '%s'", name); - node = soap_xml_get_next(node); + while (node) + { + name = + (char *) xmlNodeListGetString (node->doc, node->xmlChildrenNode, 1); + soap_env_add_itemf (res->env, "xsd:string", "echo", "Hello '%s'", name); + node = soap_xml_get_next (node); } return H_OK; } -int main(int argc, char *argv[]) +int +main (int argc, char *argv[]) { herror_t err; SoapRouter *router; - - log_set_level(HLOG_INFO); - - err = soap_server_init_args(argc, argv); - if (err != H_OK) { - log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); - herror_release(err); - return 1; + + log_set_level (HLOG_INFO); + + err = soap_server_init_args (argc, argv); + if (err != H_OK) + { + log_error4 ("%s():%s [%d]", herror_func (err), herror_message (err), + herror_code (err)); + herror_release (err); + return 1; } - - router = soap_router_new(); - soap_router_register_service(router, say_hello, method, urn); - soap_server_register_router(router, url); - log_info1("send SIGTERM to shutdown"); - soap_server_run(); + router = soap_router_new (); + soap_router_register_service (router, say_hello, method, urn); + soap_server_register_router (router, url); - log_info1("shutting down\n"); - soap_server_destroy(); + log_info1 ("send SIGTERM to shutdown"); + soap_server_run (); + + log_info1 ("shutting down\n"); + soap_server_destroy (); return 0; } - diff --git a/examples/csoap/soapclient.c b/examples/csoap/soapclient.c index 5603da1..826d518 100644 --- a/examples/csoap/soapclient.c +++ b/examples/csoap/soapclient.c @@ -10,185 +10,244 @@ #define MAX_LINE_LENGTH 65535 // stripcslashes - unescapes the string -void stripcslashes(char *str, int *len) { +void +stripcslashes (char *str, int *len) +{ char *source, *target, *end; - int nlen = *len, i; + int nlen = *len, i; char numtmp[4]; - for (source=str, end=str+nlen, target=str; source < end; source++) { - if (*source == '\\' && source+1 < end) { + for (source = str, end = str + nlen, target = str; source < end; source++) + { + if (*source == '\\' && source + 1 < end) + { source++; - switch (*source) { - case 'n': *target++='\n'; nlen--; break; - case 'r': *target++='\r'; nlen--; break; - case 'a': *target++='\a'; nlen--; break; - case 't': *target++='\t'; nlen--; break; - case 'v': *target++='\v'; nlen--; break; - case 'b': *target++='\b'; nlen--; break; - case 'f': *target++='\f'; nlen--; break; - case '\\': *target++='\\'; nlen--; break; + switch (*source) + { + case 'n': + *target++ = '\n'; + nlen--; + break; + case 'r': + *target++ = '\r'; + nlen--; + break; + case 'a': + *target++ = '\a'; + nlen--; + break; + case 't': + *target++ = '\t'; + nlen--; + break; + case 'v': + *target++ = '\v'; + nlen--; + break; + case 'b': + *target++ = '\b'; + nlen--; + break; + case 'f': + *target++ = '\f'; + nlen--; + break; + case '\\': + *target++ = '\\'; + nlen--; + break; case 'x': - if (source+1 < end && isxdigit((int)(*(source+1)))) { - numtmp[0] = *++source; - if (source+1 < end && isxdigit((int)(*(source+1)))) { - numtmp[1] = *++source; - numtmp[2] = '\0'; - nlen-=3; - } else { - numtmp[1] = '\0'; - nlen-=2; - } - *target++=(char)strtol(numtmp, NULL, 16); - break; - } - default: - i=0; - while (source < end && *source >= '0' && *source <= '7' && i<3) { - numtmp[i++] = *source++; - } - if (i) { - numtmp[i]='\0'; - *target++=(char)strtol(numtmp, NULL, 8); - nlen -= i; - source--; - } else { - *target++ = *source; - nlen--; - } + if (source + 1 < end && isxdigit ((int) (*(source + 1)))) + { + numtmp[0] = *++source; + if (source + 1 < end && isxdigit ((int) (*(source + 1)))) + { + numtmp[1] = *++source; + numtmp[2] = '\0'; + nlen -= 3; + } + else + { + numtmp[1] = '\0'; + nlen -= 2; + } + *target++ = (char) strtol (numtmp, NULL, 16); + break; + } + default: + i = 0; + while (source < end && *source >= '0' && *source <= '7' && i < 3) + { + numtmp[i++] = *source++; + } + if (i) + { + numtmp[i] = '\0'; + *target++ = (char) strtol (numtmp, NULL, 8); + nlen -= i; + source--; + } + else + { + *target++ = *source; + nlen--; + } } - } else { + } + else + { *target++ = *source; } } - if (nlen != 0) *target='\0'; + if (nlen != 0) + *target = '\0'; *len = nlen; -} +} // ParseLine - gets a line, finds the commas, unescapes the value and adds it // to the SOAP request -void ParseLine(SoapCtx *ctx, char * Buffer, int LineLen) { +void +ParseLine (SoapCtx * ctx, char *Buffer, int LineLen) +{ // if wrong line length, return - if (LineLen <= 0) return; - + if (LineLen <= 0) + return; + // alloc buffer for the line, copy it - char * Line = (char *)malloc(LineLen + 1); - memcpy(Line, Buffer, LineLen); + char *Line = (char *) malloc (LineLen + 1); + memcpy (Line, Buffer, LineLen); Line[LineLen] = '\0'; - + // find first comma - char * FirstCommaPos = strchr(Line, ','); - if (!FirstCommaPos) return; - + char *FirstCommaPos = strchr (Line, ','); + if (!FirstCommaPos) + return; + // find second comma - char * SecondCommaPos = strchr(FirstCommaPos + 1, ','); - if (!SecondCommaPos) return; - + char *SecondCommaPos = strchr (FirstCommaPos + 1, ','); + if (!SecondCommaPos) + return; + // separate the three strings FirstCommaPos[0] = '\0'; SecondCommaPos[0] = '\0'; - + // unescape - int len = strlen(SecondCommaPos + 1); - stripcslashes(SecondCommaPos + 1, &len); - + int len = strlen (SecondCommaPos + 1); + stripcslashes (SecondCommaPos + 1, &len); + // add to the request - soap_env_add_item(ctx->env, Line, FirstCommaPos + 1, SecondCommaPos - + 1); - + soap_env_add_item (ctx->env, Line, FirstCommaPos + 1, SecondCommaPos + 1); + // free the buffer - free(Line); + free (Line); } -void printusage(char *FileName) { - printf("Usage: %s [URL] [URN] [SOAPMETHOD]\n\n", FileName); - printf("- [URL] is the url of the SOAP server.\n"); - printf("- [URN] is the namespace for the method/webservice.\n"); - printf("- [SOAPMETHOD] is the method to call.\n\n"); - printf("All commandline paramaters are mandatory.\n\n"); - printf("%s reads the parameters for the SOAPMETHOD from STDIN as a comma-separated list\n\n", FileName); - printf("Each line is one parameter and has the following form:\n\n"); - printf("xsd:[TYPE],[PARAMETERNAME],[PARAMETERVALUE]\n\n"); - printf("- [TYPE] is a basic type for SOAP or a complex type defined by the WSDL.\n"); - printf("- [PARAMETERNAME] is the name for the parameter of the SOAPMETHOD on this line.\n"); - printf("- [PARAMETERVALUE] is the value for the parameter. N.B. \nand other escapes are recognized.\n"); +void +printusage (char *FileName) +{ + printf ("Usage: %s [URL] [URN] [SOAPMETHOD]\n\n", FileName); + printf ("- [URL] is the url of the SOAP server.\n"); + printf ("- [URN] is the namespace for the method/webservice.\n"); + printf ("- [SOAPMETHOD] is the method to call.\n\n"); + printf ("All commandline paramaters are mandatory.\n\n"); + printf + ("%s reads the parameters for the SOAPMETHOD from STDIN as a comma-separated list\n\n", + FileName); + printf ("Each line is one parameter and has the following form:\n\n"); + printf ("xsd:[TYPE],[PARAMETERNAME],[PARAMETERVALUE]\n\n"); + printf + ("- [TYPE] is a basic type for SOAP or a complex type defined by the WSDL.\n"); + printf + ("- [PARAMETERNAME] is the name for the parameter of the SOAPMETHOD on this line.\n"); + printf + ("- [PARAMETERVALUE] is the value for the parameter. N.B. \nand other escapes are recognized.\n"); } -int main(int argc, char *argv[]) { +int +main (int argc, char *argv[]) +{ SoapCtx *ctx, *ctx2; herror_t err; - + // check the parameter count - if (argc != 4) { - printusage(argv[0]); + if (argc != 4) + { + printusage (argv[0]); return 1; } - + // init cSOAP client - err = soap_client_init_args(argc, argv); - if (err != H_OK) { - log_error4("%s():%s [%d]", herror_func(err), - herror_message(err), herror_code(err)); - herror_release(err); + err = soap_client_init_args (argc, argv); + if (err != H_OK) + { + log_error4 ("%s():%s [%d]", herror_func (err), + herror_message (err), herror_code (err)); + herror_release (err); return 1; } // create a SoapCtx object - err = soap_ctx_new_with_method(argv[2], argv[3], &ctx); - if (err != H_OK) { - log_error4("%s():%s [%d]", herror_func(err), - herror_message(err), herror_code(err)); - herror_release(err); + err = soap_ctx_new_with_method (argv[2], argv[3], &ctx); + if (err != H_OK) + { + log_error4 ("%s():%s [%d]", herror_func (err), + herror_message (err), herror_code (err)); + herror_release (err); return 1; } - + // create buffer char Buffer[MAX_LINE_LENGTH]; int bytes_read, bytes_left; - + // read from stdin until EOF - while (!feof(stdin)) { - bytes_read = fread(Buffer, 1, MAX_LINE_LENGTH, stdin); + while (!feof (stdin)) + { + bytes_read = fread (Buffer, 1, MAX_LINE_LENGTH, stdin); // pass each line into ParseLine - char * EndLinePos; - while (EndLinePos = strchr(Buffer, '\n')) { - ParseLine(ctx, Buffer, EndLinePos - Buffer); - memmove(Buffer, EndLinePos + 1, bytes_read - (EndLinePos - - Buffer + 1)); + char *EndLinePos; + while (EndLinePos = strchr (Buffer, '\n')) + { + ParseLine (ctx, Buffer, EndLinePos - Buffer); + memmove (Buffer, EndLinePos + 1, bytes_read - (EndLinePos - + Buffer + 1)); Buffer[bytes_read - (EndLinePos - Buffer + 1)] = '\0'; } - + // no '\n' found in the whole Buffer, that means line's too -long - bytes_left = strlen(Buffer); - if (bytes_left == MAX_LINE_LENGTH) { - log_error1("The parameter line is too long."); - herror_release(err); - soap_ctx_free(ctx); - return 1; - } + long bytes_left = strlen (Buffer); + if (bytes_left == MAX_LINE_LENGTH) + { + log_error1 ("The parameter line is too long."); + herror_release (err); + soap_ctx_free (ctx); + return 1; + } } // invoke - err = soap_client_invoke(ctx, &ctx2, argv[1], ""); - if (err != H_OK) { - log_error4("[%d] %s(): %s ", herror_code(err), - herror_func(err), herror_message(err)); - herror_release(err); - soap_ctx_free(ctx); + err = soap_client_invoke (ctx, &ctx2, argv[1], ""); + if (err != H_OK) + { + log_error4 ("[%d] %s(): %s ", herror_code (err), + herror_func (err), herror_message (err)); + herror_release (err); + soap_ctx_free (ctx); return 1; } - + // print the result - soap_xml_doc_print(ctx2->env->root->doc); - + soap_xml_doc_print (ctx2->env->root->doc); + // free the objects - soap_ctx_free(ctx2); - soap_ctx_free(ctx); + soap_ctx_free (ctx2); + soap_ctx_free (ctx); // destroy the cSOAP client - soap_client_destroy(); - + soap_client_destroy (); + return 0; } diff --git a/libcsoap/soap-client.c b/libcsoap/soap-client.c index 5c3aeac..d1599bd 100644 --- a/libcsoap/soap-client.c +++ b/libcsoap/soap-client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: soap-client.c,v 1.19 2005/12/19 14:18:26 snowdrop Exp $ +* $Id: soap-client.c,v 1.20 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -27,236 +27,255 @@ /*--------------------------------- */ static int _block_socket = 0; -static herror_t _soap_client_build_result(hresponse_t *res, SoapEnv **out); +static herror_t _soap_client_build_result (hresponse_t * res, SoapEnv ** out); /*--------------------------------- */ -void soap_client_block_socket(int block) +void +soap_client_block_socket (int block) { - _block_socket = block; + _block_socket = block; } -int soap_client_get_blockmode() +int +soap_client_get_blockmode () { - return _block_socket; + return _block_socket; } -herror_t soap_client_init_args(int argc, char *argv[]) - +herror_t +soap_client_init_args (int argc, char *argv[]) { - return httpc_init(argc, argv); + return httpc_init (argc, argv); } -void soap_client_destroy() +void +soap_client_destroy () { - httpc_destroy(); + httpc_destroy (); } -static -long _file_get_size(const char* filename) +static long +_file_get_size (const char *filename) { - FILE *f = fopen(filename, "r"); + FILE *f = fopen (filename, "r"); long size; - if (!f) return -1; + if (!f) + return -1; - fseek(f, 0, SEEK_END); - size=ftell(f); - fclose(f); + fseek (f, 0, SEEK_END); + size = ftell (f); + fclose (f); return size; } herror_t -soap_client_invoke(SoapCtx *call, SoapCtx** response, const char *url, const char *soap_action) +soap_client_invoke (SoapCtx * call, SoapCtx ** response, const char *url, + const char *soap_action) { /* Status */ herror_t status; - /* Result document */ - SoapEnv* res_env; + /* Result document */ + SoapEnv *res_env; - /* Buffer variables*/ - xmlBufferPtr buffer; - char *content; - char tmp[15]; + /* Buffer variables */ + xmlBufferPtr buffer; + char *content; + char tmp[15]; - /* Transport variables */ - httpc_conn_t *conn; - hresponse_t *res; + /* Transport variables */ + httpc_conn_t *conn; + hresponse_t *res; - /* multipart/related start id*/ - char start_id[150]; - static int counter=1; - part_t *part; + /* multipart/related start id */ + char start_id[150]; + static int counter = 1; + part_t *part; - /* for copy attachments */ + /* for copy attachments */ char href[MAX_HREF_SIZE]; - - /* Create buffer */ - buffer = xmlBufferCreate(); - xmlNodeDump(buffer, call->env->root->doc,call->env->root, 1 ,0); - content = (char*)xmlBufferContent(buffer); - - /* Transport via HTTP */ - conn = httpc_new(); - if(!conn){ - return herror_new("soap_client_invoke", SOAP_ERROR_CLIENT_INIT, "Unable to create SOAP client!" ); - } - conn->block = soap_client_get_blockmode(); - - /* Set soap action */ - if (soap_action != NULL) { - httpc_set_header(conn, "SoapAction", soap_action); - } - - /* check for attachments */ - if (!call->attachments) - { - /* content-type is always 'text/xml' */ - httpc_set_header(conn, HEADER_CONTENT_TYPE, "text/xml"); - - sprintf(tmp, "%d", (int)strlen(content)); - httpc_set_header(conn, HEADER_CONTENT_LENGTH, tmp); - status = httpc_post_begin(conn, url); - - if (status != H_OK) { - httpc_close_free(conn); - xmlBufferFree(buffer); - return status; + + /* Create buffer */ + buffer = xmlBufferCreate (); + xmlNodeDump (buffer, call->env->root->doc, call->env->root, 1, 0); + content = (char *) xmlBufferContent (buffer); + + /* Transport via HTTP */ + conn = httpc_new (); + if (!conn) + { + return herror_new ("soap_client_invoke", SOAP_ERROR_CLIENT_INIT, + "Unable to create SOAP client!"); + } + conn->block = soap_client_get_blockmode (); + + /* Set soap action */ + if (soap_action != NULL) + { + httpc_set_header (conn, "SoapAction", soap_action); + } + + /* check for attachments */ + if (!call->attachments) + { + /* content-type is always 'text/xml' */ + httpc_set_header (conn, HEADER_CONTENT_TYPE, "text/xml"); + + sprintf (tmp, "%d", (int) strlen (content)); + httpc_set_header (conn, HEADER_CONTENT_LENGTH, tmp); + status = httpc_post_begin (conn, url); + + if (status != H_OK) + { + httpc_close_free (conn); + xmlBufferFree (buffer); + return status; } - - status = http_output_stream_write_string(conn->out, content); - if (status != H_OK) { - httpc_close_free(conn); - xmlBufferFree(buffer); - return status; + + status = http_output_stream_write_string (conn->out, content); + if (status != H_OK) + { + httpc_close_free (conn); + xmlBufferFree (buffer); + return status; } - - status = httpc_post_end(conn, &res); - if (status != H_OK) { - httpc_close_free(conn); - xmlBufferFree(buffer); - return status; + + status = httpc_post_end (conn, &res); + if (status != H_OK) + { + httpc_close_free (conn); + xmlBufferFree (buffer); + return status; } - } - else - { - + } + else + { + /* Use chunked transport */ - httpc_set_header(conn, HEADER_TRANSFER_ENCODING, TRANSFER_ENCODING_CHUNKED); - - sprintf(start_id, "289247829121218%d", counter++); - status = httpc_mime_begin(conn, url, start_id, "", "text/xml"); - if (status != H_OK) { - httpc_close_free(conn); - xmlBufferFree(buffer); - return status; - } - - status = httpc_mime_next(conn, start_id, "text/xml", "binary"); - if (status != H_OK) { - httpc_close_free(conn); - xmlBufferFree(buffer); - return status; - } + httpc_set_header (conn, HEADER_TRANSFER_ENCODING, + TRANSFER_ENCODING_CHUNKED); - status = http_output_stream_write(conn->out, content, strlen(content)); - if (status != H_OK) { - httpc_close_free(conn); - xmlBufferFree(buffer); - return status; + sprintf (start_id, "289247829121218%d", counter++); + status = httpc_mime_begin (conn, url, start_id, "", "text/xml"); + if (status != H_OK) + { + httpc_close_free (conn); + xmlBufferFree (buffer); + return status; } - - for (part=call->attachments->parts; part; part=part->next) + status = httpc_mime_next (conn, start_id, "text/xml", "binary"); + if (status != H_OK) { - status = httpc_mime_send_file(conn, part->id, - part->content_type, part->transfer_encoding, part->filename); - if (status != H_OK) { - log_error2("Send file failed. Status:%d", status); - httpc_close_free(conn); - xmlBufferFree(buffer); - return status; - } + httpc_close_free (conn); + xmlBufferFree (buffer); + return status; } - status = httpc_mime_end(conn, &res); + status = http_output_stream_write (conn->out, content, strlen (content)); if (status != H_OK) { - httpc_close_free(conn); - xmlBufferFree(buffer); - return status; + httpc_close_free (conn); + xmlBufferFree (buffer); + return status; } - } - /* Free buffer */ - xmlBufferFree(buffer); - /* Build result */ - status = _soap_client_build_result(res, &res_env); - if (status != H_OK) { - hresponse_free(res); - httpc_close_free(conn); - return status; - } + for (part = call->attachments->parts; part; part = part->next) + { + status = httpc_mime_send_file (conn, part->id, + part->content_type, + part->transfer_encoding, part->filename); + if (status != H_OK) + { + log_error2 ("Send file failed. Status:%d", status); + httpc_close_free (conn); + xmlBufferFree (buffer); + return status; + } + } - /* Create Context */ - *response = soap_ctx_new(res_env); + status = httpc_mime_end (conn, &res); + if (status != H_OK) + { + httpc_close_free (conn); + xmlBufferFree (buffer); + return status; + } + } + + /* Free buffer */ + xmlBufferFree (buffer); + + /* Build result */ + status = _soap_client_build_result (res, &res_env); + if (status != H_OK) + { + hresponse_free (res); + httpc_close_free (conn); + return status; + } + + /* Create Context */ + *response = soap_ctx_new (res_env); /* soap_ctx_add_files(*response, res->attachments);*/ - if (res->attachments!=NULL) { - part = res->attachments->parts; - while (part) { - soap_ctx_add_file(*response, part->filename, part->content_type, href); - part->deleteOnExit = 0; - part = part->next; - } - part = (*response)->attachments->parts; - while (part) { - part->deleteOnExit = 1; - part = part->next; - } - } - - - hresponse_free(res); - httpc_close_free(conn); - return H_OK; + if (res->attachments != NULL) + { + part = res->attachments->parts; + while (part) + { + soap_ctx_add_file (*response, part->filename, part->content_type, href); + part->deleteOnExit = 0; + part = part->next; + } + part = (*response)->attachments->parts; + while (part) + { + part->deleteOnExit = 1; + part = part->next; + } + } + + + hresponse_free (res); + httpc_close_free (conn); + return H_OK; } -static -herror_t - _soap_client_build_result(hresponse_t *res, SoapEnv** env) +static herror_t +_soap_client_build_result (hresponse_t * res, SoapEnv ** env) { - herror_t err; + herror_t err; - log_verbose2("Building result (%p)", res); + log_verbose2 ("Building result (%p)", res); - if (res == NULL) - return herror_new("_soap_client_build_result", - GENERAL_INVALID_PARAM, "hresponse_t is NULL"); + if (res == NULL) + return herror_new ("_soap_client_build_result", + GENERAL_INVALID_PARAM, "hresponse_t is NULL"); - if (res->in == NULL) - return herror_new("_soap_client_build_result", - GENERAL_INVALID_PARAM, "Empty response from server"); + if (res->in == NULL) + return herror_new ("_soap_client_build_result", + GENERAL_INVALID_PARAM, "Empty response from server"); - if (res->errcode != 200) - return herror_new("_soap_client_build_result", - GENERAL_INVALID_PARAM, "HTTP code is not 200 OK"); + if (res->errcode != 200) + return herror_new ("_soap_client_build_result", + GENERAL_INVALID_PARAM, "HTTP code is not 200 OK"); - err = soap_env_new_from_stream(res->in, env); + err = soap_env_new_from_stream (res->in, env); - if (err != H_OK) { - return err; - } + if (err != H_OK) + { + return err; + } - return H_OK; + return H_OK; } - - diff --git a/libcsoap/soap-client.h b/libcsoap/soap-client.h index b99d2aa..0795a29 100644 --- a/libcsoap/soap-client.h +++ b/libcsoap/soap-client.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-client.h,v 1.10 2005/12/19 14:06:16 snowdrop Exp $ + * $Id: soap-client.h,v 1.11 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -32,13 +32,13 @@ /** Initializes the client side soap engine */ -herror_t soap_client_init_args(int argc, char *argv[]); +herror_t soap_client_init_args (int argc, char *argv[]); /** Destroy the soap client module */ -void soap_client_destroy(); +void soap_client_destroy (); /** @@ -53,9 +53,8 @@ void soap_client_destroy(); @returns H_OK if success */ -herror_t soap_client_invoke(SoapCtx *ctx, SoapCtx** response, - const char *url, - const char *soap_action); +herror_t soap_client_invoke (SoapCtx * ctx, SoapCtx ** response, + const char *url, const char *soap_action); @@ -67,9 +66,7 @@ herror_t soap_client_invoke(SoapCtx *ctx, SoapCtx** response, @param block 1 to creat blocked sockets, 0 to create non blocking sockets. */ -void soap_client_block_socket(int block); -int soap_client_get_blockmode(); +void soap_client_block_socket (int block); +int soap_client_get_blockmode (); #endif - - diff --git a/libcsoap/soap-ctx.c b/libcsoap/soap-ctx.c index 291a161..b2d1db2 100755 --- a/libcsoap/soap-ctx.c +++ b/libcsoap/soap-ctx.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-ctx.c,v 1.5 2005/05/27 19:28:15 snowdrop Exp $ + * $Id: soap-ctx.c,v 1.6 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -25,112 +25,128 @@ #include -SoapCtx* soap_ctx_new(SoapEnv *env) /* should only be used internally */ +SoapCtx * +soap_ctx_new (SoapEnv * env) /* should only be used internally */ { - SoapCtx* ctx = (SoapCtx*)malloc(sizeof(SoapCtx)); + SoapCtx *ctx = (SoapCtx *) malloc (sizeof (SoapCtx)); ctx->env = env; ctx->attachments = NULL; - + return ctx; } -void soap_ctx_add_files(SoapCtx* ctx, attachments_t *attachments) +void +soap_ctx_add_files (SoapCtx * ctx, attachments_t * attachments) { part_t *part; char href[MAX_HREF_SIZE]; - - if (attachments==NULL) return; + + if (attachments == NULL) + return; part = attachments->parts; - while (part) { - soap_ctx_add_file(ctx, part->filename, part->content_type, href); + while (part) + { + soap_ctx_add_file (ctx, part->filename, part->content_type, href); part = part->next; } } -herror_t soap_ctx_add_file(SoapCtx* ctx, const char* filename, const char* content_type, char *dest_href) +herror_t +soap_ctx_add_file (SoapCtx * ctx, const char *filename, + const char *content_type, char *dest_href) { char cid[250]; char id[250]; part_t *part; static int counter = 1; - FILE *test = fopen(filename, "r"); - if (!test) - return herror_new("soap_ctx_add_file", FILE_ERROR_OPEN, - "Can not open file '%s'", filename); - - fclose(test); - + FILE *test = fopen (filename, "r"); + if (!test) + return herror_new ("soap_ctx_add_file", FILE_ERROR_OPEN, + "Can not open file '%s'", filename); + + fclose (test); + /* generate an id */ - sprintf(id, "005512345894583%d", counter++); - sprintf(dest_href, "cid:%s", id); - sprintf(cid, "<%s>", id); + sprintf (id, "005512345894583%d", counter++); + sprintf (dest_href, "cid:%s", id); + sprintf (cid, "<%s>", id); /* add part to context */ - part = part_new(cid, filename, content_type, NULL, NULL); - if (!ctx->attachments) ctx->attachments = attachments_new(); - attachments_add_part(ctx->attachments, part); + part = part_new (cid, filename, content_type, NULL, NULL); + if (!ctx->attachments) + ctx->attachments = attachments_new (); + attachments_add_part (ctx->attachments, part); return H_OK; } -part_t *soap_ctx_get_file(SoapCtx* ctx, xmlNodePtr node) +part_t * +soap_ctx_get_file (SoapCtx * ctx, xmlNodePtr node) { - xmlChar *prop; - char href[MAX_HREF_SIZE]; - char buffer[MAX_HREF_SIZE]; - part_t *part; - - if (!ctx->attachments) return NULL; - - prop = xmlGetProp(node, "href"); - - if (!prop) return NULL; - - strcpy(href, (const char*)prop); - if (!strncmp(href, "cid:", 4)) { - for (part = ctx->attachments->parts; part; part=part->next) - { - sprintf(buffer, "<%s>", href+4); - if (!strcmp(part->id, buffer)) - return part; - - } - } else { - for (part = ctx->attachments->parts; part; part=part->next) - { - if (!strcmp(part->location, href)) - return part; - - } - } - - return NULL; + xmlChar *prop; + char href[MAX_HREF_SIZE]; + char buffer[MAX_HREF_SIZE]; + part_t *part; + + if (!ctx->attachments) + return NULL; + + prop = xmlGetProp (node, "href"); + + if (!prop) + return NULL; + + strcpy (href, (const char *) prop); + if (!strncmp (href, "cid:", 4)) + { + for (part = ctx->attachments->parts; part; part = part->next) + { + sprintf (buffer, "<%s>", href + 4); + if (!strcmp (part->id, buffer)) + return part; + + } + } + else + { + for (part = ctx->attachments->parts; part; part = part->next) + { + if (!strcmp (part->location, href)) + return part; + + } + } + + return NULL; } -void soap_ctx_free(SoapCtx* ctx) +void +soap_ctx_free (SoapCtx * ctx) { - if (!ctx) return; - + if (!ctx) + return; + if (ctx->attachments) - attachments_free(ctx->attachments); + attachments_free (ctx->attachments); if (ctx->env) - soap_env_free(ctx->env); - - free(ctx); + soap_env_free (ctx->env); + + free (ctx); } -herror_t soap_ctx_new_with_method(const char *urn, const char *method, SoapCtx **out) +herror_t +soap_ctx_new_with_method (const char *urn, const char *method, SoapCtx ** out) { - SoapEnv *env; - herror_t err; - err = soap_env_new_with_method(urn, method, &env); - if (err != H_OK) return err; - *out = soap_ctx_new(env); + SoapEnv *env; + herror_t err; + err = soap_env_new_with_method (urn, method, &env); + if (err != H_OK) + return err; + *out = soap_ctx_new (env); return H_OK; } - diff --git a/libcsoap/soap-ctx.h b/libcsoap/soap-ctx.h index a8a8337..2df74b9 100755 --- a/libcsoap/soap-ctx.h +++ b/libcsoap/soap-ctx.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-ctx.h,v 1.5 2004/11/02 23:09:26 snowdrop Exp $ + * $Id: soap-ctx.h,v 1.6 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -38,10 +38,10 @@ typedef struct _SoapCtx { SoapEnv *env; attachments_t *attachments; -}SoapCtx; +} SoapCtx; -SoapCtx* soap_ctx_new(SoapEnv *env); /* should only be used internally */ +SoapCtx *soap_ctx_new (SoapEnv * env); /* should only be used internally */ /** Returns the attached file if any found. @@ -51,21 +51,22 @@ SoapCtx* soap_ctx_new(SoapEnv *env); /* should only be used internally */ @returns a part_t object of attachment was found, NULL otherwise. */ -part_t *soap_ctx_get_file(SoapCtx* ctx, xmlNodePtr node); +part_t *soap_ctx_get_file (SoapCtx * ctx, xmlNodePtr node); /** Creates a new soap context object. */ -herror_t soap_ctx_new_with_method(const char *urn, const char *method, SoapCtx **out); +herror_t soap_ctx_new_with_method (const char *urn, const char *method, + SoapCtx ** out); /* Size of destination dest_href should be MAX_HREF_SIZE */ -herror_t soap_ctx_add_file(SoapCtx* ctx, const char* filename, const char* content_type, char *dest_href); +herror_t soap_ctx_add_file (SoapCtx * ctx, const char *filename, + const char *content_type, char *dest_href); /* Used internally. Will switch the deleteOnExit flag from the given one to the added part. */ -void soap_ctx_add_files(SoapCtx* ctx, attachments_t *attachments); -void soap_ctx_free(SoapCtx* ctx); +void soap_ctx_add_files (SoapCtx * ctx, attachments_t * attachments); +void soap_ctx_free (SoapCtx * ctx); #endif - diff --git a/libcsoap/soap-env.c b/libcsoap/soap-env.c index 2018b3d..ec27137 100644 --- a/libcsoap/soap-env.c +++ b/libcsoap/soap-env.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: soap-env.c,v 1.10 2004/11/02 23:09:26 snowdrop Exp $ +* $Id: soap-env.c,v 1.11 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -90,12 +90,12 @@ struct XmlNodeHolder static void xmlbuilder_start_element (const xmlChar * element_name, int attr_count, - xmlChar ** keys, xmlChar ** values, - void *userData); + xmlChar ** keys, xmlChar ** values, + void *userData); static void xmlbuilder_characters (const xmlChar * element_name, - const xmlChar * chars, void *userData); + const xmlChar * chars, void *userData); static void xmlbuilder_end_element (const xmlChar * element_name, void *userData); @@ -105,7 +105,7 @@ static herror_t -soap_env_new_from_doc (xmlDocPtr doc, SoapEnv **out) +soap_env_new_from_doc (xmlDocPtr doc, SoapEnv ** out) { xmlNodePtr node; SoapEnv *env; @@ -113,16 +113,17 @@ soap_env_new_from_doc (xmlDocPtr doc, SoapEnv **out) if (doc == NULL) { log_error1 ("Can not create xml document!"); - return herror_new("soap_env_new_from_doc", - GENERAL_INVALID_PARAM, "XML Document (xmlDocPtr) is NULL"); + return herror_new ("soap_env_new_from_doc", + GENERAL_INVALID_PARAM, + "XML Document (xmlDocPtr) is NULL"); } node = xmlDocGetRootElement (doc); if (node == NULL) { log_error1 ("xml document is empty!"); - return herror_new("soap_env_new_from_doc", - XML_ERROR_EMPTY_DOCUMENT, "XML Document is empty!"); + return herror_new ("soap_env_new_from_doc", + XML_ERROR_EMPTY_DOCUMENT, "XML Document is empty!"); } env = (SoapEnv *) malloc (sizeof (SoapEnv)); @@ -130,12 +131,10 @@ soap_env_new_from_doc (xmlDocPtr doc, SoapEnv **out) /* set root */ env->root = node; - /* set method root - set call->cur (current node) to . - xpath: //Envelope/Body/ - */ - node = soap_xml_get_children (env->root);/* Check for NULL ! */ - env->cur = soap_xml_get_children (node); /* Check for NULL ! */ + /* set method root set call->cur (current node) to . xpath: + //Envelope/Body/ */ + node = soap_xml_get_children (env->root); /* Check for NULL ! */ + env->cur = soap_xml_get_children (node); /* Check for NULL ! */ *out = env; return H_OK; @@ -145,22 +144,23 @@ soap_env_new_from_doc (xmlDocPtr doc, SoapEnv **out) herror_t -soap_env_new_from_buffer (const char *buffer, SoapEnv **out) +soap_env_new_from_buffer (const char *buffer, SoapEnv ** out) { xmlDocPtr doc; herror_t err; if (buffer == NULL) - return herror_new("soap_env_new_from_buffer", - GENERAL_INVALID_PARAM, "buffer (first param) is NULL"); + return herror_new ("soap_env_new_from_buffer", + GENERAL_INVALID_PARAM, "buffer (first param) is NULL"); doc = xmlParseDoc (BAD_CAST buffer); if (doc == NULL) - return herror_new("soap_env_new_from_buffer", - XML_ERROR_PARSE, "Can not parse xml"); + return herror_new ("soap_env_new_from_buffer", + XML_ERROR_PARSE, "Can not parse xml"); err = soap_env_new_from_doc (doc, out); - if (err != H_OK) { + if (err != H_OK) + { xmlFreeDoc (doc); } @@ -170,19 +170,21 @@ soap_env_new_from_buffer (const char *buffer, SoapEnv **out) herror_t soap_env_new_with_fault (fault_code_t faultcode, - const char *faultstring, - const char *faultactor, const char *detail, SoapEnv **out) + const char *faultstring, + const char *faultactor, const char *detail, + SoapEnv ** out) { xmlDocPtr doc; herror_t err; - + doc = soap_fault_build (faultcode, faultstring, faultactor, detail); if (doc == NULL) - return herror_new("soap_env_new_with_fault", - XML_ERROR_PARSE, "Can not parse fault xml"); + return herror_new ("soap_env_new_with_fault", + XML_ERROR_PARSE, "Can not parse fault xml"); err = soap_env_new_from_doc (doc, out); - if (err != H_OK) { + if (err != H_OK) + { xmlFreeDoc (doc); } @@ -191,7 +193,7 @@ soap_env_new_with_fault (fault_code_t faultcode, herror_t -soap_env_new_with_response (SoapEnv * request, SoapEnv **out) +soap_env_new_with_response (SoapEnv * request, SoapEnv ** out) { char urn[100]; char methodname[150]; @@ -199,20 +201,24 @@ soap_env_new_with_response (SoapEnv * request, SoapEnv **out) if (request == NULL) { - return herror_new("soap_env_new_with_response", - GENERAL_INVALID_PARAM, "request (first param) is NULL"); + return herror_new ("soap_env_new_with_response", + GENERAL_INVALID_PARAM, + "request (first param) is NULL"); } if (request->root == NULL) { - return herror_new("soap_env_new_with_response", - GENERAL_INVALID_PARAM, "request (first param) has no xml structure"); - } + return herror_new ("soap_env_new_with_response", + GENERAL_INVALID_PARAM, + "request (first param) has no xml structure"); + } if (!soap_env_find_methodname (request, methodname)) { - return herror_new("soap_env_new_with_response", - GENERAL_INVALID_PARAM, "Method name '%s' not found in request", SAVE_STR(methodname)); + return herror_new ("soap_env_new_with_response", + GENERAL_INVALID_PARAM, + "Method name '%s' not found in request", + SAVE_STR (methodname)); } if (!soap_env_find_urn (request, urn)) @@ -229,7 +235,7 @@ soap_env_new_with_response (SoapEnv * request, SoapEnv **out) herror_t -soap_env_new_with_method (const char *urn, const char *method, SoapEnv **out) +soap_env_new_with_method (const char *urn, const char *method, SoapEnv ** out) { xmlDocPtr env; xmlChar buffer[1054]; @@ -238,36 +244,38 @@ soap_env_new_with_method (const char *urn, const char *method, SoapEnv **out) log_verbose2 ("URN = '%s'", urn); log_verbose2 ("Method = '%s'", method); - if (!strcmp(urn, "")) + if (!strcmp (urn, "")) { #ifdef USE_XMLSTRING xmlStrPrintf (buffer, 1054, BAD_CAST _SOAP_MSG_TEMPLATE_EMPTY_TARGET_, - soap_env_ns, soap_env_enc, soap_xsi_ns, - soap_xsd_ns, BAD_CAST method, BAD_CAST urn, BAD_CAST method); + soap_env_ns, soap_env_enc, soap_xsi_ns, + soap_xsd_ns, BAD_CAST method, BAD_CAST urn, + BAD_CAST method); #else - sprintf(buffer, _SOAP_MSG_TEMPLATE_EMPTY_TARGET_, - soap_env_ns, soap_env_enc, soap_xsi_ns, - soap_xsd_ns, method, urn, method); + sprintf (buffer, _SOAP_MSG_TEMPLATE_EMPTY_TARGET_, + soap_env_ns, soap_env_enc, soap_xsi_ns, + soap_xsd_ns, method, urn, method); #endif } else { #ifdef USE_XMLSTRING xmlStrPrintf (buffer, 1054, BAD_CAST _SOAP_MSG_TEMPLATE_, - soap_env_ns, soap_env_enc, soap_xsi_ns, - soap_xsd_ns, BAD_CAST method, BAD_CAST urn, BAD_CAST method); -#else - sprintf(buffer, _SOAP_MSG_TEMPLATE_, - soap_env_ns, soap_env_enc, soap_xsi_ns, - soap_xsd_ns, method, urn, method); + soap_env_ns, soap_env_enc, soap_xsi_ns, + soap_xsd_ns, BAD_CAST method, BAD_CAST urn, + BAD_CAST method); +#else + sprintf (buffer, _SOAP_MSG_TEMPLATE_, + soap_env_ns, soap_env_enc, soap_xsi_ns, + soap_xsd_ns, method, urn, method); #endif } - + env = xmlParseDoc (buffer); if (!env) - return herror_new("soap_env_new_with_method", - XML_ERROR_PARSE, "Can not parse xml"); + return herror_new ("soap_env_new_with_method", + XML_ERROR_PARSE, "Can not parse xml"); return soap_env_new_from_doc (env, out); @@ -275,22 +283,22 @@ soap_env_new_with_method (const char *urn, const char *method, SoapEnv **out) -static -int _soap_env_xml_io_read(void* ctx, char *buffer, int len) +static int +_soap_env_xml_io_read (void *ctx, char *buffer, int len) { - int readed; - http_input_stream_t *in = (http_input_stream_t*)ctx; - if(!http_input_stream_is_ready(in)) - return 0; - - readed = http_input_stream_read(in, buffer, len); - if (readed == -1) - return 0; - return readed; + int readed; + http_input_stream_t *in = (http_input_stream_t *) ctx; + if (!http_input_stream_is_ready (in)) + return 0; + + readed = http_input_stream_read (in, buffer, len); + if (readed == -1) + return 0; + return readed; } -static -int _soap_env_xml_io_close(void *ctx) +static int +_soap_env_xml_io_close (void *ctx) { /* do nothing */ return 0; @@ -298,17 +306,19 @@ int _soap_env_xml_io_close(void *ctx) herror_t -soap_env_new_from_stream(http_input_stream_t *in, SoapEnv **out) +soap_env_new_from_stream (http_input_stream_t * in, SoapEnv ** out) { xmlDocPtr doc; herror_t err; - - doc = xmlReadIO(_soap_env_xml_io_read, - _soap_env_xml_io_close, in, "", NULL, 0); - if (in->err != H_OK) return in->err; - if (doc == NULL) return herror_new("soap_env_new_from_stream", - XML_ERROR_PARSE, "Trying to parse not valid xml"); + doc = xmlReadIO (_soap_env_xml_io_read, + _soap_env_xml_io_close, in, "", NULL, 0); + + if (in->err != H_OK) + return in->err; + if (doc == NULL) + return herror_new ("soap_env_new_from_stream", + XML_ERROR_PARSE, "Trying to parse not valid xml"); err = soap_env_new_from_doc (doc, out); return err; } @@ -316,7 +326,7 @@ soap_env_new_from_stream(http_input_stream_t *in, SoapEnv **out) xmlNodePtr soap_env_add_item (SoapEnv * call, const char *type, - const char *name, const char *value) + const char *name, const char *value) { xmlNodePtr newnode; @@ -345,7 +355,7 @@ soap_env_add_item (SoapEnv * call, const char *type, xmlNodePtr soap_env_add_itemf (SoapEnv * call, const char *type, - const char *name, const char *format, ...) + const char *name, const char *format, ...) { va_list ap; @@ -361,8 +371,8 @@ soap_env_add_itemf (SoapEnv * call, const char *type, -xmlNodePtr -soap_env_add_attachment(SoapEnv* call, const char *name, const char *href) +xmlNodePtr +soap_env_add_attachment (SoapEnv * call, const char *name, const char *href) { xmlNodePtr newnode; @@ -388,7 +398,7 @@ soap_env_add_attachment(SoapEnv* call, const char *name, const char *href) void soap_env_add_custom (SoapEnv * call, void *obj, XmlSerializerCallback cb, - const char *type, const char *name) + const char *type, const char *name) { struct XmlNodeHolder holder; @@ -470,14 +480,15 @@ soap_env_get_body (SoapEnv * env) xmlNodePtr -soap_env_get_fault(SoapEnv * env) +soap_env_get_fault (SoapEnv * env) { xmlNodePtr node; - - node = soap_env_get_body(env); - if (!node) return NULL; + node = soap_env_get_body (env); + + if (!node) + return NULL; while (node != NULL) { @@ -530,9 +541,7 @@ _soap_env_get_body (SoapEnv * env) } /* - find tag find out namespace - xpath: //Envelope/Body/ - */ + find tag find out namespace xpath: //Envelope/Body/ */ xpathobj = soap_xpath_eval (env->root->doc, "//Envelope/Body"); if (!xpathobj) @@ -556,7 +565,7 @@ _soap_env_get_body (SoapEnv * env) return NULL; } - body = nodeset->nodeTab[0]; /* body is */ + body = nodeset->nodeTab[0]; /* body is */ xmlXPathFreeObject (xpathobj); return body; @@ -591,13 +600,13 @@ soap_env_find_urn (SoapEnv * env, char *urn) if (ns != NULL) { strcpy (urn, (char *) ns->href); - return 1; /* namespace found! */ + return 1; /* namespace found! */ } } else - { - strcpy(urn,""); - log_warn1("No namespace found"); + { + strcpy (urn, ""); + log_warn1 ("No namespace found"); return 1; } @@ -614,7 +623,7 @@ soap_env_find_methodname (SoapEnv * env, char *method) if (body == NULL) return 0; - node = soap_xml_get_children (body); /* node is the first child */ + node = soap_xml_get_children (body); /* node is the first child */ if (node == NULL) { @@ -643,7 +652,7 @@ soap_env_find_methodname (SoapEnv * env, char *method) static void xmlbuilder_start_element (const xmlChar * element_name, int attr_count, - xmlChar ** keys, xmlChar ** values, void *userData) + xmlChar ** keys, xmlChar ** values, void *userData) { struct XmlNodeHolder *holder = (struct XmlNodeHolder *) userData; xmlNodePtr parent = NULL; @@ -660,7 +669,7 @@ xmlbuilder_start_element (const xmlChar * element_name, int attr_count, static void xmlbuilder_characters (const xmlChar * element_name, const xmlChar * chars, - void *userData) + void *userData) { struct XmlNodeHolder *holder = (struct XmlNodeHolder *) userData; xmlNodePtr parent = NULL; diff --git a/libcsoap/soap-env.h b/libcsoap/soap-env.h index 523438d..6a3d909 100644 --- a/libcsoap/soap-env.h +++ b/libcsoap/soap-env.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-env.h,v 1.9 2004/11/02 23:09:26 snowdrop Exp $ + * $Id: soap-env.h,v 1.10 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -33,10 +33,10 @@ The SOAP envelope object. */ typedef struct _SoapEnv -{ +{ xmlNodePtr root; /** Pointer to the firts xml element (envelope) */ xmlNodePtr cur; /** Pointer to the current xml element. (stack) */ -}SoapEnv; +} SoapEnv; /* -------------------------------------------------------------- */ @@ -73,10 +73,10 @@ typedef struct _SoapEnv */ herror_t -soap_env_new_with_fault(fault_code_t faultcode, - const char *faultstring, - const char *faultactor, - const char *detail, SoapEnv **out); +soap_env_new_with_fault (fault_code_t faultcode, + const char *faultstring, + const char *faultactor, + const char *detail, SoapEnv ** out); /** Creates an envelope with a method to invoke a soap service. @@ -104,7 +104,8 @@ soap_env_new_with_fault(fault_code_t faultcode, */ herror_t -soap_env_new_with_method(const char *urn, const char *method, SoapEnv **out); +soap_env_new_with_method (const char *urn, const char *method, + SoapEnv ** out); /** @@ -137,7 +138,7 @@ soap_env_new_with_method(const char *urn, const char *method, SoapEnv **out); */ -herror_t soap_env_new_with_response(SoapEnv *req,SoapEnv **out); +herror_t soap_env_new_with_response (SoapEnv * req, SoapEnv ** out); /** @@ -149,7 +150,7 @@ herror_t soap_env_new_with_response(SoapEnv *req,SoapEnv **out); @returns H_OK if success */ -herror_t soap_env_new_from_doc(xmlDocPtr doc, SoapEnv **out); +herror_t soap_env_new_from_doc (xmlDocPtr doc, SoapEnv ** out); /** @@ -160,7 +161,7 @@ herror_t soap_env_new_from_doc(xmlDocPtr doc, SoapEnv **out); @param out the output envelope object @returns H_OK if success */ -herror_t soap_env_new_from_buffer(const char* buffer,SoapEnv **out); +herror_t soap_env_new_from_buffer (const char *buffer, SoapEnv ** out); /** @@ -170,18 +171,21 @@ herror_t soap_env_new_from_buffer(const char* buffer,SoapEnv **out); @param out the output envelope object @returns H_OK if success */ -herror_t soap_env_new_from_stream(http_input_stream_t *in, SoapEnv **out); +herror_t soap_env_new_from_stream (http_input_stream_t * in, SoapEnv ** out); /* --------------------------------------------------- */ /* XML Serializer functions and typedefs */ /* --------------------------------------------------- */ typedef void (*XmlSerializerCallback) - (void* /*obj*/, const xmlChar * /*root_element_name*/, - void (*OnStartElement)(const xmlChar* element_name, int attr_count, xmlChar **keys, xmlChar **values, void* userData), - void (*OnCharacters)(const xmlChar* element_name, const xmlChar* chars, void* userData), - void (*OnEndElement)(const xmlChar* element_name, void* userData), - void* /* userdata*/); + (void * /* obj */ , const xmlChar * /* root_element_name */ , + void (*OnStartElement) (const xmlChar * element_name, int attr_count, + xmlChar ** keys, xmlChar ** values, + void *userData), + void (*OnCharacters) (const xmlChar * element_name, const xmlChar * chars, + void *userData), + void (*OnEndElement) (const xmlChar * element_name, void *userData), + void * /* userdata */ ); /* ------------------------------------------------------ */ @@ -206,9 +210,9 @@ typedef void (*XmlSerializerCallback) @see tutorial */ -xmlNodePtr -soap_env_add_item(SoapEnv* env, const char *type, - const char *name, const char *value); +xmlNodePtr +soap_env_add_item (SoapEnv * env, const char *type, + const char *name, const char *value); /** @@ -227,8 +231,8 @@ soap_env_add_item(SoapEnv* env, const char *type, @see soap_ctx_add_file tutorial */ -xmlNodePtr -soap_env_add_attachment(SoapEnv* env, const char *name, const char *href); +xmlNodePtr +soap_env_add_attachment (SoapEnv * env, const char *name, const char *href); /** @@ -239,8 +243,8 @@ soap_env_add_attachment(SoapEnv* env, const char *name, const char *href); */ void -soap_env_add_custom(SoapEnv* env, void *obj, XmlSerializerCallback cb, - const char *type, const char *name); +soap_env_add_custom (SoapEnv * env, void *obj, XmlSerializerCallback cb, + const char *type, const char *name); /** Same as soap_env_add_item() with c style arguments @@ -251,9 +255,9 @@ soap_env_add_custom(SoapEnv* env, void *obj, XmlSerializerCallback cb, @see soap_env_add_item */ -xmlNodePtr -soap_env_add_itemf(SoapEnv* env, const char *type, - const char *name, const char *value, ...); +xmlNodePtr +soap_env_add_itemf (SoapEnv * env, const char *type, + const char *name, const char *value, ...); /** @@ -280,9 +284,8 @@ soap_env_add_itemf(SoapEnv* env, const char *type, @see tutorial */ -xmlNodePtr -soap_env_push_item(SoapEnv *env, const char *type, - const char *name); +xmlNodePtr +soap_env_push_item (SoapEnv * env, const char *type, const char *name); /** Sets the xml pointer 1 level higher. @@ -290,16 +293,14 @@ soap_env_push_item(SoapEnv *env, const char *type, @param env The envelope object @see soap_env_push_item */ -void -soap_env_pop_item(SoapEnv* env); +void soap_env_pop_item (SoapEnv * env); /** Free the envelope. @param env The envelope object */ -void -soap_env_free(SoapEnv *env); +void soap_env_free (SoapEnv * env); /* --------------------------------------------------- */ @@ -310,33 +311,29 @@ soap_env_free(SoapEnv *env); /** Gets the xml node pointing to SOAP Body. */ -xmlNodePtr -soap_env_get_body(SoapEnv* env); +xmlNodePtr soap_env_get_body (SoapEnv * env); /** Get the xml node pointing to SOAP method (call) */ -xmlNodePtr -soap_env_get_method(SoapEnv* env); +xmlNodePtr soap_env_get_method (SoapEnv * env); /** Get the xml node pointing to SOAP Fault */ -xmlNodePtr -soap_env_get_fault(SoapEnv* env); +xmlNodePtr soap_env_get_fault (SoapEnv * env); /** Get the xml node pointing to SOAP Header */ -xmlNodePtr -soap_env_get_header(SoapEnv* env); +xmlNodePtr soap_env_get_header (SoapEnv * env); -int soap_env_find_urn(SoapEnv *env, char *urn); -int soap_env_find_methodname(SoapEnv *env, char *methodname); +int soap_env_find_urn (SoapEnv * env, char *urn); +int soap_env_find_methodname (SoapEnv * env, char *methodname); @@ -344,5 +341,3 @@ int soap_env_find_methodname(SoapEnv *env, char *methodname); #endif - - diff --git a/libcsoap/soap-fault.c b/libcsoap/soap-fault.c index eeb146d..3c8f5c1 100644 --- a/libcsoap/soap-fault.c +++ b/libcsoap/soap-fault.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: soap-fault.c,v 1.6 2005/12/19 14:06:16 snowdrop Exp $ +* $Id: soap-fault.c,v 1.7 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -62,67 +62,67 @@ static char *fault_mu = "MustUnderstand"; static char *fault_client = "Client"; static char *fault_server = "Server"; -xmlDocPtr soap_fault_build(fault_code_t fcode, - const char *faultstring, - const char *faultactor, - const char *detail) +xmlDocPtr +soap_fault_build (fault_code_t fcode, + const char *faultstring, + const char *faultactor, const char *detail) { - /* variables */ - char *faultcode; - int bufferlen = 2000; - char *buffer; - xmlDocPtr fault; /* result */ + /* variables */ + char *faultcode; + int bufferlen = 2000; + char *buffer; + xmlDocPtr fault; /* result */ - log_verbose1("Build fault"); + log_verbose1 ("Build fault"); - switch (fcode) { + switch (fcode) + { case Fault_VersionMismatch: - faultcode = fault_vm; - break; + faultcode = fault_vm; + break; case Fault_MustUnderstand: - faultcode = fault_mu; - break; + faultcode = fault_mu; + break; case Fault_Client: - faultcode = fault_client; - break; + faultcode = fault_client; + break; case Fault_Server: - faultcode = fault_server; - break; + faultcode = fault_server; + break; default: - faultcode = fault_client; - } + faultcode = fault_client; + } - /* calculate buffer length */ - if (faultstring) bufferlen += strlen(faultstring); - if (faultactor) bufferlen += strlen(faultactor); - if (detail) bufferlen += strlen(detail); + /* calculate buffer length */ + if (faultstring) + bufferlen += strlen (faultstring); + if (faultactor) + bufferlen += strlen (faultactor); + if (detail) + bufferlen += strlen (detail); - log_verbose2("Creating buffer with %d bytes", bufferlen); - buffer = (char*)malloc(bufferlen); + log_verbose2 ("Creating buffer with %d bytes", bufferlen); + buffer = (char *) malloc (bufferlen); - sprintf(buffer, _SOAP_FAULT_TEMPLATE_, - soap_env_ns, soap_env_enc, soap_xsi_ns, - soap_xsd_ns, faultcode, - faultstring?faultstring:"error", - faultactor?faultactor:"", - detail?detail:""); + sprintf (buffer, _SOAP_FAULT_TEMPLATE_, + soap_env_ns, soap_env_enc, soap_xsi_ns, + soap_xsd_ns, faultcode, + faultstring ? faultstring : "error", + faultactor ? faultactor : "", detail ? detail : ""); - fault = xmlParseDoc(BAD_CAST buffer); - free(buffer); - - if (fault == NULL) { - log_error1("Can not create xml document!"); - - return soap_fault_build(fcode, "Can not create fault object in xml", - "soap_fault_build()", NULL); - } - - log_verbose2("Returning fault (%p)", fault); - return fault; - -} + fault = xmlParseDoc (BAD_CAST buffer); + free (buffer); + if (fault == NULL) + { + log_error1 ("Can not create xml document!"); + return soap_fault_build (fcode, "Can not create fault object in xml", + "soap_fault_build()", NULL); + } + log_verbose2 ("Returning fault (%p)", fault); + return fault; +} diff --git a/libcsoap/soap-fault.h b/libcsoap/soap-fault.h index dcc18e2..46471a0 100644 --- a/libcsoap/soap-fault.h +++ b/libcsoap/soap-fault.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-fault.h,v 1.2 2004/10/15 13:34:02 snowdrop Exp $ + * $Id: soap-fault.h,v 1.3 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -28,18 +28,18 @@ -typedef enum _fault_code { +typedef enum _fault_code +{ Fault_VersionMismatch, Fault_MustUnderstand, Fault_Client, Fault_Server -}fault_code_t; +} fault_code_t; -xmlDocPtr soap_fault_build(fault_code_t faultcode, - const char *faultstring, - const char *faultactor, - const char *detail); +xmlDocPtr soap_fault_build (fault_code_t faultcode, + const char *faultstring, + const char *faultactor, const char *detail); #endif diff --git a/libcsoap/soap-router.c b/libcsoap/soap-router.c index 966edad..74c53be 100644 --- a/libcsoap/soap-router.c +++ b/libcsoap/soap-router.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: soap-router.c,v 1.3 2004/08/26 17:06:18 rans Exp $ +* $Id: soap-router.c,v 1.4 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -24,80 +24,88 @@ #include #include -SoapRouter *soap_router_new() +SoapRouter * +soap_router_new () { - SoapRouter *router; + SoapRouter *router; - router = (SoapRouter*)malloc(sizeof(SoapRouter)); - router->service_head = NULL; - router->service_tail = NULL; + router = (SoapRouter *) malloc (sizeof (SoapRouter)); + router->service_head = NULL; + router->service_tail = NULL; - return router; + return router; } -void soap_router_register_service(SoapRouter *router, - SoapServiceFunc func, - const char* method, - const char* urn) +void +soap_router_register_service (SoapRouter * router, + SoapServiceFunc func, + const char *method, const char *urn) { - SoapService *service; - - service = soap_service_new(urn, method, func); - - if (router->service_tail == NULL) { - router->service_head = - router->service_tail = soap_service_node_new(service, NULL); - } else { - router->service_tail->next = - soap_service_node_new(service, NULL); - router->service_tail = router->service_tail->next; - } + SoapService *service; + + service = soap_service_new (urn, method, func); + + if (router->service_tail == NULL) + { + router->service_head = + router->service_tail = soap_service_node_new (service, NULL); + } + else + { + router->service_tail->next = soap_service_node_new (service, NULL); + router->service_tail = router->service_tail->next; + } } -SoapService* soap_router_find_service(SoapRouter *router, - const char* urn, - const char* method) +SoapService * +soap_router_find_service (SoapRouter * router, + const char *urn, const char *method) { - SoapServiceNode *node; + SoapServiceNode *node; - if (router == NULL || urn == NULL || method == NULL) - return NULL; + if (router == NULL || urn == NULL || method == NULL) + return NULL; - node = router->service_head; + node = router->service_head; - while (node) { - if (node->service && node->service->urn - && node->service->method) { + while (node) + { + if (node->service && node->service->urn && node->service->method) + { - if (!strcmp(node->service->urn, urn) - && !strcmp(node->service->method, method)) - return node->service; + if (!strcmp (node->service->urn, urn) + && !strcmp (node->service->method, method)) + return node->service; - } + } - node = node->next; - } + node = node->next; + } - return NULL; + return NULL; } -void soap_router_free(SoapRouter *router) +void +soap_router_free (SoapRouter * router) { - SoapServiceNode *node; - log_verbose2("enter: router=%p", router); - if (router == NULL) return; - - while (router->service_head) { - node = router->service_head->next; - /* log_verbose2("soap_service_free(%p)\n", router->service_head->service);*/ - soap_service_free(router->service_head->service); - free(router->service_head); - router->service_head = node; - } - - free(router); - log_verbose1("leave with success"); + SoapServiceNode *node; + log_verbose2 ("enter: router=%p", router); + if (router == NULL) + return; + + while (router->service_head) + { + node = router->service_head->next; + /* log_verbose2("soap_service_free(%p)\n", + router->service_head->service); */ + soap_service_free (router->service_head->service); + free (router->service_head); + router->service_head = node; + } + + free (router); + log_verbose1 ("leave with success"); } diff --git a/libcsoap/soap-router.h b/libcsoap/soap-router.h index b0a2536..f82d43c 100644 --- a/libcsoap/soap-router.h +++ b/libcsoap/soap-router.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-router.h,v 1.2 2004/02/10 09:51:10 snowdrop Exp $ + * $Id: soap-router.h,v 1.3 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -35,7 +35,7 @@ typedef struct _SoapRouter { SoapServiceNode *service_head; SoapServiceNode *service_tail; -}SoapRouter; +} SoapRouter; /** @@ -47,7 +47,7 @@ typedef struct _SoapRouter @returns Soap router @see soap_router_free */ -SoapRouter *soap_router_new(); +SoapRouter *soap_router_new (); /** @@ -60,10 +60,9 @@ SoapRouter *soap_router_new(); the client side. @param urn The urn for this service */ -void soap_router_register_service(SoapRouter *router, - SoapServiceFunc func, - const char* method, - const char* urn); +void soap_router_register_service (SoapRouter * router, + SoapServiceFunc func, + const char *method, const char *urn); /** @@ -75,9 +74,8 @@ void soap_router_register_service(SoapRouter *router, @return The service if found, NULL otherwise. */ -SoapService* soap_router_find_service(SoapRouter *router, - const char* urn, - const char* method); +SoapService *soap_router_find_service (SoapRouter * router, + const char *urn, const char *method); /** @@ -85,6 +83,6 @@ SoapService* soap_router_find_service(SoapRouter *router, @param router The router object to free */ -void soap_router_free(SoapRouter *router); +void soap_router_free (SoapRouter * router); #endif diff --git a/libcsoap/soap-server.c b/libcsoap/soap-server.c index 591ecc0..9daf639 100644 --- a/libcsoap/soap-server.c +++ b/libcsoap/soap-server.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: soap-server.c,v 1.12 2005/12/19 14:06:16 snowdrop Exp $ +* $Id: soap-server.c,v 1.13 2006/01/10 11:21:55 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -28,362 +28,409 @@ typedef struct _SoapRouterNode { - char *context; - SoapRouter *router; - struct _SoapRouterNode *next; + char *context; + SoapRouter *router; + struct _SoapRouterNode *next; -}SoapRouterNode; +} SoapRouterNode; SoapRouterNode *head = NULL; SoapRouterNode *tail = NULL; static -SoapRouterNode *router_node_new(SoapRouter *router, - const char *context, - SoapRouterNode *next); + SoapRouterNode *router_node_new (SoapRouter * router, + const char *context, + SoapRouterNode * next); -static -SoapRouter *router_find(const char *context); +static SoapRouter *router_find (const char *context); -static -void _soap_server_send_ctx(httpd_conn_t* conn, SoapCtx *ctxres); +static void _soap_server_send_ctx (httpd_conn_t * conn, SoapCtx * ctxres); /*---------------------------------*/ -void soap_server_entry(httpd_conn_t *conn, hrequest_t *req); +void soap_server_entry (httpd_conn_t * conn, hrequest_t * req); +static void _soap_server_send_env (http_output_stream_t * out, SoapEnv * env); static -void _soap_server_send_env(http_output_stream_t *out, SoapEnv* env); -static -void _soap_server_send_fault(httpd_conn_t *conn, hpair_t *header, - const char* errmsg); + void _soap_server_send_fault (httpd_conn_t * conn, hpair_t * header, + const char *errmsg); /*---------------------------------*/ -herror_t soap_server_init_args(int argc, char *argv[]) +herror_t +soap_server_init_args (int argc, char *argv[]) { - return httpd_init(argc, argv); + return httpd_init (argc, argv); } -int soap_server_register_router(SoapRouter *router, const char* context) +int +soap_server_register_router (SoapRouter * router, const char *context) { - if (!httpd_register(context, soap_server_entry)) { - return 0; - } - - if (tail == NULL) { - head = tail = router_node_new(router, context, NULL); - } else { - tail->next = router_node_new(router, context, NULL); - tail = tail->next; - } - - return 1; + if (!httpd_register (context, soap_server_entry)) + { + return 0; + } + + if (tail == NULL) + { + head = tail = router_node_new (router, context, NULL); + } + else + { + tail->next = router_node_new (router, context, NULL); + tail = tail->next; + } + + return 1; } -herror_t soap_server_run() +herror_t +soap_server_run () { - return httpd_run(); + return httpd_run (); } -void soap_server_destroy() +void +soap_server_destroy () { - SoapRouterNode *node = head; - SoapRouterNode *tmp; - - while (node != NULL) { - tmp = node->next; - log_verbose2("soap_router_free(%p)", node->router); - soap_router_free(node->router); - free(node->context); - free(node); - node = tmp; - } - httpd_destroy(); + SoapRouterNode *node = head; + SoapRouterNode *tmp; + + while (node != NULL) + { + tmp = node->next; + log_verbose2 ("soap_router_free(%p)", node->router); + soap_router_free (node->router); + free (node->context); + free (node); + node = tmp; + } + httpd_destroy (); } -void soap_server_entry(httpd_conn_t *conn, hrequest_t *req) +void +soap_server_entry (httpd_conn_t * conn, hrequest_t * req) { - hpair_t *header = NULL; - char buffer[1054]; - char urn[150]; - char method[150]; - SoapCtx *ctx, *ctxres; - SoapRouter *router; - SoapService *service; - SoapEnv *env; - herror_t err; - - if (req->method != HTTP_REQUEST_POST) { - - httpd_send_header(conn, 200, "OK"); - http_output_stream_write_string(conn->out, ""); - http_output_stream_write_string(conn->out, "

Sorry!


"); - http_output_stream_write_string(conn->out, "I only speak with 'POST' method"); - http_output_stream_write_string(conn->out, ""); - return; - } - - - header = hpairnode_new(HEADER_CONTENT_TYPE, "text/xml", NULL); - - err = soap_env_new_from_stream(req->in, &env); - if (err != H_OK) - { - _soap_server_send_fault(conn, header, herror_message(err)); - herror_release(err); - return; - } - - - if (env == NULL) { - - _soap_server_send_fault(conn, header,"Can not receive POST data!"); - - } else { - - ctx = soap_ctx_new(env); - soap_ctx_add_files(ctx, req->attachments); + hpair_t *header = NULL; + char buffer[1054]; + char urn[150]; + char method[150]; + SoapCtx *ctx, *ctxres; + SoapRouter *router; + SoapService *service; + SoapEnv *env; + herror_t err; - if (ctx->env == NULL) { + if (req->method != HTTP_REQUEST_POST) + { - _soap_server_send_fault(conn, header,"Can not parse POST data!"); + httpd_send_header (conn, 200, "OK"); + http_output_stream_write_string (conn->out, ""); + http_output_stream_write_string (conn->out, "

Sorry!


"); + http_output_stream_write_string (conn->out, + "I only speak with 'POST' method"); + http_output_stream_write_string (conn->out, ""); + return; + } - } else { - /*soap_xml_doc_print(env->root->doc);*/ + header = hpairnode_new (HEADER_CONTENT_TYPE, "text/xml", NULL); - router = router_find(req->path); + err = soap_env_new_from_stream (req->in, &env); + if (err != H_OK) + { + _soap_server_send_fault (conn, header, herror_message (err)); + herror_release (err); + return; + } - if ( router == NULL) { - _soap_server_send_fault(conn, header, "Can not find router!"); + if (env == NULL) + { - } else { + _soap_server_send_fault (conn, header, "Can not receive POST data!"); - if (!soap_env_find_urn(ctx->env, urn)) { + } + else + { - _soap_server_send_fault(conn, header, "No URN found!"); - soap_ctx_free(ctx); - return; - } else { - log_verbose2("urn: '%s'", urn); - } + ctx = soap_ctx_new (env); + soap_ctx_add_files (ctx, req->attachments); - if (!soap_env_find_methodname(ctx->env, method)) { + if (ctx->env == NULL) + { - _soap_server_send_fault(conn, header, "No method found!"); - soap_ctx_free(ctx); - return; - }else { - log_verbose2("method: '%s'", method); - } + _soap_server_send_fault (conn, header, "Can not parse POST data!"); - service = soap_router_find_service(router, urn, method); + } + else + { - if (service == NULL) { + /* soap_xml_doc_print(env->root->doc); */ - sprintf(buffer, "URN '%s' not found", urn); - _soap_server_send_fault(conn, header, buffer); - soap_ctx_free(ctx); - return; - } else { + router = router_find (req->path); - log_verbose2("func: %p", service->func); - ctxres = soap_ctx_new(NULL); - /* ===================================== */ - /* CALL SERVICE FUNCTION */ - /* ===================================== */ - err = service->func(ctx, ctxres); - if (err != H_OK) { - sprintf(buffer, "Service returned following error message: '%s'", - herror_message(err)); - herror_release(err); - _soap_server_send_fault(conn, header, buffer); - soap_ctx_free(ctx); - return; - } + if (router == NULL) + { - if (ctxres->env == NULL) { + _soap_server_send_fault (conn, header, "Can not find router!"); - sprintf(buffer, "Service '%s' returned no envelope", urn); - _soap_server_send_fault(conn, header, buffer); - soap_ctx_free(ctx); - return; - - } else { + } + else + { + + if (!soap_env_find_urn (ctx->env, urn)) + { + + _soap_server_send_fault (conn, header, "No URN found!"); + soap_ctx_free (ctx); + return; + } + else + { + log_verbose2 ("urn: '%s'", urn); + } + + if (!soap_env_find_methodname (ctx->env, method)) + { + + _soap_server_send_fault (conn, header, "No method found!"); + soap_ctx_free (ctx); + return; + } + else + { + log_verbose2 ("method: '%s'", method); + } + + service = soap_router_find_service (router, urn, method); + + if (service == NULL) + { + + sprintf (buffer, "URN '%s' not found", urn); + _soap_server_send_fault (conn, header, buffer); + soap_ctx_free (ctx); + return; + } + else + { + + log_verbose2 ("func: %p", service->func); + ctxres = soap_ctx_new (NULL); + /* ===================================== */ + /* CALL SERVICE FUNCTION */ + /* ===================================== */ + err = service->func (ctx, ctxres); + if (err != H_OK) + { + sprintf (buffer, "Service returned following error message: '%s'", + herror_message (err)); + herror_release (err); + _soap_server_send_fault (conn, header, buffer); + soap_ctx_free (ctx); + return; + } + + if (ctxres->env == NULL) + { + + sprintf (buffer, "Service '%s' returned no envelope", urn); + _soap_server_send_fault (conn, header, buffer); + soap_ctx_free (ctx); + return; + + } + else + { /* httpd_send_header(conn, 200, "OK"); _soap_server_send_env(conn->out, ctxres->env); */ - _soap_server_send_ctx(conn, ctxres); - /* free envctx */ - soap_ctx_free(ctxres); - } + _soap_server_send_ctx (conn, ctxres); + /* free envctx */ + soap_ctx_free (ctxres); + } - } + } - } - } - soap_ctx_free(ctx); - } + } + } + soap_ctx_free (ctx); + } } -static -void _soap_server_send_ctx(httpd_conn_t* conn, SoapCtx *ctx) +static void +_soap_server_send_ctx (httpd_conn_t * conn, SoapCtx * ctx) { - xmlBufferPtr buffer; - static int counter = 1; - char strbuffer[150]; - part_t *part; + xmlBufferPtr buffer; + static int counter = 1; + char strbuffer[150]; + part_t *part; - if (ctx->env == NULL || ctx->env->root == NULL) return; + if (ctx->env == NULL || ctx->env->root == NULL) + return; - buffer = xmlBufferCreate(); + buffer = xmlBufferCreate (); /* xmlIndentTreeOutput = 1;*/ - xmlThrDefIndentTreeOutput(1); + xmlThrDefIndentTreeOutput (1); /* xmlKeepBlanksDefault(0);*/ - xmlNodeDump(buffer, ctx->env->root->doc, ctx->env->root, 1 ,1); - - if (ctx->attachments) - { - sprintf(strbuffer, "000128590350940924234%d", counter++); - httpd_mime_send_header(conn, strbuffer, "", "text/xml", 200, "OK"); - httpd_mime_next(conn, strbuffer, "text/xml", "binary"); - http_output_stream_write_string(conn->out, (const char*)xmlBufferContent(buffer)); - part = ctx->attachments->parts; - while (part) - { - httpd_mime_send_file(conn, part->id, part->content_type, part->transfer_encoding, part->filename); - part = part->next; - } - httpd_mime_end(conn); - } - else - { - char buflen[100]; - xmlXPathContextPtr xpathCtx; - xmlXPathObjectPtr xpathObj; - xpathCtx = xmlXPathNewContext(ctx->env->root->doc); - xpathObj = xmlXPathEvalExpression("//Fault", xpathCtx); + xmlNodeDump (buffer, ctx->env->root->doc, ctx->env->root, 1, 1); + + if (ctx->attachments) + { + sprintf (strbuffer, "000128590350940924234%d", counter++); + httpd_mime_send_header (conn, strbuffer, "", "text/xml", 200, "OK"); + httpd_mime_next (conn, strbuffer, "text/xml", "binary"); + http_output_stream_write_string (conn->out, + (const char *) + xmlBufferContent (buffer)); + part = ctx->attachments->parts; + while (part) + { + httpd_mime_send_file (conn, part->id, part->content_type, + part->transfer_encoding, part->filename); + part = part->next; + } + httpd_mime_end (conn); + } + else + { + char buflen[100]; + xmlXPathContextPtr xpathCtx; + xmlXPathObjectPtr xpathObj; + xpathCtx = xmlXPathNewContext (ctx->env->root->doc); + xpathObj = xmlXPathEvalExpression ("//Fault", xpathCtx); #ifdef WIN32 #define snprintf(buffer, num, s1, s2) sprintf(buffer, s1,s2) #endif - snprintf(buflen,100,"%d", strlen((const char*)xmlBufferContent (buffer))); - httpd_set_header (conn, HEADER_CONTENT_LENGTH, buflen); - if((xpathObj->nodesetval) ? xpathObj->nodesetval->nodeNr : 0){ - httpd_send_header(conn, 500, "FAILED"); - } else { - httpd_send_header(conn, 200, "OK"); - } - - http_output_stream_write_string(conn->out, (const char*)xmlBufferContent(buffer)); - xmlXPathFreeObject(xpathObj); - xmlXPathFreeContext(xpathCtx); - - } - xmlBufferFree(buffer); + snprintf (buflen, 100, "%d", + strlen ((const char *) xmlBufferContent (buffer))); + httpd_set_header (conn, HEADER_CONTENT_LENGTH, buflen); + if ((xpathObj->nodesetval) ? xpathObj->nodesetval->nodeNr : 0) + { + httpd_send_header (conn, 500, "FAILED"); + } + else + { + httpd_send_header (conn, 200, "OK"); + } + + http_output_stream_write_string (conn->out, + (const char *) + xmlBufferContent (buffer)); + xmlXPathFreeObject (xpathObj); + xmlXPathFreeContext (xpathCtx); + + } + xmlBufferFree (buffer); } -static -void _soap_server_send_env(http_output_stream_t *out, SoapEnv* env) +static void +_soap_server_send_env (http_output_stream_t * out, SoapEnv * env) { - xmlBufferPtr buffer; - if (env == NULL || env->root == NULL) return; + xmlBufferPtr buffer; + if (env == NULL || env->root == NULL) + return; - buffer = xmlBufferCreate(); - xmlNodeDump(buffer, env->root->doc, env->root, 1 ,1); - http_output_stream_write_string(out, (const char*)xmlBufferContent(buffer)); - xmlBufferFree(buffer); + buffer = xmlBufferCreate (); + xmlNodeDump (buffer, env->root->doc, env->root, 1, 1); + http_output_stream_write_string (out, + (const char *) xmlBufferContent (buffer)); + xmlBufferFree (buffer); } -static -void _soap_server_send_fault(httpd_conn_t *conn, hpair_t *header, - const char* errmsg) +static void +_soap_server_send_fault (httpd_conn_t * conn, hpair_t * header, + const char *errmsg) { - SoapEnv *envres; - herror_t err; - char buffer[45]; - httpd_set_headers(conn, header); - err = httpd_send_header(conn, 500, "FAILED"); - if (err != H_OK) { - /* WARNING: unhandled exception !*/ - log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); - return; - } - - err = soap_env_new_with_fault(Fault_Server, - errmsg?errmsg:"General error", - "cSOAP_Server", NULL, &envres); - if (err != H_OK) { - log_error1(herror_message(err)); - http_output_stream_write_string(conn->out, ""); - http_output_stream_write_string(conn->out, "

Error


"); - http_output_stream_write_string(conn->out, "Error while sending fault object:
Message: "); - http_output_stream_write_string(conn->out, herror_message(err)); - http_output_stream_write_string(conn->out, "
Function: "); - http_output_stream_write_string(conn->out, herror_func(err)); - http_output_stream_write_string(conn->out, "
Error code: "); - sprintf(buffer, "%d", herror_code(err)); - http_output_stream_write_string(conn->out, buffer); - http_output_stream_write_string(conn->out, ""); - return; - - herror_release(err); - } else { - _soap_server_send_env(conn->out, envres); - } + SoapEnv *envres; + herror_t err; + char buffer[45]; + httpd_set_headers (conn, header); + err = httpd_send_header (conn, 500, "FAILED"); + if (err != H_OK) + { + /* WARNING: unhandled exception ! */ + log_error4 ("%s():%s [%d]", herror_func (err), herror_message (err), + herror_code (err)); + return; + } + + err = soap_env_new_with_fault (Fault_Server, + errmsg ? errmsg : "General error", + "cSOAP_Server", NULL, &envres); + if (err != H_OK) + { + log_error1 (herror_message (err)); + http_output_stream_write_string (conn->out, ""); + http_output_stream_write_string (conn->out, "

Error


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