Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / servers / groupwise / e-gw-message.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* 
3  * Authors : 
4  *  JP Rosevear <jpr@ximian.com>
5  *  Rodrigo Moya <rodrigo@ximian.com>
6  *
7  * Copyright 2003, Novell, Inc.
8  *
9  * This program is free software; you can redistribute it and/or 
10  * modify it under the terms of version 2 of the GNU Lesser General Public 
11  * License as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21  * USA
22  */
23
24 #include <config.h>
25 #include <string.h>
26 #include <libsoup/soup-uri.h>
27 #include "e-gw-message.h"
28
29 static void
30 print_header (gpointer name, gpointer value, gpointer data)
31 {
32         g_print ("%s: %s\n", (char *) name, (char *) value);
33 }
34
35 static void
36 debug_handler (SoupMessage *msg, gpointer user_data)
37 {
38         g_print ("%d %s\nSOAP-Debug: %p @ %lu\n",
39                 msg->status_code, msg->reason_phrase,
40                 msg, time (NULL));
41
42         /* print headers */
43         soup_message_foreach_header (msg->response_headers, print_header, NULL);
44
45         /* print response */
46         if (msg->response.length) {
47                 fputc ('\n', stdout);
48                 fwrite (msg->response.body, 1, msg->response.length, stdout);
49                 fputc ('\n', stdout);
50         }
51 }
52
53 static void
54 setup_debug (SoupSoapMessage *msg)
55 {
56         const SoupUri *suri;
57
58         suri = soup_message_get_uri (SOUP_MESSAGE (msg));
59         g_print ("%s %s%s%s HTTP/1.1\nSOAP-Debug: %p @ %lu\n",
60                  SOUP_MESSAGE (msg)->method, suri->path,
61                  suri->query ? "?" : "",
62                  suri->query ? suri->query : "",
63                  msg, (unsigned long) time (NULL));
64
65         /* print message headers */
66         print_header ("Host", suri->host, NULL);
67         soup_message_foreach_header (SOUP_MESSAGE (msg)->request_headers, print_header, NULL);
68
69         soup_message_add_handler (SOUP_MESSAGE (msg), SOUP_HANDLER_POST_BODY, debug_handler, NULL);
70 }
71
72 SoupSoapMessage *
73 e_gw_message_new_with_header (const char *uri, const char *session_id, const char *method_name)
74 {
75         SoupSoapMessage *msg;
76
77         msg = soup_soap_message_new (SOUP_METHOD_POST, uri, FALSE, NULL, NULL, NULL);
78         if (!msg) {
79                 g_warning (G_STRLOC ": Could not build SOAP message");
80                 return NULL;
81         }
82
83         soup_message_add_header (SOUP_MESSAGE (msg)->request_headers, "Content-Type", "text/xml");
84         soup_message_add_header (SOUP_MESSAGE (msg)->request_headers, "User-Agent",
85                                  "Evolution/" VERSION);
86         soup_message_add_header (SOUP_MESSAGE (msg)->request_headers,"Connection",  "Keep-Alive");
87         soup_message_add_header (SOUP_MESSAGE (msg)->request_headers, "SOAPAction", method_name);
88
89         if (g_getenv ("GROUPWISE_DEBUG")) {
90                 if (atoi (g_getenv ("GROUPWISE_DEBUG")) == 1)
91                         setup_debug (msg);
92         }
93
94         soup_soap_message_start_envelope (msg);
95         if (session_id && *session_id) {
96                 soup_soap_message_start_element (msg, "Header","SOAP-ENV", NULL);
97                 soup_soap_message_add_attribute (msg, "encodingStyle", "", "SOAP-ENV", NULL);
98                 /* FIXME: cannot use e_gw_message_write_string_parameter as it sets prefix -types*/
99                 soup_soap_message_start_element (msg, "session", NULL, NULL);
100                 soup_soap_message_write_string (msg, session_id);
101                 soup_soap_message_end_element (msg);
102                 soup_soap_message_end_element (msg);
103         }
104         soup_soap_message_start_body (msg);
105         soup_soap_message_add_attribute (msg, "encodingStyle", "", "SOAP-ENV", NULL);
106         soup_soap_message_add_namespace (msg, "types", "http://schemas.novell.com/2003/10/NCSP/types.xsd");
107
108         soup_soap_message_start_element (msg, method_name, NULL, NULL);
109
110         return msg;
111 }
112
113 void
114 e_gw_message_write_string_parameter (SoupSoapMessage *msg, const char *name, const char *prefix, const char *value)
115 {
116         soup_soap_message_start_element (msg, name, prefix, NULL);
117         soup_soap_message_write_string (msg, value);
118         soup_soap_message_end_element (msg);
119 }
120
121 void 
122 e_gw_message_write_string_parameter_with_attribute (SoupSoapMessage *msg,
123                                                     const char *name,
124                                                     const char *prefix,
125                                                     const char *value, 
126                                                     const char *attribute_name,
127                                                     const char *attribute_value)
128 {
129         soup_soap_message_start_element (msg, name, prefix, NULL);
130         soup_soap_message_add_attribute (msg, attribute_name, attribute_value, NULL, NULL);
131         soup_soap_message_write_string (msg, value);
132         soup_soap_message_end_element (msg);
133 }
134
135 void
136 e_gw_message_write_base64_parameter (SoupSoapMessage *msg, const char *name, const char *prefix, const char *value)
137 {
138         soup_soap_message_start_element (msg, name, prefix, NULL);
139         soup_soap_message_write_base64 (msg, value, strlen (value));
140         soup_soap_message_end_element (msg);
141 }
142
143 void
144 e_gw_message_write_int_parameter (SoupSoapMessage *msg, const char *name, const char *prefix, long value)
145 {
146         soup_soap_message_start_element (msg, name, prefix, NULL);
147         soup_soap_message_write_int (msg, value);
148         soup_soap_message_end_element (msg);
149 }
150
151 void
152 e_gw_message_write_footer (SoupSoapMessage *msg)
153 {
154         soup_soap_message_end_element (msg);
155         soup_soap_message_end_body (msg);
156         soup_soap_message_end_envelope (msg);
157
158         soup_soap_message_persist (msg);
159
160         if (g_getenv ("GROUPWISE_DEBUG") && (atoi (g_getenv ("GROUPWISE_DEBUG")) == 1)) {
161                 const char *header = soup_message_get_header (SOUP_MESSAGE (msg)->request_headers, "SOAPAction");
162                 if (header && g_str_equal (header, "loginRequest")) {
163                         gchar *body;
164                         gchar *begin = NULL;
165                         gchar *end = NULL;
166                         
167                         body = g_strndup (SOUP_MESSAGE (msg)->request.body, SOUP_MESSAGE (msg)->request.length);
168                         begin = g_strrstr (body, "<types:password>");
169                         if (begin) 
170                                 begin = begin + strlen ("<types:password>");
171                         end = g_strrstr (body , "</types:password>");
172                         if (begin && end) {
173                                 gchar *tmp;
174                                 for (tmp = begin; tmp < end; tmp++)
175                                         *tmp='X';
176                                 
177                         }
178                         fputc ('\n', stdout);
179                         fwrite (body, 1, SOUP_MESSAGE (msg)->request.length, stdout);
180                         fputc ('\n', stdout);   
181                         g_free (body);
182                 }
183                 else {          
184         
185                         /* print request's body */
186                         fputc ('\n', stdout);
187                         fwrite (SOUP_MESSAGE (msg)->request.body, 1, SOUP_MESSAGE (msg)->request.length, stdout);
188                         fputc ('\n', stdout);
189                 }
190         }
191 }