summaryrefslogtreecommitdiffstats
path: root/libcsoap/soap-addressing.c
blob: 53ec0b4d261e01d7641bf15225e2088614b57de8 (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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/******************************************************************
*  $Id: soap-addressing.c,v 1.12 2007/11/03 22:40:09 m0gg Exp $
*
* CSOAP Project:  A SOAP client/server library in C
* Copyright (C) 2006 Heiko Ronsdorf
*
* 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: hero@persua.de
******************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_UUID_H
#include <uuid.h>
#endif

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

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

#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif

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

#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif

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

#include <nanohttp/nanohttp-error.h>

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

#ifdef HAVE_UUID_CREATE
static const xmlChar *
_soap_addressing_uuid_error(uint32_t status)
{
  switch(status)
  {
    case uuid_s_bad_version:
      return "The UUID does not have a known version";
    case uuid_s_invalid_string_uuid:
      return "The string representation of an UUID is not valid";
    case uuid_s_no_memory:
      /** @todo: From FreeBSD 6.2 UUID(3), replace with app. msg! */  
      return "The meaning of the code escaped the writers mind";
    default:
      return "Unkown error during UUID creation";
  }
}

static char *
_soap_addressing_generate_id(void)
{
  uuid_t uuid;
  uint32_t status;
  char *ret, *buf;

  uuid_create(&uuid, &status);
  if (status != uuid_s_ok)
  {
    log_error("uuidcreate failed (%s)", _soap_addressing_uuid_error(status));
    return NULL;
  }

  uuid_to_string(&uuid, &buf, &status);
  if (status != uuid_s_ok)
  {
    log_error("uuid_to_string failed (%s)", _soap_addressing_uuid_error(status));
    return NULL;
  }

  if (!(ret = (char *)malloc(128)))
  {
    log_error("malloc failed (%s)", strerror(errno));
    free(buf);
    return NULL;
  }

  sprintf(ret, "%s/%s", soap_server_get_name(), buf);

  free(buf);

  return ret;
}
#else
static char *
_soap_addressing_generate_id(void)
{
  char *ret;
  static long counter = 0;
  static pthread_mutex_t counter_lock = PTHREAD_MUTEX_INITIALIZER;

  if (!(ret = (char *)malloc(128)))
  {
    log_error("malloc failed (%s)", strerror(errno));
    return NULL;
  }

  pthread_mutex_lock(&counter_lock);
  sprintf("%s/%li", soap_server_get_name(), counter);
  pthread_mutex_unlock(&counter_lock);

  return ret;
}
#endif

static xmlNsPtr
_soap_addressing_get_namespace(xmlNodePtr node)
{
  xmlNsPtr ns;

  if (!(ns = xmlSearchNsByHref(node->doc, node, BAD_CAST WSA_NAMESPACE)))
  {
    ns = xmlNewNs(node, BAD_CAST WSA_NAMESPACE, BAD_CAST WSA_NAMESPACE_PREFIX);
  }
  return ns;
}

static xmlNodePtr
_soap_addressing_add_node(xmlNodePtr parent, const xmlChar *name, const xmlChar *content)
{
  xmlNsPtr ns;

  ns = _soap_addressing_get_namespace(parent);
  return xmlNewChild(parent, ns, name, content);
}

static xmlAttrPtr
_soap_addressing_set_property(xmlNodePtr node, const xmlChar *name, const xmlChar *value)
{
  xmlNsPtr ns;

  ns = _soap_addressing_get_namespace(node);
  return xmlSetNsProp(node, ns, name, value);
}

static xmlAttrPtr
_soap_addressing_set_property_uri(xmlNodePtr node, const xmlChar *name, xmlURI *uri)
{
  xmlChar *buf;
  xmlAttrPtr ret;

  buf = xmlSaveUri(uri);
  ret = _soap_addressing_set_property(node, name, buf);
  xmlFree(buf);

  return ret;
}

static xmlNodePtr
_soap_addressing_get_child_element(xmlNodePtr parent, const xmlChar *name)
{
  xmlNodePtr walker;

  for (walker = soap_xml_get_children(parent); walker; walker = soap_xml_get_next_element(walker))
  {
    if (!xmlStrcmp(walker->name, name) && !xmlStrcmp(walker->ns->href, WSA_NAMESPACE))
      return walker;
  }
  return NULL;
}

static xmlURI *
_soap_addressing_extract_uri(xmlNodePtr node)
{
  xmlChar *content;
  xmlURI *uri = NULL;

  if ((content = xmlNodeGetContent(node)))
  {
    uri = xmlParseURI(content);
    xmlFree(content);
  }
  return uri;
}

