From d24d6ab7bbfda8e302af3a5cf8be62299d543c1a Mon Sep 17 00:00:00 2001 From: snowdrop Date: Thu, 28 Oct 2004 10:30:41 +0000 Subject: changed hstatus_t to herror_t and chagend the API function to return herror_t. Added herror_*() functions and fixed a bug in the socket receive comm. --- examples/csoap/echoattachments-client.c | 299 +++++++++++++++++--------------- examples/csoap/echoattachments-server.c | 52 +++++- examples/csoap/simpleclient.c | 77 ++++---- examples/csoap/simpleserver.c | 49 +++--- libcsoap/soap-client.c | 136 ++++++--------- libcsoap/soap-client.h | 15 +- libcsoap/soap-ctx.c | 8 +- libcsoap/soap-ctx.h | 4 +- libcsoap/soap-env.c | 215 +++++++++++++---------- libcsoap/soap-env.h | 48 ++--- libcsoap/soap-server.c | 76 ++++++-- libcsoap/soap-server.h | 12 +- libcsoap/soap-service.h | 4 +- nanohttp/nanohttp-client.c | 151 ++++++++-------- nanohttp/nanohttp-client.h | 22 +-- nanohttp/nanohttp-common.c | 70 +++++++- nanohttp/nanohttp-common.h | 27 ++- nanohttp/nanohttp-mime.c | 58 ++++--- nanohttp/nanohttp-mime.h | 4 +- nanohttp/nanohttp-request.c | 27 ++- nanohttp/nanohttp-request.h | 4 +- nanohttp/nanohttp-response.c | 25 +-- nanohttp/nanohttp-response.h | 4 +- nanohttp/nanohttp-server.c | 71 ++++---- nanohttp/nanohttp-server.h | 21 +-- nanohttp/nanohttp-socket.c | 104 +++++++---- nanohttp/nanohttp-socket.h | 22 +-- nanohttp/nanohttp-stream.c | 113 +++++++----- nanohttp/nanohttp-stream.h | 10 +- 29 files changed, 986 insertions(+), 742 deletions(-) diff --git a/examples/csoap/echoattachments-client.c b/examples/csoap/echoattachments-client.c index 352a73c..1550c93 100755 --- a/examples/csoap/echoattachments-client.c +++ b/examples/csoap/echoattachments-client.c @@ -1,142 +1,157 @@ -/****************************************************************** - * $Id: echoattachments-client.c,v 1.3 2004/10/15 15:10:14 snowdrop Exp $ - * - * CSOAP Project: CSOAP examples project - * Copyright (C) 2003-2004 Ferhat Ayaz - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA - * - * Email: ferhatayaz@yahoo.com - ******************************************************************/ - -#include - - -/* -static const char *url = "http://csoap.sourceforge.net/cgi-bin/csoapserver"; -*/ -static const char *url = "http://localhost:10000/echoattachment"; -static const char *urn = ""; -static const char *method = "echo"; - - -void compareFiles(const char* received, const char *sent) -{ - FILE *f1, *f2; - char c1, c2; - 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); - 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); - return; - } - - fseek(f1, 0, SEEK_END); - fseek(f2, 0, SEEK_END); - - s1 = ftell(f1); - s2 = ftell(f2); - - fseek(f1, 0, SEEK_SET); - fseek(f2, 0, SEEK_SET); - - if (s1 > s2) { - - printf("ERROR: files are not equal! Received file is bigger\n"); - fclose(f1); fclose(f2); - return; - - } else if (s2 > s1) { - - 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); - break; - } - } - - printf("OK: files are equal!\n"); - fclose(f1); fclose(f2); - -} - -int main(int argc, char *argv[]) -{ - SoapCtx *ctx, *ctx2; - char href[MAX_HREF_SIZE]; - xmlNodePtr fault; - - if (argc < 2) { - fprintf(stderr, "usage: %s [url]\n", argv[0]); - exit(1); - } - - log_set_level(HLOG_VERBOSE); - if (!soap_client_init_args(argc, argv)) { - return 1; - } - - ctx = soap_client_ctx_new(urn, method); - if (soap_ctx_add_file(ctx, argv[1], "application/octet-stream", href) != H_OK) { - fprintf(stderr, "Error while adding '%s'\n", argv[1]); - soap_ctx_free(ctx); - exit(1); - } - soap_env_add_attachment(ctx->env,"source", href); - - printf("sending request ...\n"); - if (argc > 2) - ctx2 = soap_client_invoke(ctx, argv[2], ""); - else - ctx2 = soap_client_invoke(ctx, url, ""); - - 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); - } - - soap_ctx_free(ctx2); - soap_ctx_free(ctx); - - return 0; -} - - - - +/****************************************************************** + * $Id: echoattachments-client.c,v 1.4 2004/10/28 10:30:41 snowdrop Exp $ + * + * CSOAP Project: CSOAP examples project + * Copyright (C) 2003-2004 Ferhat Ayaz + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA + * + * Email: ferhatayaz@yahoo.com + ******************************************************************/ + +#include + + + + +static const char *urn = "urn:examples"; +static const char *url = "http://localhost:10000/echoattachment"; +static const char *method = "echo"; + + +void compareFiles(const char* received, const char *sent) +{ + FILE *f1, *f2; + char c1, c2; + 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); + 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); + return; + } + + fseek(f1, 0, SEEK_END); + fseek(f2, 0, SEEK_END); + + s1 = ftell(f1); + s2 = ftell(f2); + + fseek(f1, 0, SEEK_SET); + fseek(f2, 0, SEEK_SET); + + if (s1 > s2) { + + printf("ERROR: files are not equal! Received file is bigger\n"); + fclose(f1); fclose(f2); + return; + + } else if (s2 > s1) { + + 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); + break; + } + } + + printf("OK: files are equal!\n"); + fclose(f1); fclose(f2); + +} + +int main(int argc, char *argv[]) +{ + SoapCtx *ctx, *ctx2; + char href[MAX_HREF_SIZE]; + xmlNodePtr fault; + herror_t err; + + if (argc < 2) { + fprintf(stderr, "usage: %s [url]\n", argv[0]); + exit(1); + } + + log_set_level(HLOG_VERBOSE); + 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; + } + + err = soap_client_ctx_new(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; + } + if (soap_ctx_add_file(ctx, argv[1], "application/octet-stream", href) != H_OK) { + fprintf(stderr, "Error while adding '%s'\n", argv[1]); + soap_ctx_free(ctx); + exit(1); + } + soap_env_add_attachment(ctx->env,"source", href); + + printf("sending request ...\n"); + if (argc > 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; + } + + 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); + } + + soap_ctx_free(ctx2); + soap_ctx_free(ctx); + + return 0; +} + + + + + diff --git a/examples/csoap/echoattachments-server.c b/examples/csoap/echoattachments-server.c index 701d6a6..5aeeb76 100755 --- a/examples/csoap/echoattachments-server.c +++ b/examples/csoap/echoattachments-server.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: echoattachments-server.c,v 1.2 2004/10/15 15:10:15 snowdrop Exp $ + * $Id: echoattachments-server.c,v 1.3 2004/10/28 10:30:42 snowdrop Exp $ * * CSOAP Project: CSOAP examples project * Copyright (C) 2003-2004 Ferhat Ayaz @@ -24,21 +24,26 @@ #include -static const char *url = "/echoattachment"; -static const char *urn = ""; +static const char *url = "/echoattachments"; +static const char *urn = "urn:examples"; static const char *method = "echo"; - +/* SoapCtx* echo_attachments(SoapCtx *req) { - + herror_t err; SoapEnv *env; SoapCtx* ctx; part_t *part; char href[MAX_HREF_SIZE]; - env = soap_env_new_with_response(req->env); + err = soap_env_new_with_response(req->env, &env); + if (err != H_OK) { + herror_release(err); + return NULL; + } + ctx = soap_ctx_new(env); if (req->attachments) { @@ -54,16 +59,45 @@ SoapCtx* echo_attachments(SoapCtx *req) return ctx; } +*/ -int main(int argc, char *argv[]) +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; + } + 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); + } + } + + return H_OK; +} + + +int main(int argc, char *argv[]) +{ + herror_t err; SoapRouter *router; log_set_level(HLOG_VERBOSE); - if (!soap_server_init_args(argc, argv)) { - return 1; + 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(); diff --git a/examples/csoap/simpleclient.c b/examples/csoap/simpleclient.c index 3ac1588..7b80193 100644 --- a/examples/csoap/simpleclient.c +++ b/examples/csoap/simpleclient.c @@ -1,8 +1,8 @@ /****************************************************************** - * $Id: simpleclient.c,v 1.5 2004/10/15 13:42:57 snowdrop Exp $ + * $Id: simpleclient.c,v 1.6 2004/10/28 10:30:42 snowdrop Exp $ * * CSOAP Project: CSOAP examples project - * Copyright (C) 2003 Ferhat Ayaz + * Copyright (C) 2003-2004 Ferhat Ayaz * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,67 +18,54 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA * - * Email: ayaz@jprogrammer.net + * Email: ferhatayaz@yahoo.com ******************************************************************/ #include -/* -static const char *url = "http://csoap.sourceforge.net/cgi-bin/csoapserver"; -*/ -static const char *url = "http://localhost:3031/csoapserver"; +static const char *url = "http://localhost:10000/csoapserver"; static const char *urn = "urn:examples"; static const char *method = "sayHello"; int main(int argc, char *argv[]) { - SoapCtx *ctx, *ctx2; + 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; + } + + err = soap_client_ctx_new(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; + } - log_set_level(HLOG_VERBOSE); - if (!soap_client_init_args(argc, argv)) { - return 1; - } - - ctx = soap_client_ctx_new(urn, method); soap_env_add_item(ctx->env, "xsd:string", "name", "Jonny B. Good"); if (argc > 1) - ctx2 = soap_client_invoke(ctx, argv[1], ""); + err = soap_client_invoke(ctx, &ctx2, argv[1], ""); else - ctx2 = soap_client_invoke(ctx, 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; + } soap_xml_doc_print(ctx2->env->root->doc); soap_ctx_free(ctx2); - soap_ctx_free(ctx); -} - -/* -int main2(int argc, char *argv[]) -{ - SoapEnv *env, *res; - - log_set_level(HLOG_VERBOSE); - if (!soap_client_init_args(argc, argv)) { - return 1; - } - - env = soap_env_new_with_method(urn, method); - soap_env_add_item(env, "xsd:string", "name", "Jonny B. Good"); - - if (argc > 1) - res = soap_client_invoke(env, argv[1], ""); - else - res = soap_client_invoke(env, url, ""); - soap_xml_doc_print(res->root->doc); - soap_env_free(res); - soap_env_free(env); - + soap_ctx_free(ctx); + return 0; } - -*/ - - - diff --git a/examples/csoap/simpleserver.c b/examples/csoap/simpleserver.c index 3ccd61e..a165cfc 100644 --- a/examples/csoap/simpleserver.c +++ b/examples/csoap/simpleserver.c @@ -1,8 +1,8 @@ /****************************************************************** - * $Id: simpleserver.c,v 1.10 2004/10/15 13:42:57 snowdrop Exp $ + * $Id: simpleserver.c,v 1.11 2004/10/28 10:30:42 snowdrop Exp $ * * CSOAP Project: CSOAP examples project - * Copyright (C) 2003 Ferhat Ayaz + * Copyright (C) 2003-2004 Ferhat Ayaz * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,7 +18,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA * - * Email: ayaz@jprogrammer.net + * Email: ferhatayaz@yahoo.com ******************************************************************/ #include @@ -29,54 +29,45 @@ static const char *urn = "urn:examples"; static const char *method = "sayHello"; -void add_name(xmlNodePtr node, SoapEnv *env) +herror_t say_hello(SoapCtx *req, SoapCtx* res) { - char *name; - name = (char*)xmlNodeListGetString(node->doc, - node->xmlChildrenNode, 1); - - - if (!name) return; - soap_env_add_itemf(env,"xsd:string", "echo", - "Hello '%s'", name); - - /*xmlFree(BAD_CAST name);*/ - -} - - -SoapCtx* say_hello(SoapCtx *request) -{ + herror_t err; + char *name; - SoapEnv *env; - SoapCtx* ctx; xmlNodePtr method, node; - env = soap_env_new_with_response(request->env); + err = soap_env_new_with_response(req->env, &res->env); + if (err != H_OK) { + return err; + } - method = soap_env_get_method(request->env); + method = soap_env_get_method(req->env); node = soap_xml_get_children(method); while (node) { - add_name(node, env); + name = (char*)xmlNodeListGetString(node->doc, node->xmlChildrenNode, 1); + soap_env_add_itemf(req->env,"xsd:string", "echo", "Hello '%s'", name); node = soap_xml_get_next(node); } - ctx = soap_ctx_new(env); - return ctx; + return H_OK; } int main(int argc, char *argv[]) { + herror_t err; SoapRouter *router; log_set_level(HLOG_VERBOSE); - if (!soap_server_init_args(argc, argv)) { - return 1; + 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(); diff --git a/libcsoap/soap-client.c b/libcsoap/soap-client.c index 5562399..2a56e11 100644 --- a/libcsoap/soap-client.c +++ b/libcsoap/soap-client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: soap-client.c,v 1.9 2004/10/20 14:17:36 snowdrop Exp $ +* $Id: soap-client.c,v 1.10 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -27,7 +27,7 @@ /*--------------------------------- */ static int _block_socket = 0; -static SoapEnv *_soap_client_build_result(hresponse_t *res); +static herror_t _soap_client_build_result(hresponse_t *res, SoapEnv **out); /*--------------------------------- */ void soap_client_block_socket(int block) @@ -42,11 +42,11 @@ int soap_client_get_blockmode() -int 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); } @@ -65,17 +65,15 @@ long _file_get_size(const char* filename) return size; } -SoapCtx* -soap_client_invoke(SoapCtx *call, const char *url, const char *soap_action) + +herror_t +soap_client_invoke(SoapCtx *call, SoapCtx** response, const char *url, const char *soap_action) { /* Status */ - hstatus_t status; + herror_t status; /* Result document */ - SoapEnv* doc; - - /* Result Context */ - SoapCtx *ctx; + SoapEnv* res_env; /* Buffer variables*/ xmlBufferPtr buffer; @@ -118,48 +116,27 @@ soap_client_invoke(SoapCtx *call, const char *url, const char *soap_action) if (status != H_OK) { httpc_free(conn); - xmlBufferFree(buffer); - return soap_ctx_new( - soap_env_new_with_fault(Fault_Client, - "Can not begin post envelope","","")); + xmlBufferFree(buffer); + return status; } status = http_output_stream_write_string(conn->out, content); if (status != H_OK) { httpc_free(conn); xmlBufferFree(buffer); - return soap_ctx_new( - soap_env_new_with_fault(Fault_Client, - "Can not post envelope","","")); + return status; } - /* res = httpc_post(conn, url, (int)strlen(content), content);*/ - res = httpc_post_end(conn); - if (res == NULL) { + status = httpc_post_end(conn, &res); + if (status != H_OK) { httpc_free(conn); xmlBufferFree(buffer); - return soap_ctx_new( - soap_env_new_with_fault(Fault_Client, - conn->errmsg,"","")); + return status; } } else { - /* Use content-length transport */ - /*for (part=call->attachments->parts; part; part=part->next) { - file_size = _file_get_size(part->filename); - if (file_size == -1) [ - httpc_free(conn); - xmlBufferFree(buffer); - return soap_ctx_new( - soap_env_new_with_fault(Fault_Client, - "Can not open file!","","")); - } - - total_size += file_size + BOUDARY_LENGTH; - }*/ - /* Use chunked transport */ httpc_set_header(conn, HEADER_TRANSFER_ENCODING, TRANSFER_ENCODING_CHUNKED); @@ -168,27 +145,21 @@ soap_client_invoke(SoapCtx *call, const char *url, const char *soap_action) if (status != H_OK) { httpc_free(conn); xmlBufferFree(buffer); - return soap_ctx_new( - soap_env_new_with_fault(Fault_Client, - "Can not begin MIME transport","","")); + return status; } status = httpc_mime_next(conn, start_id, "text/xml", "binary"); if (status != H_OK) { httpc_free(conn); xmlBufferFree(buffer); - return soap_ctx_new( - soap_env_new_with_fault(Fault_Client, - "MIME transport error","","")); + return status; } status = http_output_stream_write(conn->out, content, strlen(content)); if (status != H_OK) { httpc_free(conn); xmlBufferFree(buffer); - return soap_ctx_new( - soap_env_new_with_fault(Fault_Client, - "MIME transport error","","")); + return status; } @@ -200,20 +171,16 @@ soap_client_invoke(SoapCtx *call, const char *url, const char *soap_action) log_error2("Send file failed. Status:%d", status); httpc_free(conn); xmlBufferFree(buffer); - return soap_ctx_new( - soap_env_new_with_fault(Fault_Client, - "MIME transport error while sending file","","")); + return status; } } - res = httpc_mime_end(conn); - if (!res) + status = httpc_mime_end(conn, &res); + if (status != H_OK) { httpc_free(conn); xmlBufferFree(buffer); - return soap_ctx_new( - soap_env_new_with_fault(Fault_Client, - "MIME transport error","","")); + return status; } } @@ -221,59 +188,54 @@ soap_client_invoke(SoapCtx *call, const char *url, const char *soap_action) xmlBufferFree(buffer); /* Build result */ - /* TODO: If res == NULL, find out where and why it is NULL! */ - doc = _soap_client_build_result(res); + status = _soap_client_build_result(res, &res_env); + if (status != H_OK) + return status; /* Create Context */ - ctx = soap_ctx_new(doc); - soap_ctx_add_files(ctx, res->attachments); + *response = soap_ctx_new(res_env); + soap_ctx_add_files(*response, res->attachments); - return ctx; + return H_OK; } -static -SoapEnv* _soap_client_build_result(hresponse_t *res) -{ - SoapEnv *env; +static +herror_t + _soap_client_build_result(hresponse_t *res, SoapEnv** env) +{ + herror_t err; log_verbose2("Building result (%p)", res); if (res == NULL) - return soap_env_new_with_fault(Fault_Client, - "Response is NULL","",""); + return herror_new("_soap_client_build_result", + GENERAL_INVALID_PARAM, "hresponse_t is NULL"); if (res->in == NULL) - return soap_env_new_with_fault(Fault_Client, - "Empty response from server!","",""); - + return herror_new("_soap_client_build_result", + GENERAL_INVALID_PARAM, "Empty response from server"); -/* - doc = xmlParseDoc(BAD_CAST res->body); - if (doc == NULL) { - return soap_env_new_with_fault(Fault_Client, - "Response is not in XML format!","",""); - } - env = soap_env_new_from_doc(doc); -*/ - env = soap_env_new_from_stream(res->in); + err = soap_env_new_from_stream(res->in, env); - if (env == NULL) { -/* xmlFreeDoc(doc);*/ - return soap_env_new_with_fault(Fault_Client, - "Can not create envelope","",""); + if (err != H_OK) { + return err; } - return env; + return H_OK; } -SoapCtx *soap_client_ctx_new(const char *urn, const char *method) -{ - SoapCtx *ctx = soap_ctx_new(soap_env_new_with_method(urn, method)); +herror_t soap_client_ctx_new(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); - return ctx; + return H_OK; } diff --git a/libcsoap/soap-client.h b/libcsoap/soap-client.h index 6820c14..22e41bb 100644 --- a/libcsoap/soap-client.h +++ b/libcsoap/soap-client.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-client.h,v 1.5 2004/10/20 14:17:36 snowdrop Exp $ + * $Id: soap-client.h,v 1.6 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -30,7 +30,7 @@ /** Initializes the client side soap engine */ -int soap_client_init_args(int argc, char *argv[]); +herror_t soap_client_init_args(int argc, char *argv[]); /** @@ -38,19 +38,22 @@ int soap_client_init_args(int argc, char *argv[]); the given envelope. @param env envelope to send + @param response the result envelope @param url url to the soap server @soap_action value for "SoapAction:" in the HTTP request header. - @returns the result envelope. In case of failure, - this function return an envelope with a fault object. + @returns H_OK if success */ -SoapCtx* soap_client_invoke(SoapCtx *ctx, +herror_t soap_client_invoke(SoapCtx *ctx, SoapCtx** response, const char *url, const char *soap_action); -SoapCtx *soap_client_ctx_new(const char *urn, const char *method); +/** + Creates a new soap context object. +*/ +herror_t soap_client_ctx_new(const char *urn, const char *method, SoapCtx **out); /** Sets the underlaying socket to use while connecting diff --git a/libcsoap/soap-ctx.c b/libcsoap/soap-ctx.c index a77bbdd..944abf0 100755 --- a/libcsoap/soap-ctx.c +++ b/libcsoap/soap-ctx.c @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-ctx.c,v 1.1 2004/10/15 13:33:48 snowdrop Exp $ + * $Id: soap-ctx.c,v 1.2 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -49,14 +49,16 @@ void soap_ctx_add_files(SoapCtx* ctx, attachments_t *attachments) } -hstatus_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 FILE_ERROR_OPEN; + if (!test) + return herror_new("soap_ctx_add_file", FILE_ERROR_OPEN, + "Can not open file '%s'", filename); fclose(test); diff --git a/libcsoap/soap-ctx.h b/libcsoap/soap-ctx.h index c32f372..2ea107b 100755 --- a/libcsoap/soap-ctx.h +++ b/libcsoap/soap-ctx.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-ctx.h,v 1.1 2004/10/15 13:33:48 snowdrop Exp $ + * $Id: soap-ctx.h,v 1.2 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -40,7 +40,7 @@ typedef struct _SoapCtx SoapCtx* soap_ctx_new(SoapEnv *env); /* should only be used internally */ /* Size of destination dest_href should be MAX_HREF_SIZE */ -hstatus_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); void soap_ctx_add_files(SoapCtx* ctx, attachments_t *attachments); void soap_ctx_free(SoapCtx* ctx); diff --git a/libcsoap/soap-env.c b/libcsoap/soap-env.c index 7d7811b..6527028 100644 --- a/libcsoap/soap-env.c +++ b/libcsoap/soap-env.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: soap-env.c,v 1.8 2004/10/15 14:33:07 snowdrop Exp $ +* $Id: soap-env.c,v 1.9 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -102,21 +102,96 @@ static /* ---------------------------------------------------------------------------- */ -SoapEnv * + + +herror_t +soap_env_new_from_doc (xmlDocPtr doc, SoapEnv **out) +{ + xmlNodePtr node; + SoapEnv *env; + + 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"); + } + + 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!"); + } + + env = (SoapEnv *) malloc (sizeof (SoapEnv)); + + /* 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 ! */ + + *out = env; + return H_OK; +} + + + + +herror_t +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"); + + doc = xmlParseDoc (BAD_CAST buffer); + if (doc == NULL) + 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) { + xmlFreeDoc (doc); + } + + return err; +} + + +herror_t soap_env_new_with_fault (fault_code_t faultcode, const char *faultstring, - const char *faultactor, const char *detail) + 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 NULL; - return soap_env_new_from_doc (doc); + 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) { + xmlFreeDoc (doc); + } + + return err; } -SoapEnv * -soap_env_new_with_response (SoapEnv * request) +herror_t +soap_env_new_with_response (SoapEnv * request, SoapEnv **out) { char urn[100]; char methodname[150]; @@ -124,19 +199,20 @@ soap_env_new_with_response (SoapEnv * request) if (request == NULL) { - log_error1 ("request object is NULL"); - return NULL; + return herror_new("soap_env_new_with_response", + GENERAL_INVALID_PARAM, "request (first param) is NULL"); } if (request->root == NULL) { - log_error1 ("request has no xml"); - return NULL; - } + 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 NULL; + 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)) @@ -148,15 +224,14 @@ soap_env_new_with_response (SoapEnv * request) } sprintf (methodname2, "%sResponse", methodname); - return soap_env_new_with_method (urn, methodname2); + return soap_env_new_with_method (urn, methodname2, out); } -SoapEnv * -soap_env_new_with_method (const char *urn, const char *method) +herror_t +soap_env_new_with_method (const char *urn, const char *method, SoapEnv **out) { xmlDocPtr env; - SoapEnv *call; xmlChar buffer[1054]; @@ -188,19 +263,30 @@ soap_env_new_with_method (const char *urn, const char *method) #endif } - env = xmlParseDoc (buffer); - call = soap_env_new_from_doc (env); + + env = xmlParseDoc (buffer); + if (!env) + return herror_new("soap_env_new_with_method", + XML_ERROR_PARSE, "Can not parse xml"); + + return soap_env_new_from_doc (env, out); - return call; } + + static int _soap_env_xml_io_read(void* ctx, char *buffer, int len) -{ - http_input_stream_t *in = (http_input_stream_t *)ctx; - if(!http_input_stream_is_ready(in)) - return 0; - return http_input_stream_read(in, buffer, 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; } static @@ -210,15 +296,21 @@ int _soap_env_xml_io_close(void *ctx) return 0; } -SoapEnv * -soap_env_new_from_stream(http_input_stream_t *in) + +herror_t +soap_env_new_from_stream(http_input_stream_t *in, SoapEnv **out) { - xmlDocPtr doc; - + xmlDocPtr doc; + herror_t err; + doc = xmlReadIO(_soap_env_xml_io_read, _soap_env_xml_io_close, in, "", NULL, 0); - - return soap_env_new_from_doc (doc); + + 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; } @@ -346,67 +438,6 @@ soap_env_free (SoapEnv * env) } -SoapEnv * -soap_env_new_from_doc (xmlDocPtr doc) -{ - SoapEnv *env; - xmlNodePtr node; - - if (doc == NULL) - { - log_error1 ("Can not create xml document!"); - return NULL; - } - - node = xmlDocGetRootElement (doc); - if (node == NULL) - { - log_error1 ("xml document is empty!"); - return NULL; - } - - env = (SoapEnv *) malloc (sizeof (SoapEnv)); - - /* set root */ - env->root = node; - - /* set method root - set call->cur (current node) to . - xpath: //Envelope/Body/ - */ - node = soap_xml_get_children (env->root); - env->cur = soap_xml_get_children (node); - - return env; -} - - - - -SoapEnv * -soap_env_new_from_buffer (const char *buffer) -{ - xmlDocPtr doc; - SoapEnv *env; - - if (buffer == NULL) - return NULL; - - doc = xmlParseDoc (BAD_CAST buffer); - if (doc == NULL) - return NULL; - - env = soap_env_new_from_doc (doc); - if (env == NULL) - { - xmlFreeDoc (doc); - return NULL; - } - - return env; -} - - xmlNodePtr soap_env_get_body (SoapEnv * env) { diff --git a/libcsoap/soap-env.h b/libcsoap/soap-env.h index 5b3bd7f..58af136 100644 --- a/libcsoap/soap-env.h +++ b/libcsoap/soap-env.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-env.h,v 1.7 2004/10/15 13:33:13 snowdrop Exp $ + * $Id: soap-env.h,v 1.8 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -50,8 +50,8 @@ typedef struct _SoapEnv @param faultstring A fault message @param faultactor The fault actor (This can be NULL) @param detail The detail of the error (This can be NULL) - - @returns A Soap envelope object like follows + @param out the result envelope out parameter like follows + @returns H_OK if success
    
 
- */
-SoapEnv *soap_env_new_with_fault(fault_code_t faultcode, 
+ */
+herror_t
+soap_env_new_with_fault(fault_code_t faultcode, 
 				 const char *faultstring,
 				 const char *faultactor,
-				 const char *detail);
+				 const char *detail, SoapEnv **out);
 
 /**
    Creates an envelope with a method to invoke a soap service.
@@ -84,7 +85,8 @@ SoapEnv *soap_env_new_with_fault(fault_code_t faultcode,
    @param urn The urn of the soap service to invoke
    @param method The method name of the soap service
 
-   @returns A Soap envelope object like follows 
+   @param out the result envelope out parameter like follows
+   @returns H_OK if success
 
    
    
 
  */
