summaryrefslogtreecommitdiffstats
path: root/libcsoap/soap-server.c
blob: 591ecc0369b26bef11711042f4869a0d1217becb (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/******************************************************************
*  $Id: soap-server.c,v 1.12 2005/12/19 14:06:16 snowdrop 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
******************************************************************/
#include <libcsoap/soap-server.h>
#include <nanohttp/nanohttp-server.h>
#include <string.h>


typedef struct _SoapRouterNode
{
	char *context;
	SoapRouter *router;
	struct _SoapRouterNode *next;

}SoapRouterNode;

SoapRouterNode *head = NULL;
SoapRouterNode *tail = NULL;

static
SoapRouterNode *router_node_new(SoapRouter *router, 
								const char *context, 
								SoapRouterNode *next);

static
SoapRouter *router_find(const char *context);

static
void _soap_server_send_ctx(httpd_conn_t* conn, SoapCtx *ctxres);

/*---------------------------------*/
void soap_server_entry(httpd_conn_t *conn, hrequest_t *req);
static
void _soap_server_send_env(http_output_stream_t *out, SoapEnv* env);
static
void _soap_server_send_fault(httpd_conn_t *conn, hpair_t *header, 
							 const char* errmsg);
/*---------------------------------*/


herror_t soap_server_init_args(int argc, char *argv[])
{
	return httpd_init(argc, argv);
}


int soap_server_register_router(SoapRouter *router, const char* context)
{

	if (!httpd_register(context, soap_server_entry)) {
		return 0;
	}

	if (tail == NULL) {
		head = tail = router_node_new(router, context, NULL);
	} else {
		tail->next =  router_node_new(router, context, NULL);
		tail = tail->next;
	}

	return 1;
}


herror_t soap_server_run()
{
	return httpd_run();
}


void soap_server_destroy()
{
	SoapRouterNode *node = head;
	SoapRouterNode *tmp;

	while (node != NULL) {
		tmp = node->next;
		log_verbose2("soap_router_free(%p)", node->router);
		soap_router_free(node->router);
		free(node->context);
		free(node);
		node = tmp;
	}
	httpd_destroy();
}


void soap_server_entry(httpd_conn_t *conn, hrequest_t *req)
{
	hpair_t *header = NULL;
	char buffer[1054];
	char urn[150];
	char method[150];
	SoapCtx *ctx, *ctxres;
	SoapRouter *router;
	SoapService *service;
	SoapEnv *env;
	herror_t err;

	if (req->method != HTTP_REQUEST_POST) {

		httpd_send_header(conn, 200, "OK");
		http_output_stream_write_string(conn->out, "<html><head></head><body>");
		http_output_stream_write_string(conn->out, "<h1>Sorry! </h1><hr>");
		http_output_stream_write_string(conn->out, "I only speak with 'POST' method");
		http_output_stream_write_string(conn->out, "</body></html>");
		return;
	}


	header = hpairnode_new(HEADER_CONTENT_TYPE, "text/xml", NULL);

	err = soap_env_new_from_stream(req->in, &env);
	if (err != H_OK) 
	{
  		_soap_server_send_fault(conn, header, herror_message(err));
		herror_release(err);
		return;
	}


	if (env == NULL) {

		_soap_server_send_fault(conn, header,"Can not receive POST data!");

	} else {

	  ctx = soap_ctx_new(env);
	  soap_ctx_add_files(ctx, req->attachments);

		if (ctx->env == NULL) {

			_soap_server_send_fault(conn, header,"Can not parse POST data!");

		} else {

			/*soap_xml_doc_print(env->root->doc);*/

			router = router_find(req->path);

			if ( router == NULL) {

				_soap_server_send_fault(conn, header, "Can not find router!");

			} else {

				if (!soap_env_find_urn(ctx->env, urn)) {

					_soap_server_send_fault(conn, header, "No URN found!");
					soap_ctx_free(ctx);
					return;
				} else {
					log_verbose2("urn: '%s'", urn);
				}

				if (!soap_env_find_methodname(ctx->env, method)) {

					_soap_server_send_fault(conn, header, "No method found!");
					soap_ctx_free(ctx);
					return;
				}else {
					log_verbose2("method: '%s'", method);
				}

				service = soap_router_find_service(router, urn, method);

				if (service == NULL) {

					sprintf(buffer, "URN '%s' not found", urn);
					_soap_server_send_fault(conn, header, buffer);
					soap_ctx_free(ctx);
					return;
				} else {

					log_verbose2("func: %p", service->func);
					ctxres = soap_ctx_new(NULL);
					/* ===================================== */
          /*       CALL SERVICE FUNCTION           */
					/* ===================================== */
					err = service->func(ctx, ctxres);
					if (err != H_OK) {
						sprintf(buffer, "Service returned following error message: '%s'",
							herror_message(err));
						herror_release(err);
						_soap_server_send_fault(conn, header, buffer);
						soap_ctx_free(ctx);
						return;
					}

					if (ctxres->env == NULL) {

						sprintf(buffer, "Service '%s' returned no envelope", urn);
						_soap_server_send_fault(conn, header, buffer);
						soap_ctx_free(ctx);
						return;

					} else {

/*						httpd_send_header(conn, 200, "OK");
						_soap_server_send_env(conn->out, ctxres->env);
*/
  				 _soap_server_send_ctx(conn, ctxres);
						/* free envctx */
						soap_ctx_free(ctxres);
					}

				}

			}   
		}
		soap_ctx_free(ctx);
	}
}


static
void _soap_server_send_ctx(httpd_conn_t* conn, SoapCtx *ctx)
{
	xmlBufferPtr buffer;
	static int counter = 1;
	char strbuffer[150];
	part_t *part;

	if (ctx->env == NULL || ctx->env->root == NULL) return;

	buffer = xmlBufferCreate();
/*	xmlIndentTreeOutput = 1;*/
	xmlThrDefIndentTreeOutput(1);
/*  xmlKeepBlanksDefault(0);*/
	xmlNodeDump(buffer, ctx->env->root->doc, ctx->env->root, 1 ,1);

	if (ctx->attachments)
	{
	  sprintf(strbuffer, "000128590350940924234%d", counter++);
	  httpd_mime_send_header(conn, strbuffer, "", "text/xml", 200, "OK");
	  httpd_mime_next(conn, strbuffer, "text/xml", "binary");
		http_output_stream_write_string(conn->out, (const char*)xmlBufferContent(buffer));
		part = ctx->attachments->parts;
		while (part)
		{
  		httpd_mime_send_file(conn, part->id, part->content_type, part->transfer_encoding, part->filename);
		  part = part->next;
		}
		httpd_mime_end(conn);
	}
	else
	{
      char buflen[100];
      xmlXPathContextPtr xpathCtx;
      xmlXPathObjectPtr xpathObj;
      xpathCtx = xmlXPathNewContext(ctx->env->root->doc);
      xpathObj = xmlXPathEvalExpression("//Fault", xpathCtx);
#ifdef WIN32
#define snprintf(buffer, num, s1, s2) sprintf(buffer, s1,s2)
#endif
      snprintf(buflen,100,"%d", strlen((const char*)xmlBufferContent (buffer)));
      httpd_set_header (conn, HEADER_CONTENT_LENGTH, buflen);
      if((xpathObj->nodesetval) ? xpathObj->nodesetval->nodeNr : 0){
          httpd_send_header(conn, 500, "FAILED");
      } else {
          httpd_send_header(conn, 200, "OK");
      }

		http_output_stream_write_string(conn->out, (const char*)xmlBufferContent(buffer));
       xmlXPathFreeObject(xpathObj);
       xmlXPathFreeContext(xpathCtx);

	}
	xmlBufferFree(buffer);  

}

static
void _soap_server_send_env(http_output_stream_t *out, SoapEnv* env)
{
	xmlBufferPtr buffer;
	if (env == NULL || env->root == NULL) return;

	buffer = xmlBufferCreate();
	xmlNodeDump(buffer, env->root->doc, env->root, 1 ,1);
	http_output_stream_write_string(out,  (const char*)xmlBufferContent(buffer));
	xmlBufferFree(buffer);  

}

static
void _soap_server_send_fault(httpd_conn_t *conn, hpair_t *header, 
							 const char* errmsg)
{
	SoapEnv *envres;
	herror_t err;
	char buffer[45];
	httpd_set_headers(conn, header);
	err = httpd_send_header(conn, 500, "FAILED");
	if (err != H_OK) {
		 /* WARNING: unhandled exception !*/
		log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err));
		return;
	}

	err = soap_env_new_with_fault(Fault_Server,
		errmsg?errmsg:"General error",
		"cSOAP_Server", NULL, &envres);
	 if (err != H_OK) {
		 log_error1(herror_message(err));
		http_output_stream_write_string(conn->out, "<html><head></head><body>");
		http_output_stream_write_string(conn->out, "<h1>Error</h1><hr>");
		http_output_stream_write_string(conn->out, "Error while sending fault object:<br>Message: ");
		http_output_stream_write_string(conn->out, herror_message(err));
		http_output_stream_write_string(conn->out, "<br>Function: ");
		http_output_stream_write_string(conn->out, herror_func(err));
		http_output_stream_write_string(conn->out, "<br>Error code: ");
		sprintf(buffer, "%d", herror_code(err));
		http_output_stream_write_string(conn->out, buffer);
		http_output_stream_write_string(conn->out, "</body></html>");
		return;

		 herror_release(err);
	 } else {
	_soap_server_send_env(conn->out, envres);
	 }

}



static 
SoapRouterNode *router_node_new(SoapRouter *router, 
								const char *context,
								SoapRouterNode *next)
{
	SoapRouterNode *node;
	const char *noname = "/lost_found";

	node = (SoapRouterNode*)malloc(sizeof(SoapRouterNode));
	if (context) {
		node->context = (char*)malloc(strlen(context)+1);
		strcpy(node->context, context);
	} else {
		log_warn2("context is null. Using '%s'", noname);
		node->context = (char*)malloc(strlen(noname)+1);
		strcpy(node->context, noname);
	}

	node->router = router;
	node->next = next;

	return node;
}


static
SoapRouter *router_find(const char* context)
{
	SoapRouterNode *node = head;

	while (node != NULL) {
		if (!strcmp(node->context, context))
			return node->router;
		node = node->next;
	}

	return NULL;
}