static xmlNodePtr
_soap_addressing_set_content_uri(xmlNodePtr node, xmlURI *uri)
{
  xmlChar *buf;

  if (uri == NULL)
    return NULL;

  buf = xmlSaveUri(uri);
  xmlNodeSetContent(node, buf);
  xmlFree(buf);

  return node;
}

xmlURI *
soap_addressing_get_address(xmlNodePtr endpoint_reference)
{
  xmlNodePtr address;
  
  address = _soap_addressing_get_child_element(endpoint_reference, WSA_ADDRESS);
  if (address == NULL)
    return NULL;

  return _soap_addressing_extract_uri(address);
}

xmlNodePtr
soap_addressing_set_address(xmlNodePtr endpoint_reference, xmlURI *address)
{
  xmlNodePtr node;

  node = _soap_addressing_get_child_element(endpoint_reference, WSA_ADDRESS);
  if (node == NULL)
    node = _soap_addressing_add_node(endpoint_reference, WSA_ADDRESS, NULL);

  return _soap_addressing_set_content_uri(node, address);  
}

xmlNodePtr
soap_addressing_get_reference_properties(xmlNodePtr endpoint_reference)
{
  return _soap_addressing_get_child_element(endpoint_reference, WSA_REFERENCE_PROPERTIES);
}

xmlNodePtr
soap_addressing_set_reference_properties(xmlNodePtr endpoint_reference, xmlNodePtr properties)
{
  xmlNodePtr node;

  node = soap_addressing_get_reference_properties(endpoint_reference);
  if (node != NULL)
  {
    xmlUnlinkNode(node);
    xmlFreeNode(node);
  }

  node = xmlCopyNode(properties, 1);
  xmlUnlinkNode(node);
  xmlAddChild(endpoint_reference, node);

  return node;
}

xmlNodePtr
soap_addressing_get_metadata(xmlNodePtr endpoint_reference)
{
  return _soap_addressing_get_child_element(endpoint_reference, WSA_METADATA);
}

xmlNodePtr
soap_addressing_set_metadata(xmlNodePtr endpoint_reference, xmlNodePtr metadata)
{
  xmlNodePtr node;

  node = soap_addressing_get_metadata(endpoint_reference);
  if (node != NULL)
  {
    xmlUnlinkNode(node);
    xmlFreeNode(node);
  }

  node = xmlCopyNode(metadata, 1);
  xmlUnlinkNode(node);
  xmlAddChild(endpoint_reference, node);

  return node;
}

xmlURI *
soap_addressing_get_message_id(struct SoapEnv *envelope)
{
  xmlNodePtr id;

  id = _soap_addressing_get_child_element(envelope->header, WSA_MESSAGE_ID);
  if (id == NULL)
    return NULL;

  return _soap_addressing_extract_uri(id);
}

xmlNodePtr
soap_addressing_set_message_id(struct SoapEnv *envelope, xmlURI *id)
{
  xmlNodePtr node;

  node = _soap_addressing_get_child_element(envelope->header, WSA_MESSAGE_ID);
  if (node == NULL)
    node = _soap_addressing_add_node(envelope->header, WSA_MESSAGE_ID, NULL);

  return _soap_addressing_set_content_uri(node, id);
}

xmlNodePtr
soap_addressing_set_message_id_string(struct SoapEnv *envelope, xmlChar *id)
{
  xmlNodePtr node;
  xmlChar *tmp;

  if (id == NULL)
    tmp = _soap_addressing_generate_id();
  else
    tmp = id;

  log_verbose("setting message id = \"%s\"", tmp);

  node = _soap_addressing_get_child_element(envelope->header, WSA_MESSAGE_ID);
  if (node == NULL)
    node = _soap_addressing_add_node(envelope->header, WSA_MESSAGE_ID, tmp);
  else
    xmlNodeSetContent(node, tmp);

  if (id == NULL)
    xmlFree(tmp);

  return node;
}

xmlChar *soap_addressing_get_message_id_string(struct SoapEnv *envelope)
{
  xmlURI *uri;
  xmlChar *ret;

  if (!(uri = soap_addressing_get_message_id(envelope)))
    return NULL;

  ret = xmlSaveUri(uri);
  xmlFreeURI(uri);

  return ret;
}

xmlNodePtr
soap_addressing_get_relates_to(struct SoapEnv *envelope)
{
  return _soap_addressing_get_child_element(envelope->header, WSA_RELATES_TO);
}

