00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027
00028 #ifdef HAVE_STDIO_H
00029 #include <stdio.h>
00030 #endif
00031
00032 #ifdef HAVE_STRING_H
00033 #include <string.h>
00034 #endif
00035
00036 #ifdef HAVE_ERRNO_H
00037 #include <errno.h>
00038 #endif
00039
00040 #ifdef HAVE_NETINET_IN_H
00041 #include <netinet/in.h>
00042 #endif
00043
00044 #include <libxml/tree.h>
00045
00046 #include <nanohttp/nanohttp-error.h>
00047 #include <nanohttp/nanohttp-common.h>
00048 #include <nanohttp/nanohttp-logging.h>
00049
00050 #include "soap-fault.h"
00051 #include "soap-env.h"
00052 #include "soap-ctx.h"
00053
00054 struct SoapCtx *
00055 soap_ctx_new(struct SoapEnv * env)
00056 {
00057 struct SoapCtx *ctx;
00058
00059 if (!(ctx = (struct SoapCtx *) malloc(sizeof(struct SoapCtx))))
00060 {
00061 log_error2("malloc failed (%s)", strerror(errno));
00062 return NULL;
00063 }
00064
00065 ctx->env = env;
00066 ctx->attachments = NULL;
00067
00068 return ctx;
00069 }
00070
00071 void
00072 soap_ctx_add_files(struct SoapCtx *ctx, struct attachments_t *attachments)
00073 {
00074 struct part_t *part;
00075 char href[MAX_HREF_SIZE];
00076
00077 if (attachments == NULL)
00078 return;
00079
00080 part = attachments->parts;
00081 while (part)
00082 {
00083 soap_ctx_add_file(ctx, part->filename, part->content_type, href);
00084 part = part->next;
00085 }
00086
00087 return;
00088 }
00089
00090 herror_t
00091 soap_ctx_add_file(struct SoapCtx * ctx, const char *filename,
00092 const char *content_type, char *dest_href)
00093 {
00094 char cid[250];
00095 char id[250];
00096 struct part_t *part;
00097 static int counter = 1;
00098 FILE *test = fopen(filename, "r");
00099 if (!test)
00100 return herror_new("soap_ctx_add_file", FILE_ERROR_OPEN,
00101 "Can not open file '%s'", filename);
00102
00103 fclose(test);
00104
00105
00106 sprintf(id, "005512345894583%d", counter++);
00107 sprintf(dest_href, "cid:%s", id);
00108 sprintf(cid, "<%s>", id);
00109
00110
00111 part = part_new(cid, filename, content_type, NULL, NULL);
00112 if (!ctx->attachments)
00113 ctx->attachments = attachments_new();
00114 attachments_add_part(ctx->attachments, part);
00115
00116 return H_OK;
00117 }
00118
00119 struct part_t *
00120 soap_ctx_get_file(struct SoapCtx * ctx, xmlNodePtr node)
00121 {
00122 xmlChar *prop;
00123 char href[MAX_HREF_SIZE];
00124 char buffer[MAX_HREF_SIZE];
00125 struct part_t *part;
00126
00127 if (!ctx->attachments)
00128 return NULL;
00129
00130 prop = xmlGetProp(node, "href");
00131
00132 if (!prop)
00133 return NULL;
00134
00135 strcpy(href, (const char *) prop);
00136 if (!strncmp(href, "cid:", 4))
00137 {
00138 for (part = ctx->attachments->parts; part; part = part->next)
00139 {
00140 sprintf(buffer, "<%s>", href + 4);
00141 if (!strcmp(part->id, buffer))
00142 return part;
00143
00144 }
00145 }
00146 else
00147 {
00148 for (part = ctx->attachments->parts; part; part = part->next)
00149 {
00150 if (!strcmp(part->location, href))
00151 return part;
00152
00153 }
00154 }
00155
00156 return NULL;
00157 }
00158
00159 void
00160 soap_ctx_free(struct SoapCtx * ctx)
00161 {
00162 if (!ctx)
00163 return;
00164
00165 if (ctx->attachments)
00166 attachments_free(ctx->attachments);
00167
00168 if (ctx->env)
00169 soap_env_free(ctx->env);
00170
00171 free(ctx);
00172
00173 return;
00174 }
00175
00176 herror_t
00177 soap_ctx_new_with_method(const char *urn, const char *method, struct SoapCtx ** out)
00178 {
00179 struct SoapEnv *env;
00180 herror_t err;
00181
00182 if ((err = soap_env_new_with_method(urn, method, &env)) != H_OK)
00183 return err;
00184
00185 *out = soap_ctx_new(env);
00186
00187 return H_OK;
00188 }