soap-fault.c

Go to the documentation of this file.
00001 /******************************************************************
00002 *  $Id: soap-fault.c,v 1.16 2006/12/14 19:36:49 m0gg Exp $
00003 *
00004 * CSOAP Project:  A SOAP client/server library in C
00005 * Copyright (C) 2003  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: ayaz@jprogrammer.net
00023 ******************************************************************/
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #ifdef HAVE_STRING_H
00029 #include <string.h>
00030 #endif
00031 
00032 #ifdef HAVE_ERRNO_H
00033 #include <errno.h>
00034 #endif
00035 
00036 #include <libxml/xpath.h>
00037 
00038 #include <nanohttp/nanohttp-error.h>
00039 #include <nanohttp/nanohttp-logging.h>
00040 
00041 #include "soap-xml.h"
00042 #include "soap-env.h"
00043 #include "soap-ctx.h"
00044 #include "soap-service.h"
00045 #include "soap-router.h"
00046 #include "soap-server.h"
00047 #include "soap-fault.h"
00048 
00049 /*
00050 Parameters:
00051 1- soap_env_ns
00052 2- soap_env_enc
00053 3- xsi_ns
00054 4- xsd_ns
00055 5- faultcode
00056 6- faultstring
00057 7- faultactor
00058 8- detail
00059 */
00060 #define  _SOAP_FAULT_TEMPLATE_ \
00061         "<SOAP-ENV:Envelope" \
00062         " xmlns:SOAP-ENV=\"%s\"" \
00063         " SOAP-ENV:encoding=\"%s\"" \
00064         " xmlns:xsi=\"%s\"" \
00065         " xmlns:xsd=\"%s\">" \
00066          "<SOAP-ENV:Header />" \
00067          "<SOAP-ENV:Body>" \
00068           "<SOAP-ENV:Fault>"\
00069            "<faultcode>%s</faultcode>"\
00070            "<faultstring>%s</faultstring>"\
00071            "<faultactor>%s</faultactor>"\
00072            "<detail>%s</detail>"\
00073           "</SOAP-ENV:Fault>" \
00074          "</SOAP-ENV:Body>"\
00075         "</SOAP-ENV:Envelope>"
00076 
00077 
00078 static const char const *fault_vm = "VersionMismatch";
00079 static const char const *fault_mu = "MustUnderstand";
00080 static const char const *fault_deu = "DataEncodingUnkown";
00081 static const char const *fault_client = "Client";
00082 static const char const *fault_server = "Server";
00083 
00084 xmlDocPtr
00085 soap_fault_build(int fault_code, const char *fault_string, const char *fault_actor, const char *detail)
00086 {
00087 
00088   /* variables */
00089   const char *faultcode;
00090   int bufferlen = 2000;
00091   char *buffer;
00092   xmlDocPtr fault;              /* result */
00093 
00094   log_verbose1("Build fault");
00095 
00096   switch (fault_code)
00097   {
00098   case SOAP_FAULT_VERSION_MISMATCH:
00099     faultcode = fault_vm;
00100     break;
00101   case SOAP_FAULT_MUST_UNDERSTAND:
00102     faultcode = fault_mu;
00103     break;
00104   case SOAP_FAULT_DATA_ENCODING_UNKOWN:
00105     faultcode = fault_deu;
00106     break;
00107   case SOAP_FAULT_RECEIVER:
00108     faultcode = fault_server;
00109     break;
00110   case SOAP_FAULT_SENDER:
00111   default:
00112     faultcode = fault_client;
00113     break;
00114   }
00115 
00116   /* calculate buffer length */
00117   if (fault_string)
00118     bufferlen += strlen(fault_string);
00119   if (fault_actor)
00120     bufferlen += strlen(fault_actor);
00121   if (detail)
00122     bufferlen += strlen(detail);
00123 
00124   log_verbose2("Creating buffer with %d bytes", bufferlen);
00125   if (!(buffer = (char *) malloc(bufferlen)))
00126   {
00127     log_error2("malloc failed (%s)", errno);
00128     return NULL;
00129   }
00130 
00131   sprintf(buffer, _SOAP_FAULT_TEMPLATE_,
00132           soap_env_ns, soap_env_enc, soap_xsi_ns,
00133           soap_xsd_ns, faultcode,
00134           fault_string ? fault_string : "error",
00135           fault_actor ? fault_actor : "", detail ? detail : "");
00136 
00137   fault = xmlParseDoc(BAD_CAST buffer);
00138   free(buffer);
00139 
00140   if (fault == NULL)
00141   {
00142     log_error1("Cannot create XML document!");
00143 
00144     return soap_fault_build(fault_code, "Cannot create fault object in XML", soap_server_get_name(), NULL);
00145   }
00146 
00147   log_verbose2("Returning fault (%p)", fault);
00148   return fault;
00149 }

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