summaryrefslogtreecommitdiffstats
path: root/libcsoap/soap-nhttp.c
blob: 22f78dea60738f84af6f827ebfda74568940c0a9 (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/******************************************************************
*  $Id: soap-nhttp.c,v 1.10 2006/12/06 11:27:20 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_STDIO_H
#include <stdio.h>
#endif

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

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

#include <libxml/tree.h>
#include <libxml/uri.h>
#include <libxml/xpath.h>

#include <nanohttp/nanohttp-error.h>
#include <nanohttp/nanohttp-common.h>
#include <nanohttp/nanohttp-logging.h>
#include <nanohttp/nanohttp-stream.h>
#include <nanohttp/nanohttp-request.h>
#include <nanohttp/nanohttp-response.h>

#include <nanohttp/nanohttp-client.h>
#include <nanohttp/nanohttp-server.h>

#include "soap-fault.h"
#include "soap-env.h"
#include "soap-ctx.h"
#include "soap-service.h"
#include "soap-client.h"
#include "soap-transport.h"
#include "soap-addressing.h"
#include "soap-xml.h"

#include "soap-admin.h"
#include "soap-wsil.h"

#include "soap-nhttp.h"

static herror_t
_soap_nhttp_send_document(httpd_conn_t *conn, xmlDocPtr doc)
{
  char length[16];
  xmlChar *buf;
  int size;

  xmlDocDumpMemory(doc, &buf, &size);

  sprintf(length, "%d", size);
  httpd_set_header(conn, HEADER_CONTENT_TYPE, "text/xml");
  httpd_set_header(conn, HEADER_CONTENT_LENGTH, length);
  httpd_send_header(conn, 200, HTTP_STATUS_200_REASON_PHRASE);

  http_output_stream_write(conn->out, buf, size);

  xmlFree(buf);

  return H_OK;
}

static herror_t
_soap_nhttp_send_fault(httpd_conn_t *conn, const char *message)
{
  xmlDocPtr doc;
  herror_t ret;

  doc = soap_fault_build(SOAP_FAULT_SENDER, message, soap_transport_get_name(), NULL);
  ret = _soap_nhttp_send_document(conn, doc);
  xmlFreeDoc(doc);

  return ret;
}

static herror_t
_soap_nhttp_send(httpd_conn_t *conn, struct SoapCtx *context, struct SoapEnv *env)
{
  return _soap_nhttp_send_document(conn, env->root->doc);
}

static int
_soap_nhttp_xml_io_read(void *ctx, char *buffer, int len)
{
  int ret;

  struct http_input_stream_t *in = (struct http_input_stream_t *)ctx;
  if (!http_input_stream_is_ready(in))
    return 0;

  if ((ret = http_input_stream_read(in, buffer, len)) == -1)
    return 0;

  return ret;
}

static int
_soap_nhttp_xml_io_close(void *ctx)
{
  /* nothing */
  return 0;
}

static herror_t
_soap_nhttp_env_new_from_stream(struct http_input_stream_t *in, struct SoapEnv **out)
{
  xmlDocPtr doc;

  doc = xmlReadIO(_soap_nhttp_xml_io_read, _soap_nhttp_xml_io_close, in, "", NULL, 0);
  if (in->err != H_OK)
    return in->err;

  if (doc == NULL)
    return herror_new("_soap_nhttp_env_new_from_stream", XML_ERROR_PARSE, "Trying to parse invalid XML");

  return soap_env_new_from_doc(doc, out);
}

