group: disable sipe_core_group_set_alias for UCS
[siplcs.git] / src / core / sipe-http-request.c
blob28b0bcb22d2f68874a1a5c8f2b2d84c4dfbed11d
1 /**
2 * @file sipe-http-request.c
4 * pidgin-sipe
6 * Copyright (C) 2013 SIPE Project <http://sipe.sourceforge.net/>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * SIPE HTTP request layer implementation
26 * - request handling: creation, parameters, deletion, cancelling
27 * - session handling: creation, closing
28 * - client authorization handling
29 * - connection request queue handling
30 * - compile HTTP header contents and hand-off to transport layer
31 * - process HTTP response and hand-off to user callback
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
38 #include <string.h>
40 #include <glib.h>
42 #include "sipmsg.h"
43 #include "sip-sec.h"
44 #include "sipe-backend.h"
45 #include "sipe-core.h"
46 #include "sipe-core-private.h"
47 #include "sipe-http.h"
49 #define _SIPE_HTTP_PRIVATE_IF_REQUEST
50 #include "sipe-http-request.h"
51 #define _SIPE_HTTP_PRIVATE_IF_TRANSPORT
52 #include "sipe-http-transport.h"
54 struct sipe_http_session {
55 gchar *cookie; /* extremely simplistic cookie jar :-) */
58 struct sipe_http_request {
59 struct sipe_http_connection_public *connection;
61 struct sipe_http_session *session;
63 gchar *path;
64 gchar *headers;
65 gchar *body; /* NULL for GET */
66 gchar *content_type; /* NULL if body == NULL */
67 gchar *authorization;
69 const gchar *domain; /* not copied */
70 const gchar *user; /* not copied */
71 const gchar *password; /* not copied */
73 sipe_http_response_callback *cb;
74 gpointer cb_data;
76 guint32 flags;
79 #define SIPE_HTTP_REQUEST_FLAG_FIRST 0x00000001
80 #define SIPE_HTTP_REQUEST_FLAG_REDIRECT 0x00000002
81 #define SIPE_HTTP_REQUEST_FLAG_AUTHDATA 0x00000004
83 static void sipe_http_request_free(struct sipe_core_private *sipe_private,
84 struct sipe_http_request *req,
85 guint status)
87 if (req->cb)
88 /* Callback: aborted/failed/cancelled */
89 (*req->cb)(sipe_private,
90 status,
91 NULL,
92 NULL,
93 req->cb_data);
94 g_free(req->path);
95 g_free(req->headers);
96 g_free(req->body);
97 g_free(req->content_type);
98 g_free(req->authorization);
99 g_free(req);
102 static void sipe_http_request_send(struct sipe_http_connection_public *conn_public)
104 struct sipe_http_request *req = conn_public->pending_requests->data;
105 gchar *header;
106 gchar *content = NULL;
107 gchar *cookie = NULL;
109 if (req->body)
110 content = g_strdup_printf("Content-Length: %" G_GSIZE_FORMAT "\r\n"
111 "Content-Type: %s\r\n",
112 strlen(req->body),
113 req->content_type);
115 if (req->session && req->session->cookie)
116 cookie = g_strdup_printf("Cookie: %s\r\n", req->session->cookie);
118 header = g_strdup_printf("%s /%s HTTP/1.1\r\n"
119 "Host: %s\r\n"
120 "User-Agent: Sipe/" PACKAGE_VERSION "\r\n"
121 "%s%s%s%s",
122 content ? "POST" : "GET",
123 req->path,
124 conn_public->host,
125 req->authorization ? req->authorization : "",
126 req->headers ? req->headers : "",
127 cookie ? cookie : "",
128 content ? content : "");
129 g_free(cookie);
130 g_free(content);
132 /* only use authorization once */
133 g_free(req->authorization);
134 req->authorization = NULL;
136 sipe_http_transport_send(conn_public,
137 header,
138 req->body);
139 g_free(header);
142 gboolean sipe_http_request_pending(struct sipe_http_connection_public *conn_public)
144 return(conn_public->pending_requests != NULL);
147 void sipe_http_request_next(struct sipe_http_connection_public *conn_public)
149 sipe_http_request_send(conn_public);
152 static void sipe_http_request_enqueue(struct sipe_core_private *sipe_private,
153 struct sipe_http_request *req,
154 const struct sipe_http_parsed_uri *parsed_uri)
156 struct sipe_http_connection_public *conn_public;
158 req->path = g_strdup(parsed_uri->path);
159 req->connection = conn_public = sipe_http_transport_new(sipe_private,
160 parsed_uri->host,
161 parsed_uri->port,
162 parsed_uri->tls);
163 if (!sipe_http_request_pending(conn_public))
164 req->flags |= SIPE_HTTP_REQUEST_FLAG_FIRST;
166 conn_public->pending_requests = g_slist_append(conn_public->pending_requests,
167 req);
170 /* TRUE indicates failure */
171 static gboolean sipe_http_request_response_redirection(struct sipe_core_private *sipe_private,
172 struct sipe_http_request *req,
173 struct sipmsg *msg)
175 const gchar *location = sipmsg_find_header(msg, "Location");
176 gboolean failed = TRUE;
178 if (location) {
179 struct sipe_http_parsed_uri *parsed_uri = sipe_http_parse_uri(location);
181 if (parsed_uri) {
182 /* remove request from old connection */
183 struct sipe_http_connection_public *conn_public = req->connection;
184 conn_public->pending_requests = g_slist_remove(conn_public->pending_requests,
185 req);
187 /* free old request data */
188 g_free(req->path);
189 req->flags &= ~SIPE_HTTP_REQUEST_FLAG_FIRST;
191 /* resubmit request on other connection */
192 sipe_http_request_enqueue(sipe_private, req, parsed_uri);
193 failed = FALSE;
195 sipe_http_parsed_uri_free(parsed_uri);
196 } else
197 SIPE_DEBUG_INFO("sipe_http_request_response_redirection: invalid redirection to '%s'",
198 location);
199 } else
200 SIPE_DEBUG_INFO_NOFORMAT("sipe_http_request_response_redirection: no URL found?!?");
202 return(failed);
205 /* TRUE indicates failure */
206 static gboolean sipe_http_request_response_unauthorized(struct sipe_core_private *sipe_private,
207 struct sipe_http_request *req,
208 struct sipmsg *msg)
210 const gchar *header = NULL;
211 const gchar *name;
212 guint type;
213 gboolean failed = TRUE;
215 #if defined(HAVE_LIBKRB5) || defined(HAVE_SSPI)
216 #define DEBUG_STRING ", NTLM and Negotiate"
217 /* Use "Negotiate" unless the user requested "NTLM" */
218 if (sipe_private->authentication_type != SIPE_AUTHENTICATION_TYPE_NTLM)
219 header = sipmsg_find_auth_header(msg, "Negotiate");
220 if (header) {
221 type = SIPE_AUTHENTICATION_TYPE_NEGOTIATE;
222 name = "Negotiate";
223 } else
224 #else
225 #define DEBUG_STRING " and NTLM"
226 (void) sipe_private; /* keep compiler happy */
227 #endif
229 header = sipmsg_find_auth_header(msg, "NTLM");
230 type = SIPE_AUTHENTICATION_TYPE_NTLM;
231 name = "NTLM";
234 /* only fall back to "Basic" after everything else fails */
235 if (!header) {
236 header = sipmsg_find_auth_header(msg, "Basic");
237 type = SIPE_AUTHENTICATION_TYPE_BASIC;
238 name = "Basic";
241 if (header) {
242 struct sipe_http_connection_public *conn_public = req->connection;
244 if (!conn_public->context) {
245 gboolean valid = req->flags & SIPE_HTTP_REQUEST_FLAG_AUTHDATA;
246 conn_public->context = sip_sec_create_context(type,
247 !valid, /* Single Sign-On flag */
248 TRUE, /* connection-based for HTTP */
249 valid ? req->domain : NULL,
250 valid ? req->user : NULL,
251 valid ? req->password : NULL);
255 if (conn_public->context) {
256 gchar **parts = g_strsplit(header, " ", 0);
257 gchar *spn = g_strdup_printf("HTTP/%s", conn_public->host);
258 gchar *token;
260 SIPE_DEBUG_INFO("sipe_http_request_response_unauthorized: init context target '%s' token '%s'",
261 spn, parts[1] ? parts[1] : "<NULL>");
263 if (sip_sec_init_context_step(conn_public->context,
264 spn,
265 parts[1],
266 &token,
267 NULL)) {
269 /* generate authorization header */
270 req->authorization = g_strdup_printf("Authorization: %s %s\r\n",
271 name,
272 token ? token : "");
273 g_free(token);
276 * Keep the request in the queue. As it is at
277 * the head it will be pulled automatically
278 * by the transport layer after returning.
280 failed = FALSE;
282 } else
283 SIPE_DEBUG_INFO_NOFORMAT("sipe_http_request_response_unauthorized: security context init step failed");
285 g_free(spn);
286 g_strfreev(parts);
287 } else
288 SIPE_DEBUG_INFO_NOFORMAT("sipe_http_request_response_unauthorized: security context creation failed");
289 } else
290 SIPE_DEBUG_INFO_NOFORMAT("sipe_http_request_response_unauthorized: only Basic" DEBUG_STRING " authentication schemes are supported");
292 return(failed);
295 static void sipe_http_request_response_callback(struct sipe_core_private *sipe_private,
296 struct sipe_http_request *req,
297 struct sipmsg *msg)
299 const gchar *hdr;
301 /* Set-Cookie: RMID=732423sdfs73242; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.example.net */
302 if (req->session &&
303 ((hdr = sipmsg_find_header(msg, "Set-Cookie")) != NULL)) {
304 gchar **parts, **current;
305 const gchar *part;
306 gchar *new = NULL;
308 g_free(req->session->cookie);
309 req->session->cookie = NULL;
311 current = parts = g_strsplit(hdr, ";", 0);
312 while ((part = *current++) != NULL) {
313 /* strip these parts from cookie */
314 if (!(strstr(part, "path=") ||
315 strstr(part, "domain=") ||
316 strstr(part, "expires=") ||
317 strstr(part, "secure"))) {
318 gchar *tmp = new;
319 new = new ?
320 g_strconcat(new, ";", part, NULL) :
321 g_strdup(part);
322 g_free(tmp);
325 g_strfreev(parts);
327 if (new) {
328 req->session->cookie = new;
329 SIPE_DEBUG_INFO("sipe_http_request_response_callback: cookie: %s", new);
333 /* Callback: success */
334 (*req->cb)(sipe_private,
335 msg->response,
336 msg->headers,
337 msg->body,
338 req->cb_data);
340 /* remove completed request */
341 sipe_http_request_cancel(req);
344 void sipe_http_request_response(struct sipe_http_connection_public *conn_public,
345 struct sipmsg *msg)
347 struct sipe_core_private *sipe_private = conn_public->sipe_private;
348 struct sipe_http_request *req = conn_public->pending_requests->data;
349 gboolean failed;
351 if ((req->flags & SIPE_HTTP_REQUEST_FLAG_REDIRECT) &&
352 (msg->response >= SIPE_HTTP_STATUS_REDIRECTION) &&
353 (msg->response < SIPE_HTTP_STATUS_CLIENT_ERROR)) {
354 failed = sipe_http_request_response_redirection(sipe_private,
355 req,
356 msg);
358 } else if (msg->response == SIPE_HTTP_STATUS_CLIENT_UNAUTHORIZED) {
359 failed = sipe_http_request_response_unauthorized(sipe_private,
360 req,
361 msg);
363 } else {
364 /* On error throw away the security context */
365 if ((msg->response >= SIPE_HTTP_STATUS_CLIENT_ERROR) &&
366 conn_public->context) {
367 SIPE_DEBUG_INFO("sipe_http_request_response: response was %d, throwing away security context",
368 msg->response);
369 sip_sec_destroy_context(conn_public->context);
370 conn_public->context = NULL;
373 /* All other cases are passed on to the user */
374 sipe_http_request_response_callback(sipe_private, req, msg);
376 /* req is no longer valid */
377 failed = FALSE;
380 if (failed) {
381 /* Callback: request failed */
382 (*req->cb)(sipe_private,
383 SIPE_HTTP_STATUS_FAILED,
384 NULL,
385 NULL,
386 req->cb_data);
388 /* remove failed request */
389 sipe_http_request_cancel(req);
393 void sipe_http_request_shutdown(struct sipe_http_connection_public *conn_public,
394 gboolean abort)
396 if (conn_public->pending_requests) {
397 GSList *entry = conn_public->pending_requests;
398 while (entry) {
399 sipe_http_request_free(conn_public->sipe_private,
400 entry->data,
401 abort ?
402 SIPE_HTTP_STATUS_ABORTED :
403 SIPE_HTTP_STATUS_FAILED);
404 entry = entry->next;
406 g_slist_free(conn_public->pending_requests);
407 conn_public->pending_requests = NULL;
410 if (conn_public->context) {
411 sip_sec_destroy_context(conn_public->context);
412 conn_public->context = NULL;
416 struct sipe_http_request *sipe_http_request_new(struct sipe_core_private *sipe_private,
417 const struct sipe_http_parsed_uri *parsed_uri,
418 const gchar *headers,
419 const gchar *body,
420 const gchar *content_type,
421 sipe_http_response_callback *callback,
422 gpointer callback_data)
424 struct sipe_http_request *req;
425 if (!parsed_uri)
426 return(NULL);
427 if (sipe_http_shutting_down(sipe_private)) {
428 SIPE_DEBUG_ERROR("sipe_http_request_new: new HTTP request during shutdown: THIS SHOULD NOT HAPPEN! Debugging information:\n"
429 "Host: %s\n"
430 "Port: %d\n"
431 "Path: %s\n"
432 "Headers: %s\n"
433 "Body: %s\n",
434 parsed_uri->host,
435 parsed_uri->port,
436 parsed_uri->path,
437 headers ? headers : "<NONE>",
438 body ? body : "<EMPTY>");
439 return(NULL);
442 req = g_new0(struct sipe_http_request, 1);
443 req->flags = 0;
444 req->cb = callback;
445 req->cb_data = callback_data;
446 if (headers)
447 req->headers = g_strdup(headers);
448 if (body) {
449 req->body = g_strdup(body);
450 req->content_type = g_strdup(content_type);
453 /* default authentication */
454 if (!SIPE_CORE_PRIVATE_FLAG_IS(SSO))
455 sipe_http_request_authentication(req,
456 sipe_private->authdomain,
457 sipe_private->authuser,
458 sipe_private->password);
460 sipe_http_request_enqueue(sipe_private, req, parsed_uri);
462 return(req);
465 void sipe_http_request_ready(struct sipe_http_request *request)
467 struct sipe_http_connection_public *conn_public = request->connection;
469 /* pass first request on already opened connection through directly */
470 if ((request->flags & SIPE_HTTP_REQUEST_FLAG_FIRST) &&
471 conn_public->connected)
472 sipe_http_request_send(conn_public);
475 struct sipe_http_session *sipe_http_session_start(void)
477 return(g_new0(struct sipe_http_session, 1));
480 void sipe_http_session_close(struct sipe_http_session *session)
482 if (session) {
483 g_free(session->cookie);
484 g_free(session);
488 void sipe_http_request_cancel(struct sipe_http_request *request)
490 struct sipe_http_connection_public *conn_public = request->connection;
491 conn_public->pending_requests = g_slist_remove(conn_public->pending_requests,
492 request);
494 /* cancelled by requester, don't use callback */
495 request->cb = NULL;
497 sipe_http_request_free(conn_public->sipe_private,
498 request,
499 SIPE_HTTP_STATUS_CANCELLED);
502 void sipe_http_request_session(struct sipe_http_request *request,
503 struct sipe_http_session *session)
505 request->session = session;
508 void sipe_http_request_allow_redirect(struct sipe_http_request *request)
510 request->flags |= SIPE_HTTP_REQUEST_FLAG_REDIRECT;
513 void sipe_http_request_authentication(struct sipe_http_request *request,
514 const gchar *domain,
515 const gchar *user,
516 const gchar *password)
518 request->flags |= SIPE_HTTP_REQUEST_FLAG_AUTHDATA;
519 request->domain = domain;
520 request->user = user;
521 request->password = password;
525 Local Variables:
526 mode: c
527 c-file-style: "bsd"
528 indent-tabs-mode: t
529 tab-width: 8
530 End: