From 7ed3b0bb398f43429c6c0d0d9bd8b5bada1d7389 Mon Sep 17 00:00:00 2001 From: snowdrop Date: Fri, 15 Oct 2004 13:33:13 +0000 Subject: added attachment support --- libcsoap/soap-client.c | 176 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 165 insertions(+), 11 deletions(-) (limited to 'libcsoap/soap-client.c') diff --git a/libcsoap/soap-client.c b/libcsoap/soap-client.c index d287609..58886ff 100644 --- a/libcsoap/soap-client.c +++ b/libcsoap/soap-client.c @@ -1,5 +1,5 @@ /****************************************************************** -* $Id: soap-client.c,v 1.7 2004/09/19 07:05:03 snowdrop Exp $ +* $Id: soap-client.c,v 1.8 2004/10/15 13:33:13 snowdrop Exp $ * * CSOAP Project: A SOAP client/server library in C * Copyright (C) 2003 Ferhat Ayaz @@ -38,35 +38,173 @@ int soap_client_init_args(int argc, char *argv[]) } -SoapEnv* -soap_client_invoke(SoapEnv *call, const char *url, const char *soap_action) + +static +long _file_get_size(const char* filename) +{ + FILE *f = fopen(filename, "r"); + long size; + + if (!f) return -1; + + fseek(f, 0, SEEK_END); + size=ftell(f); + fclose(f); + return size; +} + +SoapCtx* +soap_client_invoke(SoapCtx *call, const char *url, const char *soap_action) { + /* Status */ + hstatus_t status; + /* Result document */ SoapEnv* doc; + /* Result Context */ + SoapCtx *ctx; + /* Buffer variables*/ xmlBufferPtr buffer; char *content; + char tmp[15]; /* Transport variables */ httpc_conn_t *conn; hresponse_t *res; + /* multipart/related start id*/ + char start_id[150]; + static int counter=1; + part_t *part; + int file_count=0; + long total_size; + long file_size; + /* Create buffer */ buffer = xmlBufferCreate(); - xmlNodeDump(buffer, call->root->doc,call->root, 1 ,0); + xmlNodeDump(buffer, call->env->root->doc,call->env->root, 1 ,0); content = (char*)xmlBufferContent(buffer); /* Transport via HTTP */ conn = httpc_new(); - /* content-type is always 'text/xml' */ - httpc_set_header(conn, HEADER_CONTENT_TYPE, "text/xml"); - + /* Set soap action */ if (soap_action != NULL) { httpc_set_header(conn, "SoapAction", soap_action); } - res = httpc_post(conn, url, (int)strlen(content), content); + + /* check for attachments */ + if (!call->attachments) + { + /* content-type is always 'text/xml' */ + httpc_set_header(conn, HEADER_CONTENT_TYPE, "text/xml"); + + sprintf(tmp, "%d", (int)strlen(content)); + httpc_set_header(conn, HEADER_CONTENT_LENGTH, tmp); + status = httpc_post_begin(conn, url); + + if (status != H_OK) { + httpc_free(conn); + xmlBufferFree(buffer); + return soap_ctx_new( + soap_env_new_with_fault(Fault_Client, + "Can not begin post envelope","","")); + } + + 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","","")); + } + + /* res = httpc_post(conn, url, (int)strlen(content), content);*/ + res = httpc_post_end(conn); + if (res == NULL) { + httpc_free(conn); + xmlBufferFree(buffer); + return soap_ctx_new( + soap_env_new_with_fault(Fault_Client, + conn->errmsg,"","")); + } + } + 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); + + sprintf(start_id, "289247829121218%d", counter++); + status = httpc_mime_begin(conn, url, start_id, "", "text/xml"); + 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","","")); + } + + 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","","")); + } + + 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","","")); + } + + + for (part=call->attachments->parts; part; part=part->next) + { + status = httpc_mime_send_file(conn, part->id, + part->content_type, part->transfer_encoding, part->filename); + if (status != H_OK) { + log_error2("Send file failed. Status:%d", status); + httpc_free(conn); + xmlBufferFree(buffer); + return soap_ctx_new( + soap_env_new_with_fault(Fault_Client, + "MIME transport error while sending file","","")); + } + } + + res = httpc_mime_end(conn); + if (!res) + { + httpc_free(conn); + xmlBufferFree(buffer); + return soap_ctx_new( + soap_env_new_with_fault(Fault_Client, + "MIME transport error","","")); + } + } /* Free buffer */ xmlBufferFree(buffer); @@ -75,7 +213,11 @@ soap_client_invoke(SoapEnv *call, const char *url, const char *soap_action) /* TODO: If res == NULL, find out where and why it is NULL! */ doc = _soap_client_build_result(res); - return doc; + /* Create Context */ + ctx = soap_ctx_new(doc); + soap_ctx_add_files(ctx, res->attachments); + + return ctx; } @@ -84,6 +226,7 @@ SoapEnv* _soap_client_build_result(hresponse_t *res) { xmlDocPtr doc; SoapEnv *env; + char *buffer; log_verbose2("Building result (%p)", res); @@ -91,12 +234,13 @@ SoapEnv* _soap_client_build_result(hresponse_t *res) return soap_env_new_with_fault(Fault_Client, "Response is NULL","",""); - if (res->body == NULL) + + if (res->in == NULL) return soap_env_new_with_fault(Fault_Client, "Empty response from server!","",""); - +/* doc = xmlParseDoc(BAD_CAST res->body); if (doc == NULL) { return soap_env_new_with_fault(Fault_Client, @@ -104,6 +248,8 @@ SoapEnv* _soap_client_build_result(hresponse_t *res) } env = soap_env_new_from_doc(doc); +*/ + env = soap_env_new_from_stream(res->in); if (env == NULL) { xmlFreeDoc(doc); @@ -114,3 +260,11 @@ SoapEnv* _soap_client_build_result(hresponse_t *res) return env; } + +SoapCtx *soap_client_ctx_new(const char *urn, const char *method) +{ + SoapCtx *ctx = soap_ctx_new(soap_env_new_with_method(urn, method)); + + return ctx; +} + -- cgit v1.1-32-gdbae