1 #define _XOPEN_SOURCE 500 /* strdup from string.h */
8 /* Private structure for write_body() call back */
15 /* Close connection to server and destroy CURL handle associated
17 _hidden isds_error
close_connection(struct isds_ctx
*context
) {
18 if (!context
) return IE_INVALID_CONTEXT
;
21 curl_easy_cleanup(context
->curl
);
23 isds_log(ILF_HTTP
, ILL_DEBUG
, _("Connection to server %s closed\n"),
27 return IE_CONNECTION_CLOSED
;
32 /* CURL call back function called when chunk of HTTP reponse body is available.
33 * @buffer points to new data
34 * @size * @nmemb is length of the chunk in bytes. Zero means empty body.
35 * @userp is private structure.
36 * Must reuturn the length of the chunk, otherwise CURL will signal
37 * CURL_WRITE_ERROR. */
38 static size_t write_body(void *buffer
, size_t size
, size_t nmemb
, void *userp
) {
39 struct soap_body
*body
= (struct soap_body
*) userp
;
42 /* FIXME: Check for (size * nmemb + body->lengt) !> SIZE_T_MAX.
43 * Precompute the product then. */
45 if (!body
) return 0; /* This should never happen */
46 if (0 == (size
* nmemb
)) return 0; /* Empty body */
48 new_data
= realloc(body
->data
, body
->length
+ size
* nmemb
);
49 if (!new_data
) return 0;
51 memcpy(new_data
+ body
->length
, buffer
, size
* nmemb
);
53 body
->data
= new_data
;
54 body
->length
+= size
* nmemb
;
56 return (size
* nmemb
);
60 /* CURL progress callback proxy to rearrange arguments.
61 * @curl_data is session context */
62 static int progress_proxy(void *curl_data
, double download_total
,
63 double download_current
, double upload_total
, double upload_current
) {
64 struct isds_ctx
*context
= (struct isds_ctx
*) curl_data
;
67 if (context
&& context
->progress_callback
) {
68 abort
= context
->progress_callback(
69 upload_total
, upload_current
,
70 download_total
, download_current
,
71 context
->progress_callback_data
);
73 isds_log(ILF_HTTP
, ILL_INFO
,
74 _("Application aborted HTTP transfer"));
83 * @context holds the base URL,
84 * @url is a (CGI) file of SOAP URL,
85 * @request is body for POST request
86 * @request_length is length of @request in bytes
87 * @reponse is automatically reallocated() buffer to fit HTTP response with
88 * @response_length (does not need to match allocatef memory exactly). You must
89 * free() the @response.
90 * @mime_type is automatically allocated MIME type send by server (*NULL if not
91 * sent). Set NULL if you don't care.
92 * @charset is charset of the body signaled by server. The same constrains
93 * like on @mime_type apply.
94 * @http_code is final HTTP code returned by server. This can be 200, 401, 500
95 * or any other one. Pass NULL if you don't interrest.
96 * In case of error, the response memory, MIME type, charset and lenght will be
97 * deallocated and zerod automatically. Thus be sure they are preallocated or
98 * they points to NULL.
99 * Be ware that successful return value does not mean the HTTP request has
100 * been accepted by the server. You must cosult @http_code. OTOH, failure
101 * return value means the request could not been sent (e.g. SSL error).
102 * Side effect: message buffer */
103 static isds_error
http(struct isds_ctx
*context
, const char *url
,
104 const void *request
, const size_t request_length
,
105 void **response
, size_t *response_length
,
106 char **mime_type
, char **charset
, long *http_code
) {
109 isds_error err
= IE_SUCCESS
;
110 struct soap_body body
;
112 struct curl_slist
*headers
= NULL
;
115 if (!context
) return IE_INVALID_CONTEXT
;
116 if (!url
) return IE_INVAL
;
117 if (request_length
> 0 && !request
) return IE_INVAL
;
118 if (!response
|| !response_length
) return IE_INVAL
;
120 /* Set the body here to allow deallocataion in leave block */
121 body
.data
= *response
;
124 /* Set Request-URI */
125 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_URL
, url
);
127 /* Set TLS options */
128 if (!curl_err
&& context
->tls_verify_server
) {
129 if (!*context
->tls_verify_server
)
130 isds_log(ILF_SEC
, ILL_WARNING
,
131 _("Disabling server identity verification. "
132 "That was your decision.\n"));
133 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_SSL_VERIFYPEER
,
134 (*context
->tls_verify_server
)? 1L : 0L);
136 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_SSL_VERIFYHOST
,
137 (*context
->tls_verify_server
)? 2L : 0L);
140 if (!curl_err
&& context
->tls_ca_file
) {
141 isds_log(ILF_SEC
, ILL_INFO
,
142 _("CA certificates will be searched in `%s' file since now\n"),
143 context
->tls_ca_file
);
144 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_CAINFO
,
145 context
->tls_ca_file
);
147 if (!curl_err
&& context
->tls_ca_dir
) {
148 isds_log(ILF_SEC
, ILL_INFO
,
149 _("CA certificates will be searched in `%s' directory "
150 "since now\n"), context
->tls_ca_file
);
151 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_CAINFO
,
152 context
->tls_ca_file
);
156 /* Set credentials */
157 #if HAVE_DECL_CURLOPT_USERNAME /* Since curl-7.19.1 */
158 if (!curl_err
&& context
->username
) {
159 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_USERNAME
,
162 if (!curl_err
&& context
->password
) {
163 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_PASSWORD
,
167 if (!curl_err
&& (context
->username
|| context
->password
)) {
168 char *userpwd
= astrcat3(context
->username
, ":", context
->password
);
170 isds_log_message(context
, _("Could not pass credentials to CURL"));
174 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_USERPWD
, userpwd
);
177 #endif /* not HAVE_DECL_CURLOPT_USERNAME */
181 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_NOSIGNAL
, 1);
183 if (!curl_err
&& context
->timeout
) {
184 #if HAVE_DECL_CURLOPT_TIMEOUT_MS /* Since curl-7.16.2 */
185 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_TIMEOUT_MS
,
188 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_TIMEOUT
,
189 context
->timeout
/ 1000);
190 #endif /* not HAVE_DECL_CURLOPT_TIMEOUT_MS */
193 /* Register callback */
194 if (context
->progress_callback
) {
196 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_NOPROGRESS
, 0);
199 curl_err
= curl_easy_setopt(context
->curl
,
200 CURLOPT_PROGRESSFUNCTION
, progress_proxy
);
203 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_PROGRESSDATA
,
208 /* Set other CURL features */
210 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_FAILONERROR
, 0);
213 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
216 /* TODO: Make the redirect depth configurable */
217 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_MAXREDIRS
, 8);
220 curl_err
= curl_easy_setopt(context
->curl
,
221 CURLOPT_UNRESTRICTED_AUTH
, 1);
224 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_COOKIEFILE
, "");
227 /* Set get-response function */
229 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_WRITEFUNCTION
,
233 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_WRITEDATA
, &body
);
236 /* Set MIME types and headers requires by SOAP 1.1.
237 * SOAP 1.1 requires text/xml, SOAP 1.2 requires application/soap+xml */
239 headers
= curl_slist_append(headers
,
240 "Accept: application/soap+xml,application/xml,text/xml");
245 headers
= curl_slist_append(headers
, "Content-Type: text/xml");
250 headers
= curl_slist_append(headers
, "SOAPAction: ");
255 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_HTTPHEADER
, headers
);
258 /* Set user agent identification */
259 /* TODO: Present library version, curl etc. in User-Agent */
260 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_USERAGENT
, "libisds");
263 /* Set POST request body */
265 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_POST
, 1);
268 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_POSTFIELDS
, request
);
271 curl_err
= curl_easy_setopt(context
->curl
, CURLOPT_POSTFIELDSIZE
,
275 /* Check for errors so far */
277 isds_log_message(context
, curl_easy_strerror(curl_err
));
278 if (curl_err
== CURLE_ABORTED_BY_CALLBACK
)
285 isds_log(ILF_HTTP
, ILL_DEBUG
, _("Sending POST request to %s\n"), url
);
286 isds_log(ILF_HTTP
, ILL_DEBUG
,
287 _("POST body length: %zu, content follows:\n"), request_length
);
288 isds_log(ILF_HTTP
, ILL_DEBUG
, "%.*s\n", request_length
, request
);
289 isds_log(ILF_HTTP
, ILL_DEBUG
, _("End of POST body\n"));
290 if ((log_facilities
& ILF_HTTP
) && (log_level
>= ILL_DEBUG
) ) {
291 curl_easy_setopt(context
->curl
, CURLOPT_VERBOSE
, 1);
296 curl_err
= curl_easy_perform(context
->curl
);
298 /* Wipe credentials out of the handler */
299 #if HAVE_DECL_CURLOPT_USERNAME /* Since curl-7.19.1 */
300 if (context
->username
) {
301 curl_easy_setopt(context
->curl
, CURLOPT_USERNAME
, NULL
);
303 if (context
->password
) {
304 curl_easy_setopt(context
->curl
, CURLOPT_PASSWORD
, NULL
);
307 if (context
->username
|| context
->password
) {
308 curl_easy_setopt(context
->curl
, CURLOPT_USERPWD
, NULL
);
310 #endif /* not HAVE_DECL_CURLOPT_USERNAME */
313 curl_err
= curl_easy_getinfo(context
->curl
, CURLINFO_CONTENT_TYPE
,
317 /* TODO: CURL is not internationalized yet. Collect CURL messages for
319 isds_printf_message(context
,
320 _("%s: %s"), url
, _(curl_easy_strerror(curl_err
)));
325 isds_log(ILF_HTTP
, ILL_DEBUG
, _("Final response to %s received\n"), url
);
326 isds_log(ILF_HTTP
, ILL_DEBUG
,
327 _("Response body length: %zu, content follows:\n"),
329 isds_log(ILF_HTTP
, ILL_DEBUG
, "%.*s\n", body
.length
, body
.data
);
330 isds_log(ILF_HTTP
, ILL_DEBUG
, _("End of response body\n"));
333 /* Extract MIME type and charset */
338 sep
= strchr(content_type
, ';');
339 if (sep
) offset
= (size_t) (sep
- content_type
);
340 else offset
= strlen(content_type
);
343 *mime_type
= malloc(offset
+ 1);
348 memcpy(*mime_type
, content_type
, offset
);
349 (*mime_type
)[offset
] = '\0';
356 sep
= strstr(sep
, "charset=");
360 *charset
= strdup(sep
+ 8);
370 /* Get HTTP response code */
372 curl_err
= curl_easy_getinfo(context
->curl
,
373 CURLINFO_RESPONSE_CODE
, http_code
);
381 curl_slist_free_all(headers
);
397 if (err
!= IE_ABORTED
) close_connection(context
);
400 *response
= body
.data
;
401 *response_length
= body
.length
;
408 * @context holds the base URL,
409 * @file is a (CGI) file of SOAP URL,
410 * @request is XML node set with SOAP request body.
411 * @file must be NULL, @request should be NULL rather than empty, if they should
412 * not be signaled in the SOAP request.
413 * @reponse is automatically allocated() node set with SOAP response body.
414 * You must xmlFreeNodeList() it. This is literal body, empty (NULL), one node
415 * or more nodes can be returned.
416 * @raw_response is automatically allocated bitstream with response body. Use
417 * NULL if you don't care
418 * @raw_response_length is size of @raw_response in bytes
419 * In case of error the response will be deallocated automatically.
420 * Side effect: message buffer */
421 _hidden isds_error
soap(struct isds_ctx
*context
, const char *file
,
422 const xmlNodePtr request
, xmlNodePtr
*response
,
423 void **raw_response
, size_t *raw_response_length
) {
425 isds_error err
= IE_SUCCESS
;
427 char *mime_type
= NULL
;
429 xmlBufferPtr http_request
= NULL
;
430 xmlSaveCtxtPtr save_ctx
= NULL
;
431 xmlDocPtr request_soap_doc
= NULL
;
432 xmlNodePtr request_soap_envelope
= NULL
, request_soap_body
= NULL
;
433 xmlNsPtr soap_ns
= NULL
;
434 void *http_response
= NULL
;
435 size_t response_length
= 0;
436 xmlDocPtr response_soap_doc
= NULL
;
437 xmlNodePtr response_root
= NULL
;
438 xmlXPathContextPtr xpath_ctx
= NULL
;
439 xmlXPathObjectPtr response_soap_headers
= NULL
, response_soap_body
= NULL
,
440 response_soap_fault
= NULL
;
443 if (!context
) return IE_INVALID_CONTEXT
;
444 if (!response
) return IE_INVAL
;
445 if (!raw_response_length
&& raw_response
) return IE_INVAL
;
447 xmlFreeNodeList(*response
);
449 if (raw_response
) *raw_response
= NULL
;
451 url
= astrcat(context
->url
, file
);
452 if (!url
) return IE_NOMEM
;
454 /* Build SOAP request envelope */
455 request_soap_doc
= xmlNewDoc(BAD_CAST
"1.0");
456 if (!request_soap_doc
) {
457 isds_log_message(context
, _("Could not build SOAP request document"));
461 request_soap_envelope
= xmlNewNode(NULL
, BAD_CAST
"Envelope");
462 if (!request_soap_envelope
) {
463 isds_log_message(context
, _("Could not build SOAP request envelope"));
467 xmlDocSetRootElement(request_soap_doc
, request_soap_envelope
);
468 /* Only this way we get namespace definition as @xmlns:soap,
469 * otherwise we get namespace prefix without definition */
470 soap_ns
= xmlNewNs(request_soap_envelope
, BAD_CAST SOAP_NS
, NULL
);
472 isds_log_message(context
, _("Could not create SOAP name space"));
476 xmlSetNs(request_soap_envelope
, soap_ns
);
477 request_soap_body
= xmlNewChild(request_soap_envelope
, NULL
,
478 BAD_CAST
"Body", NULL
);
479 if (!request_soap_body
) {
480 isds_log_message(context
,
481 _("Could not add Body to SOAP request envelope"));
486 /* Append request XML node set to SOAP body if request is not empty */
487 /* XXX: Copy of request must be used, otherwise xmlFreeDoc(request_soap_doc)
488 * would destroy this outer structure. */
490 xmlNodePtr request_copy
= xmlCopyNodeList(request
);
492 isds_log_message(context
,
493 _("Could not copy request content"));
497 if (!xmlAddChildList(request_soap_body
, request_copy
)) {
498 xmlFreeNodeList(request_copy
);
499 isds_log_message(context
,
500 _("Could not add request content to SOAP "
501 "request envelope"));
508 /* Serialize the SOAP request into HTTP request body */
509 http_request
= xmlBufferCreate();
511 isds_log_message(context
,
512 _("Could not create xmlBuffer for HTTP request body"));
516 /* Last argument 1 means format the XML tree. This is pretty but it breaks
517 * digital signatures probably because ISDS abandoned XMLDSig */
518 save_ctx
= xmlSaveToBuffer(http_request
, "UTF-8", 1);
520 isds_log_message(context
,
521 _("Could not create XML serializer"));
525 /* XXX: According LibXML documentation, this function does not return
526 * meaningfull value yet */
527 xmlSaveDoc(save_ctx
, request_soap_doc
);
528 if (-1 == xmlSaveFlush(save_ctx
)) {
529 isds_log_message(context
,
530 _("Could not serialize SOAP request to HTTP request bddy"));
535 isds_log(ILF_SOAP
, ILL_DEBUG
,
536 _("SOAP request to sent to %s:\n%.*s\nEnd of SOAP request\n"),
537 url
, http_request
->use
, http_request
->content
);
539 err
= http(context
, url
, http_request
->content
, http_request
->use
,
540 &http_response
, &response_length
,
541 &mime_type
, NULL
, &http_code
);
543 /* TODO: HTTP binding for SOAP prescribes non-200 HTTP return codes
544 * to be processes too. */
550 /* Check for HTTP return code */
551 isds_log(ILF_SOAP
, ILL_DEBUG
, _("Server returned %ld HTTP code\n"),
554 /* XXX: We must see which code is used for not permitted ISDS
555 * operation like downloading message without proper user
556 * permissions. In that cat we should keep connection opened. */
558 err
= IE_NOT_LOGGED_IN
;
559 isds_log_message(context
, _("Authentication failed"));
564 isds_log_message(context
,
565 _("Code 404: Document not found on server"));
568 /* 500 should return standard SOAP message */
571 /* Check for Content-Type: text/xml.
572 * Do it after HTTP code check because 401 Unauthorized returns HTML web
573 * page for browsers. */
574 if (mime_type
&& strcmp(mime_type
, "text/xml")
575 && strcmp(mime_type
, "application/soap+xml")
576 && strcmp(mime_type
, "application/xml")) {
577 char *mime_type_locale
= utf82locale(mime_type
);
578 isds_printf_message(context
,
579 _("%s: bad MIME type sent by server: %s"), url
,
581 free(mime_type_locale
);
586 /* TODO: Convert returned body into XML default encoding */
588 /* Parse the HTTP body as XML */
589 response_soap_doc
= xmlParseMemory(http_response
, response_length
);
590 if (!response_soap_doc
) {
595 xpath_ctx
= xmlXPathNewContext(response_soap_doc
);
601 if (register_namespaces(xpath_ctx
, MESSAGE_NS_UNSIGNED
)) {
606 isds_log(ILF_SOAP
, ILL_DEBUG
,
607 _("SOAP response received:\n%.*s\nEnd of SOAP response\n"),
608 response_length
, http_response
);
611 /* Check for SOAP version */
612 response_root
= xmlDocGetRootElement(response_soap_doc
);
613 if (!response_root
) {
614 isds_log_message(context
, "SOAP response has no root element");
618 if (xmlStrcmp(response_root
->name
, BAD_CAST
"Envelope") ||
619 xmlStrcmp(response_root
->ns
->href
, BAD_CAST SOAP_NS
)) {
620 isds_log_message(context
, "SOAP response is not SOAP 1.1 document");
625 /* Check for SOAP Headers */
626 response_soap_headers
= xmlXPathEvalExpression(
627 BAD_CAST
"/soap:Envelope/soap:Header/"
628 "*[@soap:mustUnderstand/text() = true()]", xpath_ctx
);
629 if (!response_soap_headers
) {
633 if (!xmlXPathNodeSetIsEmpty(response_soap_headers
->nodesetval
)) {
634 isds_log_message(context
,
635 _("SOAP response requires unsupported feature"));
636 /* TODO: log the headers
637 * xmlChar *fragment = NULL;
638 * fragment = xmlXPathCastNodeSetToSting(response_soap_headers->nodesetval);*/
644 response_soap_body
= xmlXPathEvalExpression(
645 BAD_CAST
"/soap:Envelope/soap:Body", xpath_ctx
);
646 if (!response_soap_body
) {
650 if (xmlXPathNodeSetIsEmpty(response_soap_body
->nodesetval
)) {
651 isds_log_message(context
,
652 _("SOAP response does not contain SOAP Body element"));
656 if (response_soap_body
->nodesetval
->nodeNr
> 1) {
657 isds_log_message(context
,
658 _("SOAP response has more than one Body element"));
663 /* Check for SOAP Fault */
664 response_soap_fault
= xmlXPathEvalExpression(
665 BAD_CAST
"/soap:Envelope/soap:Body/soap:Fault", xpath_ctx
);
666 if (!response_soap_fault
) {
670 if (!xmlXPathNodeSetIsEmpty(response_soap_fault
->nodesetval
)) {
671 /* TODO: log the faultcode and faultstring */
672 isds_log_message(context
, _("SOAP response signals Fault"));
678 /* Extract XML Tree with ISDS response from SOAP envelope and return it.
679 * XXX: response_soap_body is Body, we need children which may not exist
680 * (i.e. empty Body). */
681 /* TODO: Destroy SOAP response but Body childern. This is more memory
682 * friendly than copying (potentialy) fat body */
683 if (response_soap_body
->nodesetval
->nodeTab
[0]->children
) {
684 *response
= xmlDocCopyNodeList(response_soap_doc
,
685 response_soap_body
->nodesetval
->nodeTab
[0]->children
);
690 } else *response
= NULL
;
692 /* Save raw response */
694 *raw_response
= http_response
;
695 *raw_response_length
= response_length
;
696 http_response
= NULL
;
702 xmlFreeNodeList(*response
);
706 xmlXPathFreeObject(response_soap_fault
);
707 xmlXPathFreeObject(response_soap_body
);
708 xmlXPathFreeObject(response_soap_headers
);
709 xmlXPathFreeContext(xpath_ctx
);
710 xmlFreeDoc(response_soap_doc
);
713 xmlSaveClose(save_ctx
);
714 xmlBufferFree(http_request
);
715 xmlFreeDoc(request_soap_doc
); /* recursive, frees request_body, soap_ns*/
724 * void xmlInitParser(void)
725 * Initialization function for the XML parser. This is not reentrant. Call
726 * once before processing in case of use in multithreaded programs.
728 * int xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
729 * Initialize a parser context
731 * xmlDocPtr xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur,
732 * const * char URL, const char * encoding, int options);
733 * Parse in-memory NULL-terminated document @cur.
735 * xmlDocPtr xmlParseMemory(const char * buffer, int size)
736 * Parse an XML in-memory block and build a tree.
738 * xmlParserCtxtPtr xmlCreateMemoryParserCtxt(const char * buffer, int
740 * Create a parser context for an XML in-memory document.
742 * xmlParserCtxtPtr xmlCreateDocParserCtxt(const xmlChar * cur)
743 * Creates a parser context for an XML in-memory document.
745 * xmlDocPtr xmlCtxtReadMemory(xmlParserCtxtPtr ctxt,
746 * const char * buffer, int size, const char * URL, const char * encoding,
748 * Parse an XML in-memory document and build a tree. This reuses the existing
749 * @ctxt parser context.
751 * void xmlCleanupParser(void)
752 * Cleanup function for the XML library. It tries to reclaim all parsing
753 * related glob document related memory. Calling this function should not
754 * prevent reusing the libr finished using the library or XML document built
757 * void xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
758 * Clear (release owned resources) and reinitialize a parser context.
760 * void xmlCtxtReset(xmlParserCtxtPtr ctxt)
761 * Reset a parser context
763 * void xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
764 * Free all the memory used by a parser context. However the parsed document
765 * in ctxt->myDoc is not freed.
767 * void xmlFreeDoc(xmlDocPtr cur)
768 * Free up all the structures used by a document, tree included.