Basic SOAP test, using Aonaware's SOAP->DICT gateway
authorDan Winship <danw@novell.com>
Thu, 8 Jul 2004 18:15:37 +0000 (18:15 +0000)
committerDan Winship <danw@src.gnome.org>
Thu, 8 Jul 2004 18:15:37 +0000 (18:15 +0000)
2004-07-08  Dan Winship  <danw@novell.com>

* tests/dict.c: Basic SOAP test, using Aonaware's SOAP->DICT
gateway

2004-07-07  Fernando Herrera  <fherrera@onirica.com>

* libsoup/soup-soap-response.c: (finalize), (init),
(soup_soap_response_from_string): Use a parse context for the
xml document, so we can safely use the option to ignore
blank spaces and '\n'.

ChangeLog
libsoup/soup-soap-response.c
tests/.cvsignore
tests/Makefile.am
tests/dict.c [new file with mode: 0644]

index 3fc2fe6..0c8e47e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2004-07-08  Dan Winship  <danw@novell.com>
+
+       * tests/dict.c: Basic SOAP test, using Aonaware's SOAP->DICT
+       gateway
+
+2004-07-07  Fernando Herrera  <fherrera@onirica.com>
+
+       * libsoup/soup-soap-response.c: (finalize), (init),
+       (soup_soap_response_from_string): Use a parse context for the
+       xml document, so we can safely use the option to ignore
+       blank spaces and '\n'.
+
 2004-07-06  Dan Winship  <danw@novell.com>
 
        * libsoup/soup-uri.c (soup_uri_new_with_base): if the protocol is
index b0746d1..a7842b8 100644 (file)
@@ -15,6 +15,7 @@
 struct _SoupSoapResponsePrivate {
        /* the XML document */
        xmlDocPtr xmldoc;
+       xmlParserCtxtPtr ctxt;
        xmlNodePtr xml_root;
        xmlNodePtr xml_body;
        xmlNodePtr xml_method;
@@ -34,6 +35,11 @@ finalize (GObject *object)
                xmlFreeDoc (response->priv->xmldoc);
                response->priv->xmldoc = NULL;
        }
+       if (response->priv->ctxt) {
+               xmlFreeParserCtxt (response->priv->ctxt);
+               response->priv->ctxt = NULL;
+       }
+       
 
        response->priv->xml_root = NULL;
        response->priv->xml_body = NULL;
@@ -66,6 +72,7 @@ init (SoupSoapResponse *response, SoupSoapResponseClass *klass)
 {
        response->priv = g_new0 (SoupSoapResponsePrivate, 1);
 
+       response->priv->ctxt = xmlNewParserCtxt ();
        response->priv->xmldoc = xmlNewDoc ("1.0");
 }
 
@@ -152,8 +159,9 @@ soup_soap_response_from_string (SoupSoapResponse *response, const char *xmlstr)
        if (response->priv->xmldoc)
                old_doc = response->priv->xmldoc;
 
