soap-ctx.c

Go to the documentation of this file.
00001 /******************************************************************
00002  *  $Id: soap-ctx.c,v 1.14 2006/11/25 15:06:57 m0gg Exp $
00003  *
00004  * CSOAP Project:  A SOAP client/server library in C
00005  * Copyright (C) 2003-2004  Ferhat Ayaz
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Library General Public
00018  * License along with this library; if not, write to the
00019  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  * Boston, MA  02111-1307, USA.
00021  * 
00022  * Email: ferhatayaz@jprogrammer.net
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)     /* should only be used internally */
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   /* generate an id */
00106   sprintf(id, "005512345894583%d", counter++);
00107   sprintf(dest_href, "cid:%s", id);
00108   sprintf(cid, "<%s>", id);
00109 
00110   /* add part to context */
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 }

Generated on Thu Jan 25 23:36:03 2007 for csoap by  doxygen 1.4.6