(redirect_handler): PROPFIND is defined to be "safe and
[platform/upstream/libsoup.git] / libsoup / soup-session.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * soup-session.c
4  *
5  * Copyright (C) 2000-2003, Ximian, Inc.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <unistd.h>
13 #include <string.h>
14 #include <stdlib.h>
15
16 #include "soup-auth.h"
17 #include "soup-auth-basic.h"
18 #include "soup-auth-digest.h"
19 #include "soup-auth-manager.h"
20 #include "soup-auth-manager-ntlm.h"
21 #include "soup-connection.h"
22 #include "soup-marshal.h"
23 #include "soup-message-private.h"
24 #include "soup-message-queue.h"
25 #include "soup-session.h"
26 #include "soup-session-private.h"
27 #include "soup-socket.h"
28 #include "soup-ssl.h"
29 #include "soup-uri.h"
30
31 /**
32  * SECTION:soup-session
33  * @short_description: Soup session state object
34  *
35  * #SoupSession is the object that controls client-side HTTP. A
36  * #SoupSession encapsulates all of the state that libsoup is keeping
37  * on behalf of your program; cached HTTP connections, authentication
38  * information, etc.
39  *
40  * Most applications will only need a single #SoupSession; the primary
41  * reason you might need multiple sessions is if you need to have
42  * multiple independent authentication contexts. (Eg, you are
43  * connecting to a server and authenticating as two different users at
44  * different times; the easiest way to ensure that each #SoupMessage
45  * is sent with the authentication information you intended is to use
46  * one session for the first user, and a second session for the other
47  * user.)
48  *
49  * #SoupSession itself is an abstract class, with two subclasses. If
50  * you are using the glib main loop, you will generally want to use
51  * #SoupSessionAsync, which uses non-blocking I/O and callbacks. On
52  * the other hand, if your application is threaded and you want to do
53  * synchronous I/O in a separate thread from the UI, use
54  * #SoupSessionSync.
55  **/
56
57 typedef struct {
58         SoupURI    *root_uri;
59
60         GSList     *connections;      /* CONTAINS: SoupConnection */
61         guint       num_conns;
62
63         GHashTable *auth_realms;      /* path -> scheme:realm */
64         GHashTable *auths;            /* scheme:realm -> SoupAuth */
65 } SoupSessionHost;
66
67 typedef struct {
68         SoupURI *proxy_uri;
69         SoupAuth *proxy_auth;
70
71         guint max_conns, max_conns_per_host;
72
73         char *ssl_ca_file;
74         SoupSSLCredentials *ssl_creds;
75
76         SoupMessageQueue *queue;
77
78         char *user_agent;
79
80         SoupAuthManager *auth_manager;
81         SoupAuthManagerNTLM *ntlm_manager;
82
83         GHashTable *hosts; /* SoupURI -> SoupSessionHost */
84         GHashTable *conns; /* SoupConnection -> SoupSessionHost */
85         guint num_conns;
86
87         /* Must hold the host_lock before potentially creating a
88          * new SoupSessionHost, or adding/removing a connection.
89          * Must not emit signals or destroy objects while holding it.
90          */
91         GMutex *host_lock;
92
93         GMainContext *async_context;
94
95         /* Holds the timeout value for the connection, when
96            no response is received.
97         */
98         guint timeout;
99 } SoupSessionPrivate;
100 #define SOUP_SESSION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_SESSION, SoupSessionPrivate))
101
102 static void     free_host      (SoupSessionHost *host);
103
104 static void queue_message   (SoupSession *session, SoupMessage *msg,
105                              SoupSessionCallback callback, gpointer user_data);
106 static void requeue_message (SoupSession *session, SoupMessage *msg);
107 static void cancel_message  (SoupSession *session, SoupMessage *msg,
108                              guint status_code);
109
110 /* temporary until we fix this to index hosts by SoupAddress */
111 extern guint     soup_uri_host_hash  (gconstpointer  key);
112 extern gboolean  soup_uri_host_equal (gconstpointer  v1,
113                                       gconstpointer  v2);
114 extern SoupURI  *soup_uri_copy_root  (SoupURI *uri);
115
116 #define SOUP_SESSION_MAX_CONNS_DEFAULT 10
117 #define SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT 2
118
119 #define SOUP_SESSION_USER_AGENT_BASE "libsoup/" PACKAGE_VERSION
120
121 G_DEFINE_TYPE (SoupSession, soup_session, G_TYPE_OBJECT)
122
123 enum {
124         REQUEST_STARTED,
125         AUTHENTICATE,
126         LAST_SIGNAL
127 };
128
129 static guint signals[LAST_SIGNAL] = { 0 };
130
131 enum {
132         PROP_0,
133
134         PROP_PROXY_URI,
135         PROP_MAX_CONNS,
136         PROP_MAX_CONNS_PER_HOST,
137         PROP_USE_NTLM,
138         PROP_SSL_CA_FILE,
139         PROP_ASYNC_CONTEXT,
140         PROP_TIMEOUT,
141         PROP_USER_AGENT,
142
143         LAST_PROP
144 };
145
146 static void set_property (GObject *object, guint prop_id,
147                           const GValue *value, GParamSpec *pspec);
148 static void get_property (GObject *object, guint prop_id,
149                           GValue *value, GParamSpec *pspec);
150
151 static void
152 soup_session_init (SoupSession *session)
153 {
154         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
155
156         priv->queue = soup_message_queue_new ();
157
158         priv->host_lock = g_mutex_new ();
159         priv->hosts = g_hash_table_new (soup_uri_host_hash,
160                                         soup_uri_host_equal);
161         priv->conns = g_hash_table_new (NULL, NULL);
162
163         priv->max_conns = SOUP_SESSION_MAX_CONNS_DEFAULT;
164         priv->max_conns_per_host = SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT;
165
166         priv->timeout = 0;
167
168         priv->auth_manager = soup_auth_manager_new (session);
169         soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_BASIC);
170         soup_auth_manager_add_type (priv->auth_manager, SOUP_TYPE_AUTH_DIGEST);
171 }
172
173 static gboolean
174 foreach_free_host (gpointer key, gpointer host, gpointer data)
175 {
176         free_host (host);
177         return TRUE;
178 }
179
180 static void
181 cleanup_hosts (SoupSessionPrivate *priv)
182 {
183         GHashTable *old_hosts;
184
185         g_mutex_lock (priv->host_lock);
186         old_hosts = priv->hosts;
187         priv->hosts = g_hash_table_new (soup_uri_host_hash,
188                                         soup_uri_host_equal);
189         g_mutex_unlock (priv->host_lock);
190
191         g_hash_table_foreach_remove (old_hosts, foreach_free_host, NULL);
192         g_hash_table_destroy (old_hosts);
193 }
194
195 static void
196 dispose (GObject *object)
197 {
198         SoupSession *session = SOUP_SESSION (object);
199         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
200
201         soup_session_abort (session);
202         cleanup_hosts (priv);
203
204         G_OBJECT_CLASS (soup_session_parent_class)->dispose (object);
205 }
206
207 static void
208 finalize (GObject *object)
209 {
210         SoupSession *session = SOUP_SESSION (object);
211         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
212
213         soup_message_queue_destroy (priv->queue);
214
215         g_mutex_free (priv->host_lock);
216         g_hash_table_destroy (priv->hosts);
217         g_hash_table_destroy (priv->conns);
218
219         g_free (priv->user_agent);
220
221         soup_auth_manager_free (priv->auth_manager);
222         if (priv->ntlm_manager)
223                 soup_auth_manager_ntlm_free (priv->ntlm_manager);
224
225         if (priv->proxy_uri)
226                 soup_uri_free (priv->proxy_uri);
227
228         if (priv->ssl_creds)
229                 soup_ssl_free_client_credentials (priv->ssl_creds);
230
231         if (priv->async_context)
232                 g_main_context_unref (priv->async_context);
233
234         G_OBJECT_CLASS (soup_session_parent_class)->finalize (object);
235 }
236
237 static void
238 soup_session_class_init (SoupSessionClass *session_class)
239 {
240         GObjectClass *object_class = G_OBJECT_CLASS (session_class);
241
242         g_type_class_add_private (session_class, sizeof (SoupSessionPrivate));
243
244         /* virtual method definition */
245         session_class->queue_message = queue_message;
246         session_class->requeue_message = requeue_message;
247         session_class->cancel_message = cancel_message;
248
249         /* virtual method override */
250         object_class->dispose = dispose;
251         object_class->finalize = finalize;
252         object_class->set_property = set_property;
253         object_class->get_property = get_property;
254
255         /* signals */
256
257         /**
258          * SoupSession::request-started:
259          * @session: the session
260          * @msg: the request being sent
261          * @socket: the socket the request is being sent on
262          *
263          * Emitted just before a request is sent.
264          **/
265         signals[REQUEST_STARTED] =
266                 g_signal_new ("request-started",
267                               G_OBJECT_CLASS_TYPE (object_class),
268                               G_SIGNAL_RUN_FIRST,
269                               G_STRUCT_OFFSET (SoupSessionClass, request_started),
270                               NULL, NULL,
271                               soup_marshal_NONE__OBJECT_OBJECT,
272                               G_TYPE_NONE, 2,
273                               SOUP_TYPE_MESSAGE,
274                               SOUP_TYPE_SOCKET);
275
276         /**
277          * SoupSession::authenticate:
278          * @session: the session
279          * @msg: the #SoupMessage being sent
280          * @auth: the #SoupAuth to authenticate
281          * @retrying: %TRUE if this is the second (or later) attempt
282          *
283          * Emitted when the session requires authentication. If
284          * credentials are available call soup_auth_authenticate() on
285          * @auth. If these credentials fail, the signal will be
286          * emitted again, with @retrying set to %TRUE, which will
287          * continue until you return without calling
288          * soup_auth_authenticate() on @auth.
289          *
290          * Note that this may be emitted before @msg's body has been
291          * fully read.
292          *
293          * If you call soup_session_pause_message() on @msg before
294          * returning, then you can authenticate @auth asynchronously
295          * (as long as you g_object_ref() it to make sure it doesn't
296          * get destroyed), and then unpause @msg when you are ready
297          * for it to continue.
298          **/
299         signals[AUTHENTICATE] =
300                 g_signal_new ("authenticate",
301                               G_OBJECT_CLASS_TYPE (object_class),
302                               G_SIGNAL_RUN_FIRST,
303                               G_STRUCT_OFFSET (SoupSessionClass, authenticate),
304                               NULL, NULL,
305                               soup_marshal_NONE__OBJECT_OBJECT_BOOLEAN,
306                               G_TYPE_NONE, 3,
307                               SOUP_TYPE_MESSAGE,
308                               SOUP_TYPE_AUTH,
309                               G_TYPE_BOOLEAN);
310
311         /* properties */
312         g_object_class_install_property (
313                 object_class, PROP_PROXY_URI,
314                 g_param_spec_boxed (SOUP_SESSION_PROXY_URI,
315                                     "Proxy URI",
316                                     "The HTTP Proxy to use for this session",
317                                     SOUP_TYPE_URI,
318                                     G_PARAM_READWRITE));
319         g_object_class_install_property (
320                 object_class, PROP_MAX_CONNS,
321                 g_param_spec_int (SOUP_SESSION_MAX_CONNS,
322                                   "Max Connection Count",
323                                   "The maximum number of connections that the session can open at once",
324                                   1,
325                                   G_MAXINT,
326                                   SOUP_SESSION_MAX_CONNS_DEFAULT,
327                                   G_PARAM_READWRITE));
328         g_object_class_install_property (
329                 object_class, PROP_MAX_CONNS_PER_HOST,
330                 g_param_spec_int (SOUP_SESSION_MAX_CONNS_PER_HOST,
331                                   "Max Per-Host Connection Count",
332                                   "The maximum number of connections that the session can open at once to a given host",
333                                   1,
334                                   G_MAXINT,
335                                   SOUP_SESSION_MAX_CONNS_PER_HOST_DEFAULT,
336                                   G_PARAM_READWRITE));
337         g_object_class_install_property (
338                 object_class, PROP_USE_NTLM,
339                 g_param_spec_boolean (SOUP_SESSION_USE_NTLM,
340                                       "Use NTLM",
341                                       "Whether or not to use NTLM authentication",
342                                       FALSE,
343                                       G_PARAM_READWRITE));
344         g_object_class_install_property (
345                 object_class, PROP_SSL_CA_FILE,
346                 g_param_spec_string (SOUP_SESSION_SSL_CA_FILE,
347                                      "SSL CA file",
348                                      "File containing SSL CA certificates",
349                                      NULL,
350                                      G_PARAM_READWRITE));
351         g_object_class_install_property (
352                 object_class, PROP_ASYNC_CONTEXT,
353                 g_param_spec_pointer (SOUP_SESSION_ASYNC_CONTEXT,
354                                       "Async GMainContext",
355                                       "The GMainContext to dispatch async I/O in",
356                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
357         g_object_class_install_property (
358                 object_class, PROP_TIMEOUT,
359                 g_param_spec_uint (SOUP_SESSION_TIMEOUT,
360                                    "Timeout value",
361                                    "Value in seconds to timeout a blocking I/O",
362                                    0, G_MAXUINT, 0,
363                                    G_PARAM_READWRITE));
364
365         /**
366          * SoupSession:user-agent:
367          *
368          * If non-%NULL, the value to use for the "User-Agent" header
369          * on #SoupMessage<!-- -->s sent from this session.
370          *
371          * RFC 2616 says: "The User-Agent request-header field
372          * contains information about the user agent originating the
373          * request. This is for statistical purposes, the tracing of
374          * protocol violations, and automated recognition of user
375          * agents for the sake of tailoring responses to avoid
376          * particular user agent limitations. User agents SHOULD
377          * include this field with requests."
378          *
379          * The User-Agent header contains a list of one or more
380          * product tokens, separated by whitespace, with the most
381          * significant product token coming first. The tokens must be
382          * brief, ASCII, and mostly alphanumeric (although "-", "_",
383          * and "." are also allowed), and may optionally include a "/"
384          * followed by a version string. You may also put comments,
385          * enclosed in parentheses, between or after the tokens.
386          *
387          * If you set a %user_agent property that has trailing
388          * whitespace, #SoupSession will append its own product token
389          * (eg, "<literal>libsoup/2.3.2</literal>") to the end of the
390          * header for you.
391          **/
392         g_object_class_install_property (
393                 object_class, PROP_USER_AGENT,
394                 g_param_spec_string (SOUP_SESSION_USER_AGENT,
395                                      "User-Agent string",
396                                      "User-Agent string",
397                                      NULL,
398                                      G_PARAM_READWRITE));
399 }
400
401 static gboolean
402 safe_uri_equal (SoupURI *a, SoupURI *b)
403 {
404         if (!a && !b)
405                 return TRUE;
406
407         if ((a && !b) || (b && !a))
408                 return FALSE;
409
410         return soup_uri_equal (a, b);
411 }
412
413 static gboolean
414 safe_str_equal (const char *a, const char *b)
415 {
416         if (!a && !b)
417                 return TRUE;
418
419         if ((a && !b) || (b && !a))
420                 return FALSE;
421
422         return strcmp (a, b) == 0;
423 }
424
425 static void
426 set_property (GObject *object, guint prop_id,
427               const GValue *value, GParamSpec *pspec)
428 {
429         SoupSession *session = SOUP_SESSION (object);
430         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
431         SoupURI *uri;
432         gboolean need_abort = FALSE;
433         gboolean ca_file_changed = FALSE;
434         const char *new_ca_file, *user_agent;
435
436         switch (prop_id) {
437         case PROP_PROXY_URI:
438                 uri = g_value_get_boxed (value);
439
440                 if (!safe_uri_equal (priv->proxy_uri, uri))
441                         need_abort = TRUE;
442
443                 if (priv->proxy_uri)
444                         soup_uri_free (priv->proxy_uri);
445
446                 priv->proxy_uri = uri ? soup_uri_copy (uri) : NULL;
447
448                 if (need_abort) {
449                         soup_session_abort (session);
450                         cleanup_hosts (priv);
451                 }
452
453                 break;
454         case PROP_MAX_CONNS:
455                 priv->max_conns = g_value_get_int (value);
456                 break;
457         case PROP_MAX_CONNS_PER_HOST:
458                 priv->max_conns_per_host = g_value_get_int (value);
459                 break;
460         case PROP_USE_NTLM:
461                 if (g_value_get_boolean (value)) {
462                         if (!priv->ntlm_manager)
463                                 priv->ntlm_manager = soup_auth_manager_ntlm_new (session);
464                 } else {
465                         if (priv->ntlm_manager) {
466                                 soup_auth_manager_ntlm_free (priv->ntlm_manager);
467                                 priv->ntlm_manager = NULL;
468                         }
469                 }
470                 break;
471         case PROP_SSL_CA_FILE:
472                 new_ca_file = g_value_get_string (value);
473
474                 if (!safe_str_equal (priv->ssl_ca_file, new_ca_file))
475                         ca_file_changed = TRUE;
476
477                 g_free (priv->ssl_ca_file);
478                 priv->ssl_ca_file = g_strdup (new_ca_file);
479
480                 if (ca_file_changed) {
481                         if (priv->ssl_creds) {
482                                 soup_ssl_free_client_credentials (priv->ssl_creds);
483                                 priv->ssl_creds = NULL;
484                         }
485
486                         cleanup_hosts (priv);
487                 }
488
489                 break;
490         case PROP_ASYNC_CONTEXT:
491                 priv->async_context = g_value_get_pointer (value);
492                 if (priv->async_context)
493                         g_main_context_ref (priv->async_context);
494                 break;
495         case PROP_TIMEOUT:
496                 priv->timeout = g_value_get_uint (value);
497                 break;
498         case PROP_USER_AGENT:
499                 g_free (priv->user_agent);
500                 user_agent = g_value_get_string (value);
501                 if (!user_agent)
502                         priv->user_agent = NULL;
503                 else if (!*user_agent) {
504                         priv->user_agent =
505                                 g_strdup (SOUP_SESSION_USER_AGENT_BASE);
506                 } else if (g_str_has_suffix (user_agent, " ")) {
507                         priv->user_agent =
508                                 g_strdup_printf ("%s%s", user_agent,
509                                                  SOUP_SESSION_USER_AGENT_BASE);
510                 } else
511                         priv->user_agent = g_strdup (user_agent);
512                 break;
513         default:
514                 break;
515         }
516 }
517
518 static void
519 get_property (GObject *object, guint prop_id,
520               GValue *value, GParamSpec *pspec)
521 {
522         SoupSession *session = SOUP_SESSION (object);
523         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
524
525         switch (prop_id) {
526         case PROP_PROXY_URI:
527                 g_value_set_boxed (value, priv->proxy_uri);
528                 break;
529         case PROP_MAX_CONNS:
530                 g_value_set_int (value, priv->max_conns);
531                 break;
532         case PROP_MAX_CONNS_PER_HOST:
533                 g_value_set_int (value, priv->max_conns_per_host);
534                 break;
535         case PROP_USE_NTLM:
536                 g_value_set_boolean (value, priv->ntlm_manager != NULL);
537                 break;
538         case PROP_SSL_CA_FILE:
539                 g_value_set_string (value, priv->ssl_ca_file);
540                 break;
541         case PROP_ASYNC_CONTEXT:
542                 g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
543                 break;
544         case PROP_TIMEOUT:
545                 g_value_set_uint (value, priv->timeout);
546                 break;
547         case PROP_USER_AGENT:
548                 g_value_set_string (value, priv->user_agent);
549                 break;
550         default:
551                 break;
552         }
553 }
554
555
556 /**
557  * soup_session_get_async_context:
558  * @session: a #SoupSession
559  *
560  * Gets @session's async_context. This does not add a ref to the
561  * context, so you will need to ref it yourself if you want it to
562  * outlive its session.
563  *
564  * Return value: @session's #GMainContext, which may be %NULL
565  **/
566 GMainContext *
567 soup_session_get_async_context (SoupSession *session)
568 {
569         SoupSessionPrivate *priv;
570
571         g_return_val_if_fail (SOUP_IS_SESSION (session), NULL);
572         priv = SOUP_SESSION_GET_PRIVATE (session);
573
574         return priv->async_context;
575 }
576
577 /* Hosts */
578
579 static SoupSessionHost *
580 soup_session_host_new (SoupSession *session, SoupURI *source_uri)
581 {
582         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
583         SoupSessionHost *host;
584
585         host = g_slice_new0 (SoupSessionHost);
586         host->root_uri = soup_uri_copy_root (source_uri);
587
588         if (host->root_uri->scheme == SOUP_URI_SCHEME_HTTPS &&
589             !priv->ssl_creds) {
590                 priv->ssl_creds =
591                         soup_ssl_get_client_credentials (priv->ssl_ca_file);
592         }
593
594         return host;
595 }
596
597 /* Note: get_host_for_message doesn't lock the host_lock. The caller
598  * must do it itself if there's a chance the host doesn't already
599  * exist.
600  */
601 static SoupSessionHost *
602 get_host_for_message (SoupSession *session, SoupMessage *msg)
603 {
604         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
605         SoupSessionHost *host;
606         SoupURI *source = soup_message_get_uri (msg);
607
608         host = g_hash_table_lookup (priv->hosts, source);
609         if (host)
610                 return host;
611
612         host = soup_session_host_new (session, source);
613         g_hash_table_insert (priv->hosts, host->root_uri, host);
614
615         return host;
616 }
617
618 static void
619 free_host (SoupSessionHost *host)
620 {
621         while (host->connections) {
622                 SoupConnection *conn = host->connections->data;
623
624                 host->connections = g_slist_remove (host->connections, conn);
625                 soup_connection_disconnect (conn);
626         }
627
628         soup_uri_free (host->root_uri);
629         g_slice_free (SoupSessionHost, host);
630 }       
631
632 void
633 soup_session_emit_authenticate (SoupSession *session, SoupMessage *msg,
634                                 SoupAuth *auth, gboolean retrying)
635 {
636         g_signal_emit (session, signals[AUTHENTICATE], 0, msg, auth, retrying);
637 }
638
639 static void
640 redirect_handler (SoupMessage *msg, gpointer user_data)
641 {
642         SoupSession *session = user_data;
643         const char *new_loc;
644         SoupURI *new_uri;
645
646         new_loc = soup_message_headers_get (msg->response_headers, "Location");
647         g_return_if_fail (new_loc != NULL);
648
649         if (msg->status_code == SOUP_STATUS_MOVED_PERMANENTLY ||
650             msg->status_code == SOUP_STATUS_TEMPORARY_REDIRECT) {
651                 /* Don't redirect non-safe methods */
652                 if (msg->method != SOUP_METHOD_GET &&
653                     msg->method != SOUP_METHOD_HEAD &&
654                     msg->method != SOUP_METHOD_OPTIONS &&
655                     msg->method != SOUP_METHOD_PROPFIND)
656                         return;
657         } else if (msg->status_code == SOUP_STATUS_SEE_OTHER ||
658                    msg->status_code == SOUP_STATUS_FOUND) {
659                 /* Redirect using a GET */
660                 g_object_set (msg,
661                               SOUP_MESSAGE_METHOD, SOUP_METHOD_GET,
662                               NULL);
663                 soup_message_set_request (msg, NULL,
664                                           SOUP_MEMORY_STATIC, NULL, 0);
665                 soup_message_headers_set_encoding (msg->request_headers,
666                                                    SOUP_ENCODING_NONE);
667         } else {
668                 /* Three possibilities:
669                  *
670                  *   1) This was a non-3xx response that happened to
671                  *      have a "Location" header
672                  *   2) It's a non-redirecty 3xx response (300, 304,
673                  *      305, 306)
674                  *   3) It's some newly-defined 3xx response (308+)
675                  *
676                  * We ignore all of these cases. In the first two,
677                  * redirecting would be explicitly wrong, and in the
678                  * last case, we have no clue if the 3xx response is
679                  * supposed to be redirecty or non-redirecty. Plus,
680                  * 2616 says unrecognized status codes should be
681                  * treated as the equivalent to the x00 code, and we
682                  * don't redirect on 300, so therefore we shouldn't
683                  * redirect on 308+ either.
684                  */
685                 return;
686         }
687
688         /* Location is supposed to be an absolute URI, but some sites
689          * are lame, so we use soup_uri_new_with_base().
690          */
691         new_uri = soup_uri_new_with_base (soup_message_get_uri (msg), new_loc);
692         if (!new_uri) {
693                 soup_message_set_status_full (msg,
694                                               SOUP_STATUS_MALFORMED,
695                                               "Invalid Redirect URL");
696                 return;
697         }
698
699         soup_message_set_uri (msg, new_uri);
700         soup_uri_free (new_uri);
701
702         soup_session_requeue_message (session, msg);
703 }
704
705 static void
706 connection_started_request (SoupConnection *conn, SoupMessage *msg,
707                             gpointer data)
708 {
709         SoupSession *session = data;
710         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
711
712         if (priv->user_agent) {
713                 soup_message_headers_replace (msg->request_headers,
714                                               "User-Agent", priv->user_agent);
715         }
716
717         g_signal_emit (session, signals[REQUEST_STARTED], 0,
718                        msg, soup_connection_get_socket (conn));
719 }
720
721 static void
722 find_oldest_connection (gpointer key, gpointer host, gpointer data)
723 {
724         SoupConnection *conn = key, **oldest = data;
725
726         /* Don't prune a connection that is currently in use, or
727          * hasn't been used yet.
728          */
729         if (soup_connection_is_in_use (conn) ||
730             soup_connection_last_used (conn) == 0)
731                 return;
732
733         if (!*oldest || (soup_connection_last_used (conn) <
734                          soup_connection_last_used (*oldest)))
735                 *oldest = conn;
736 }
737
738 /**
739  * soup_session_try_prune_connection:
740  * @session: a #SoupSession
741  *
742  * Finds the least-recently-used idle connection in @session and closes
743  * it.
744  *
745  * Return value: %TRUE if a connection was closed, %FALSE if there are
746  * no idle connections.
747  **/
748 gboolean
749 soup_session_try_prune_connection (SoupSession *session)
750 {
751         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
752         SoupConnection *oldest = NULL;
753
754         g_mutex_lock (priv->host_lock);
755         g_hash_table_foreach (priv->conns, find_oldest_connection,
756                               &oldest);
757         if (oldest) {
758                 /* Ref the connection before unlocking the mutex in
759                  * case someone else tries to prune it too.
760                  */
761                 g_object_ref (oldest);
762                 g_mutex_unlock (priv->host_lock);
763                 soup_connection_disconnect (oldest);
764                 g_object_unref (oldest);
765                 return TRUE;
766         } else {
767                 g_mutex_unlock (priv->host_lock);
768                 return FALSE;
769         }
770 }
771
772 static void
773 connection_disconnected (SoupConnection *conn, gpointer user_data)
774 {
775         SoupSession *session = user_data;
776         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
777         SoupSessionHost *host;
778
779         g_mutex_lock (priv->host_lock);
780
781         host = g_hash_table_lookup (priv->conns, conn);
782         if (host) {
783                 g_hash_table_remove (priv->conns, conn);
784                 host->connections = g_slist_remove (host->connections, conn);
785                 host->num_conns--;
786         }
787
788         g_signal_handlers_disconnect_by_func (conn, connection_disconnected, session);
789         priv->num_conns--;
790
791         g_mutex_unlock (priv->host_lock);
792         g_object_unref (conn);
793 }
794
795 static void
796 connect_result (SoupConnection *conn, guint status, gpointer user_data)
797 {
798         SoupSession *session = user_data;
799         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
800         SoupSessionHost *host;
801         SoupMessageQueueIter iter;
802         SoupMessage *msg;
803
804         g_mutex_lock (priv->host_lock);
805
806         host = g_hash_table_lookup (priv->conns, conn);
807         if (!host) {
808                 g_mutex_unlock (priv->host_lock);
809                 return;
810         }
811
812         if (status == SOUP_STATUS_OK) {
813                 soup_connection_reserve (conn);
814                 host->connections = g_slist_prepend (host->connections, conn);
815                 g_mutex_unlock (priv->host_lock);
816                 return;
817         }
818
819         /* The connection failed. */
820         g_mutex_unlock (priv->host_lock);
821         connection_disconnected (conn, session);
822
823         if (host->connections) {
824                 /* Something went wrong this time, but we have at
825                  * least one open connection to this host. So just
826                  * leave the message in the queue so it can use that
827                  * connection once it's free.
828                  */
829                 return;
830         }
831
832         /* There are two possibilities: either status is
833          * SOUP_STATUS_TRY_AGAIN, in which case the session implementation
834          * will create a new connection (and all we need to do here
835          * is downgrade the message from CONNECTING to QUEUED); or
836          * status is something else, probably CANT_CONNECT or
837          * CANT_RESOLVE or the like, in which case we need to cancel
838          * any messages waiting for this host, since they're out
839          * of luck.
840          */
841         for (msg = soup_message_queue_first (priv->queue, &iter); msg; msg = soup_message_queue_next (priv->queue, &iter)) {
842                 if (get_host_for_message (session, msg) == host) {
843                         if (status == SOUP_STATUS_TRY_AGAIN) {
844                                 if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING)
845                                         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
846                         } else {
847                                 soup_session_cancel_message (session, msg,
848                                                              status);
849                         }
850                 }
851         }
852 }
853
854 /**
855  * soup_session_get_connection:
856  * @session: a #SoupSession
857  * @msg: a #SoupMessage
858  * @try_pruning: on return, whether or not to try pruning a connection
859  * @is_new: on return, %TRUE if the returned connection is new and not
860  * yet connected
861  * 
862  * Tries to find or create a connection for @msg; this is an internal
863  * method for #SoupSession subclasses.
864  *
865  * If there is an idle connection to the relevant host available, then
866  * that connection will be returned (with *@is_new set to %FALSE). The
867  * connection will be marked "reserved", so the caller must call
868  * soup_connection_release() if it ends up not using the connection
869  * right away.
870  *
871  * If there is no idle connection available, but it is possible to
872  * create a new connection, then one will be created and returned,
873  * with *@is_new set to %TRUE. The caller MUST then call
874  * soup_connection_connect_sync() or soup_connection_connect_async()
875  * to connect it. If the connection attempt succeeds, the connection
876  * will be marked "reserved" and added to @session's connection pool
877  * once it connects. If the connection attempt fails, the connection
878  * will be unreffed.
879  *
880  * If no connection is available and a new connection cannot be made,
881  * soup_session_get_connection() will return %NULL. If @session has
882  * the maximum number of open connections open, but does not have the
883  * maximum number of per-host connections open to the relevant host,
884  * then *@try_pruning will be set to %TRUE. In this case, the caller
885  * can call soup_session_try_prune_connection() to close an idle
886  * connection, and then try soup_session_get_connection() again. (If
887  * calling soup_session_try_prune_connection() wouldn't help, then
888  * *@try_pruning is left untouched; it is NOT set to %FALSE.)
889  *
890  * Return value: a #SoupConnection, or %NULL
891  **/
892 SoupConnection *
893 soup_session_get_connection (SoupSession *session, SoupMessage *msg,
894                              gboolean *try_pruning, gboolean *is_new)
895 {
896         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
897         SoupConnection *conn;
898         SoupSessionHost *host;
899         GSList *conns;
900
901         g_mutex_lock (priv->host_lock);
902
903         host = get_host_for_message (session, msg);
904         for (conns = host->connections; conns; conns = conns->next) {
905                 if (!soup_connection_is_in_use (conns->data)) {
906                         soup_connection_reserve (conns->data);
907                         g_mutex_unlock (priv->host_lock);
908                         *is_new = FALSE;
909                         return conns->data;
910                 }
911         }
912
913         if (soup_message_get_io_status (msg) == SOUP_MESSAGE_IO_STATUS_CONNECTING) {
914                 /* We already started a connection for this
915                  * message, so don't start another one.
916                  */
917                 g_mutex_unlock (priv->host_lock);
918                 return NULL;
919         }
920
921         if (host->num_conns >= priv->max_conns_per_host) {
922                 g_mutex_unlock (priv->host_lock);
923                 return NULL;
924         }
925
926         if (priv->num_conns >= priv->max_conns) {
927                 *try_pruning = TRUE;
928                 g_mutex_unlock (priv->host_lock);
929                 return NULL;
930         }
931
932         conn = soup_connection_new (
933                 SOUP_CONNECTION_ORIGIN_URI, host->root_uri,
934                 SOUP_CONNECTION_PROXY_URI, priv->proxy_uri,
935                 SOUP_CONNECTION_SSL_CREDENTIALS, priv->ssl_creds,
936                 SOUP_CONNECTION_ASYNC_CONTEXT, priv->async_context,
937                 SOUP_CONNECTION_TIMEOUT, priv->timeout,
938                 NULL);
939         g_signal_connect (conn, "connect_result",
940                           G_CALLBACK (connect_result),
941                           session);
942         g_signal_connect (conn, "disconnected",
943                           G_CALLBACK (connection_disconnected),
944                           session);
945         g_signal_connect (conn, "request_started",
946                           G_CALLBACK (connection_started_request),
947                           session);
948
949         g_hash_table_insert (priv->conns, conn, host);
950
951         /* We increment the connection counts so it counts against the
952          * totals, but we don't add it to the host's connection list
953          * yet, since it's not ready for use.
954          */
955         priv->num_conns++;
956         host->num_conns++;
957
958         /* Mark the request as connecting, so we don't try to open
959          * another new connection for it while waiting for this one.
960          */
961         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_CONNECTING);
962
963         g_mutex_unlock (priv->host_lock);
964         *is_new = TRUE;
965         return conn;
966 }
967
968 SoupMessageQueue *
969 soup_session_get_queue (SoupSession *session)
970 {
971         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
972
973         return priv->queue;
974 }
975
976 static void
977 message_finished (SoupMessage *msg, gpointer user_data)
978 {
979         SoupSession *session = user_data;
980         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
981
982         if (!SOUP_MESSAGE_IS_STARTING (msg)) {
983                 soup_message_queue_remove_message (priv->queue, msg);
984                 g_signal_handlers_disconnect_by_func (msg, message_finished, session);
985         }
986 }
987
988 static void
989 queue_message (SoupSession *session, SoupMessage *msg,
990                SoupSessionCallback callback, gpointer user_data)
991 {
992         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
993
994         g_signal_connect_after (msg, "finished",
995                                 G_CALLBACK (message_finished), session);
996
997         if (!(soup_message_get_flags (msg) & SOUP_MESSAGE_NO_REDIRECT)) {
998                 soup_message_add_header_handler (
999                         msg, "got_body", "Location",
1000                         G_CALLBACK (redirect_handler), session);
1001         }
1002
1003         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1004         soup_message_queue_append (priv->queue, msg);
1005 }
1006
1007 /**
1008  * SoupSessionCallback:
1009  * @session: the session
1010  * @msg: the message that has finished
1011  * @user_data: the data passed to soup_session_queue_message
1012  *
1013  * Prototype for the callback passed to soup_session_queue_message(),
1014  * qv.
1015  **/
1016
1017 /**
1018  * soup_session_queue_message:
1019  * @session: a #SoupSession
1020  * @msg: the message to queue
1021  * @callback: a #SoupSessionCallback which will be called after the
1022  * message completes or when an unrecoverable error occurs.
1023  * @user_data: a pointer passed to @callback.
1024  * 
1025  * Queues the message @msg for sending. All messages are processed
1026  * while the glib main loop runs. If @msg has been processed before,
1027  * any resources related to the time it was last sent are freed.
1028  *
1029  * Upon message completion, the callback specified in @callback will
1030  * be invoked (in the thread associated with @session's async
1031  * context). If after returning from this callback the message has not
1032  * been requeued, @msg will be unreffed.
1033  */
1034 void
1035 soup_session_queue_message (SoupSession *session, SoupMessage *msg,
1036                             SoupSessionCallback callback, gpointer user_data)
1037 {
1038         g_return_if_fail (SOUP_IS_SESSION (session));
1039         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1040
1041         SOUP_SESSION_GET_CLASS (session)->queue_message (session, msg,
1042                                                          callback, user_data);
1043 }
1044
1045 static void
1046 requeue_message (SoupSession *session, SoupMessage *msg)
1047 {
1048         soup_message_set_io_status (msg, SOUP_MESSAGE_IO_STATUS_QUEUED);
1049 }
1050
1051 /**
1052  * soup_session_requeue_message:
1053  * @session: a #SoupSession
1054  * @msg: the message to requeue
1055  *
1056  * This causes @msg to be placed back on the queue to be attempted
1057  * again.
1058  **/
1059 void
1060 soup_session_requeue_message (SoupSession *session, SoupMessage *msg)
1061 {
1062         g_return_if_fail (SOUP_IS_SESSION (session));
1063         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1064
1065         SOUP_SESSION_GET_CLASS (session)->requeue_message (session, msg);
1066 }
1067
1068
1069 /**
1070  * soup_session_send_message:
1071  * @session: a #SoupSession
1072  * @msg: the message to send
1073  * 
1074  * Synchronously send @msg. This call will not return until the
1075  * transfer is finished successfully or there is an unrecoverable
1076  * error.
1077  *
1078  * @msg is not freed upon return.
1079  *
1080  * Return value: the HTTP status code of the response
1081  */
1082 guint
1083 soup_session_send_message (SoupSession *session, SoupMessage *msg)
1084 {
1085         g_return_val_if_fail (SOUP_IS_SESSION (session), SOUP_STATUS_MALFORMED);
1086         g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_STATUS_MALFORMED);
1087
1088         return SOUP_SESSION_GET_CLASS (session)->send_message (session, msg);
1089 }
1090
1091
1092 /**
1093  * soup_session_pause_message:
1094  * @session: a #SoupSession
1095  * @msg: a #SoupMessage currently running on @session
1096  *
1097  * Pauses HTTP I/O on @msg. Call soup_session_unpause_message() to
1098  * resume I/O.
1099  **/
1100 void
1101 soup_session_pause_message (SoupSession *session,
1102                             SoupMessage *msg)
1103 {
1104         g_return_if_fail (SOUP_IS_SESSION (session));
1105         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1106
1107         soup_message_io_pause (msg);
1108 }
1109
1110 /**
1111  * soup_session_unpause_message:
1112  * @session: a #SoupSession
1113  * @msg: a #SoupMessage currently running on @session
1114  *
1115  * Resumes HTTP I/O on @msg. Use this to resume after calling
1116  * soup_sessino_pause_message().
1117  *
1118  * If @msg is being sent via blocking I/O, this will resume reading or
1119  * writing immediately. If @msg is using non-blocking I/O, then
1120  * reading or writing won't resume until you return to the main loop.
1121  **/
1122 void
1123 soup_session_unpause_message (SoupSession *session,
1124                               SoupMessage *msg)
1125 {
1126         g_return_if_fail (SOUP_IS_SESSION (session));
1127         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1128
1129         soup_message_io_unpause (msg);
1130 }
1131
1132
1133 static void
1134 cancel_message (SoupSession *session, SoupMessage *msg, guint status_code)
1135 {
1136         SoupSessionPrivate *priv = SOUP_SESSION_GET_PRIVATE (session);
1137
1138         soup_message_queue_remove_message (priv->queue, msg);
1139         soup_message_io_stop (msg);
1140         soup_message_set_status (msg, status_code);
1141         soup_message_finished (msg);
1142 }
1143
1144 /**
1145  * soup_session_cancel_message:
1146  * @session: a #SoupSession
1147  * @msg: the message to cancel
1148  * @status_code: status code to set on @msg (generally
1149  * %SOUP_STATUS_CANCELLED)
1150  *
1151  * Causes @session to immediately finish processing @msg, with a final
1152  * status_code of @status_code. Depending on when you cancel it, the
1153  * response state may be incomplete or inconsistent.
1154  **/
1155 void
1156 soup_session_cancel_message (SoupSession *session, SoupMessage *msg,
1157                              guint status_code)
1158 {
1159         g_return_if_fail (SOUP_IS_SESSION (session));
1160         g_return_if_fail (SOUP_IS_MESSAGE (msg));
1161
1162         SOUP_SESSION_GET_CLASS (session)->cancel_message (session, msg, status_code);
1163 }
1164
1165 static void
1166 gather_conns (gpointer key, gpointer host, gpointer data)
1167 {
1168         SoupConnection *conn = key;
1169         GSList **conns = data;
1170
1171         *conns = g_slist_prepend (*conns, conn);
1172 }
1173
1174 /**
1175  * soup_session_abort:
1176  * @session: the session
1177  *
1178  * Cancels all pending requests in @session.
1179  **/
1180 void
1181 soup_session_abort (SoupSession *session)
1182 {
1183         SoupSessionPrivate *priv;
1184         SoupMessageQueueIter iter;
1185         SoupMessage *msg;
1186         GSList *conns, *c;
1187
1188         g_return_if_fail (SOUP_IS_SESSION (session));
1189         priv = SOUP_SESSION_GET_PRIVATE (session);
1190
1191         for (msg = soup_message_queue_first (priv->queue, &iter);
1192              msg;
1193              msg = soup_message_queue_next (priv->queue, &iter)) {
1194                 soup_session_cancel_message (session, msg,
1195                                              SOUP_STATUS_CANCELLED);
1196         }
1197
1198         /* Close all connections */
1199         g_mutex_lock (priv->host_lock);
1200         conns = NULL;
1201         g_hash_table_foreach (priv->conns, gather_conns, &conns);
1202
1203         for (c = conns; c; c = c->next)
1204                 g_object_ref (c->data);
1205         g_mutex_unlock (priv->host_lock);
1206         for (c = conns; c; c = c->next) {
1207                 soup_connection_disconnect (c->data);
1208                 g_object_unref (c->data);
1209         }
1210
1211         g_slist_free (conns);
1212 }