-       /* parse the string */
-       response->priv->xmldoc = xmlParseMemory (xmlstr, strlen (xmlstr));
+       /* parse the string. We are using a parse context to make libxml 
+        * ignore blanks and '\n' for this document. */
+       response->priv->xmldoc = xmlCtxtReadMemory (response->priv->ctxt, xmlstr, strlen (xmlstr), NULL, NULL, XML_PARSE_NOBLANKS);
        if (!response->priv->xmldoc) {
                response->priv->xmldoc = old_doc;
                return FALSE;
index 87e6da0..674da08 100644 (file)
@@ -1,6 +1,7 @@
 Makefile
 Makefile.in
 auth-test
+dict
 dns
 get
 revserver
index 7d32ac3..1d7ed7f 100644 (file)
@@ -1,11 +1,13 @@
 INCLUDES =             \
        -I$(top_srcdir) \
+       $(XML_CFLAGS)   \
        $(GLIB_CFLAGS)
 
 LIBS = $(top_builddir)/libsoup/libsoup-$(SOUP_API_VERSION).la
 
 noinst_PROGRAMS =      \
        auth-test       \
+       dict            \
        dns             \
        get             \
        revserver       \
@@ -14,11 +16,12 @@ noinst_PROGRAMS =   \
        uri-parsing
 
 auth_test_SOURCES = auth-test.c
+dict_SOURCES = dict.c
 dns_SOURCES = dns.c
 get_SOURCES = get.c
+revserver_SOURCES = revserver.c
 simple_httpd_SOURCES = simple-httpd.c
 simple_proxy_SOURCES = simple-proxy.c
-revserver_SOURCES = revserver.c
 uri_parsing_SOURCES = uri-parsing.c
 
 EXTRA_DIST = libsoup.supp test-cert.pem test-key.pem
diff --git a/tests/dict.c b/tests/dict.c
new file mode 100644 (file)
index 0000000..afaed0e
--- /dev/null
@@ -0,0 +1,164 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2001-2003, Ximian, Inc.
+ */
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <libsoup/soup.h>
+#include <libsoup/soup-soap-message.h>
+#include <libsoup/soup-soap-response.h>
+
+SoupSession *session;
+GMainLoop *loop;
+
+static void
+got_response (SoupMessage *msg, gpointer user_data)
+{
+       SoupSoapResponse *response;
+       SoupSoapParameter *param, *subparam;
+       char *word, *dict, *def;
+       int count = 0;
+
+       if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
+               fprintf (stderr, "%d %s\n", msg->status_code, msg->reason_phrase);
+               exit (1);
+       }
+
+       response = soup_soap_message_parse_response (SOUP_SOAP_MESSAGE (msg));
+       if (!response) {
+               fprintf (stderr, "Could not parse SOAP response\n");
+               exit (1);
+       }
+
+       param = soup_soap_response_get_first_parameter_by_name (response, "DefineResult");
+       if (!param) {
+               fprintf (stderr, "Could not find result in SOAP response\n");
+               exit (1);
+       }
+
+       param = soup_soap_parameter_get_first_child_by_name (param, "Definitions");
+       if (!param)
+               goto done;
+
+       for (param = soup_soap_parameter_get_first_child_by_name (param, "Definition");
+            param;
+            param = soup_soap_parameter_get_next_child_by_name (param, "Definition")) {
+               subparam = soup_soap_parameter_get_first_child_by_name (param, "Word");
+               if (!subparam)
+                       continue;
+               word = soup_soap_parameter_get_string_value (subparam);
+
+               subparam = soup_soap_parameter_get_first_child_by_name (param, "Dictionary");
+               if (subparam)
+                       subparam = soup_soap_parameter_get_first_child_by_name (subparam, "Name");
+               if (subparam)
+                       dict = soup_soap_parameter_get_string_value (subparam);
+               else
+                       dict = NULL;
+
+               printf ("% 2d. %s (%s):\n", ++count, word, dict);
+               g_free (word);
+               g_free (dict);
+
+               subparam = soup_soap_parameter_get_first_child_by_name (param, "WordDefinition");
+               if (subparam) {
+                       def = soup_soap_parameter_get_string_value (subparam);
+                       printf ("%s\n", def);
+                       g_free (def);
+               }
+       }
+
+ done:
+       if (count == 0)
+               printf ("No definition\n");
+
+       g_object_unref (response);
+       g_main_quit (loop);
+}
+
+static void
+usage (void)
+{
+       fprintf (stderr, "Usage: dict [-p proxy_uri] WORD\n");
+       exit (1);
+}
+
+int
+main (int argc, char **argv)
+{
+       SoupUri *proxy = NULL;
+       SoupSoapMessage *msg;
+       int opt;
+
+       g_type_init ();
+       g_thread_init (NULL);
+
+       while ((opt = getopt (argc, argv, "p:")) != -1) {
+               switch (opt) {
+               case 'p':
+                       proxy = soup_uri_new (optarg);
+                       if (!proxy) {
+                               fprintf (stderr, "Could not parse %s as URI\n",
+                                        optarg);
+                               exit (1);
+                       }
+                       break;
+
+               case '?':
+                       usage ();
+                       break;
+               }
+       }
+       argc -= optind;
+       argv += optind;
+
+       if (argc != 1)
+               usage ();
+
+       session = soup_session_async_new_with_options (
+               SOUP_SESSION_PROXY_URI, proxy,
+               NULL);
+
+       msg = soup_soap_message_new ("POST",
+                                    "http://services.aonaware.com/DictService/DictService.asmx",
+                                    FALSE, NULL, NULL, NULL);
+       if (!msg) {
+               fprintf (stderr, "Could not create web service request\n");
+               exit (1);
+       }
+
+       soup_message_add_header (SOUP_MESSAGE (msg)->request_headers,
+                                "SOAPAction", "http://services.aonaware.com/webservices/Define");
+
+       soup_soap_message_start_envelope (msg);
+       soup_soap_message_start_body (msg);
+
+       soup_soap_message_start_element (msg, "Define", NULL,
+                                        "http://services.aonaware.com/webservices/");
+       soup_soap_message_add_namespace (msg, NULL, "http://services.aonaware.com/webservices/");
+       soup_soap_message_start_element (msg, "word", NULL, NULL);
+       soup_soap_message_write_string (msg, argv[0]);
+       soup_soap_message_end_element (msg);
+       soup_soap_message_end_element (msg);
+
+       soup_soap_message_end_body (msg);
+       soup_soap_message_end_envelope (msg);
+       soup_soap_message_persist (msg);
+
+       soup_session_queue_message (session, SOUP_MESSAGE (msg),
+                                   got_response, NULL);
+
+       loop = g_main_loop_new (NULL, TRUE);
+       g_main_run (loop);
+       g_main_loop_unref (loop);
+
+       return 0;
+}