xmlNodePtr
soap_addressing_add_relates_to(struct SoapEnv *envelope, xmlURI *id, xmlURI *type)
{
  xmlNodePtr node;

  node = soap_addressing_get_relates_to(envelope);
  if (node != NULL)
  {
    xmlUnlinkNode(node);
    xmlFreeNode(node);
  }

  node = _soap_addressing_add_node(envelope->header, WSA_RELATES_TO, NULL);
  _soap_addressing_set_content_uri(node, id);
  _soap_addressing_set_property_uri(node, WSA_RELATIONSHIP_TYPE, type);

  return node;
}

xmlNodePtr
soap_addressing_get_reply_to(struct SoapEnv *envelope)
{
  return _soap_addressing_get_child_element(envelope->header, WSA_REPLY_TO);
}

xmlNodePtr
soap_addressing_set_reply_to(struct SoapEnv *envelope, xmlNodePtr address)
{
  xmlNodePtr ret;
  xmlNodePtr node;

  node = soap_addressing_get_reply_to(envelope);
  if (node != NULL)
  {
    xmlUnlinkNode(node);
    xmlFreeNode(node);
  }

  ret = _soap_addressing_add_node(envelope->header, WSA_REPLY_TO, NULL);
  node = xmlCopyNode(address, 1);
  xmlUnlinkNode(node);
  xmlAddChild(ret, node);

  return ret;
}

xmlURI *
soap_addressing_get_reply_to_address(struct SoapEnv *envelope)
{
  xmlNodePtr reply_to;

  reply_to = soap_addressing_get_reply_to(envelope);
  if (reply_to == NULL)
    return NULL;

  return soap_addressing_get_address(reply_to);
}

xmlNodePtr
soap_addressing_set_reply_to_address(struct SoapEnv *envelope, xmlURI *address)
{
  xmlNodePtr ret;
  xmlNodePtr node;

  node = soap_addressing_get_reply_to(envelope);
  if (node != NULL)
  {
    xmlUnlinkNode(node);
    xmlFreeNode(node);
  }

  ret = _soap_addressing_add_node(envelope->header, WSA_REPLY_TO, NULL);
  soap_addressing_set_address(ret, address);

  return ret;
}

xmlNodePtr
soap_addressing_set_from(struct SoapEnv *envelope, xmlNodePtr address)
{
  xmlNodePtr ret;
  xmlNodePtr node;

  node = soap_addressing_get_from(envelope);
  if (node != NULL)
  {
    xmlUnlinkNode(node);
    xmlFreeNode(node);
  }

  ret = _soap_addressing_add_node(envelope->header, WSA_FROM, NULL);
  node = xmlCopyNode(address, 1);
  xmlUnlinkNode(node);
  xmlAddChild(ret, node);

  return ret;
}

xmlNodePtr
soap_addressing_set_from_address_string(struct SoapEnv *envelope, const char *from)
{
  xmlURI *uri;
  xmlNodePtr ret;

  uri = xmlParseURI(from);
  ret = soap_addressing_set_from_address(envelope, uri);
  xmlFreeURI(uri);

  return ret;
}

xmlNodePtr
soap_addressing_get_from(struct SoapEnv *envelope)
{
  return _soap_addressing_get_child_element(envelope->header, WSA_FROM);
}

xmlURI *
soap_addressing_get_from_address(struct SoapEnv *envelope)
{
  xmlNodePtr from;

  from = soap_addressing_get_from(envelope);
  if (from == NULL)
    return NULL;

  return soap_addressing_get_address(from);
}

xmlChar *
soap_addressing_get_from_address_string(struct SoapEnv *envelope)
{
  xmlURI *uri;
  xmlChar *ret;
  
  if (!(uri = soap_addressing_get_from_address(envelope)))
    return NULL;

  ret = xmlSaveUri(uri);
  xmlFreeURI(uri);
  
  return ret;
}

xmlNodePtr
soap_addressing_set_from_address(struct SoapEnv *envelope, xmlURI *address)
{
  xmlNodePtr ret;
  xmlNodePtr node;

  node = soap_addressing_get_from(envelope);
  if (node != NULL)
  {
    xmlUnlinkNode(node);
    xmlFreeNode(node);
  }

  ret = _soap_addressing_add_node(envelope->header, WSA_FROM, NULL);
  soap_addressing_set_address(ret, address);

  return ret;
}

xmlNodePtr
soap_addressing_get_fault_to(struct SoapEnv *envelope)
{
  return _soap_addressing_get_child_element(envelope->header, WSA_FAULT_TO);
}

xmlNodePtr
soap_addressing_set_fault_to(struct SoapEnv *envelope, xmlNodePtr address)
{
  xmlNodePtr ret;
  xmlNodePtr node;

  node = soap_addressing_get_fault_to(envelope);
  if (node != NULL)
  {
    xmlUnlinkNode(node);
    xmlFreeNode(node);
  }

  ret = _soap_addressing_add_node(envelope->header, WSA_FAULT_TO, NULL);
  node = xmlCopyNode(address, 1);
  xmlUnlinkNode(node);
  xmlAddChild(ret, node);

  return ret;
}