-SoapEnv *soap_env_new_with_method(const char *urn, const char *method);
+herror_t
+soap_env_new_with_method(const char *urn, const char *method, SoapEnv **out);
 
 
 /**
@@ -115,7 +118,8 @@ SoapEnv *soap_env_new_with_method(const char *urn, const char *method);
    @param req The request object. A response object will be created
     to this request.
 
-   @returns A Soap envelope object like follows 
+   @param out the result envelope out paramter like follows
+   @returns H_OK if success
 
    
    method != HTTP_REQUEST_POST) {
 
@@ -125,10 +126,17 @@ void soap_server_entry(httpd_conn_t *conn, hrequest_t *req)
 		return;
 	}
 
-/*	postdata = httpd_get_postdata(conn, req, &received, -1);*/
-  env = soap_env_new_from_stream(req->in);
 
-	header = hpairnode_new(HEADER_CONTENT_TYPE, "text/xml", NULL);
+	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) {
 
@@ -183,13 +191,22 @@ void soap_server_entry(httpd_conn_t *conn, hrequest_t *req)
 					return;
 				} else {
 
-					log_verbose2("func: %p", service->func);
+					log_verbose2("func: %p", service->func);
+					ctxres = soap_ctx_new(NULL);
 					/* ===================================== */
           /*       CALL SERVICE FUNCTION           */
 					/* ===================================== */
-					ctxres = service->func(ctx);
-					log_verbose2("func returned: (%p)", ctxres);
-					if (ctxres == NULL) {
+					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);
@@ -271,13 +288,38 @@ static
 void _soap_server_send_fault(httpd_conn_t *conn, hpair_t *header, 
 							 const char* errmsg)
 {
-	SoapEnv *envres;
+	SoapEnv *envres;
+	herror_t err;
+	char buffer[45];
 	httpd_set_headers(conn, header);
-	httpd_send_header(conn, 500, "FAILED");
-	envres = soap_env_new_with_fault(Fault_Server,
+	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);
-	_soap_server_send_env(conn->out, envres);
+		"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); + } } diff --git a/libcsoap/soap-server.h b/libcsoap/soap-server.h index 13abd92..62d136c 100644 --- a/libcsoap/soap-server.h +++ b/libcsoap/soap-server.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-server.h,v 1.3 2004/10/15 13:33:13 snowdrop Exp $ + * $Id: soap-server.h,v 1.4 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -19,7 +19,7 @@ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * - * Email: ayaz@jprogrammer.net + * Email: ferhatayaz@yahoo.com ******************************************************************/ #ifndef cSOAP_SERVER_H #define cSOAP_SERVER_H @@ -34,7 +34,9 @@ - + + +
ArgumentDescription
-NHTTPport [port]Port to listen (default: 10000)
-NHTTPport [port]Port to listen (default: 10000)
-NHTTPmaxconn [num]Maximum thread connections
-NHTTPlog [logfilename]logfile
@param argc commandline arg count @@ -42,7 +44,7 @@ @returns 1 if success, 0 otherwise */ -int soap_server_init_args(int argc, char *argv[]); +herror_t soap_server_init_args(int argc, char *argv[]); /** @@ -66,7 +68,7 @@ int soap_server_register_router(SoapRouter *router, const char* context); Enters the server loop and starts to listen to http requests. */ -int soap_server_run(); +herror_t soap_server_run(); /** diff --git a/libcsoap/soap-service.h b/libcsoap/soap-service.h index 7c2855e..c03d7df 100644 --- a/libcsoap/soap-service.h +++ b/libcsoap/soap-service.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: soap-service.h,v 1.2 2004/10/15 13:34:02 snowdrop Exp $ + * $Id: soap-service.h,v 1.3 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -28,7 +28,7 @@ #include #include -typedef SoapCtx* (*SoapServiceFunc)(SoapCtx*); +typedef herror_t (*SoapServiceFunc)(SoapCtx*, SoapCtx*); typedef struct _SoapService diff --git a/nanohttp/nanohttp-client.c b/nanohttp/nanohttp-client.c index de55427..c393573 100644 --- a/nanohttp/nanohttp-client.c +++ b/nanohttp/nanohttp-client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-client.c,v 1.22 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-client.c,v 1.23 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -48,7 +48,7 @@ FUNCTION: httpc_init DESC: Initialize http client connection NOTE: This will be called from soap_client_init_args() ----------------------------------------------------*/ -int +herror_t httpc_init(int argc, char *argv[]) { int i; @@ -63,7 +63,7 @@ httpc_init(int argc, char *argv[]) log_set_file(argv[i+1]); } } - return 0; + return H_OK; } /*-------------------------------------------------- @@ -198,11 +198,11 @@ FUNCTION: httpc_send_header DESC: Sends the current header information stored in conn through conn->sock. ----------------------------------------------------*/ -int +herror_t httpc_send_header(httpc_conn_t * conn) { hpair_t *p; - int status; + herror_t status; char buffer[1024]; p = conn->header; @@ -266,17 +266,20 @@ If success, this function will return 0. >0 otherwise. ----------------------------------------------------*/ static -int +herror_t httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, const char *urlstr) -{ +{ + hurl_t url; char buffer[4096]; - int status; + herror_t status; if (conn == NULL) { - log_error1("Connection object is NULL"); - return 1; + return herror_new( + "httpc_talk_to_server", + GENERAL_INVALID_PARAM, + "httpc_conn_t param is NULL"); } /* Build request header */ httpc_header_add_date(conn); @@ -295,9 +298,7 @@ httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, /* Open connection */ status = hsocket_open(&conn->sock, url.host, url.port); if (status != H_OK) { - log_error3("Can not open connection to '%s' (status:%d)", - SAVE_STR(url.host), status); - return 3; + return status; } status = hsocket_block(conn->sock, conn->block); if (status != H_OK) { @@ -321,23 +322,27 @@ httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, } else { log_error1("Unknown method type!"); - return 15; + return herror_new( + "httpc_talk_to_server", + GENERAL_INVALID_PARAM, + "hreq_method_t must be HTTP_REQUEST_GET or HTTP_REQUEST_POST"); } status = hsocket_send(conn->sock, buffer); if (status != H_OK) { log_error2("Can not send request (status:%d)", status); hsocket_close(conn->sock); - return 4; + 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 5; - } - return status; + return status; + } + + return H_OK; } @@ -345,28 +350,26 @@ httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, FUNCTION: httpc_get DESC: ----------------------------------------------------*/ -hresponse_t * -httpc_get(httpc_conn_t *conn, const char *urlstr) +herror_t +httpc_get(httpc_conn_t *conn, hresponse_t** out, const char *urlstr) { - int status; - hresponse_t *res; + herror_t status; status = httpc_talk_to_server(HTTP_REQUEST_GET, conn, urlstr); if (status != H_OK) { - _httpc_set_error(conn, status, "Can not comunicate: %s", urlstr); - return NULL; + return status; } - res = hresponse_new_from_socket(conn->sock); - if (res == NULL) + status = hresponse_new_from_socket(conn->sock, out); + if (status != H_OK) { - _httpc_set_error(conn, -1, "Can not get response from '%s'", urlstr); - return NULL; + return status; } - - return res; + + + return H_OK; } @@ -374,10 +377,10 @@ httpc_get(httpc_conn_t *conn, const char *urlstr) FUNCTION: httpc_post_begin DESC: Returns H_OK if success ----------------------------------------------------*/ -int httpc_post_begin(httpc_conn_t *conn, const char *url) +herror_t httpc_post_begin(httpc_conn_t *conn, const char *url) { - int status; + herror_t status; status = httpc_talk_to_server(HTTP_REQUEST_POST, conn, url); if (status != H_OK) @@ -394,27 +397,24 @@ FUNCTION: httpc_post_begin DESC: End a "POST" method and receive the response. You MUST call httpc_post_end() before! ----------------------------------------------------*/ -hresponse_t *httpc_post_end(httpc_conn_t *conn) +herror_t httpc_post_end(httpc_conn_t *conn, hresponse_t** out) { - int status; - hresponse_t *res; + herror_t status; status = http_output_stream_flush(conn->out); if (status != H_OK) { - _httpc_set_error(conn, status, "Can not flush output stream"); - return NULL; + return status; } - res = hresponse_new_from_socket(conn->sock); - if (res == NULL) - { - _httpc_set_error(conn, -1, "Can not get response "); - return NULL; + status = hresponse_new_from_socket(conn->sock, out); + if (status != H_OK) + { + return status; } - - return res; + + return H_OK; } @@ -422,6 +422,7 @@ hresponse_t *httpc_post_end(httpc_conn_t *conn) /* --------------------------------------------------- DIME support functions httpc_dime_* function set -----------------------------------------------------*/ +/* int httpc_dime_begin(httpc_conn_t *conn, const char *url) { int status; @@ -545,7 +546,7 @@ hresponse_t* httpc_dime_end(httpc_conn_t *conn) int status; hresponse_t *res; - /* Flush put stream */ + Flush put stream status = http_output_stream_flush(conn->out); if (status != H_OK) @@ -564,7 +565,7 @@ hresponse_t* httpc_dime_end(httpc_conn_t *conn) return res; } - +*/ /* --------------------------------------------------- MIME support functions httpc_mime_* function set -----------------------------------------------------*/ @@ -576,12 +577,12 @@ void _httpc_mime_get_boundary(httpc_conn_t *conn, char *dest) log_verbose2("boundary= \"%s\"", dest); } -int httpc_mime_begin(httpc_conn_t *conn, const char *url, +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) { - int status; + herror_t status; char buffer[300]; char temp[75]; char boundary[75]; @@ -626,12 +627,12 @@ int httpc_mime_begin(httpc_conn_t *conn, const char *url, } -int httpc_mime_next(httpc_conn_t *conn, +herror_t httpc_mime_next(httpc_conn_t *conn, const char* content_id, const char* content_type, const char* transfer_encoding) { - int status; + herror_t status; char buffer[512]; char boundary[75]; @@ -659,10 +660,9 @@ int httpc_mime_next(httpc_conn_t *conn, } -hresponse_t *httpc_mime_end(httpc_conn_t *conn) +herror_t httpc_mime_end(httpc_conn_t *conn, hresponse_t** out) { - hresponse_t *res; - int status; + herror_t status; char buffer[512]; char boundary[75]; @@ -675,25 +675,23 @@ hresponse_t *httpc_mime_end(httpc_conn_t *conn) (const byte_t*)buffer, strlen(buffer)); if (status != H_OK) - return NULL; + return status; /* Flush put stream */ status = http_output_stream_flush(conn->out); if (status != H_OK) { - _httpc_set_error(conn, status, "Can not flush output stream"); - return NULL; + return status; } - res = hresponse_new_from_socket(conn->sock); - if (res == NULL) + status = hresponse_new_from_socket(conn->sock, out); + if (status != H_OK) { - _httpc_set_error(conn, -1, "Can not get response "); - return NULL; + return status; } - - return res; + + return H_OK; } @@ -702,19 +700,20 @@ hresponse_t *httpc_mime_end(httpc_conn_t *conn) Send boundary and part header and continue with next part */ -int +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) { - int status; + herror_t status; FILE *fd = fopen (filename, "rb"); byte_t buffer[MAX_FILE_BUFFER_SIZE]; size_t size; if (fd == NULL) - return FILE_ERROR_OPEN; + 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); @@ -730,17 +729,23 @@ httpc_mime_send_file (httpc_conn_t * conn, if (size == -1) { fclose (fd); - return FILE_ERROR_READ; - } - - status = http_output_stream_write (conn->out, buffer, size); - if (status != H_OK) { - fclose (fd); - return status; + 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; + } + } } - fclose (fd); + fclose (fd); + log_verbose1("file sent!"); return H_OK; } diff --git a/nanohttp/nanohttp-client.h b/nanohttp/nanohttp-client.h index 6b54a03..b03c0a0 100644 --- a/nanohttp/nanohttp-client.h +++ b/nanohttp/nanohttp-client.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-client.h,v 1.11 2004/10/20 14:17:41 snowdrop Exp $ + * $Id: nanohttp-client.h,v 1.12 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -58,7 +58,7 @@ typedef struct httpc_conn /** initialize the httpc_* module */ -hstatus_t httpc_init(int argc, char *argv[]); +herror_t httpc_init(int argc, char *argv[]); /** Creates a new connection @@ -78,19 +78,20 @@ int httpc_set_header(httpc_conn_t *conn, const char* key, const char* value); /** Invoke a "GET" method request and receive the response */ -hresponse_t *httpc_get(httpc_conn_t *conn, const char *url); +herror_t +httpc_get(httpc_conn_t *conn, hresponse_t** out, const char *urlstr); /** Start a "POST" method request Returns: HSOCKET_OK or error flag */ -int 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! */ -hresponse_t *httpc_post_end(httpc_conn_t *conn); +herror_t httpc_post_end(httpc_conn_t *conn, hresponse_t **out); /* -------------------------------------------------------------- @@ -100,12 +101,13 @@ hresponse_t *httpc_post_end(httpc_conn_t *conn); /* DIME support httpc_dime_* function set */ +/* int httpc_dime_begin(httpc_conn_t *conn, const char *url); int httpc_dime_next(httpc_conn_t* conn, long content_length, const char *content_type, const char *id, const char *dime_options, int last); hresponse_t* httpc_dime_end(httpc_conn_t *conn); - +*/ /* -------------------------------------------------------------- MIME RELATED FUNCTIONS @@ -118,7 +120,7 @@ hresponse_t* httpc_dime_end(httpc_conn_t *conn); Begin MIME multipart/related POST request Returns: HSOCKET_OK or error flag */ -int httpc_mime_begin(httpc_conn_t *conn, const char *url, +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); @@ -127,7 +129,7 @@ int httpc_mime_begin(httpc_conn_t *conn, const char *url, Send boundary and part header and continue with next part */ -int httpc_mime_next(httpc_conn_t *conn, +herror_t httpc_mime_next(httpc_conn_t *conn, const char* content_id, const char* content_type, const char* transfer_encoding); @@ -135,14 +137,14 @@ int httpc_mime_next(httpc_conn_t *conn, /** Finish MIME request and get the response */ -hresponse_t *httpc_mime_end(httpc_conn_t *conn); +herror_t httpc_mime_end(httpc_conn_t *conn, hresponse_t** out); /** Send boundary and part header and continue with next part */ -int httpc_mime_send_file (httpc_conn_t * conn, +herror_t httpc_mime_send_file (httpc_conn_t * conn, const char *content_id, const char *content_type, const char *transfer_encoding, diff --git a/nanohttp/nanohttp-common.c b/nanohttp/nanohttp-common.c index df968c3..819c98b 100644 --- a/nanohttp/nanohttp-common.c +++ b/nanohttp/nanohttp-common.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-common.c,v 1.14 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-common.c,v 1.15 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -33,6 +33,61 @@ #ifdef MEM_DEBUG #include #endif + + + +typedef struct _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, ...) +{ + 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); + + return (herror_t)impl; + +} + +int herror_code(herror_t err) +{ + herror_impl_t* impl = (herror_impl_t*)err; + if (!err) return H_OK; + return impl->errcode; +} + +char* herror_func(herror_t err) +{ + herror_impl_t* impl = (herror_impl_t*)err; + if (!err) return ""; + return impl->func; +} + +char* herror_message(herror_t err) +{ + herror_impl_t* impl = (herror_impl_t*)err; + if (!err) return ""; + return impl->message; +} + +void herror_release(herror_t err) +{ + 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'}; @@ -372,7 +427,7 @@ hurl_dump(const hurl_t * url) } -int hurl_parse(hurl_t* url, const char *urlstr) +herror_t hurl_parse(hurl_t* url, const char *urlstr) { int iprotocol; int ihost; @@ -392,17 +447,17 @@ int hurl_parse(hurl_t* url, const char *urlstr) if (iprotocol == 0) { log_error1("no protocol"); - return URL_ERROR_NO_PROTOCOL; + return herror_new("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol"); } if (iprotocol + 3 >= len) { log_error1("no host"); - return URL_ERROR_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 URL_ERROR_NO_PROTOCOL; + return herror_new("hurl_parse", URL_ERROR_NO_PROTOCOL, "No protocol"); } /* find host */ ihost = iprotocol + 3; @@ -414,7 +469,7 @@ int hurl_parse(hurl_t* url, const char *urlstr) if (ihost == iprotocol + 1) { log_error1("no host"); - return URL_ERROR_NO_HOST; + return herror_new("hurl_parse", URL_ERROR_NO_HOST, "No host"); } /* find port */ iport = ihost; @@ -435,7 +490,8 @@ int hurl_parse(hurl_t* url, const char *urlstr) url->protocol = PROTOCOL_HTTPS; else if (strcmpigcase(protocol, "ftp")) url->protocol = PROTOCOL_FTP; - else return URL_ERROR_UNKNOWN_PROTOCOL; + 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; diff --git a/nanohttp/nanohttp-common.h b/nanohttp/nanohttp-common.h index f7b6c10..79ca625 100644 --- a/nanohttp/nanohttp-common.h +++ b/nanohttp/nanohttp-common.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-common.h,v 1.13 2004/10/20 14:17:41 snowdrop Exp $ + * $Id: nanohttp-common.h,v 1.14 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -118,8 +118,18 @@ #define MIME_ERROR_NO_ROOT_PART 1304 #define MIME_ERROR_NOT_MIME_MESSAGE 1305 + +/* General errors */ +#define GENERAL_INVALID_PARAM 1400 +#define GENERAL_HEADER_PARSE_ERROR 1401 - +/* Thread errors */ +#define THREAD_BEGIN_ERROR 1500 + +/* XML Errors */ +#define XML_ERROR_EMPTY_DOCUMENT 1600 +#define XML_ERROR_PARSE 1601 + /* Set Sleep function platform depended */ @@ -136,7 +146,7 @@ struct tm *localtime_r(const time_t *const timep, struct tm *p_tm); #endif typedef unsigned char byte_t; -typedef int hstatus_t; +typedef void* herror_t; @@ -159,7 +169,14 @@ typedef enum _hreq_method HTTP_REQUEST_GET }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); + /* string function to compare strings ignoring case @@ -350,7 +367,7 @@ typedef struct _hurl URL_ERROR_NO_PROTOCOL URL_ERROR_NO_HOST */ -hstatus_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 diff --git a/nanohttp/nanohttp-mime.c b/nanohttp/nanohttp-mime.c index 952cc22..2b6794d 100755 --- a/nanohttp/nanohttp-mime.c +++ b/nanohttp/nanohttp-mime.c @@ -3,7 +3,7 @@ * | \/ | | | | \/ | | _/ * |_''_| |_| |_''_| |_'/ PARSER * -* $Id: nanohttp-mime.c,v 1.2 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-mime.c,v 1.3 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -509,13 +509,21 @@ MIME_read_status mime_streamreader_function(void* userdata, if (!http_input_stream_is_ready(in)) return MIME_READ_EOF; - readed = http_input_stream_read(in, dest, *size); + readed = http_input_stream_read(in, dest, *size); + /* + log_info1("http_input_stream_read() returned 0"); + */ + if (readed == -1) { + log_error4("[%d] %s():%s ", herror_code(in->err), herror_func(in->err), herror_message(in->err)); + } + *size = readed; - if (*size!=-1) { - _log_str("reader.log", dest, *size); + if (*size!=-1) { + /* + _log_str("reader.log", dest, *size); + */ return MIME_READ_OK; - } - + } return MIME_READ_ERROR; } @@ -654,7 +662,7 @@ static void _mime_received_bytes(void *data, const unsigned char* bytes, int size) { int i=0; - char *id; + char *id, *type; mime_callback_data_t *cbdata = (mime_callback_data_t*)data; if (!cbdata ) { @@ -720,13 +728,18 @@ void _mime_received_bytes(void *data, const unsigned char* bytes, int size) 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); - if (id != NULL) - { - strcpy(cbdata->current_part->id, id); - if (!strcmp(id, cbdata->root_id)) - cbdata->message->root_part = cbdata->current_part; - } + 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)) + cbdata->message->root_part = cbdata->current_part; + } + type = hpairnode_get(cbdata->current_part->header, HEADER_CONTENT_TYPE); + if (type != NULL) + { + strcpy(cbdata->current_part->content_type, type); + } i++; break; } @@ -854,7 +867,7 @@ mime_message_parse_from_file(FILE *in, const char* root_id, -hstatus_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; @@ -864,7 +877,8 @@ hstatus_t mime_get_attachments(content_type_t *ctype, http_input_stream_t *in, a /* Check for MIME message */ if (!(ctype && !strcmp(ctype->type, "multipart/related"))) - return MIME_ERROR_NOT_MIME_MESSAGE; + 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"); @@ -872,14 +886,16 @@ hstatus_t mime_get_attachments(content_type_t *ctype, http_input_stream_t *in, a { /* TODO (#1#): Handle Error in http form */ log_error1("'boundary' not set for multipart/related"); - return MIME_ERROR_NO_BOUNDARY_PARAM; + 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 MIME_ERROR_NO_START_PARAM; + return herror_new("mime_get_attachments", MIME_ERROR_NO_START_PARAM, + "'start' not set for multipart/related"); } /* TODO (#1#): Set this not to working directory @@ -890,14 +906,16 @@ hstatus_t mime_get_attachments(content_type_t *ctype, http_input_stream_t *in, a { /* TODO (#1#): Handle Error in http form */ log_error1("MIME Parse Error"); - return MIME_ERROR_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 MIME_ERROR_NO_ROOT_PART; + return herror_new("mime_get_attachments", MIME_ERROR_NO_ROOT_PART, + "No root part found!"); } /* delete root_part from list */ diff --git a/nanohttp/nanohttp-mime.h b/nanohttp/nanohttp-mime.h index f78efa1..b51e44d 100755 --- a/nanohttp/nanohttp-mime.h +++ b/nanohttp/nanohttp-mime.h @@ -3,7 +3,7 @@ * | \/ | | | | \/ | | _/ * |_''_| |_| |_''_| |_'/ PARSER * -* $Id: nanohttp-mime.h,v 1.2 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-mime.h,v 1.3 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -40,7 +40,7 @@ -hstatus_t mime_get_attachments(content_type_t *ctype, +herror_t mime_get_attachments(content_type_t *ctype, http_input_stream_t *in, attachments_t **dest); diff --git a/nanohttp/nanohttp-request.c b/nanohttp/nanohttp-request.c index 0eb5a22..7661592 100755 --- a/nanohttp/nanohttp-request.c +++ b/nanohttp/nanohttp-request.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-request.c,v 1.2 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-request.c,v 1.3 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -217,10 +217,11 @@ hrequest_free(hrequest_t * req) } -hrequest_t * -hrequest_new_from_socket(hsocket_t sock) +herror_t +hrequest_new_from_socket(hsocket_t sock, hrequest_t **out) { - int i=0, status; + int i=0, readed; + herror_t status; hrequest_t *req; char buffer[MAX_HEADER_SIZE+1]; attachments_t *mimeMessage; @@ -228,11 +229,11 @@ hrequest_new_from_socket(hsocket_t sock) /* Read header */ while (iin = http_input_stream_new(sock, req->header); @@ -266,7 +263,7 @@ hrequest_new_from_socket(hsocket_t sock) { /* TODO (#1#): Handle error */ hrequest_free(req); - return NULL; + return status; } else { @@ -275,8 +272,8 @@ hrequest_new_from_socket(hsocket_t sock) } } - - return req; + *out = req; + return H_OK; } diff --git a/nanohttp/nanohttp-request.h b/nanohttp/nanohttp-request.h index c445596..8989247 100755 --- a/nanohttp/nanohttp-request.h +++ b/nanohttp/nanohttp-request.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-request.h,v 1.1 2004/10/15 13:30:42 snowdrop Exp $ + * $Id: nanohttp-request.h,v 1.2 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -45,7 +45,7 @@ typedef struct hrequest char root_part_id[150]; }hrequest_t; -hrequest_t *hrequest_new_from_socket(hsocket_t sock); +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 8f55856..f2e13c5 100755 --- a/nanohttp/nanohttp-response.c +++ b/nanohttp/nanohttp-response.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-response.c,v 1.1 2004/10/15 13:30:42 snowdrop Exp $ +* $Id: nanohttp-response.c,v 1.2 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -121,10 +121,12 @@ _hresponse_parse_header(const char *buffer) return res; } -hresponse_t * -hresponse_new_from_socket(hsocket_t sock) + +herror_t +hresponse_new_from_socket(hsocket_t sock, hresponse_t **out) { - int i=0, status; + int i=0, readed; + herror_t status; hresponse_t *res; attachments_t *mimeMessage; char buffer[MAX_HEADER_SIZE+1]; @@ -133,11 +135,11 @@ read_header: /* for errorcode: 100 (continue) */ /* Read header */ while (iin = http_input_stream_new_from_file(mimeMessage->root_part->filename); } } - - return res; + *out = res; + return H_OK; } diff --git a/nanohttp/nanohttp-response.h b/nanohttp/nanohttp-response.h index 7607eb7..63dd463 100755 --- a/nanohttp/nanohttp-response.h +++ b/nanohttp/nanohttp-response.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-response.h,v 1.1 2004/10/15 13:30:42 snowdrop Exp $ + * $Id: nanohttp-response.h,v 1.2 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -43,7 +43,7 @@ typedef struct hresponse char root_part_id[150]; }hresponse_t; -hresponse_t *hresponse_new_from_socket(hsocket_t sock); +herror_t hresponse_new_from_socket(hsocket_t sock, hresponse_t **out); void hresponse_free(hresponse_t *res); diff --git a/nanohttp/nanohttp-server.c b/nanohttp/nanohttp-server.c index 2ee15c7..71dbba7 100644 --- a/nanohttp/nanohttp-server.c +++ b/nanohttp/nanohttp-server.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-server.c,v 1.27 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-server.c,v 1.28 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -94,12 +94,13 @@ static void WSAReaper(void *x); * NOTE: This will be called from soap_server_init_args() * ----------------------------------------------------- */ -int +herror_t httpd_init (int argc, char *argv[]) { - int i, status; + int i; + herror_t status; status = hsocket_module_init (); - if (status != 0) + if (status != H_OK) return status; /* write argument information */ @@ -136,12 +137,15 @@ httpd_init (int argc, char *argv[]) memset ((char *) &_httpd_connection[i], 0, sizeof (_httpd_connection[i])); } -#ifdef WIN32 +#ifdef WIN32 + /* if (_beginthread (WSAReaper, 0, NULL) == -1) { log_error1 ("Winsock reaper thread failed to start"); - return (-1); - } + return herror_new("httpd_init", THREAD_BEGIN_ERROR, + "_beginthread() failed while starting WSAReaper"); + } + */ #endif /* create socket */ @@ -244,7 +248,7 @@ httpd_response_set_content_type (httpd_conn_t * res, const char *content_type) * FUNCTION: httpd_response_send_header * ----------------------------------------------------- */ -int +herror_t httpd_send_header (httpd_conn_t * res, int code, const char *text) { struct tm stm; @@ -252,7 +256,7 @@ httpd_send_header (httpd_conn_t * res, int code, const char *text) char buffer[255]; char header[1024]; hpair_t *cur; - int status; + herror_t status; /* set status code */ sprintf (header, "HTTP/1.1 %d %s\r\n", code, text); @@ -291,9 +295,11 @@ 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; res->out = http_output_stream_new (res->sock, res->header); - return status; + return H_OK; } @@ -381,10 +387,12 @@ httpd_session_main (void *data) hrequest_t *req = NULL; /* only for test */ httpd_conn_t *rconn; hservice_t *service = NULL; - long content_length = 0; + long content_length = 0; + herror_t status; header[0] = '\0'; - len = 0; + len = 0; + log_verbose1 ("starting httpd_session_main()"); @@ -393,10 +401,11 @@ httpd_session_main (void *data) /* req = hrequest_new_from_buffer (header);*/ rconn = httpd_new(conn->sock); - req = hrequest_new_from_socket (conn->sock); - if (req == NULL) + status = hrequest_new_from_socket (conn->sock, &req); + if (status != H_OK) { - httpd_send_internal_error (rconn, "Request parse error!"); + httpd_send_internal_error (rconn, herror_message(status)/*"Request parse error!"*/); + herror_release(status); } else { @@ -597,10 +606,10 @@ _httpd_start_thread (conndata_t * conn) * ----------------------------------------------------- */ -int +herror_t httpd_run () { - int err; + herror_t err; conndata_t *conn; fd_set fds; struct timeval timeout; @@ -614,7 +623,7 @@ httpd_run () err = hsocket_listen (_httpd_socket); if (err != H_OK) { - log_error2 ("httpd_run(): '%d'", err); + log_error2 ("httpd_run(): '%d'", herror_message(err)); return err; } log_verbose2 ("listening to port '%d'", _httpd_port); @@ -626,7 +635,7 @@ httpd_run () err = hsocket_block (_httpd_socket, 0); if (err != H_OK) { - log_error2 ("httpd_run(): '%d'", err); + log_error2 ("httpd_run(): '%s'", herror_message(err)); return err; } @@ -673,8 +682,8 @@ httpd_run () err = hsocket_accept (_httpd_socket, &(conn->sock)); if (err != H_OK) { - log_error2 ("Can not accept socket: %d", err); - return -1; /* this is hard core! */ + log_error2 ("Can not accept socket: %s", herror_message(err)); + return err; /* this is hard core! */ } /* Now start a thread */ @@ -805,7 +814,7 @@ _httpd_mime_get_boundary (httpd_conn_t * conn, char *dest) Begin MIME multipart/related POST Returns: H_OK or error flag */ -int +herror_t httpd_mime_send_header (httpd_conn_t * conn, const char *related_start, const char *related_start_info, @@ -861,12 +870,12 @@ httpd_mime_send_header (httpd_conn_t * conn, Send boundary and part header and continue with next part */ -int +herror_t httpd_mime_next (httpd_conn_t * conn, const char *content_id, const char *content_type, const char *transfer_encoding) { - int status; + herror_t status; char buffer[512]; char boundary[75]; @@ -901,19 +910,20 @@ httpd_mime_next (httpd_conn_t * conn, Send boundary and part header and continue with next part */ -int +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) { - int status; + herror_t status; FILE *fd = fopen (filename, "rb"); byte_t buffer[MAX_FILE_BUFFER_SIZE]; size_t size; if (fd == NULL) - return FILE_ERROR_OPEN; + 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); @@ -929,7 +939,8 @@ httpd_mime_send_file (httpd_conn_t * conn, if (size == -1) { fclose (fd); - return FILE_ERROR_READ; + 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); @@ -947,10 +958,10 @@ httpd_mime_send_file (httpd_conn_t * conn, Finish MIME request Returns: H_OK or error flag */ -int +herror_t httpd_mime_end (httpd_conn_t * conn) { - int status; + herror_t status; char buffer[512]; char boundary[75]; diff --git a/nanohttp/nanohttp-server.h b/nanohttp/nanohttp-server.h index 513f4de..e344eba 100644 --- a/nanohttp/nanohttp-server.h +++ b/nanohttp/nanohttp-server.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-server.h,v 1.7 2004/10/20 14:17:41 snowdrop Exp $ + * $Id: nanohttp-server.h,v 1.8 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -62,22 +62,23 @@ typedef struct tag_hservice /* Begin httpd_* function set */ -int httpd_init(int argc, char *argv[]); +herror_t httpd_init(int argc, char *argv[]); int httpd_register(const char* ctx, httpd_service service); -int httpd_run(); +herror_t httpd_run(); void httpd_destroy(); hservice_t *httpd_services(); -int httpd_send_header(httpd_conn_t *res, +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); - + +/* unsigned char *httpd_get_postdata(httpd_conn_t *conn, hrequest_t *req, long *received, long max); - +*/ /* -------------------------------------------------------------- MIME RELATED FUNCTIONS ---------------------------------------------------------------*/ @@ -89,7 +90,7 @@ unsigned char *httpd_get_postdata(httpd_conn_t *conn, Begin MIME multipart/related POST Returns: HSOCKET_OK or error flag */ -int httpd_mime_send_header(httpd_conn_t *conn, +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); @@ -98,7 +99,7 @@ int httpd_mime_send_header(httpd_conn_t *conn, Send boundary and part header and continue with next part */ -int httpd_mime_next(httpd_conn_t *conn, +herror_t httpd_mime_next(httpd_conn_t *conn, const char* content_id, const char* content_type, const char* transfer_encoding); @@ -107,7 +108,7 @@ int httpd_mime_next(httpd_conn_t *conn, Send boundary and part header and continue with next part */ -int httpd_mime_send_file(httpd_conn_t *conn, +herror_t httpd_mime_send_file(httpd_conn_t *conn, const char* content_id, const char* content_type, const char* transfer_encoding, @@ -117,7 +118,7 @@ int httpd_mime_send_file(httpd_conn_t *conn, Finish MIME request Returns: HSOCKET_OK or error flag */ -int 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 d7a1d59..7b378fc 100644 --- a/nanohttp/nanohttp-socket.c +++ b/nanohttp/nanohttp-socket.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-socket.c,v 1.29 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-socket.c,v 1.30 2004/10/28 10:30:46 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -73,7 +73,7 @@ FUNCTION: hsocket_module_init NOTE: This will be called from httpd_init() for server and from httpc_init() for client ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_module_init () { #ifdef WIN32 @@ -103,7 +103,7 @@ hsocket_module_destroy () /*-------------------------------------------------- FUNCTION: hsocket_init ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_init (hsocket_t * sock) { /* just set the descriptor to -1 */ @@ -123,7 +123,7 @@ hsocket_free (hsocket_t sock) /*-------------------------------------------------- FUNCTION: hsocket_open ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_open (hsocket_t * dsock, const char *hostname, int port) { hsocket_t sock; @@ -133,12 +133,12 @@ hsocket_open (hsocket_t * dsock, const char *hostname, int port) sock = socket (AF_INET, SOCK_STREAM, 0); if (sock <= 0) - return HSOCKET_ERROR_CREATE; + return herror_new("hsocket_open", HSOCKET_ERROR_CREATE, strerror (errno)); /* Get host data */ host = gethostbyname (hostname); if (host == NULL) - return HSOCKET_ERROR_GET_HOSTNAME; + return herror_new("hsocket_open", HSOCKET_ERROR_GET_HOSTNAME, strerror (errno)); ip = inet_ntoa (*(struct in_addr *) *host->h_addr_list); address.sin_addr.s_addr = inet_addr (ip); @@ -149,7 +149,7 @@ hsocket_open (hsocket_t * dsock, const char *hostname, int port) /* connect to the server */ if (connect (sock, (struct sockaddr *) &address, sizeof (address)) != 0) - return HSOCKET_ERROR_CONNECT; + return herror_new("hsocket_open", HSOCKET_ERROR_CONNECT, "Connect to '%s:%d' failed", hostname, port); *dsock = sock; return H_OK; @@ -158,7 +158,7 @@ hsocket_open (hsocket_t * dsock, const char *hostname, int port) /*-------------------------------------------------- FUNCTION: hsocket_bind ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_bind (hsocket_t * dsock, int port) { hsocket_t sock; @@ -169,7 +169,7 @@ hsocket_bind (hsocket_t * dsock, int port) if (sock == -1) { log_error2 ("Can not create socket: '%s'", strerror (errno)); - return HSOCKET_ERROR_CREATE; + return herror_new("hsocket_bind", HSOCKET_ERROR_CREATE, strerror (errno)); } /* bind socket */ addr.sin_family = AF_INET; @@ -181,7 +181,7 @@ hsocket_bind (hsocket_t * dsock, int port) if (bind (sock, (struct sockaddr *) &addr, sizeof (struct sockaddr)) == -1) { log_error2 ("Can not bind: '%s'", strerror (errno)); - return HSOCKET_ERROR_BIND; + return herror_new("hsocket_bind", HSOCKET_ERROR_BIND, strerror (errno)); } *dsock = sock; return H_OK; @@ -190,7 +190,7 @@ hsocket_bind (hsocket_t * dsock, int port) /*---------------------------------------------------------- FUNCTION: hsocket_accept ----------------------------------------------------------*/ -hstatus_t +herror_t hsocket_accept (hsocket_t sock, hsocket_t * dest) { socklen_t asize; @@ -198,7 +198,8 @@ hsocket_accept (hsocket_t sock, hsocket_t * dest) struct sockaddr_in addr; if (sock <= 0) - return HSOCKET_ERROR_NOT_INITIALIZED; + return herror_new("hsocket_accept", HSOCKET_ERROR_NOT_INITIALIZED, + "Called hsocket_listen() before initializing!"); asize = sizeof (struct sockaddr_in); #ifdef WIN32 @@ -207,7 +208,7 @@ hsocket_accept (hsocket_t sock, hsocket_t * dest) sockfd = accept (sock, (struct sockaddr *) &addr, &asize); if (sockfd == INVALID_SOCKET) { if (WSAGetLastError () != WSAEWOULDBLOCK) - return HSOCKET_ERROR_ACCEPT; + return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT, strerror (errno)); } else { break; } @@ -216,7 +217,7 @@ hsocket_accept (hsocket_t sock, hsocket_t * dest) /* TODO (#1#): why not a loop like in win32? */ sockfd = accept (sock, (struct sockaddr *) &addr, &asize); if (sockfd == -1) { - return HSOCKET_ERROR_ACCEPT; + return herror_new("hsocket_accept", HSOCKET_ERROR_ACCEPT, strerror (errno)); } #endif /* TODO (#1#): Write to access.log file */ @@ -231,16 +232,17 @@ hsocket_accept (hsocket_t sock, hsocket_t * dest) /*-------------------------------------------------- FUNCTION: hsocket_listen ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_listen (hsocket_t sock) { if (sock <= 0) - return HSOCKET_ERROR_NOT_INITIALIZED; + return herror_new("hsocket_listen", HSOCKET_ERROR_NOT_INITIALIZED, + "Called hsocket_listen() before initializing!"); if (listen (sock, 15) == -1) { log_error2 ("Can not listen: '%s'", strerror (errno)); - return HSOCKET_ERROR_LISTEN; + return herror_new("hsocket_listen", HSOCKET_ERROR_LISTEN, strerror (errno)); } return H_OK; @@ -315,34 +317,49 @@ hsocket_close (hsocket_t sock) #endif log_verbose1 ("closed"); } + + +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; +} /*-------------------------------------------------- FUNCTION: hsocket_send ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_nsend (hsocket_t sock, const byte_t *bytes, int n) { int size; - + int total=0; if (sock <= 0) - return HSOCKET_ERROR_NOT_INITIALIZED; + return herror_new("hsocket_nsend", HSOCKET_ERROR_NOT_INITIALIZED, + "Called hsocket_listen() before initializing!"); + /* TODO (#1#): check return value and send again until n bytes sent */ while (1) { - size = send((int) sock, bytes, n, 0); + size = send((int) sock, bytes + total, n, 0); + /* size = _test_send_to_file(filename, bytes, n);*/ #ifdef WIN32 if (size == INVALID_SOCKET) if (WSAGetLastError () == WSAEWOULDBLOCK) continue; else - return HSOCKET_ERROR_SEND; + return herror_new("hsocket_nsend", HSOCKET_ERROR_SEND, strerror (errno)); #else if (size == -1) - return HSOCKET_ERROR_SEND; + return herror_new("hsocket_nsend", HSOCKET_ERROR_SEND, strerror (errno)); #endif - n -= size; + n -= size; + total += size; if (n<=0) break; } return H_OK; @@ -351,7 +368,7 @@ hsocket_nsend (hsocket_t sock, const byte_t *bytes, int n) /*-------------------------------------------------- FUNCTION: hsocket_send ----------------------------------------------------*/ -hstatus_t +herror_t hsocket_send (hsocket_t sock, const char *str) { return hsocket_nsend (sock, str, strlen (str)); @@ -360,14 +377,16 @@ hsocket_send (hsocket_t sock, const char *str) /* return: -1 is error. read bytes otherwise */ -int -hsocket_read (hsocket_t sock, byte_t *buffer, int total, int force) +herror_t +hsocket_read (hsocket_t sock, byte_t *buffer, int total, int force, int *received) { int status; int totalRead; int wsa_error; totalRead = 0; - +/* + log_verbose3("Entering hsocket_read(total=%d,force=%d)", total, force); +*/ do { status = recv(sock, &buffer[totalRead], total - totalRead, 0); @@ -379,12 +398,12 @@ hsocket_read (hsocket_t sock, byte_t *buffer, int total, int force) switch (wsa_error) { case WSAEWOULDBLOCK: - case WSAEALREADY: - case WSAEINPROGRESS: + /* case WSAEALREADY: + case WSAEINPROGRESS: */ continue; default: log_error2("WSAGetLastError()=%d", wsa_error); - return -1; + return herror_new("hsocket_read", HSOCKET_ERROR_RECEIVE, strerror (errno)); } } @@ -398,24 +417,32 @@ hsocket_read (hsocket_t sock, byte_t *buffer, int total, int force) } */ if (status == -1) - return -1; + return herror_new("hsocket_read", HSOCKET_ERROR_RECEIVE, strerror (errno)); #endif - if (!force) { - return status; + if (!force) { + *received = status; + /* + log_verbose3("Leaving !force (received=%d)(status=%d)", *received, status); + */ + return H_OK; } totalRead += status; if (totalRead == total) { - return totalRead; + *received = totalRead; + /* + log_verbose4("Leaving totalRead == total (received=%d)(status=%d)(totalRead=%d)", *received, status, totalRead); + */ + return H_OK; } } while (1); } -hstatus_t +herror_t hsocket_block(hsocket_t sock, int block) { #ifdef WIN32 @@ -423,7 +450,8 @@ hsocket_block(hsocket_t sock, int block) #endif if (sock <= 0) - return HSOCKET_ERROR_NOT_INITIALIZED; + return herror_new("hsocket_block", HSOCKET_ERROR_NOT_INITIALIZED, + "Called hsocket_listen() before initializing!"); #ifdef WIN32 /*#define HSOCKET_BLOCKMODE 0 @@ -434,7 +462,7 @@ hsocket_block(hsocket_t sock, int block) if (ioctlsocket (sock, FIONBIO, (u_long FAR *) & iMode) == INVALID_SOCKET) { log_error1 ("ioctlsocket error"); - return HSOCKET_ERROR_IOCTL; + return herror_new("hsocket_block", HSOCKET_ERROR_IOCTL, strerror (errno)); } #else /* fcntl(sock, F_SETFL, O_NONBLOCK); */ /* TODO (#1#): check for *nix the non blocking sockets */ diff --git a/nanohttp/nanohttp-socket.h b/nanohttp/nanohttp-socket.h index 8deb273..83d8f72 100644 --- a/nanohttp/nanohttp-socket.h +++ b/nanohttp/nanohttp-socket.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-socket.h,v 1.13 2004/10/15 13:29:37 snowdrop Exp $ + * $Id: nanohttp-socket.h,v 1.14 2004/10/28 10:30:47 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -48,7 +48,7 @@ @returns This function should always return H_OK. */ -hstatus_t hsocket_module_init(); +herror_t hsocket_module_init(); /** @@ -66,7 +66,7 @@ void hsocket_module_destroy(); @returns This function should always return H_OK. */ -hstatus_t hsocket_init(hsocket_t *sock); +herror_t hsocket_init(hsocket_t *sock); /** @@ -90,7 +90,7 @@ void hsocket_free(hsocket_t sock);
HSOCKET_ERROR_GET_HOSTNAME
HSOCKET_ERROR_CONNECT */ -hstatus_t hsocket_open(hsocket_t *sock, const char* host, int port); +herror_t hsocket_open(hsocket_t *sock, const char* host, int port); /** @@ -114,7 +114,7 @@ void hsocket_close(hsocket_t sock); @see hsocket_listen */ -hstatus_t hsocket_bind(hsocket_t *sock, int port); +herror_t hsocket_bind(hsocket_t *sock, int port); /** @@ -128,7 +128,7 @@ hstatus_t hsocket_bind(hsocket_t *sock, int port);
HSOCKET_ERROR_NOT_INITIALIZED
HSOCKET_ERROR_LISTEN */ -hstatus_t hsocket_listen(hsocket_t sock); +herror_t hsocket_listen(hsocket_t sock); /** @@ -142,7 +142,7 @@ hstatus_t hsocket_listen(hsocket_t sock);
HSOCKET_ERROR_NOT_INITIALIZED
HSOCKET_ERROR_ACCEPT */ -hstatus_t hsocket_accept(hsocket_t sock, hsocket_t *dest); +herror_t hsocket_accept(hsocket_t sock, hsocket_t *dest); /** @@ -156,7 +156,7 @@ hstatus_t hsocket_accept(hsocket_t sock, hsocket_t *dest);
HSOCKET_ERROR_NOT_INITIALIZED
HSOCKET_ERROR_SEND */ -hstatus_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); /** @@ -169,7 +169,7 @@ hstatus_t hsocket_nsend(hsocket_t sock, const byte_t* bytes, int size);
HSOCKET_ERROR_NOT_INITIALIZED
HSOCKET_ERROR_SEND */ -hstatus_t hsocket_send(hsocket_t sock, const char* str); +herror_t hsocket_send(hsocket_t sock, const char* str); /** @@ -188,7 +188,7 @@ hstatus_t hsocket_send(hsocket_t sock, const char* str); the socket. */ -int hsocket_read(hsocket_t sock, byte_t* buffer, int size, int force); +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. @@ -199,7 +199,7 @@ int hsocket_read(hsocket_t sock, byte_t* buffer, int size, int force);
HSOCKET_ERROR_NOT_INITIALIZED
HSOCKET_ERROR_IOCTL */ -hstatus_t hsocket_block(hsocket_t sock, int block); +herror_t hsocket_block(hsocket_t sock, int block); #endif diff --git a/nanohttp/nanohttp-stream.c b/nanohttp/nanohttp-stream.c index 039eadc..991d2ab 100755 --- a/nanohttp/nanohttp-stream.c +++ b/nanohttp/nanohttp-stream.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: nanohttp-stream.c,v 1.3 2004/10/20 14:17:41 snowdrop Exp $ +* $Id: nanohttp-stream.c,v 1.4 2004/10/28 10:30:47 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -180,21 +180,22 @@ static int _http_input_stream_content_length_read( http_input_stream_t *stream, byte_t *dest, int size) { - int status; + herror_t status; + int read; /* check limit */ if (stream->content_length - stream->received < size) size = stream->content_length - stream->received; /* read from socket */ - status = hsocket_read(stream->sock, dest, size, 1); - if (status == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; + status = hsocket_read(stream->sock, dest, size, 1, &read); + if (status != H_OK) { + stream->err = status; return -1; } - stream->received += status; - return status; + stream->received += read; + return read; } static @@ -203,14 +204,22 @@ int _http_input_stream_chunked_read_chunk_size( { char chunk[25]; int status, i = 0; - int chunk_size; + int chunk_size; + herror_t err; while (1) { - status = hsocket_read(stream->sock, &(chunk[i]), 1, 1); - - if (status == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; + 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)); + + stream->err = err; return -1; } @@ -221,13 +230,17 @@ int _http_input_stream_chunked_read_chunk_size( 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);*/ + 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 = STREAM_ERROR_NO_CHUNK_SIZE; + stream->err = herror_new( + "_http_input_stream_chunked_read_chunk_size", STREAM_ERROR_NO_CHUNK_SIZE, + "reached max line == %d", i); return -1; } else @@ -235,8 +248,10 @@ int _http_input_stream_chunked_read_chunk_size( } /* this should never happens */ - stream->err = STREAM_ERROR_NO_CHUNK_SIZE; - 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 @@ -245,7 +260,8 @@ int _http_input_stream_chunked_read( { int status, counter; int remain, read=0; - char ch; + char ch; + herror_t err; while (size > 0) { @@ -258,10 +274,10 @@ int _http_input_stream_chunked_read( counter = 100; /* maximum for stop infinity */ while (1) { - status = hsocket_read(stream->sock, &ch, 1, 1); + err = hsocket_read(stream->sock, &ch, 1, 1, &status ); - if (status == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; + if (err != H_OK) { + stream->err = err; return -1; } @@ -270,7 +286,8 @@ int _http_input_stream_chunked_read( break; } if (counter-- == 0) { - stream->err = STREAM_ERROR_WRONG_CHUNK_SIZE; + stream->err = herror_new("_http_input_stream_chunked_read", + STREAM_ERROR_WRONG_CHUNK_SIZE,"Wrong chunk-size"); return -1; } } @@ -298,20 +315,29 @@ int _http_input_stream_chunked_read( /* show remaining chunk size in socket */ if (remain < size) { - /* read from socket */ - status = hsocket_read(stream->sock, &(dest[read]), remain, 1); - if (status == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; - return -1; - } - + /* read from socket */ + 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; + } } else { /* read from socket */ - status = hsocket_read(stream->sock, &(dest[read]), size, 1); - if (status == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; + 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; } } @@ -330,11 +356,12 @@ 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 */ - status = hsocket_read(stream->sock, dest, size, 0); - if (status == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; + err = hsocket_read(stream->sock, dest, size, 0, &status ); + if (err != H_OK) { + stream->err = err; return -1; } @@ -354,7 +381,8 @@ int _http_input_stream_file_read( readed = fread(dest, 1, size, stream->fd); if (readed == -1) { - stream->err = HSOCKET_ERROR_RECEIVE; + stream->err = herror_new("_http_input_stream_file_read", + HSOCKET_ERROR_RECEIVE, "fread() returned -1"); return -1; } @@ -420,7 +448,8 @@ int http_input_stream_read(http_input_stream_t *stream, readed=_http_input_stream_file_read(stream, dest, size); break; default: - stream->err = STREAM_ERROR_INVALID_TYPE; + stream->err = herror_new("http_input_stream_read", + STREAM_ERROR_INVALID_TYPE, "%d is invalid stream type", stream->type); return -1; } @@ -493,10 +522,10 @@ void http_output_stream_free(http_output_stream_t *stream) Writes 'size' bytes of 'bytes' into stream. Returns socket error flags or H_OK. */ -hstatus_t http_output_stream_write(http_output_stream_t *stream, +herror_t http_output_stream_write(http_output_stream_t *stream, const byte_t *bytes, int size) { - int status; + herror_t status; char chunked[15]; if (stream->type == HTTP_TRANSFER_CHUNKED) @@ -529,16 +558,16 @@ hstatus_t http_output_stream_write(http_output_stream_t *stream, Writes 'strlen()' bytes of 'str' into stream. Returns socket error flags or H_OK. */ -int http_output_stream_write_string(http_output_stream_t *stream, +herror_t http_output_stream_write_string(http_output_stream_t *stream, const char *str) { return http_output_stream_write(stream, str, strlen(str)); } -int http_output_stream_flush(http_output_stream_t *stream) +herror_t http_output_stream_flush(http_output_stream_t *stream) { - int status; + herror_t status; if (stream->type == HTTP_TRANSFER_CHUNKED) { diff --git a/nanohttp/nanohttp-stream.h b/nanohttp/nanohttp-stream.h index fb44284..7b0f0f8 100755 --- a/nanohttp/nanohttp-stream.h +++ b/nanohttp/nanohttp-stream.h @@ -1,5 +1,5 @@ /****************************************************************** - * $Id: nanohttp-stream.h,v 1.2 2004/10/15 14:26:15 snowdrop Exp $ + * $Id: nanohttp-stream.h,v 1.3 2004/10/28 10:30:47 snowdrop Exp $ * * CSOAP Project: A http client/server library in C * Copyright (C) 2003-2004 Ferhat Ayaz @@ -88,7 +88,7 @@ typedef enum http_transfer_type typedef struct http_input_stream { hsocket_t sock; - hstatus_t err; + herror_t err; http_transfer_type_t type; int received; int content_length; @@ -236,7 +236,7 @@ void http_output_stream_free(http_output_stream_t *stream);
HSOCKET_ERROR_NOT_INITIALIZED
HSOCKET_ERROR_SEND */ -hstatus_t http_output_stream_write(http_output_stream_t *stream, +herror_t http_output_stream_write(http_output_stream_t *stream, const byte_t *bytes, int size); /** @@ -249,7 +249,7 @@ hstatus_t http_output_stream_write(http_output_stream_t *stream,
HSOCKET_ERROR_NOT_INITIALIZED
HSOCKET_ERROR_SEND */ -hstatus_t http_output_stream_write_string(http_output_stream_t *stream, +herror_t http_output_stream_write_string(http_output_stream_t *stream, const char *str); @@ -263,7 +263,7 @@ hstatus_t http_output_stream_write_string(http_output_stream_t *stream,
HSOCKET_ERROR_NOT_INITIALIZED
HSOCKET_ERROR_SEND */ -hstatus_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