2 * @file sipe-http-request.c
6 * Copyright (C) 2013-2018 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
42 #include "sipe-common.h"
45 #include "sipe-backend.h"
46 #include "sipe-core.h"
47 #include "sipe-core-private.h"
48 #include "sipe-http.h"
50 #define _SIPE_HTTP_PRIVATE_IF_REQUEST
51 #include "sipe-http-request.h"
52 #define _SIPE_HTTP_PRIVATE_IF_TRANSPORT
53 #include "sipe-http-transport.h"
55 struct sipe_http_session
{
56 GHashTable
*cookie_jar
;
59 struct sipe_http_request
{
60 struct sipe_http_connection_public
*connection
;
62 struct sipe_http_session
*session
;
66 gchar
*body
; /* NULL for GET */
67 gchar
*content_type
; /* NULL if body == NULL */
70 const gchar
*user
; /* not copied */
71 const gchar
*password
; /* not copied */
73 sipe_http_response_callback
*cb
;
79 #define SIPE_HTTP_REQUEST_FLAG_FIRST 0x00000001
80 #define SIPE_HTTP_REQUEST_FLAG_REDIRECT 0x00000002
81 #define SIPE_HTTP_REQUEST_FLAG_AUTHDATA 0x00000004
82 #define SIPE_HTTP_REQUEST_FLAG_HANDSHAKE 0x00000008
84 static void sipe_http_request_free(struct sipe_core_private
*sipe_private
,
85 struct sipe_http_request
*req
,
89 /* Callback: aborted/failed/cancelled */
90 (*req
->cb
)(sipe_private
,
98 g_free(req
->content_type
);
99 g_free(req
->authorization
);
103 static void add_cookie_cb(SIPE_UNUSED_PARAMETER
const gchar
*key
,
107 g_string_append_printf(string
, "Cookie: %s\r\n", cookie
);
110 static void sipe_http_request_send(struct sipe_http_connection_public
*conn_public
)
112 struct sipe_http_request
*req
= conn_public
->pending_requests
->data
;
114 gchar
*content
= NULL
;
115 gchar
*cookie
= NULL
;
118 content
= g_strdup_printf("Content-Length: %" G_GSIZE_FORMAT
"\r\n"
119 "Content-Type: %s\r\n",
123 if (req
->session
&& g_hash_table_size(req
->session
->cookie_jar
)) {
124 GString
*cookies
= g_string_new("");
125 g_hash_table_foreach(req
->session
->cookie_jar
,
126 (GHFunc
) add_cookie_cb
,
128 cookie
= g_string_free(cookies
, FALSE
);
131 header
= g_strdup_printf("%s /%s HTTP/1.1\r\n"
135 content
? "POST" : "GET",
138 sipe_core_user_agent(conn_public
->sipe_private
),
139 conn_public
->cached_authorization
? conn_public
->cached_authorization
:
140 req
->authorization
? req
->authorization
: "",
141 req
->headers
? req
->headers
: "",
142 cookie
? cookie
: "",
143 content
? content
: "");
147 /* only use authorization once */
148 g_free(req
->authorization
);
149 req
->authorization
= NULL
;
151 sipe_http_transport_send(conn_public
,
157 gboolean
sipe_http_request_pending(struct sipe_http_connection_public
*conn_public
)
159 return(conn_public
->pending_requests
!= NULL
);
162 void sipe_http_request_next(struct sipe_http_connection_public
*conn_public
)
164 sipe_http_request_send(conn_public
);
167 static void sipe_http_request_enqueue(struct sipe_core_private
*sipe_private
,
168 struct sipe_http_request
*req
,
169 const struct sipe_http_parsed_uri
*parsed_uri
)
171 struct sipe_http_connection_public
*conn_public
;
173 req
->path
= g_strdup(parsed_uri
->path
);
174 req
->connection
= conn_public
= sipe_http_transport_new(sipe_private
,
178 if (!sipe_http_request_pending(conn_public
))
179 req
->flags
|= SIPE_HTTP_REQUEST_FLAG_FIRST
;
181 conn_public
->pending_requests
= g_slist_append(conn_public
->pending_requests
,
185 static void sipe_http_request_drop_context(struct sipe_http_connection_public
*conn_public
)
187 g_free(conn_public
->cached_authorization
);
188 conn_public
->cached_authorization
= NULL
;
189 sip_sec_destroy_context(conn_public
->context
);
190 conn_public
->context
= NULL
;
193 static void sipe_http_request_finalize_negotiate(struct sipe_http_request
*req
,
196 #if defined(HAVE_GSSAPI_GSSAPI_H) || defined(HAVE_SSPI)
198 * Negotiate can send a final package in the successful response.
199 * We need to forward this to the context or otherwise it will
200 * never reach the ready state.
202 struct sipe_http_connection_public
*conn_public
= req
->connection
;
204 if (sip_sec_context_type(conn_public
->context
) == SIPE_AUTHENTICATION_TYPE_NEGOTIATE
) {
205 const gchar
*header
= sipmsg_find_auth_header(msg
, "Negotiate");
208 gchar
**parts
= g_strsplit(header
, " ", 0);
209 gchar
*spn
= g_strdup_printf("HTTP/%s", conn_public
->host
);
212 SIPE_DEBUG_INFO("sipe_http_request_finalize_negotiate: init context target '%s' token '%s'",
213 spn
, parts
[1] ? parts
[1] : "<NULL>");
215 if (sip_sec_init_context_step(conn_public
->context
,
222 SIPE_DEBUG_INFO_NOFORMAT("sipe_http_request_finalize_negotiate: security context init step failed, throwing away context");
223 sipe_http_request_drop_context(conn_public
);
231 (void) req
; /* keep compiler happy */
232 (void) msg
; /* keep compiler happy */
237 /* TRUE indicates failure */
238 static gboolean
sipe_http_request_response_redirection(struct sipe_core_private
*sipe_private
,
239 struct sipe_http_request
*req
,
242 const gchar
*location
= sipmsg_find_header(msg
, "Location");
243 gboolean failed
= TRUE
;
245 sipe_http_request_finalize_negotiate(req
, msg
);
248 struct sipe_http_parsed_uri
*parsed_uri
= sipe_http_parse_uri(location
);
251 /* remove request from old connection */
252 struct sipe_http_connection_public
*conn_public
= req
->connection
;
253 conn_public
->pending_requests
= g_slist_remove(conn_public
->pending_requests
,
256 /* free old request data */
258 req
->flags
&= ~( SIPE_HTTP_REQUEST_FLAG_FIRST
|
259 SIPE_HTTP_REQUEST_FLAG_HANDSHAKE
);
261 /* resubmit request on other connection */
262 sipe_http_request_enqueue(sipe_private
, req
, parsed_uri
);
265 sipe_http_parsed_uri_free(parsed_uri
);
267 SIPE_DEBUG_INFO("sipe_http_request_response_redirection: invalid redirection to '%s'",
270 SIPE_DEBUG_INFO_NOFORMAT("sipe_http_request_response_redirection: no URL found?!?");
275 /* TRUE indicates failure */
276 static gboolean
sipe_http_request_response_unauthorized(struct sipe_core_private
*sipe_private
,
277 struct sipe_http_request
*req
,
280 struct sipe_http_connection_public
*conn_public
= req
->connection
;
281 const gchar
*header
= NULL
;
283 gboolean failed
= TRUE
;
286 * There are some buggy HTTP servers out there that add superfluous
287 * WWW-Authenticate: headers during the authentication handshake.
288 * Look only for the header of the active security context.
290 if (conn_public
->context
) {
291 const gchar
*name
= sip_sec_context_name(conn_public
->context
);
293 header
= sipmsg_find_auth_header(msg
, name
);
294 type
= sip_sec_context_type(conn_public
->context
);
297 SIPE_DEBUG_INFO("sipe_http_request_response_unauthorized: expected authentication scheme %s not found",
302 if (conn_public
->cached_authorization
) {
304 * The "Basic" scheme doesn't have any state.
306 * If we enter here then we have already tried "Basic"
307 * authentication once for this request and it was
308 * rejected by the server. As all future requests will
309 * also be rejected, we need to abort here in order to
310 * prevent an endless request/401/request/... loop.
312 SIPE_DEBUG_INFO("sipe_http_request_response_unauthorized: Basic authentication has failed for host '%s', please check user name and password!",
318 #if defined(HAVE_GSSAPI_GSSAPI_H) || defined(HAVE_SSPI)
319 #define DEBUG_STRING ", NTLM and Negotiate"
320 /* Use "Negotiate" unless the user requested "NTLM" */
321 if (sipe_private
->authentication_type
!= SIPE_AUTHENTICATION_TYPE_NTLM
)
322 header
= sipmsg_find_auth_header(msg
, "Negotiate");
324 type
= SIPE_AUTHENTICATION_TYPE_NEGOTIATE
;
327 #define DEBUG_STRING " and NTLM"
328 (void) sipe_private
; /* keep compiler happy */
331 header
= sipmsg_find_auth_header(msg
, "NTLM");
332 type
= SIPE_AUTHENTICATION_TYPE_NTLM
;
335 /* only fall back to "Basic" after everything else fails */
337 header
= sipmsg_find_auth_header(msg
, "Basic");
338 type
= SIPE_AUTHENTICATION_TYPE_BASIC
;
343 if (!conn_public
->context
) {
344 gboolean valid
= req
->flags
& SIPE_HTTP_REQUEST_FLAG_AUTHDATA
;
345 conn_public
->context
= sip_sec_create_context(type
,
346 !valid
, /* Single Sign-On flag */
347 TRUE
, /* connection-based for HTTP */
348 valid
? req
->user
: NULL
,
349 valid
? req
->password
: NULL
);
352 if (conn_public
->context
) {
353 gchar
**parts
= g_strsplit(header
, " ", 0);
354 gchar
*spn
= g_strdup_printf("HTTP/%s", conn_public
->host
);
356 const gchar
*token_in
= parts
[1];
358 SIPE_DEBUG_INFO("sipe_http_request_response_unauthorized: init context target '%s' token '%s'",
359 spn
, token_in
? token_in
: "<NULL>");
362 * If we receive a NULL token during the handshake
363 * then the authentication scheme has failed.
365 if ((req
->flags
& SIPE_HTTP_REQUEST_FLAG_HANDSHAKE
) &&
367 SIPE_DEBUG_INFO_NOFORMAT("sipe_http_request_response_unauthorized: authentication failed, throwing away context");
368 sipe_http_request_drop_context(conn_public
);
370 } else if (sip_sec_init_context_step(conn_public
->context
,
376 /* handshake has started */
377 req
->flags
|= SIPE_HTTP_REQUEST_FLAG_HANDSHAKE
;
379 /* generate authorization header */
380 req
->authorization
= g_strdup_printf("Authorization: %s %s\r\n",
381 sip_sec_context_name(conn_public
->context
),
382 token_out
? token_out
: "");
386 * authorization never changes for Basic
387 * authentication scheme, so we can keep it.
389 if (type
== SIPE_AUTHENTICATION_TYPE_BASIC
) {
390 g_free(conn_public
->cached_authorization
);
391 conn_public
->cached_authorization
= g_strdup(req
->authorization
);
395 * Keep the request in the queue. As it is at
396 * the head it will be pulled automatically
397 * by the transport layer after returning.
402 SIPE_DEBUG_INFO_NOFORMAT("sipe_http_request_response_unauthorized: security context init step failed, throwing away context");
403 sipe_http_request_drop_context(conn_public
);
409 SIPE_DEBUG_INFO_NOFORMAT("sipe_http_request_response_unauthorized: security context creation failed");
411 SIPE_DEBUG_INFO_NOFORMAT("sipe_http_request_response_unauthorized: only Basic" DEBUG_STRING
" authentication schemes are supported");
416 static void sipe_http_request_response_callback(struct sipe_core_private
*sipe_private
,
417 struct sipe_http_request
*req
,
420 sipe_http_request_finalize_negotiate(req
, msg
);
422 /* Set-Cookie: RMID=732423sdfs73242; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.example.net */
427 /* extract all cookies from header */
428 while ((hdr
= sipmsg_find_header_instance(msg
,
430 instance
++)) != NULL
) {
431 gchar
**parts
, **current
;
435 current
= parts
= g_strsplit(hdr
, ";", 0);
436 while ((part
= *current
++) != NULL
) {
437 /* strip these parts from cookie */
438 if (!(strstr(part
, "path=") ||
439 strstr(part
, "domain=") ||
440 strstr(part
, "expires=") ||
441 strstr(part
, "secure"))) {
444 g_strconcat(new, ";", part
, NULL
) :
451 g_hash_table_insert(req
->session
->cookie_jar
,
454 SIPE_DEBUG_INFO("sipe_http_request_response_callback: cookie: %s", new);
460 /* Callback: success */
461 (*req
->cb
)(sipe_private
,
467 /* remove completed request */
468 sipe_http_request_cancel(req
);
471 void sipe_http_request_response(struct sipe_http_connection_public
*conn_public
,
474 struct sipe_core_private
*sipe_private
= conn_public
->sipe_private
;
475 struct sipe_http_request
*req
= conn_public
->pending_requests
->data
;
478 if ((req
->flags
& SIPE_HTTP_REQUEST_FLAG_REDIRECT
) &&
479 (msg
->response
>= SIPE_HTTP_STATUS_REDIRECTION
) &&
480 (msg
->response
< SIPE_HTTP_STATUS_CLIENT_ERROR
)) {
481 failed
= sipe_http_request_response_redirection(sipe_private
,
485 } else if (msg
->response
== SIPE_HTTP_STATUS_CLIENT_UNAUTHORIZED
) {
486 failed
= sipe_http_request_response_unauthorized(sipe_private
,
491 /* On some errors throw away the security context */
492 if (((msg
->response
== SIPE_HTTP_STATUS_CLIENT_FORBIDDEN
) ||
493 (msg
->response
== SIPE_HTTP_STATUS_CLIENT_PROXY_AUTH
) ||
494 (msg
->response
>= SIPE_HTTP_STATUS_SERVER_ERROR
)) &&
495 conn_public
->context
) {
496 SIPE_DEBUG_INFO("sipe_http_request_response: response was %d, throwing away security context",
498 sipe_http_request_drop_context(conn_public
);
501 /* All other cases are passed on to the user */
502 sipe_http_request_response_callback(sipe_private
, req
, msg
);
504 /* req is no longer valid */
509 /* Callback: request failed */
510 (*req
->cb
)(sipe_private
,
511 SIPE_HTTP_STATUS_FAILED
,
516 /* remove failed request */
517 sipe_http_request_cancel(req
);
521 void sipe_http_request_shutdown(struct sipe_http_connection_public
*conn_public
,
524 if (conn_public
->pending_requests
) {
525 GSList
*entry
= conn_public
->pending_requests
;
526 guint status
= abort
?
527 SIPE_HTTP_STATUS_ABORTED
:
528 SIPE_HTTP_STATUS_FAILED
;
529 gboolean warn
= conn_public
->connected
&& !abort
;
531 struct sipe_http_request
*req
= entry
->data
;
534 SIPE_DEBUG_ERROR("sipe_http_request_shutdown: pending request at shutdown: could indicate missing _ready() call on request. Debugging information:\n"
542 req
->body
? "POST" : "GET");
545 sipe_http_request_free(conn_public
->sipe_private
,
550 g_slist_free(conn_public
->pending_requests
);
551 conn_public
->pending_requests
= NULL
;
554 if (conn_public
->context
) {
555 g_free(conn_public
->cached_authorization
);
556 conn_public
->cached_authorization
= NULL
;
557 sip_sec_destroy_context(conn_public
->context
);
558 conn_public
->context
= NULL
;
562 struct sipe_http_request
*sipe_http_request_new(struct sipe_core_private
*sipe_private
,
563 const struct sipe_http_parsed_uri
*parsed_uri
,
564 const gchar
*headers
,
566 const gchar
*content_type
,
567 sipe_http_response_callback
*callback
,
568 gpointer callback_data
)
570 struct sipe_http_request
*req
;
573 if (sipe_http_shutting_down(sipe_private
)) {
574 SIPE_DEBUG_ERROR("sipe_http_request_new: new HTTP request during shutdown: THIS SHOULD NOT HAPPEN! Debugging information:\n"
583 headers
? headers
: "<NONE>",
584 body
? body
: "<EMPTY>");
588 req
= g_new0(struct sipe_http_request
, 1);
591 req
->cb_data
= callback_data
;
593 req
->headers
= g_strdup(headers
);
595 req
->body
= g_strdup(body
);
596 req
->content_type
= g_strdup(content_type
);
599 /* default authentication */
600 if (!SIPE_CORE_PRIVATE_FLAG_IS(SSO
))
601 sipe_http_request_authentication(req
,
602 sipe_private
->authuser
,
603 sipe_private
->password
);
605 sipe_http_request_enqueue(sipe_private
, req
, parsed_uri
);
610 void sipe_http_request_ready(struct sipe_http_request
*request
)
612 struct sipe_http_connection_public
*conn_public
= request
->connection
;
614 /* pass first request on already opened connection through directly */
615 if ((request
->flags
& SIPE_HTTP_REQUEST_FLAG_FIRST
) &&
616 conn_public
->connected
)
617 sipe_http_request_send(conn_public
);
620 struct sipe_http_session
*sipe_http_session_start(void)
622 struct sipe_http_session
*session
= g_new0(struct sipe_http_session
, 1);
623 session
->cookie_jar
= g_hash_table_new_full(g_str_hash
,
630 void sipe_http_session_close(struct sipe_http_session
*session
)
633 g_hash_table_destroy(session
->cookie_jar
);
638 void sipe_http_request_cancel(struct sipe_http_request
*request
)
640 struct sipe_http_connection_public
*conn_public
= request
->connection
;
641 conn_public
->pending_requests
= g_slist_remove(conn_public
->pending_requests
,
644 /* cancelled by requester, don't use callback */
647 sipe_http_request_free(conn_public
->sipe_private
,
649 SIPE_HTTP_STATUS_CANCELLED
);
652 void sipe_http_request_session(struct sipe_http_request
*request
,
653 struct sipe_http_session
*session
)
655 request
->session
= session
;
658 void sipe_http_request_allow_redirect(struct sipe_http_request
*request
)
660 request
->flags
|= SIPE_HTTP_REQUEST_FLAG_REDIRECT
;
663 void sipe_http_request_authentication(struct sipe_http_request
*request
,
665 const gchar
*password
)
667 request
->flags
|= SIPE_HTTP_REQUEST_FLAG_AUTHDATA
;
668 request
->user
= user
;
669 request
->password
= password
;