xmlURI *
soap_addressing_get_fault_to_address(struct SoapEnv *envelope)
{
  xmlNodePtr fault_to;

  fault_to = soap_addressing_get_fault_to(envelope);
  if (fault_to == NULL)
    return NULL;

  return soap_addressing_get_address(fault_to);
}

xmlNodePtr
soap_addressing_set_fault_to_address(struct SoapEnv *envelope, xmlURI *address)
{
   xmlNodePtr ret;
  xmlNodePtr node;

  node = soap_addressing_get_fault_to(envelope);
  if (node != NULL)
  {
    xmlUnlinkNode(node);
    xmlFreeNode(node);
  }

  ret = _soap_addressing_add_node(envelope->header, WSA_FAULT_TO, NULL);
  soap_addressing_set_address(ret, address);

  return ret;
}

xmlNodePtr
soap_addressing_get_to(struct SoapEnv *envelope)
{
  return _soap_addressing_get_child_element(envelope->header, WSA_TO);
}

xmlNodePtr
soap_addressing_set_to(struct SoapEnv *envelope, xmlNodePtr address)
{
  xmlNodePtr ret;
  xmlNodePtr node;

  node = soap_addressing_get_to(envelope);
  if (node != NULL)
  {
    xmlUnlinkNode(node);
    xmlFreeNode(node);
  }

  ret = _soap_addressing_add_node(envelope->header, WSA_TO, NULL);
  node = xmlCopyNode(address, 1);
  xmlUnlinkNode(node);
  xmlAddChild(ret, node);

  return ret;
}

xmlURI *
soap_addressing_get_to_address(struct SoapEnv *envelope)
{
  xmlNodePtr to;

  if (!(to = soap_addressing_get_to(envelope)))
    return NULL;

  return soap_addressing_get_address(to);
}

xmlChar *
soap_addressing_get_to_address_string(struct SoapEnv *envelope)
{
  xmlURI *uri;
  xmlChar *ret;
  
  if (!(uri = soap_addressing_get_to_address(envelope)))
    return NULL;

  ret = xmlSaveUri(uri);
  xmlFreeURI(uri);
  
  return ret;
}

xmlNodePtr
soap_addressing_set_to_address_string(struct SoapEnv *envelope, const char *to)
{
  xmlURI *uri;
  xmlNodePtr ret;

  if (!(uri = xmlParseURI(to)))
    return NULL;

  ret = soap_addressing_set_to_address(envelope, uri);
  xmlFreeURI(uri);

  return ret;
}

xmlNodePtr
soap_addressing_set_to_address(struct SoapEnv *envelope, xmlURI *address)
{
  xmlNodePtr ret;
  xmlNodePtr node;

  node = soap_addressing_get_to(envelope);
  if (node != NULL)
  {
    xmlUnlinkNode(node);
    xmlFreeNode(node);
  }

  ret = _soap_addressing_add_node(envelope->header, WSA_TO, NULL);
  soap_addressing_set_address(ret, address);

  return ret;
}

xmlURI *
soap_addressing_get_action(struct SoapEnv *envelope)
{
  xmlNodePtr action;

  action = _soap_addressing_get_child_element(envelope->header, WSA_ACTION);
  if (action == NULL)
    return NULL;

  return _soap_addressing_extract_uri(action);
}

xmlChar *
soap_addressing_get_action_string(struct SoapEnv *envelope)
{
  xmlURI *uri;
  xmlChar *ret;
  
  if (!(uri = soap_addressing_get_action(envelope)))
    return NULL;

  ret = xmlSaveUri(uri);
  xmlFreeURI(uri);

  return ret;
}

xmlNodePtr
soap_addressing_set_action(struct SoapEnv *envelope, xmlURI *action)
{
  xmlNodePtr node;

  node = _soap_addressing_get_child_element(envelope->header, WSA_ACTION);
  if (node == NULL)
    node = _soap_addressing_add_node(envelope->header, WSA_ACTION, NULL);

  return _soap_addressing_set_content_uri(node, action);
}

xmlNodePtr
soap_addressing_set_action_string(struct SoapEnv *envelope, const char *action)
{
  xmlURI *uri;
  xmlNodePtr ret;

  if (!(uri = xmlParseURI(action)))
    return NULL;

  ret = soap_addressing_set_action(envelope, uri);
  xmlFreeURI(uri);

  return ret;
}