static void
soap_nhttp_process(httpd_conn_t * conn, struct hrequest_t * req)
{
  char *action;
  struct SoapEnv *env;
  struct SoapCtx *ctx;
  struct SoapCtx *response;
  herror_t err;

/*  if (req->method == HTTP_REQUEST_GET && router->wsdl)
  {
    _soap_nhttp_send_document(conn, router->wsdl);
    return;
  } */

  if (req->method != HTTP_REQUEST_POST)
  {
    httpd_send_header(conn, 200, HTTP_STATUS_200_REASON_PHRASE);
    http_output_stream_write_string(conn->out,
      "<html>"
        "<head>"
        "</head>"
        "<body>"
          "<h1>Sorry!</h1>"
          "<hr />"
          "<div>I only speak with 'POST' method.</div>"
        "</body>"
      "</html>");
    return;
  }

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

  if (env == NULL)
  {
    _soap_nhttp_send_fault(conn, "Can not receive POST data!");
    return;
  }

  ctx = soap_ctx_new(env);

  if ((action = hpairnode_get_ignore_case(req->header, SOAP_NHTTP_SOAP_ACTION)))
  {
    xmlURI *uri;

    uri = xmlParseURI(action);
    soap_addressing_set_action(env, uri);
    xmlFree(uri);
  }

  xmlDocFormatDump(stdout, ctx->env->root->doc, 1);

  soap_ctx_add_files(ctx, req->attachments);

  /* only local part is interesting...
  soap_addressing_set_to_address_string(ctx->env, req->path); */

  soap_transport_process(ctx, &response);

  _soap_nhttp_send_document(conn, response->env->root->doc);

  soap_ctx_free(response);

  soap_ctx_free(ctx);

  return;
}

herror_t
soap_nhttp_server_init_args(int argc, char **argv)
{
  herror_t err;
 
  if ((err = httpd_init(argc, argv)) != H_OK)
  {
    log_error2("httpd_init failed (%s)", herror_message(err));
    return err;
  }

  if ((err = soap_wsil_init_args(argc, argv)) != H_OK)
  {
    log_error2("soap_wsil_init_args failed (%s)", herror_message(err));
    return err;
  }

  return soap_admin_init_args(argc, argv);
}

static herror_t
_soap_nhttp_client_build_result(hresponse_t * res, struct SoapEnv ** env)
{
  log_verbose2("Building result (%p)", res);

  if (res == NULL)
    return herror_new("_soap_client_build_result",
                      GENERAL_INVALID_PARAM, "hresponse_t is NULL");

  if (res->in == NULL)
    return herror_new("_soap_client_build_result",
                      GENERAL_INVALID_PARAM, "Empty response from server");

  if (res->errcode != 200)
    return herror_new("_soap_client_build_result",
                      GENERAL_INVALID_PARAM, "HTTP code is not OK (%i)", res->errcode);

  return _soap_nhttp_env_new_from_stream(res->in, env);
}

