| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 1999-2008 Novell, Inc. (www.novell.com) |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "evolution-ews-config.h" |
| 7 |
|
| 8 |
#include <libedataserver/eds-version.h> |
| 9 |
#include <libedataserver/libedataserver.h> |
| 10 |
|
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
#include <libxml/tree.h> |
| 14 |
#include <libsoup/soup.h> |
| 15 |
#include "e-soap-response.h" |
| 16 |
|
| 17 |
struct _ESoapResponsePrivate { |
| 18 |
/* the XML document */ |
| 19 |
xmlDocPtr xmldoc; |
| 20 |
xmlNodePtr xml_root; |
| 21 |
xmlNodePtr xml_body; |
| 22 |
xmlNodePtr xml_method; |
| 23 |
xmlNodePtr soap_fault; |
| 24 |
GList *parameters; |
| 25 |
|
| 26 |
/* Serialization fields */ |
| 27 |
xmlParserCtxtPtr ctxt; |
| 28 |
|
| 29 |
/* Content stealing */ |
| 30 |
gchar *steal_node; |
| 31 |
gchar *steal_dir; |
| 32 |
gboolean steal_base64; |
| 33 |
|
| 34 |
gint steal_b64_state; |
| 35 |
guint steal_b64_save; |
| 36 |
gint steal_fd; |
| 37 |
|
| 38 |
/* Progress callbacks */ |
| 39 |
ESoapResponseProgressFn progress_fn; |
| 40 |
gpointer progress_data; |
| 41 |
}; |
| 42 |
|
| 43 |
G_DEFINE_TYPE_WITH_PRIVATE (ESoapResponse, e_soap_response, G_TYPE_OBJECT) |
| 44 |
|
| 45 |
static xmlNode * |
| 46 |
soup_xml_real_node (xmlNode *node) |
| 47 |
{ |
| 48 |
while (node && (node->type == XML_COMMENT_NODE || |
| 49 |
xmlIsBlankNode (node))) |
| 50 |
node = node->next; |
| 51 |
return node; |
| 52 |
} |
| 53 |
|
| 54 |
static void |
| 55 |
soap_response_finalize (GObject *object) |
| 56 |
{ |
| 57 |
ESoapResponse *resp = E_SOAP_RESPONSE (object); |
| 58 |
|
| 59 |
g_clear_pointer (&resp->priv->xmldoc, xmlFreeDoc); |
| 60 |
g_list_free (resp->priv->parameters); |
| 61 |
|
| 62 |
if (resp->priv->ctxt) { |
| 63 |
if (resp->priv->ctxt->myDoc) |
| 64 |
xmlFreeDoc (resp->priv->ctxt->myDoc); |
| 65 |
xmlFreeParserCtxt (resp->priv->ctxt); |
| 66 |
} |
| 67 |
|
| 68 |
g_free (resp->priv->steal_node); |
| 69 |
g_free (resp->priv->steal_dir); |
| 70 |
|
| 71 |
if (resp->priv->steal_fd != -1) |
| 72 |
close (resp->priv->steal_fd); |
| 73 |
|
| 74 |
/* Chain up to parent's finalize() method. */ |
| 75 |
G_OBJECT_CLASS (e_soap_response_parent_class)->finalize (object); |
| 76 |
} |
| 77 |
|
| 78 |
static void |
| 79 |
e_soap_response_class_init (ESoapResponseClass *class) |
| 80 |
{ |
| 81 |
GObjectClass *object_class; |
| 82 |
|
| 83 |
object_class = G_OBJECT_CLASS (class); |
| 84 |
object_class->finalize = soap_response_finalize; |
| 85 |
} |
| 86 |
|
| 87 |
static void |
| 88 |
e_soap_response_init (ESoapResponse *response) |
| 89 |
{ |
| 90 |
response->priv = e_soap_response_get_instance_private (response); |
| 91 |
|
| 92 |
response->priv->xmldoc = xmlNewDoc ((const xmlChar *)"1.0"); |
| 93 |
response->priv->steal_fd = -1; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* e_soap_response_new: |
| 98 |
* |
| 99 |
* Create a new empty #ESoapResponse object, which can be modified |
| 100 |
* with the accessor functions provided with this class. |
| 101 |
* |
| 102 |
* Returns: the new #ESoapResponse (or %NULL if there was an |
| 103 |
* error). |
| 104 |
*/ |
| 105 |
ESoapResponse * |
| 106 |
e_soap_response_new (void) |
| 107 |
{ |
| 108 |
ESoapResponse *response; |
| 109 |
|
| 110 |
response = g_object_new (E_TYPE_SOAP_RESPONSE, NULL); |
| 111 |
return response; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* e_soap_response_new_from_string: |
| 116 |
* @xmlstr: the XML string to parse. |
| 117 |
* @xmlstr_len: XML string's length or -1 for a null-terminated string |
| 118 |
* |
| 119 |
* Create a new #ESoapResponse object from the XML string contained |
| 120 |
* in @xmlstr. |
| 121 |
* |
| 122 |
* Returns: the new #ESoapResponse (or %NULL if there was an |
| 123 |
* error). |
| 124 |
*/ |
| 125 |
ESoapResponse * |
| 126 |
e_soap_response_new_from_string (const gchar *xmlstr, |
| 127 |
gint xmlstr_length) |
| 128 |
{ |
| 129 |
ESoapResponse *response; |
| 130 |
|
| 131 |
g_return_val_if_fail (xmlstr != NULL, NULL); |
| 132 |
|
| 133 |
response = g_object_new (E_TYPE_SOAP_RESPONSE, NULL); |
| 134 |
if (!e_soap_response_from_string (response, xmlstr, xmlstr_length)) { |
| 135 |
g_object_unref (response); |
| 136 |
return NULL; |
| 137 |
} |
| 138 |
|
| 139 |
return response; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* e_soap_response_new_from_xmldoc: |
| 144 |
* @xmldoc: the XML document to parse. |
| 145 |
* |
| 146 |
* Create a new #ESoapResponse object from the XML document contained |
| 147 |
* in @xmldoc. |
| 148 |
* |
| 149 |
* Returns: the new #ESoapResponse (or %NULL if there was an |
| 150 |
* error). |
| 151 |
*/ |
| 152 |
ESoapResponse * |
| 153 |
e_soap_response_new_from_xmldoc (xmlDoc *xmldoc) |
| 154 |
{ |
| 155 |
ESoapResponse *response; |
| 156 |
|
| 157 |
g_return_val_if_fail (xmldoc != NULL, NULL); |
| 158 |
|
| 159 |
response = g_object_new (E_TYPE_SOAP_RESPONSE, NULL); |
| 160 |
if (!e_soap_response_from_xmldoc (response, xmldoc)) { |
| 161 |
g_object_unref (response); |
| 162 |
return NULL; |
| 163 |
} |
| 164 |
|
| 165 |
return response; |
| 166 |
} |
| 167 |
|
| 168 |
static void |
| 169 |
parse_parameters (ESoapResponse *response, |
| 170 |
xmlNodePtr xml_method) |
| 171 |
{ |
| 172 |
xmlNodePtr tmp; |
| 173 |
|
| 174 |
for (tmp = soup_xml_real_node (xml_method->children); |
| 175 |
tmp != NULL; |
| 176 |
tmp = soup_xml_real_node (tmp->next)) { |
| 177 |
if (!strcmp ((const gchar *) tmp->name, "Fault")) { |
| 178 |
response->priv->soap_fault = tmp; |
| 179 |
continue; |
| 180 |
} else { |
| 181 |
/* regular parameters */ |
| 182 |
response->priv->parameters = g_list_append ( |
| 183 |
response->priv->parameters, tmp); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* e_soap_response_from_string: |
| 190 |
* @response: the #ESoapResponse object. |
| 191 |
* @xmlstr: XML string to parse. |
| 192 |
* @xmlstr_len: XML string's length or -1 for a null-terminated string |
| 193 |
* |
| 194 |
* Parses the string contained in @xmlstr and sets all properties from |
| 195 |
* it in the @response object. |
| 196 |
* |
| 197 |
* Returns: %TRUE if successful, %FALSE otherwise. |
| 198 |
*/ |
| 199 |
gboolean |
| 200 |
e_soap_response_from_string (ESoapResponse *response, |
| 201 |
const gchar *xmlstr, |
| 202 |
gint xmlstr_length) |
| 203 |
{ |
| 204 |
xmlDocPtr xmldoc; |
| 205 |
|
| 206 |
g_return_val_if_fail (E_IS_SOAP_RESPONSE (response), FALSE); |
| 207 |
g_return_val_if_fail (xmlstr != NULL, FALSE); |
| 208 |
|
| 209 |
/* parse the string */ |
| 210 |
xmldoc = xmlParseMemory (xmlstr, xmlstr_length == -1 ? strlen (xmlstr) : xmlstr_length); |
| 211 |
if (!xmldoc) |
| 212 |
return FALSE; |
| 213 |
|
| 214 |
return e_soap_response_from_xmldoc (response, xmldoc); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* e_soap_response_from_xmldoc: |
| 219 |
* @response: the #ESoapResponse object. |
| 220 |
* @xmldoc: XML document. |
| 221 |
* |
| 222 |
* Sets all properties from the @xmldoc in the @response object. |
| 223 |
* |
| 224 |
* Returns: %TRUE if successful, %FALSE otherwise. |
| 225 |
*/ |
| 226 |
gboolean |
| 227 |
e_soap_response_from_xmldoc (ESoapResponse *response, |
| 228 |
xmlDoc *xmldoc) |
| 229 |
{ |
| 230 |
xmlNodePtr xml_root, xml_body, xml_method = NULL; |
| 231 |
|
| 232 |
g_return_val_if_fail (E_IS_SOAP_RESPONSE (response), FALSE); |
| 233 |
g_return_val_if_fail (xmldoc != NULL, FALSE); |
| 234 |
|
| 235 |
xml_root = xmlDocGetRootElement (xmldoc); |
| 236 |
if (!xml_root) { |
| 237 |
xmlFreeDoc (xmldoc); |
| 238 |
return FALSE; |
| 239 |
} |
| 240 |
|
| 241 |
if (strcmp ((const gchar *) xml_root->name, "Envelope") != 0) { |
| 242 |
xmlFreeDoc (xmldoc); |
| 243 |
return FALSE; |
| 244 |
} |
| 245 |
|
| 246 |
xml_body = soup_xml_real_node (xml_root->children); |
| 247 |
if (xml_body != NULL) { |
| 248 |
if (strcmp ((const gchar *) xml_body->name, "Header") == 0) { |
| 249 |
/* read header parameters */ |
| 250 |
parse_parameters (response, xml_body); |
| 251 |
xml_body = soup_xml_real_node (xml_body->next); |
| 252 |
} |
| 253 |
if (strcmp ((const gchar *) xml_body->name, "Body") != 0) { |
| 254 |
xmlFreeDoc (xmldoc); |
| 255 |
return FALSE; |
| 256 |
} |
| 257 |
|
| 258 |
xml_method = soup_xml_real_node (xml_body->children); |
| 259 |
|
| 260 |
/* read all parameters */ |
| 261 |
if (xml_method) |
| 262 |
parse_parameters (response, xml_method); |
| 263 |
} |
| 264 |
|
| 265 |
xmlFreeDoc (response->priv->xmldoc); |
| 266 |
response->priv->xmldoc = xmldoc; |
| 267 |
|
| 268 |
response->priv->xml_root = xml_root; |
| 269 |
response->priv->xml_body = xml_body; |
| 270 |
response->priv->xml_method = xml_method; |
| 271 |
|
| 272 |
return TRUE; |
| 273 |
} |
| 274 |
|
| 275 |
static void |
| 276 |
soap_sax_startElementNs (gpointer _ctxt, |
| 277 |
const xmlChar *localname, |
| 278 |
const xmlChar *prefix, |
| 279 |
const xmlChar *uri, |
| 280 |
gint nb_namespaces, |
| 281 |
const xmlChar **namespaces, |
| 282 |
gint nb_attributes, |
| 283 |
gint nb_defaulted, |
| 284 |
const xmlChar **attributes) |
| 285 |
{ |
| 286 |
xmlParserCtxt *ctxt = _ctxt; |
| 287 |
ESoapResponse *response = ctxt->_private; |
| 288 |
gchar *fname; |
| 289 |
|
| 290 |
xmlSAX2StartElementNs ( |
| 291 |
ctxt, localname, prefix, uri, nb_namespaces, |
| 292 |
namespaces, nb_attributes, nb_defaulted, |
| 293 |
attributes); |
| 294 |
|
| 295 |
/* steal_node can contain multiple node name separated by " " */ |
| 296 |
if (response->priv->steal_node && *response->priv->steal_node) { |
| 297 |
gchar **prop = g_strsplit (response->priv->steal_node, " ", 0); |
| 298 |
gint i = 0; |
| 299 |
gboolean isnode = FALSE; |
| 300 |
|
| 301 |
while (prop[i]) { |
| 302 |
if (strcmp ((const gchar *) localname, prop[i]) == 0) { |
| 303 |
isnode = TRUE; |
| 304 |
break; |
| 305 |
} |
| 306 |
i++; |
| 307 |
} |
| 308 |
g_strfreev (prop); |
| 309 |
|
| 310 |
if (!isnode) return; |
| 311 |
} else |
| 312 |
return; |
| 313 |
|
| 314 |
fname = g_build_filename (response->priv->steal_dir, "XXXXXX", NULL); |
| 315 |
response->priv->steal_fd = g_mkstemp (fname); |
| 316 |
if (response->priv->steal_fd != -1) { |
| 317 |
if (response->priv->steal_base64) { |
| 318 |
gchar *enc = g_base64_encode ((guchar *) fname, strlen (fname)); |
| 319 |
xmlSAX2Characters (ctxt, (xmlChar *) enc, strlen (enc)); |
| 320 |
g_free (enc); |
| 321 |
} else |
| 322 |
xmlSAX2Characters (ctxt, (xmlChar *) fname, strlen (fname)); |
| 323 |
} else { |
| 324 |
gint err = errno; |
| 325 |
|
| 326 |
g_warning ("%s: Failed to create temp file '%s': %s\n", G_STRFUNC, fname, g_strerror (err)); |
| 327 |
} |
| 328 |
g_free (fname); |
| 329 |
} |
| 330 |
|
| 331 |
static void |
| 332 |
soap_sax_endElementNs (gpointer _ctxt, |
| 333 |
const xmlChar *localname, |
| 334 |
const xmlChar *prefix, |
| 335 |
const xmlChar *uri) |
| 336 |
{ |
| 337 |
xmlParserCtxt *ctxt = _ctxt; |
| 338 |
ESoapResponse *response = ctxt->_private; |
| 339 |
|
| 340 |
if (response->priv->steal_fd != -1) { |
| 341 |
close (response->priv->steal_fd); |
| 342 |
response->priv->steal_fd = -1; |
| 343 |
} |
| 344 |
xmlSAX2EndElementNs (ctxt, localname, prefix, uri); |
| 345 |
} |
| 346 |
|
| 347 |
static void |
| 348 |
soap_sax_characters (gpointer _ctxt, |
| 349 |
const xmlChar *ch, |
| 350 |
gint len) |
| 351 |
{ |
| 352 |
xmlParserCtxt *ctxt = _ctxt; |
| 353 |
ESoapResponse *response = ctxt->_private; |
| 354 |
|
| 355 |
if (response->priv->steal_fd == -1) |
| 356 |
xmlSAX2Characters (ctxt, ch, len); |
| 357 |
else if (!response->priv->steal_base64) { |
| 358 |
if (write (response->priv->steal_fd, (const gchar *) ch, len) != len) { |
| 359 |
write_err: |
| 360 |
/* Handle error better */ |
| 361 |
g_warning ("Failed to write streaming data to file"); |
| 362 |
} |
| 363 |
} else { |
| 364 |
guchar *bdata = g_malloc ((len * 3 / 4) + 3); |
| 365 |
gsize blen; |
| 366 |
|
| 367 |
blen = g_base64_decode_step ( |
| 368 |
(const gchar *) ch, len, |
| 369 |
bdata, &response->priv->steal_b64_state, |
| 370 |
&response->priv->steal_b64_save); |
| 371 |
if (write (response->priv->steal_fd, (const gchar *) bdata, blen) != blen) { |
| 372 |
g_free (bdata); |
| 373 |
goto write_err; |
| 374 |
} |
| 375 |
g_free (bdata); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
#define BUFFER_SIZE 16384 |
| 380 |
|
| 381 |
xmlDoc * |
| 382 |
e_soap_response_xmldoc_from_message_sync (ESoapResponse *response, |
| 383 |
SoupMessage *msg, |
| 384 |
GInputStream *response_data, |
| 385 |
GCancellable *cancellable, |
| 386 |
GError **error) |
| 387 |
{ |
| 388 |
const gchar *size; |
| 389 |
xmlDoc *xmldoc = NULL; |
| 390 |
gboolean success; |
| 391 |
gpointer buffer; |
| 392 |
gsize response_size = 0; |
| 393 |
gsize response_received = 0; |
| 394 |
gsize progress_percent = 0; |
| 395 |
gsize nread = 0; |
| 396 |
|
| 397 |
g_return_val_if_fail (E_IS_SOAP_RESPONSE (response), FALSE); |
| 398 |
g_return_val_if_fail (SOUP_IS_MESSAGE (msg), FALSE); |
| 399 |
g_return_val_if_fail (G_IS_INPUT_STREAM (response_data), FALSE); |
| 400 |
|
| 401 |
/* Discard the existing context, if there is one, and start again */ |
| 402 |
if (response->priv->ctxt) { |
| 403 |
if (response->priv->ctxt->myDoc) |
| 404 |
xmlFreeDoc (response->priv->ctxt->myDoc); |
| 405 |
xmlFreeParserCtxt (response->priv->ctxt); |
| 406 |
response->priv->ctxt = NULL; |
| 407 |
} |
| 408 |
|
| 409 |
if (response->priv->steal_fd != -1) { |
| 410 |
close (response->priv->steal_fd); |
| 411 |
response->priv->steal_fd = -1; |
| 412 |
} |
| 413 |
|
| 414 |
if (!SOUP_STATUS_IS_SUCCESSFUL (soup_message_get_status (msg))) { |
| 415 |
g_set_error_literal (error, E_SOUP_SESSION_ERROR, soup_message_get_status (msg), soup_message_get_reason_phrase (msg)); |
| 416 |
return NULL; |
| 417 |
} |
| 418 |
|
| 419 |
size = soup_message_headers_get_one (soup_message_get_response_headers (msg), "Content-Length"); |
| 420 |
|
| 421 |
if (size) |
| 422 |
response_size = g_ascii_strtoll (size, NULL, 10); |
| 423 |
|
| 424 |
buffer = g_malloc (BUFFER_SIZE); |
| 425 |
|
| 426 |
while (success = g_input_stream_read_all (response_data, buffer, BUFFER_SIZE, &nread, cancellable, error), |
| 427 |
success && nread > 0) { |
| 428 |
response_received += nread; |
| 429 |
|
| 430 |
if (response_size && response->priv->progress_fn) { |
| 431 |
gint pc = response_received * 100 / response_size; |
| 432 |
if (progress_percent != pc) { |
| 433 |
progress_percent = pc; |
| 434 |
response->priv->progress_fn (response->priv->progress_data, progress_percent); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
if (!response->priv->ctxt) { |
| 439 |
response->priv->ctxt = xmlCreatePushParserCtxt (NULL, response, buffer, nread, NULL); |
| 440 |
response->priv->ctxt->_private = response; |
| 441 |
response->priv->ctxt->sax->startElementNs = soap_sax_startElementNs; |
| 442 |
response->priv->ctxt->sax->endElementNs = soap_sax_endElementNs; |
| 443 |
response->priv->ctxt->sax->characters = soap_sax_characters; |
| 444 |
xmlCtxtUseOptions (response->priv->ctxt, XML_PARSE_RECOVER | XML_PARSE_HUGE | XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET); |
| 445 |
} else { |
| 446 |
xmlParseChunk (response->priv->ctxt, buffer, nread, 0); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
g_free (buffer); |
| 451 |
|
| 452 |
if (success) { |
| 453 |
if (response->priv->ctxt) { |
| 454 |
xmlParseChunk (response->priv->ctxt, 0, 0, 1); |
| 455 |
|
| 456 |
xmldoc = response->priv->ctxt->myDoc; |
| 457 |
|
| 458 |
xmlFreeParserCtxt (response->priv->ctxt); |
| 459 |
response->priv->ctxt = NULL; |
| 460 |
} else { |
| 461 |
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "No data read"); |
| 462 |
success = FALSE; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
if (response->priv->ctxt) { |
| 467 |
if (response->priv->ctxt->myDoc) |
| 468 |
xmlFreeDoc (response->priv->ctxt->myDoc); |
| 469 |
xmlFreeParserCtxt (response->priv->ctxt); |
| 470 |
response->priv->ctxt = NULL; |
| 471 |
} |
| 472 |
|
| 473 |
if (response->priv->steal_fd != -1) { |
| 474 |
close (response->priv->steal_fd); |
| 475 |
response->priv->steal_fd = -1; |
| 476 |
} |
| 477 |
|
| 478 |
return xmldoc; |
| 479 |
} |
| 480 |
|
| 481 |
gboolean |
| 482 |
e_soap_response_from_message_sync (ESoapResponse *response, |
| 483 |
SoupMessage *msg, |
| 484 |
GInputStream *response_data, |
| 485 |
GCancellable *cancellable, |
| 486 |
GError **error) |
| 487 |
{ |
| 488 |
xmlDoc *xmldoc; |
| 489 |
gboolean success = FALSE; |
| 490 |
|
| 491 |
g_return_val_if_fail (E_IS_SOAP_RESPONSE (response), FALSE); |
| 492 |
g_return_val_if_fail (SOUP_IS_MESSAGE (msg), FALSE); |
| 493 |
g_return_val_if_fail (G_IS_INPUT_STREAM (response_data), FALSE); |
| 494 |
|
| 495 |
xmldoc = e_soap_response_xmldoc_from_message_sync (response, msg, response_data, cancellable, error); |
| 496 |
|
| 497 |
if (xmldoc) { |
| 498 |
success = e_soap_response_from_xmldoc (response, xmldoc); |
| 499 |
if (!success) |
| 500 |
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Received invalid SOAP response"); |
| 501 |
} |
| 502 |
|
| 503 |
return success; |
| 504 |
} |
| 505 |
|
| 506 |
/** |
| 507 |
* e_soap_response_set_store_node_data: |
| 508 |
* @response: the %ESoapResponse |
| 509 |
* @nodename: the name of the XML node from which to store data |
| 510 |
* @directory: cache directory in which to create data files |
| 511 |
* @base64: flag to request base64 decoding of node content |
| 512 |
* |
| 513 |
* This requests that character data for certain XML nodes should |
| 514 |
* be streamed directly to a disk file as it arrives, rather than |
| 515 |
* being stored in memory. |
| 516 |
* |
| 517 |
* It is used only with e_soap_response_from_message_sync(). |
| 518 |
*/ |
| 519 |
void |
| 520 |
e_soap_response_set_store_node_data (ESoapResponse *response, |
| 521 |
const gchar *nodename, |
| 522 |
const gchar *directory, |
| 523 |
gboolean base64) |
| 524 |
{ |
| 525 |
g_return_if_fail (E_IS_SOAP_RESPONSE (response)); |
| 526 |
g_return_if_fail (response->priv->steal_node == NULL); |
| 527 |
|
| 528 |
response->priv->steal_node = g_strdup (nodename); |
| 529 |
response->priv->steal_dir = g_strdup (directory); |
| 530 |
response->priv->steal_base64 = base64; |
| 531 |
} |
| 532 |
|
| 533 |
/** |
| 534 |
* e_soap_response_set_progress_fn: |
| 535 |
* @response: the %ESoapResponse |
| 536 |
* @fn: callback function to be given progress updates |
| 537 |
* @object: first argument to callback function |
| 538 |
* |
| 539 |
* Starts the top level SOAP Envelope element. |
| 540 |
* |
| 541 |
* It is used only with e_soap_response_from_message_sync(). |
| 542 |
*/ |
| 543 |
void |
| 544 |
e_soap_response_set_progress_fn (ESoapResponse *response, |
| 545 |
ESoapResponseProgressFn fn, |
| 546 |
gpointer object) |
| 547 |
{ |
| 548 |
g_return_if_fail (E_IS_SOAP_RESPONSE (response)); |
| 549 |
|
| 550 |
response->priv->progress_fn = fn; |
| 551 |
response->priv->progress_data = object; |
| 552 |
} |
| 553 |
|
| 554 |
/** |
| 555 |
* e_soap_response_get_method_name: |
| 556 |
* @response: the #ESoapResponse object. |
| 557 |
* |
| 558 |
* Gets the method name from the SOAP response. |
| 559 |
* |
| 560 |
* Returns: the method name. |
| 561 |
*/ |
| 562 |
const gchar * |
| 563 |
e_soap_response_get_method_name (ESoapResponse *response) |
| 564 |
{ |
| 565 |
g_return_val_if_fail (E_IS_SOAP_RESPONSE (response), NULL); |
| 566 |
g_return_val_if_fail (response->priv->xml_method != NULL, NULL); |
| 567 |
|
| 568 |
return (const gchar *) response->priv->xml_method->name; |
| 569 |
} |
| 570 |
|
| 571 |
/** |
| 572 |
* e_soap_response_set_method_name: |
| 573 |
* @response: the #ESoapResponse object. |
| 574 |
* @method_name: the method name to set. |
| 575 |
* |
| 576 |
* Sets the method name on the given #ESoapResponse. |
| 577 |
*/ |
| 578 |
void |
| 579 |
e_soap_response_set_method_name (ESoapResponse *response, |
| 580 |
const gchar *method_name) |
| 581 |
{ |
| 582 |
g_return_if_fail (E_IS_SOAP_RESPONSE (response)); |
| 583 |
g_return_if_fail (response->priv->xml_method != NULL); |
| 584 |
g_return_if_fail (method_name != NULL); |
| 585 |
|
| 586 |
xmlNodeSetName ( |
| 587 |
response->priv->xml_method, |
| 588 |
(const xmlChar *) method_name); |
| 589 |
} |
| 590 |
|
| 591 |
/** |
| 592 |
* e_soap_parameter_get_name: |
| 593 |
* @param: the parameter |
| 594 |
* |
| 595 |
* Returns the parameter name. |
| 596 |
* |
| 597 |
* Returns: the parameter name. |
| 598 |
*/ |
| 599 |
const gchar * |
| 600 |
e_soap_parameter_get_name (ESoapParameter *param) |
| 601 |
{ |
| 602 |
g_return_val_if_fail (param != NULL, NULL); |
| 603 |
|
| 604 |
return (const gchar *) param->name; |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* e_soap_parameter_get_int_value: |
| 609 |
* @param: the parameter |
| 610 |
* |
| 611 |
* Returns the parameter's (integer) value. |
| 612 |
* |
| 613 |
* Returns: the parameter value as an integer |
| 614 |
*/ |
| 615 |
gint |
| 616 |
e_soap_parameter_get_int_value (ESoapParameter *param) |
| 617 |
{ |
| 618 |
gint i; |
| 619 |
xmlChar *s; |
| 620 |
g_return_val_if_fail (param != NULL, -1); |
| 621 |
|
| 622 |
s = xmlNodeGetContent (param); |
| 623 |
if (s) { |
| 624 |
i = atoi ((gchar *) s); |
| 625 |
xmlFree (s); |
| 626 |
|
| 627 |
return i; |
| 628 |
} |
| 629 |
|
| 630 |
return -1; |
| 631 |
} |
| 632 |
|
| 633 |
/** |
| 634 |
* e_soap_parameter_get_uint64_value: |
| 635 |
* @param: the parameter |
| 636 |
* |
| 637 |
* Returns: the parameter value as a 64-bit unsigned integer |
| 638 |
*/ |
| 639 |
guint64 |
| 640 |
e_soap_parameter_get_uint64_value (ESoapParameter *param) |
| 641 |
{ |
| 642 |
xmlChar *str; |
| 643 |
|
| 644 |
g_return_val_if_fail (param != NULL, G_MAXUINT64); |
| 645 |
|
| 646 |
str = xmlNodeGetContent (param); |
| 647 |
if (str) { |
| 648 |
guint64 val; |
| 649 |
|
| 650 |
val = g_ascii_strtoull ((gchar *) str, NULL, 10); |
| 651 |
xmlFree (str); |
| 652 |
|
| 653 |
return val; |
| 654 |
} |
| 655 |
|
| 656 |
return G_MAXUINT64; |
| 657 |
} |
| 658 |
|
| 659 |
/** |
| 660 |
* e_soap_parameter_get_string_value: |
| 661 |
* @param: the parameter |
| 662 |
* |
| 663 |
* Returns the parameter's value. |
| 664 |
* |
| 665 |
* Returns: the parameter value as a string, which must be freed |
| 666 |
* by the caller. |
| 667 |
*/ |
| 668 |
gchar * |
| 669 |
e_soap_parameter_get_string_value (ESoapParameter *param) |
| 670 |
{ |
| 671 |
xmlChar *xml_s; |
| 672 |
gchar *s; |
| 673 |
g_return_val_if_fail (param != NULL, NULL); |
| 674 |
|
| 675 |
xml_s = xmlNodeGetContent (param); |
| 676 |
s = g_strdup ((gchar *) xml_s); |
| 677 |
xmlFree (xml_s); |
| 678 |
|
| 679 |
return s; |
| 680 |
} |
| 681 |
|
| 682 |
/** |
| 683 |
* e_soap_parameter_get_first_child: |
| 684 |
* @param: (nullable): An #ESoapParameter. |
| 685 |
* |
| 686 |
* Gets the first child of the given #ESoapParameter. This is used |
| 687 |
* for compound data types, which can contain several parameters |
| 688 |
* themselves. |
| 689 |
* |
| 690 |
* Returns: the first child or %NULL if there are no children. |
| 691 |
*/ |
| 692 |
ESoapParameter * |
| 693 |
e_soap_parameter_get_first_child (ESoapParameter *param) |
| 694 |
{ |
| 695 |
if (!param) |
| 696 |
return NULL; |
| 697 |
|
| 698 |
return soup_xml_real_node (param->children); |
| 699 |
} |
| 700 |
|
| 701 |
/** |
| 702 |
* e_soap_parameter_get_first_child_by_name: |
| 703 |
* @param: (nullable): An #ESoapParameter. |
| 704 |
* @name: The name of the child parameter to look for. |
| 705 |
* |
| 706 |
* Gets the first child of the given #ESoapParameter whose name is |
| 707 |
* @name. |
| 708 |
* |
| 709 |
* Returns: the first child with the given name or %NULL if there |
| 710 |
* are no children. |
| 711 |
*/ |
| 712 |
ESoapParameter * |
| 713 |
e_soap_parameter_get_first_child_by_name (ESoapParameter *param, |
| 714 |
const gchar *name) |
| 715 |
{ |
| 716 |
ESoapParameter *tmp; |
| 717 |
|
| 718 |
g_return_val_if_fail (name != NULL, NULL); |
| 719 |
|
| 720 |
for (tmp = e_soap_parameter_get_first_child (param); |
| 721 |
tmp != NULL; |
| 722 |
tmp = e_soap_parameter_get_next_child (tmp)) { |
| 723 |
if (!strcmp (name, (const gchar *) tmp->name)) |
| 724 |
return tmp; |
| 725 |
} |
| 726 |
|
| 727 |
return NULL; |
| 728 |
} |
| 729 |
|
| 730 |
/** |
| 731 |
* e_soap_parameter_get_next_child: |
| 732 |
* @param: (nullable): An #ESoapParameter, or %NULL. |
| 733 |
* |
| 734 |
* Gets the next sibling of the given #ESoapParameter. This is used |
| 735 |
* for compound data types, which can contain several parameters |
| 736 |
* themselves. |
| 737 |
* |
| 738 |
* FIXME: the name of this method is wrong |
| 739 |
* |
| 740 |
* Returns: the next sibling, or %NULL if there are no more |
| 741 |
* siblings. |
| 742 |
*/ |
| 743 |
ESoapParameter * |
| 744 |
e_soap_parameter_get_next_child (ESoapParameter *param) |
| 745 |
{ |
| 746 |
if (!param) |
| 747 |
return NULL; |
| 748 |
|
| 749 |
return soup_xml_real_node (param->next); |
| 750 |
} |
| 751 |
|
| 752 |
/** |
| 753 |
* e_soap_parameter_get_next_child_by_name: |
| 754 |
* @param: (nullable): An #ESoapParameter. |
| 755 |
* @name: The name of the sibling parameter to look for. |
| 756 |
* |
| 757 |
* Gets the next sibling of the given #ESoapParameter whose name is |
| 758 |
* @name. |
| 759 |
* |
| 760 |
* FIXME: the name of this method is wrong |
| 761 |
* |
| 762 |
* Returns: the next sibling with the given name, or %NULL |
| 763 |
*/ |
| 764 |
ESoapParameter * |
| 765 |
e_soap_parameter_get_next_child_by_name (ESoapParameter *param, |
| 766 |
const gchar *name) |
| 767 |
{ |
| 768 |
ESoapParameter *tmp; |
| 769 |
|
| 770 |
g_return_val_if_fail (name != NULL, NULL); |
| 771 |
|
| 772 |
for (tmp = e_soap_parameter_get_next_child (param); |
| 773 |
tmp != NULL; |
| 774 |
tmp = e_soap_parameter_get_next_child (tmp)) { |
| 775 |
if (!strcmp (name, (const gchar *) tmp->name)) |
| 776 |
return tmp; |
| 777 |
} |
| 778 |
|
| 779 |
return NULL; |
| 780 |
} |
| 781 |
|
| 782 |
/** |
| 783 |
* e_soap_parameter_get_property: |
| 784 |
* @param: the parameter |
| 785 |
* @prop_name: Name of the property to retrieve. |
| 786 |
* |
| 787 |
* Returns the named property of @param. |
| 788 |
* |
| 789 |
* Returns: the property, which must be freed by the caller. |
| 790 |
*/ |
| 791 |
gchar * |
| 792 |
e_soap_parameter_get_property (ESoapParameter *param, |
| 793 |
const gchar *prop_name) |
| 794 |
{ |
| 795 |
xmlChar *xml_s; |
| 796 |
gchar *s; |
| 797 |
|
| 798 |
g_return_val_if_fail (param != NULL, NULL); |
| 799 |
g_return_val_if_fail (prop_name != NULL, NULL); |
| 800 |
|
| 801 |
xml_s = xmlGetProp (param, (const xmlChar *) prop_name); |
| 802 |
s = g_strdup ((gchar *) xml_s); |
| 803 |
xmlFree (xml_s); |
| 804 |
|
| 805 |
return s; |
| 806 |
} |
| 807 |
|
| 808 |
/** |
| 809 |
* e_soap_response_get_parameters: |
| 810 |
* @response: the #ESoapResponse object. |
| 811 |
* |
| 812 |
* Returns the list of parameters received in the SOAP response. |
| 813 |
* |
| 814 |
* Returns: a list of #ESoapParameter |
| 815 |
*/ |
| 816 |
const GList * |
| 817 |
e_soap_response_get_parameters (ESoapResponse *response) |
| 818 |
{ |
| 819 |
g_return_val_if_fail (E_IS_SOAP_RESPONSE (response), NULL); |
| 820 |
|
| 821 |
return (const GList *) response->priv->parameters; |
| 822 |
} |
| 823 |
|
| 824 |
ESoapParameter * |
| 825 |
e_soap_response_get_parameter (ESoapResponse *response) |
| 826 |
{ |
| 827 |
g_return_val_if_fail (E_IS_SOAP_RESPONSE (response), NULL); |
| 828 |
|
| 829 |
return response->priv->xml_method; |
| 830 |
} |
| 831 |
|
| 832 |
/** |
| 833 |
* e_soap_response_get_first_parameter: |
| 834 |
* @response: the #ESoapResponse object. |
| 835 |
* |
| 836 |
* Retrieves the first parameter contained in the SOAP response. |
| 837 |
* |
| 838 |
* Returns: an #ESoapParameter representing the first |
| 839 |
* parameter, or %NULL if there are no parameters. |
| 840 |
*/ |
| 841 |
ESoapParameter * |
| 842 |
e_soap_response_get_first_parameter (ESoapResponse *response) |
| 843 |
{ |
| 844 |
g_return_val_if_fail (E_IS_SOAP_RESPONSE (response), NULL); |
| 845 |
|
| 846 |
if (response->priv->parameters == NULL) |
| 847 |
return NULL; |
| 848 |
|
| 849 |
return response->priv->parameters->data; |
| 850 |
} |
| 851 |
|
| 852 |
/** |
| 853 |
* e_soap_response_get_first_parameter_by_name: |
| 854 |
* @response: the #ESoapResponse object. |
| 855 |
* @name: the name of the parameter to look for. |
| 856 |
* @error: return location for a #GError, or %NULL |
| 857 |
* |
| 858 |
* Retrieves the first parameter contained in the SOAP response whose |
| 859 |
* name is @name. If no parameter is found, the function sets @error |
| 860 |
* and returns %NULL. |
| 861 |
* |
| 862 |
* The function also checks for a SOAP "faultstring" parameter and, |
| 863 |
* if found, uses it to set the #GError message. |
| 864 |
* |
| 865 |
* Returns: an #ESoapParameter representing the first parameter |
| 866 |
* with the given name, or %NULL. |
| 867 |
*/ |
| 868 |
ESoapParameter * |
| 869 |
e_soap_response_get_first_parameter_by_name (ESoapResponse *response, |
| 870 |
const gchar *name, |
| 871 |
GError **error) |
| 872 |
{ |
| 873 |
GList *l; |
| 874 |
|
| 875 |
g_return_val_if_fail (E_IS_SOAP_RESPONSE (response), NULL); |
| 876 |
g_return_val_if_fail (name != NULL, NULL); |
| 877 |
|
| 878 |
for (l = response->priv->parameters; l != NULL; l = l->next) { |
| 879 |
ESoapParameter *param = (ESoapParameter *) l->data; |
| 880 |
|
| 881 |
if (strcmp (name, (const gchar *) param->name) == 0) |
| 882 |
return param; |
| 883 |
} |
| 884 |
|
| 885 |
/* XXX These are probably not the best error codes, but |
| 886 |
* wanted to avoid EWS_CONNECTION_ERROR codes since |
| 887 |
* this class is potentially reusable. */ |
| 888 |
|
| 889 |
for (l = response->priv->parameters; l != NULL; l = l->next) { |
| 890 |
ESoapParameter *param = (ESoapParameter *) l->data; |
| 891 |
|
| 892 |
if (strcmp ("faultstring", (const gchar *) param->name) == 0) { |
| 893 |
gchar *string; |
| 894 |
|
| 895 |
string = e_soap_parameter_get_string_value (param); |
| 896 |
|
| 897 |
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
| 898 |
(string != NULL) ? string : "<faultstring> in SOAP response"); |
| 899 |
|
| 900 |
g_free (string); |
| 901 |
|
| 902 |
return NULL; |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
| 907 |
"Missing <%s> in SOAP response", name); |
| 908 |
|
| 909 |
return NULL; |
| 910 |
} |
| 911 |
|
| 912 |
/** |
| 913 |
* e_soap_response_get_next_parameter: |
| 914 |
* @response: the #ESoapResponse object. |
| 915 |
* @from: the parameter to start from. |
| 916 |
* |
| 917 |
* Retrieves the parameter following @from in the #ESoapResponse |
| 918 |
* object. |
| 919 |
* |
| 920 |
* Returns: an #ESoapParameter representing the parameter. |
| 921 |
*/ |
| 922 |
ESoapParameter * |
| 923 |
e_soap_response_get_next_parameter (ESoapResponse *response, |
| 924 |
ESoapParameter *from) |
| 925 |
{ |
| 926 |
GList *l; |
| 927 |
|
| 928 |
g_return_val_if_fail (E_IS_SOAP_RESPONSE (response), NULL); |
| 929 |
g_return_val_if_fail (from != NULL, NULL); |
| 930 |
|
| 931 |
l = g_list_find (response->priv->parameters, (gconstpointer) from); |
| 932 |
if (!l) |
| 933 |
return NULL; |
| 934 |
|
| 935 |
return l->next ? (ESoapParameter *) l->next->data : NULL; |
| 936 |
} |
| 937 |
|
| 938 |
/** |
| 939 |
* e_soap_response_get_next_parameter_by_name: |
| 940 |
* @response: the #ESoapResponse object. |
| 941 |
* @from: the parameter to start from. |
| 942 |
* @name: the name of the parameter to look for. |
| 943 |
* |
| 944 |
* Retrieves the first parameter following @from in the |
| 945 |
* #ESoapResponse object whose name matches @name. |
| 946 |
* |
| 947 |
* Returns: an #ESoapParameter representing the parameter. |
| 948 |
*/ |
| 949 |
ESoapParameter * |
| 950 |
e_soap_response_get_next_parameter_by_name (ESoapResponse *response, |
| 951 |
ESoapParameter *from, |
| 952 |
const gchar *name) |
| 953 |
{ |
| 954 |
ESoapParameter *param; |
| 955 |
|
| 956 |
g_return_val_if_fail (E_IS_SOAP_RESPONSE (response), NULL); |
| 957 |
g_return_val_if_fail (from != NULL, NULL); |
| 958 |
g_return_val_if_fail (name != NULL, NULL); |
| 959 |
|
| 960 |
param = e_soap_response_get_next_parameter (response, from); |
| 961 |
while (param) { |
| 962 |
const gchar *param_name = e_soap_parameter_get_name (param); |
| 963 |
|
| 964 |
if (param_name) { |
| 965 |
if (!strcmp (name, param_name)) |
| 966 |
return param; |
| 967 |
} |
| 968 |
|
| 969 |
param = e_soap_response_get_next_parameter (response, param); |
| 970 |
} |
| 971 |
|
| 972 |
return NULL; |
| 973 |
} |
| 974 |
|
| 975 |
/** |
| 976 |
* e_soap_response_dump_response: |
| 977 |
**/ |
| 978 |
gint |
| 979 |
e_soap_response_dump_response (ESoapResponse *response, |
| 980 |
FILE *buffer) |
| 981 |
{ |
| 982 |
xmlChar *xmlbuff; |
| 983 |
gint buffersize, ret; |
| 984 |
|
| 985 |
g_return_val_if_fail (E_IS_SOAP_RESPONSE (response), -1); |
| 986 |
|
| 987 |
xmlDocDumpFormatMemory ( |
| 988 |
response->priv->xmldoc, |
| 989 |
&xmlbuff, &buffersize, 1); |
| 990 |
ret = fputs ((gchar *) xmlbuff, buffer); |
| 991 |
xmlFree (xmlbuff); |
| 992 |
|
| 993 |
return ret; |
| 994 |
} |
| 995 |
|
| 996 |
gchar * |
| 997 |
e_soap_response_dump_parameter (ESoapResponse *response, |
| 998 |
ESoapParameter *param) |
| 999 |
{ |
| 1000 |
xmlBuffer *buffer; |
| 1001 |
gint len; |
| 1002 |
gchar *data; |
| 1003 |
|
| 1004 |
g_return_val_if_fail (E_IS_SOAP_RESPONSE (response), NULL); |
| 1005 |
g_return_val_if_fail (param != NULL, NULL); |
| 1006 |
|
| 1007 |
buffer = xmlBufferCreate (); |
| 1008 |
len = xmlNodeDump (buffer, response->priv->xmldoc, param, 0, 0); |
| 1009 |
|
| 1010 |
if (len <= 0) { |
| 1011 |
xmlBufferFree (buffer); |
| 1012 |
return NULL; |
| 1013 |
} |
| 1014 |
|
| 1015 |
data = g_strndup ((const gchar *) buffer->content, len); |
| 1016 |
|
| 1017 |
xmlBufferFree (buffer); |
| 1018 |
|
| 1019 |
return data; |
| 1020 |
} |