summaryrefslogtreecommitdiffstats
path: root/libcsoap/soap-fault.c
blob: ab56979f098534193446e03148b667a98f9aef09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/******************************************************************
*  $Id: soap-fault.c,v 1.17 2007/11/03 22:40:09 m0gg Exp $
*
* CSOAP Project:  A SOAP client/server library in C
* Copyright (C) 2003  Ferhat Ayaz
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA  02111-1307, USA.
* 
* Email: ayaz@jprogrammer.net
******************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif

#include <nanohttp/nanohttp-error.h>

#include <libxml/tree.h>

#include "soap-logging.h"
#include "soap-xml.h"
#include "soap-env.h"
#include "soap-ctx.h"
#include "soap-service.h"
#include "soap-router.h"
#include "soap-server.h"
#include "soap-fault.h"

/*
Parameters:
1- soap_env_ns
2- soap_env_enc
3- xsi_ns
4- xsd_ns
5- faultcode
6- faultstring
7- faultactor
8- detail
*/
#define  _SOAP_FAULT_TEMPLATE_ \
	"<SOAP-ENV:Envelope" \
        " xmlns:SOAP-ENV=\"%s\"" \
        " SOAP-ENV:encoding=\"%s\"" \
	" xmlns:xsi=\"%s\"" \
	" xmlns:xsd=\"%s\">" \
         "<SOAP-ENV:Header />" \
	 "<SOAP-ENV:Body>" \
	  "<SOAP-ENV:Fault>"\
	   "<faultcode>%s</faultcode>"\
	   "<faultstring>%s</faultstring>"\
	   "<faultactor>%s</faultactor>"\
	   "<detail>%s</detail>"\
	  "</SOAP-ENV:Fault>" \
	 "</SOAP-ENV:Body>"\
	"</SOAP-ENV:Envelope>"


static const char const *fault_vm = "VersionMismatch";
static const char const *fault_mu = "MustUnderstand";
static const char const *fault_deu = "DataEncodingUnkown";
static const char const *fault_client = "Client";
static const char const *fault_server = "Server";

xmlDocPtr
soap_fault_build(int fault_code, const char *fault_string, const char *fault_actor, const char *detail)
{

  /* variables */
  const char *faultcode;
  int bufferlen = 2000;
  char *buffer;
  xmlDocPtr fault;              /* result */

  log_verbose("Build fault");

  switch (fault_code)
  {
  case SOAP_FAULT_VERSION_MISMATCH:
    faultcode = fault_vm;
    break;
  case SOAP_FAULT_MUST_UNDERSTAND:
    faultcode = fault_mu;
    break;
  case SOAP_FAULT_DATA_ENCODING_UNKOWN:
    faultcode = fault_deu;
    break;
  case SOAP_FAULT_RECEIVER:
    faultcode = fault_server;
    break;
  case SOAP_FAULT_SENDER:
  default:
    faultcode = fault_client;
    break;
  }

  /* calculate buffer length */
  if (fault_string)
    bufferlen += strlen(fault_string);
  if (fault_actor)
    bufferlen += strlen(fault_actor);
  if (detail)
    bufferlen += strlen(detail);

  log_verbose("Creating buffer with %d bytes", bufferlen);
  if (!(buffer = (char *) malloc(bufferlen)))
  {
    log_error("malloc failed (%s)", errno);
    return NULL;
  }

  sprintf(buffer, _SOAP_FAULT_TEMPLATE_,
          soap_env_ns, soap_env_enc, soap_xsi_ns,
          soap_xsd_ns, faultcode,
          fault_string ? fault_string : "error",
          fault_actor ? fault_actor : "", detail ? detail : "");

  fault = xmlParseDoc(BAD_CAST buffer);
  free(buffer);

  if (fault == NULL)
  {
    log_error("Cannot create XML document!");

    return soap_fault_build(fault_code, "Cannot create fault object in XML", soap_server_get_name(), NULL);
  }

  log_verbose("Returning fault (%p)", fault);
  return fault;
}