static herror_t
_soap_nhttp_client_invoke(void *unused, struct SoapCtx *request, struct SoapCtx **response)
{
  herror_t status;

  /* Buffer variables */
  xmlChar *buffer;
  int size;
  char tmp[15];
  char *action;
  char *url;
  struct SoapEnv *res_env;

  /* Transport variables */
  httpc_conn_t *conn;
  hresponse_t *res;

  /* multipart/related start id */
  char start_id[150];
  static int counter = 1;
  struct part_t *part;

  /* for copy attachments */
  char href[MAX_HREF_SIZE];

  log_verbose1("nanohttp client");

  xmlDocDumpMemory(request->env->root->doc, &buffer, &size);

  /* xmlDocFormatDump(stdout, request->env->root->doc, 1); */

  /* Transport via HTTP */
  if (!(conn = httpc_new()))
  {
    return herror_new("soap_client_invoke", SOAP_ERROR_CLIENT_INIT, "Unable to create HTTP client!");
  }

  if ((action = soap_addressing_get_action_string(request->env)))
    httpc_set_header(conn, SOAP_NHTTP_SOAP_ACTION, action);
  else
    httpc_set_header(conn, SOAP_NHTTP_SOAP_ACTION, "");
  log_verbose2("action is \"%s\"", action);
  free(action);

  httpc_set_header(conn, HEADER_CONNECTION, "Close");

  if (!(url = soap_addressing_get_to_address_string(request->env)))
    return herror_new("soap_nhttp_client_invoke", 0, "Missing client URL");
  log_verbose2("url is \"%s\"", url);

  if (!request->attachments)
  {
    /* content-type is always 'text/xml' */
    httpc_set_header(conn, HEADER_CONTENT_TYPE, "text/xml");

    sprintf(tmp, "%d", size);
    httpc_set_header(conn, HEADER_CONTENT_LENGTH, tmp);

    if ((status = httpc_post_begin(conn, url)) != H_OK)
    {
      httpc_close_free(conn);
      xmlFree(buffer);
      return status;
    }

    if ((status = http_output_stream_write(conn->out, buffer, size)) != H_OK)
    {
      httpc_close_free(conn);
      xmlFree(buffer);
      return status;
    }

    if ((status = httpc_post_end(conn, &res)) != H_OK)
    {
      httpc_close_free(conn);
      xmlFree(buffer);
      return status;
    }
  }
  else
  {
    httpc_set_header(conn, HEADER_TRANSFER_ENCODING, TRANSFER_ENCODING_CHUNKED);

    sprintf(start_id, "289247829121218%d", counter++);
    if ((status = httpc_mime_begin(conn, url, start_id, "", "text/xml")) != H_OK)
    {
      httpc_close_free(conn);
      xmlFree(buffer);
      return status;
    }

    if ((status = httpc_mime_next(conn, start_id, "text/xml", "binary")) != H_OK)
    {
      httpc_close_free(conn);
      xmlFree(buffer);
      return status;
    }

    if ((status = http_output_stream_write(conn->out, buffer, size)) != H_OK)
    {
      httpc_close_free(conn);
      xmlFree(buffer);
      return status;
    }

    for (part=request->attachments->parts; part; part=part->next)
    {
      if ((status = httpc_mime_send_file(conn, part->id, part->content_type, part->transfer_encoding, part->filename)) != H_OK)
      {
        log_error2("httpc_mime_send_file failed (%s)", herror_message(status));
	httpc_close_free(conn);
	xmlFree(buffer);
	return status;
      }
    }
    
    if ((status = httpc_mime_end(conn, &res)) != H_OK)
    {
      httpc_close_free(conn);
      xmlFree(buffer);
      return status;
    }
  }

  xmlFree(buffer);
  free(url);

  if ((status = _soap_nhttp_client_build_result(res, &res_env)) != H_OK)
  {
    hresponse_free(res);
    httpc_close_free(conn);
    return status;
  }

  *response = soap_ctx_new(res_env);
  /* soap_ctx_add_files(*response, res->attachments) */

  if (res->attachments != NULL)
  {
    part = res->attachments->parts;
    while (part)
    {
      soap_ctx_add_file(*response, part->filename, part->content_type, href);
      part->deleteOnExit = 0;
      part = part->next;
    }
  }

  hresponse_free(res);
  httpc_close_free(conn);

  log_verbose1("done");

  return H_OK;
}

herror_t
soap_nhttp_client_init_args(int argc, char **argv)
{
  herror_t status;

  if ((status = httpc_init(argc, argv)) != H_OK)
  {
    log_error2("httpc_init failed (%s)", herror_message(status));
    return status;
  }

  soap_transport_add("https", NULL, _soap_nhttp_client_invoke);
  soap_transport_add("http", NULL, _soap_nhttp_client_invoke);

  return H_OK;
}

herror_t
soap_nhttp_register(const char *context)
{
  herror_t status;

  if ((status = httpd_register(context, soap_nhttp_process)) != H_OK)
  {
    log_error2("httpd_register_secure failed (%s)", herror_message(status));
    return status;
  }

  return H_OK;
}

herror_t
soap_nhttp_server_run(void)
{
  return httpd_run();
}

void
soap_nhttp_server_destroy(void)
{
  httpd_destroy();

  return;
}

void
soap_nhttp_client_destroy(void)
{
  httpc_destroy();

  return;
}

short
soap_nhttp_get_port(void)
{
  return httpd_get_port();
}

const char *
soap_nhttp_get_protocol(void)
{
  return httpd_get_protocol();
}