1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * soup-message.c: HTTP request/response
5 * Copyright (C) 2000-2003, Ximian, Inc.
11 #include "soup-address.h"
12 #include "soup-auth.h"
13 #include "soup-enum-types.h"
14 #include "soup-marshal.h"
15 #include "soup-message.h"
16 #include "soup-message-private.h"
17 #include "soup-misc.h"
21 * SECTION:soup-message
22 * @short_description: An HTTP request and response.
23 * @see_also: #SoupMessageHeaders, #SoupMessageBody
25 * A #SoupMessage represents an HTTP message that is being sent or
28 * For client-side usage, you would create a #SoupMessage with
29 * soup_message_new() or soup_message_new_from_uri(), set up its
30 * fields appropriate, and send it via a #SoupSession.
32 * For server-side usage, #SoupServer will create #SoupMessage<!--
33 * -->s automatically for incoming requests, which your application
34 * will receive via handlers.
36 * Note that libsoup's terminology here does not quite match the HTTP
37 * specification: in RFC 2616, an "HTTP-message" is
38 * <emphasis>either</emphasis> a Request, <emphasis>or</emphasis> a
39 * Response. In libsoup, a #SoupMessage combines both the request and
45 * @method: the HTTP method
46 * @status_code: the HTTP status code
47 * @reason_phrase: the status phrase associated with @status_code
48 * @request_body: the request body
49 * @request_headers: the request headers
50 * @response_body: the response body
51 * @response_headers: the response headers
53 * Represents an HTTP message being sent or received.
55 * As described in the #SoupMessageBody documentation, the
56 * @request_body and @response_body %data fields will not necessarily
57 * be filled in at all times. When they are filled in, they will be
58 * terminated with a '\0' byte (which is not included in the %length),
59 * so you can use them as ordinary C strings (assuming that you know
60 * that the body doesn't have any other '\0' bytes).
62 * For a client-side #SoupMessage, @request_body's %data is usually
63 * filled in right before libsoup writes the request to the network,
64 * but you should not count on this; use soup_message_body_flatten()
65 * if you want to ensure that %data is filled in. @response_body's
66 * %data will be filled in before #SoupMessage::finished is emitted.
68 * For a server-side #SoupMessage, @request_body's %data will be
69 * filled in before #SoupMessage::got_body is emitted.
71 * To prevent the %data field from being filled in at all (eg, if you
72 * are handling the data from a #SoupMessage::got_chunk, and so don't
73 * need to see it all at the end), call
74 * soup_message_body_set_accumulate() on @response_body or
75 * @request_body as appropriate, passing %FALSE.
78 G_DEFINE_TYPE (SoupMessage, soup_message, G_TYPE_OBJECT)
98 static guint signals[LAST_SIGNAL] = { 0 };
114 static void got_body (SoupMessage *req);
115 static void restarted (SoupMessage *req);
116 static void finished (SoupMessage *req);
118 static void set_property (GObject *object, guint prop_id,
119 const GValue *value, GParamSpec *pspec);
120 static void get_property (GObject *object, guint prop_id,
121 GValue *value, GParamSpec *pspec);
124 soup_message_init (SoupMessage *msg)
126 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
128 priv->io_status = SOUP_MESSAGE_IO_STATUS_IDLE;
129 priv->http_version = priv->orig_http_version = SOUP_HTTP_1_1;
131 msg->request_body = soup_message_body_new ();
132 msg->request_headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_REQUEST);
133 msg->response_body = soup_message_body_new ();
134 msg->response_headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_RESPONSE);
138 finalize (GObject *object)
140 SoupMessage *msg = SOUP_MESSAGE (object);
141 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
143 soup_message_io_cleanup (msg);
144 if (priv->chunk_allocator_dnotify)
145 priv->chunk_allocator_dnotify (priv->chunk_allocator_data);
148 soup_uri_free (priv->uri);
150 g_object_unref (priv->addr);
153 g_object_unref (priv->auth);
154 if (priv->proxy_auth)
155 g_object_unref (priv->proxy_auth);
157 soup_message_body_free (msg->request_body);
158 soup_message_headers_free (msg->request_headers);
159 soup_message_body_free (msg->response_body);
160 soup_message_headers_free (msg->response_headers);
162 g_free (msg->reason_phrase);
164 G_OBJECT_CLASS (soup_message_parent_class)->finalize (object);
168 soup_message_class_init (SoupMessageClass *message_class)
170 GObjectClass *object_class = G_OBJECT_CLASS (message_class);
172 g_type_class_add_private (message_class, sizeof (SoupMessagePrivate));
174 /* virtual method definition */
175 message_class->got_body = got_body;
176 message_class->restarted = restarted;
177 message_class->finished = finished;
179 /* virtual method override */
180 object_class->finalize = finalize;
181 object_class->set_property = set_property;
182 object_class->get_property = get_property;
187 * SoupMessage::wrote-informational:
190 * Emitted immediately after writing a 1xx (Informational)
191 * response for a (server-side) message.
193 signals[WROTE_INFORMATIONAL] =
194 g_signal_new ("wrote_informational",
195 G_OBJECT_CLASS_TYPE (object_class),
197 G_STRUCT_OFFSET (SoupMessageClass, wrote_informational),
199 soup_marshal_NONE__NONE,
203 * SoupMessage::wrote-headers:
206 * Emitted immediately after writing the headers for a
207 * message. (For a client-side message, this is after writing
208 * the request headers; for a server-side message, it is after
209 * writing the response headers.)
211 signals[WROTE_HEADERS] =
212 g_signal_new ("wrote_headers",
213 G_OBJECT_CLASS_TYPE (object_class),
215 G_STRUCT_OFFSET (SoupMessageClass, wrote_headers),
217 soup_marshal_NONE__NONE,
221 * SoupMessage::wrote-chunk:
224 * Emitted immediately after writing a body chunk for a message.
226 * Note that this signal is not parallel to
227 * #SoupMessage::got_chunk; it is emitted only when a complete
228 * chunk (added with soup_message_body_append() or
229 * soup_message_body_append_buffer()) has been written. To get
230 * more useful continuous progress information, use
231 * #SoupMessage::wrote_body_data.
233 signals[WROTE_CHUNK] =
234 g_signal_new ("wrote_chunk",
235 G_OBJECT_CLASS_TYPE (object_class),
237 G_STRUCT_OFFSET (SoupMessageClass, wrote_chunk),
239 soup_marshal_NONE__NONE,
243 * SoupMessage::wrote-body-data:
245 * @chunk: the data written
247 * Emitted immediately after writing a portion of the message
248 * body to the network.
250 * Unlike #SoupMessage::wrote_chunk, this is emitted after
251 * every successful write() call, not only after finishing a
254 signals[WROTE_BODY_DATA] =
255 g_signal_new ("wrote_body_data",
256 G_OBJECT_CLASS_TYPE (object_class),
258 0, /* FIXME after next ABI break */
260 soup_marshal_NONE__BOXED,
265 * SoupMessage::wrote-body:
268 * Emitted immediately after writing the complete body for a
269 * message. (For a client-side message, this means that
270 * libsoup is done writing and is now waiting for the response
271 * from the server. For a server-side message, this means that
272 * libsoup has finished writing the response and is nearly
273 * done with the message.)
275 signals[WROTE_BODY] =
276 g_signal_new ("wrote_body",
277 G_OBJECT_CLASS_TYPE (object_class),
279 G_STRUCT_OFFSET (SoupMessageClass, wrote_body),
281 soup_marshal_NONE__NONE,
285 * SoupMessage::got-informational:
288 * Emitted after receiving a 1xx (Informational) response for
289 * a (client-side) message. The response_headers will be
290 * filled in with the headers associated with the
291 * informational response; however, those header values will
292 * be erased after this signal is done.
294 * If you cancel or requeue @msg while processing this signal,
295 * then the current HTTP I/O will be stopped after this signal
296 * emission finished, and @msg's connection will be closed.
298 signals[GOT_INFORMATIONAL] =
299 g_signal_new ("got_informational",
300 G_OBJECT_CLASS_TYPE (object_class),
302 G_STRUCT_OFFSET (SoupMessageClass, got_informational),
304 soup_marshal_NONE__NONE,
308 * SoupMessage::got-headers:
311 * Emitted after receiving all message headers for a message.
312 * (For a client-side message, this is after receiving the
313 * Status-Line and response headers; for a server-side
314 * message, it is after receiving the Request-Line and request
317 * See also soup_message_add_header_handler() and
318 * soup_message_add_status_code_handler(), which can be used
319 * to connect to a subset of emissions of this signal.
321 * If you cancel or requeue @msg while processing this signal,
322 * then the current HTTP I/O will be stopped after this signal
323 * emission finished, and @msg's connection will be closed.
324 * (If you need to requeue a message--eg, after handling
325 * authentication or redirection--it is usually better to
326 * requeue it from a #SoupMessage::got_body handler rather
327 * than a #SoupMessage::got_header handler, so that the
328 * existing HTTP connection can be reused.)
330 signals[GOT_HEADERS] =
331 g_signal_new ("got_headers",
332 G_OBJECT_CLASS_TYPE (object_class),
334 G_STRUCT_OFFSET (SoupMessageClass, got_headers),
336 soup_marshal_NONE__NONE,
340 * SoupMessage::got-chunk:
342 * @chunk: the just-read chunk
344 * Emitted after receiving a chunk of a message body. Note
345 * that "chunk" in this context means any subpiece of the
346 * body, not necessarily the specific HTTP 1.1 chunks sent by
349 * If you cancel or requeue @msg while processing this signal,
350 * then the current HTTP I/O will be stopped after this signal
351 * emission finished, and @msg's connection will be closed.
354 g_signal_new ("got_chunk",
355 G_OBJECT_CLASS_TYPE (object_class),
357 G_STRUCT_OFFSET (SoupMessageClass, got_chunk),
359 soup_marshal_NONE__BOXED,
361 /* Use %G_SIGNAL_TYPE_STATIC_SCOPE so that
362 * the %SOUP_MEMORY_TEMPORARY buffers used
363 * by soup-message-io.c when emitting this
364 * signal don't get forcibly copied by
367 SOUP_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE);
370 * SoupMessage::got-body:
373 * Emitted after receiving the complete message body. (For a
374 * server-side message, this means it has received the request
375 * body. For a client-side message, this means it has received
376 * the response body and is nearly done with the message.)
378 * See also soup_message_add_header_handler() and
379 * soup_message_add_status_code_handler(), which can be used
380 * to connect to a subset of emissions of this signal.
383 g_signal_new ("got_body",
384 G_OBJECT_CLASS_TYPE (object_class),
386 G_STRUCT_OFFSET (SoupMessageClass, got_body),
388 soup_marshal_NONE__NONE,
392 * SoupMessage::restarted:
395 * Emitted when a request that was already sent once is now
396 * being sent again (eg, because the first attempt received a
397 * redirection response, or because we needed to use
401 g_signal_new ("restarted",
402 G_OBJECT_CLASS_TYPE (object_class),
404 G_STRUCT_OFFSET (SoupMessageClass, restarted),
406 soup_marshal_NONE__NONE,
410 * SoupMessage::finished:
413 * Emitted when all HTTP processing is finished for a message.
414 * (After #SoupMessage::got_body for client-side messages, or
415 * after #SoupMessage::wrote_body for server-side messages.)
418 g_signal_new ("finished",
419 G_OBJECT_CLASS_TYPE (object_class),
421 G_STRUCT_OFFSET (SoupMessageClass, finished),
423 soup_marshal_NONE__NONE,
427 g_object_class_install_property (
428 object_class, PROP_METHOD,
429 g_param_spec_string (SOUP_MESSAGE_METHOD,
431 "The message's HTTP method",
434 g_object_class_install_property (
435 object_class, PROP_URI,
436 g_param_spec_boxed (SOUP_MESSAGE_URI,
438 "The message's Request-URI",
441 g_object_class_install_property (
442 object_class, PROP_HTTP_VERSION,
443 g_param_spec_enum (SOUP_MESSAGE_HTTP_VERSION,
445 "The HTTP protocol version to use",
446 SOUP_TYPE_HTTP_VERSION,
449 g_object_class_install_property (
450 object_class, PROP_FLAGS,
451 g_param_spec_flags (SOUP_MESSAGE_FLAGS,
453 "Various message options",
454 SOUP_TYPE_MESSAGE_FLAGS,
457 g_object_class_install_property (
458 object_class, PROP_SERVER_SIDE,
459 g_param_spec_boolean (SOUP_MESSAGE_SERVER_SIDE,
461 "Whether or not the message is server-side rather than client-side",
463 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
464 g_object_class_install_property (
465 object_class, PROP_STATUS_CODE,
466 g_param_spec_uint (SOUP_MESSAGE_STATUS_CODE,
468 "The HTTP response status code",
471 g_object_class_install_property (
472 object_class, PROP_REASON_PHRASE,
473 g_param_spec_string (SOUP_MESSAGE_REASON_PHRASE,
475 "The HTTP response reason phrase",
481 set_property (GObject *object, guint prop_id,
482 const GValue *value, GParamSpec *pspec)
484 SoupMessage *msg = SOUP_MESSAGE (object);
485 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
489 msg->method = g_intern_string (g_value_get_string (value));
492 soup_message_set_uri (msg, g_value_get_boxed (value));
494 case PROP_HTTP_VERSION:
495 soup_message_set_http_version (msg, g_value_get_enum (value));
498 soup_message_set_flags (msg, g_value_get_flags (value));
500 case PROP_SERVER_SIDE:
501 priv->server_side = g_value_get_boolean (value);
503 case PROP_STATUS_CODE:
504 soup_message_set_status (msg, g_value_get_uint (value));
506 case PROP_REASON_PHRASE:
507 soup_message_set_status_full (msg, msg->status_code,
508 g_value_get_string (value));
511 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
517 get_property (GObject *object, guint prop_id,
518 GValue *value, GParamSpec *pspec)
520 SoupMessage *msg = SOUP_MESSAGE (object);
521 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
525 g_value_set_string (value, msg->method);
528 g_value_set_boxed (value, priv->uri);
530 case PROP_HTTP_VERSION:
531 g_value_set_enum (value, priv->http_version);
534 g_value_set_flags (value, priv->msg_flags);
536 case PROP_SERVER_SIDE:
537 g_value_set_boolean (value, priv->server_side);
539 case PROP_STATUS_CODE:
540 g_value_set_uint (value, msg->status_code);
542 case PROP_REASON_PHRASE:
543 g_value_set_string (value, msg->reason_phrase);
546 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
554 * @method: the HTTP method for the created request
555 * @uri_string: the destination endpoint (as a string)
557 * Creates a new empty #SoupMessage, which will connect to @uri
559 * Return value: the new #SoupMessage (or %NULL if @uri could not
563 soup_message_new (const char *method, const char *uri_string)
568 g_return_val_if_fail (method != NULL, NULL);
569 g_return_val_if_fail (uri_string != NULL, NULL);
571 uri = soup_uri_new (uri_string);
579 msg = soup_message_new_from_uri (method, uri);
585 * soup_message_new_from_uri:
586 * @method: the HTTP method for the created request
587 * @uri: the destination endpoint (as a #SoupURI)
589 * Creates a new empty #SoupMessage, which will connect to @uri
591 * Return value: the new #SoupMessage
594 soup_message_new_from_uri (const char *method, SoupURI *uri)
596 return g_object_new (SOUP_TYPE_MESSAGE,
597 SOUP_MESSAGE_METHOD, method,
598 SOUP_MESSAGE_URI, uri,
603 * soup_message_set_request:
605 * @content_type: MIME Content-Type of the body
606 * @req_use: a #SoupMemoryUse describing how to handle @req_body
607 * @req_body: a data buffer containing the body of the message request.
608 * @req_length: the byte length of @req_body.
610 * Convenience function to set the request body of a #SoupMessage. If
611 * @content_type is %NULL, the request body must be empty as well.
614 soup_message_set_request (SoupMessage *msg,
615 const char *content_type,
616 SoupMemoryUse req_use,
617 const char *req_body,
620 g_return_if_fail (SOUP_IS_MESSAGE (msg));
621 g_return_if_fail (content_type != NULL || req_length == 0);
624 soup_message_headers_replace (msg->request_headers,
625 "Content-Type", content_type);
626 soup_message_body_append (msg->request_body, req_use,
627 req_body, req_length);
629 soup_message_headers_remove (msg->request_headers,
631 soup_message_body_truncate (msg->request_body);
636 * soup_message_set_response:
638 * @content_type: MIME Content-Type of the body
639 * @resp_use: a #SoupMemoryUse describing how to handle @resp_body
640 * @resp_body: a data buffer containing the body of the message response.
641 * @resp_length: the byte length of @resp_body.
643 * Convenience function to set the response body of a #SoupMessage. If
644 * @content_type is %NULL, the response body must be empty as well.
647 soup_message_set_response (SoupMessage *msg,
648 const char *content_type,
649 SoupMemoryUse resp_use,
650 const char *resp_body,
653 g_return_if_fail (SOUP_IS_MESSAGE (msg));
654 g_return_if_fail (content_type != NULL || resp_length == 0);
657 soup_message_headers_replace (msg->response_headers,
658 "Content-Type", content_type);
659 soup_message_body_append (msg->response_body, resp_use,
660 resp_body, resp_length);
662 soup_message_headers_remove (msg->response_headers,
664 soup_message_body_truncate (msg->response_body);
669 * soup_message_wrote_informational:
670 * @msg: a #SoupMessage
672 * Emits the %wrote_informational signal, indicating that the IO layer
673 * finished writing an informational (1xx) response for @msg.
676 soup_message_wrote_informational (SoupMessage *msg)
678 g_signal_emit (msg, signals[WROTE_INFORMATIONAL], 0);
682 * soup_message_wrote_headers:
683 * @msg: a #SoupMessage
685 * Emits the %wrote_headers signal, indicating that the IO layer
686 * finished writing the (non-informational) headers for @msg.
689 soup_message_wrote_headers (SoupMessage *msg)
691 g_signal_emit (msg, signals[WROTE_HEADERS], 0);
695 * soup_message_wrote_chunk:
696 * @msg: a #SoupMessage
698 * Emits the %wrote_chunk signal, indicating that the IO layer
699 * finished writing a chunk of @msg's body.
702 soup_message_wrote_chunk (SoupMessage *msg)
704 g_signal_emit (msg, signals[WROTE_CHUNK], 0);
708 * soup_message_wrote_body_data:
709 * @msg: a #SoupMessage
710 * @chunk: the data written
712 * Emits the %wrote_body_data signal, indicating that the IO layer
713 * finished writing a portion of @msg's body.
716 soup_message_wrote_body_data (SoupMessage *msg, SoupBuffer *chunk)
718 g_signal_emit (msg, signals[WROTE_BODY_DATA], 0, chunk);
722 * soup_message_wrote_body:
723 * @msg: a #SoupMessage
725 * Emits the %wrote_body signal, indicating that the IO layer finished
726 * writing the body for @msg.
729 soup_message_wrote_body (SoupMessage *msg)
731 g_signal_emit (msg, signals[WROTE_BODY], 0);
735 * soup_message_got_informational:
736 * @msg: a #SoupMessage
738 * Emits the %got_informational signal, indicating that the IO layer
739 * read a complete informational (1xx) response for @msg.
742 soup_message_got_informational (SoupMessage *msg)
744 g_signal_emit (msg, signals[GOT_INFORMATIONAL], 0);
748 * soup_message_got_headers:
749 * @msg: a #SoupMessage
751 * Emits the %got_headers signal, indicating that the IO layer
752 * finished reading the (non-informational) headers for @msg.
755 soup_message_got_headers (SoupMessage *msg)
757 g_signal_emit (msg, signals[GOT_HEADERS], 0);
761 * soup_message_got_chunk:
762 * @msg: a #SoupMessage
763 * @chunk: the newly-read chunk
765 * Emits the %got_chunk signal, indicating that the IO layer finished
766 * reading a chunk of @msg's body.
769 soup_message_got_chunk (SoupMessage *msg, SoupBuffer *chunk)
771 g_signal_emit (msg, signals[GOT_CHUNK], 0, chunk);
775 got_body (SoupMessage *req)
777 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (req);
778 SoupMessageBody *body;
780 body = priv->server_side ? req->request_body : req->response_body;
781 if (soup_message_body_get_accumulate (body)) {
784 buffer = soup_message_body_flatten (body);
785 soup_buffer_free (buffer);
790 * soup_message_got_body:
791 * @msg: a #SoupMessage
793 * Emits the %got_body signal, indicating that the IO layer finished
794 * reading the body for @msg.
797 soup_message_got_body (SoupMessage *msg)
799 g_signal_emit (msg, signals[GOT_BODY], 0);
803 restarted (SoupMessage *req)
805 soup_message_io_stop (req);
809 * soup_message_restarted:
810 * @msg: a #SoupMessage
812 * Emits the %restarted signal, indicating that @msg should be
816 soup_message_restarted (SoupMessage *msg)
818 g_signal_emit (msg, signals[RESTARTED], 0);
822 finished (SoupMessage *req)
824 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (req);
826 soup_message_io_stop (req);
827 priv->io_status = SOUP_MESSAGE_IO_STATUS_FINISHED;
831 * soup_message_finished:
832 * @msg: a #SoupMessage
834 * Emits the %finished signal, indicating that @msg has been completely
838 soup_message_finished (SoupMessage *msg)
840 g_signal_emit (msg, signals[FINISHED], 0);
844 header_handler_free (gpointer header_name, GClosure *closure)
846 g_free (header_name);
850 header_handler_metamarshal (GClosure *closure, GValue *return_value,
851 guint n_param_values, const GValue *param_values,
852 gpointer invocation_hint, gpointer marshal_data)
854 SoupMessage *msg = g_value_get_object (¶m_values[0]);
855 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
856 const char *header_name = marshal_data;
857 SoupMessageHeaders *hdrs;
859 if (priv->io_status != SOUP_MESSAGE_IO_STATUS_RUNNING)
862 hdrs = priv->server_side ? msg->request_headers : msg->response_headers;
863 if (soup_message_headers_get (hdrs, header_name)) {
864 closure->marshal (closure, return_value, n_param_values,
865 param_values, invocation_hint,
866 ((GCClosure *)closure)->callback);
871 * soup_message_add_header_handler:
872 * @msg: a #SoupMessage
873 * @signal: signal to connect the handler to.
874 * @header: HTTP response header to match against
875 * @callback: the header handler
876 * @user_data: data to pass to @handler_cb
878 * Adds a signal handler to @msg for @signal, as with
879 * g_signal_connect(), but with two differences: the @callback will
880 * only be run if @msg has a header named @header, and it will only be
881 * run if no earlier handler cancelled or requeued the message.
883 * If @signal is one of the "got" signals (eg, "got_headers"), or
884 * "finished" or "restarted", then @header is matched against the
885 * incoming message headers (that is, the #request_headers for a
886 * client #SoupMessage, or the #response_headers for a server
887 * #SoupMessage). If @signal is one of the "wrote" signals, then
888 * @header is matched against the outgoing message headers.
890 * Return value: the handler ID from g_signal_connect()
893 soup_message_add_header_handler (SoupMessage *msg,
899 SoupMessagePrivate *priv;
903 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), 0);
904 g_return_val_if_fail (signal != NULL, 0);
905 g_return_val_if_fail (header != NULL, 0);
906 g_return_val_if_fail (callback != NULL, 0);
908 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
910 closure = g_cclosure_new (callback, user_data, NULL);
912 header_name = g_strdup (header);
913 g_closure_set_meta_marshal (closure, header_name,
914 header_handler_metamarshal);
915 g_closure_add_finalize_notifier (closure, header_name,
916 header_handler_free);
918 return g_signal_connect_closure (msg, signal, closure, FALSE);
922 status_handler_metamarshal (GClosure *closure, GValue *return_value,
923 guint n_param_values, const GValue *param_values,
924 gpointer invocation_hint, gpointer marshal_data)
926 SoupMessage *msg = g_value_get_object (¶m_values[0]);
927 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
928 guint status = GPOINTER_TO_UINT (marshal_data);
930 if (priv->io_status != SOUP_MESSAGE_IO_STATUS_RUNNING)
933 if (msg->status_code == status) {
934 closure->marshal (closure, return_value, n_param_values,
935 param_values, invocation_hint,
936 ((GCClosure *)closure)->callback);
941 * soup_message_add_status_code_handler:
942 * @msg: a #SoupMessage
943 * @signal: signal to connect the handler to.
944 * @status_code: status code to match against
945 * @callback: the header handler
946 * @user_data: data to pass to @handler_cb
948 * Adds a signal handler to @msg for @signal, as with
949 * g_signal_connect() but with two differences: the @callback will
950 * only be run if @msg has the status @status_code, and it will only
951 * be run if no earlier handler cancelled or requeued the message.
953 * @signal must be a signal that will be emitted after @msg's status
954 * is set. For a client #SoupMessage, this means it can't be a "wrote"
955 * signal. For a server #SoupMessage, this means it can't be a "got"
958 * Return value: the handler ID from g_signal_connect()
961 soup_message_add_status_code_handler (SoupMessage *msg,
969 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), 0);
970 g_return_val_if_fail (signal != NULL, 0);
971 g_return_val_if_fail (callback != NULL, 0);
973 closure = g_cclosure_new (callback, user_data, NULL);
974 g_closure_set_meta_marshal (closure, GUINT_TO_POINTER (status_code),
975 status_handler_metamarshal);
977 return g_signal_connect_closure (msg, signal, closure, FALSE);
982 * soup_message_set_auth:
983 * @msg: a #SoupMessage
984 * @auth: a #SoupAuth, or %NULL
986 * Sets @msg to authenticate to its destination using @auth, which
987 * must have already been fully authenticated. If @auth is %NULL, @msg
988 * will not authenticate to its destination.
991 soup_message_set_auth (SoupMessage *msg, SoupAuth *auth)
993 SoupMessagePrivate *priv;
996 g_return_if_fail (SOUP_IS_MESSAGE (msg));
997 g_return_if_fail (auth == NULL || SOUP_IS_AUTH (auth));
998 g_return_if_fail (auth == NULL || soup_auth_is_authenticated (auth));
1000 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1003 g_object_unref (priv->auth);
1004 soup_message_headers_remove (msg->request_headers,
1011 g_object_ref (priv->auth);
1012 token = soup_auth_get_authorization (auth, msg);
1013 soup_message_headers_replace (msg->request_headers,
1014 "Authorization", token);
1019 * soup_message_get_auth:
1020 * @msg: a #SoupMessage
1022 * Gets the #SoupAuth used by @msg for authentication.
1024 * Return value: the #SoupAuth used by @msg for authentication, or
1025 * %NULL if @msg is unauthenticated.
1028 soup_message_get_auth (SoupMessage *msg)
1030 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
1032 return SOUP_MESSAGE_GET_PRIVATE (msg)->auth;
1036 * soup_message_set_proxy_auth:
1037 * @msg: a #SoupMessage
1038 * @auth: a #SoupAuth, or %NULL
1040 * Sets @msg to authenticate to its proxy using @auth, which must have
1041 * already been fully authenticated. If @auth is %NULL, @msg will not
1042 * authenticate to its proxy.
1045 soup_message_set_proxy_auth (SoupMessage *msg, SoupAuth *auth)
1047 SoupMessagePrivate *priv;
1050 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1051 g_return_if_fail (auth == NULL || SOUP_IS_AUTH (auth));
1052 g_return_if_fail (auth == NULL || soup_auth_is_authenticated (auth));
1054 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1056 if (priv->proxy_auth) {
1057 g_object_unref (priv->proxy_auth);
1058 soup_message_headers_remove (msg->request_headers,
1059 "Proxy-Authorization");
1061 priv->proxy_auth = auth;
1062 if (!priv->proxy_auth)
1065 g_object_ref (priv->proxy_auth);
1066 token = soup_auth_get_authorization (auth, msg);
1067 soup_message_headers_replace (msg->request_headers,
1068 "Proxy-Authorization", token);
1073 * soup_message_get_proxy_auth:
1074 * @msg: a #SoupMessage
1076 * Gets the #SoupAuth used by @msg for authentication to its proxy..
1078 * Return value: the #SoupAuth used by @msg for authentication to its
1079 * proxy, or %NULL if @msg isn't authenticated to its proxy.
1082 soup_message_get_proxy_auth (SoupMessage *msg)
1084 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
1086 return SOUP_MESSAGE_GET_PRIVATE (msg)->proxy_auth;
1090 * soup_message_cleanup_response:
1091 * @req: a #SoupMessage
1093 * Cleans up all response data on @req, so that the request can be sent
1094 * again and receive a new response. (Eg, as a result of a redirect or
1095 * authorization request.)
1098 soup_message_cleanup_response (SoupMessage *req)
1100 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (req);
1102 soup_message_body_truncate (req->response_body);
1103 soup_message_headers_clear (req->response_headers);
1105 req->status_code = SOUP_STATUS_NONE;
1106 if (req->reason_phrase) {
1107 g_free (req->reason_phrase);
1108 req->reason_phrase = NULL;
1110 priv->http_version = priv->orig_http_version;
1112 g_object_notify (G_OBJECT (req), SOUP_MESSAGE_STATUS_CODE);
1113 g_object_notify (G_OBJECT (req), SOUP_MESSAGE_REASON_PHRASE);
1114 g_object_notify (G_OBJECT (req), SOUP_MESSAGE_HTTP_VERSION);
1119 * @SOUP_MESSAGE_NO_REDIRECT: The session should not follow redirect
1120 * (3xx) responses received by this message.
1121 * @SOUP_MESSAGE_OVERWRITE_CHUNKS: Deprecated: equivalent to calling
1122 * soup_message_body_set_accumulate() on the incoming message body
1123 * (ie, %response_body for a client-side request), passing %FALSE.
1125 * Various flags that can be set on a #SoupMessage to alter its
1130 * soup_message_set_flags:
1131 * @msg: a #SoupMessage
1132 * @flags: a set of #SoupMessageFlags values
1134 * Sets the specified flags on @msg.
1137 soup_message_set_flags (SoupMessage *msg, SoupMessageFlags flags)
1139 SoupMessagePrivate *priv;
1141 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1142 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1144 if ((priv->msg_flags ^ flags) & SOUP_MESSAGE_OVERWRITE_CHUNKS) {
1145 soup_message_body_set_accumulate (
1146 priv->server_side ? msg->request_body : msg->response_body,
1147 !(flags & SOUP_MESSAGE_OVERWRITE_CHUNKS));
1150 priv->msg_flags = flags;
1151 g_object_notify (G_OBJECT (msg), SOUP_MESSAGE_FLAGS);
1155 * soup_message_get_flags:
1156 * @msg: a #SoupMessage
1158 * Gets the flags on @msg
1160 * Return value: the flags
1163 soup_message_get_flags (SoupMessage *msg)
1165 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), 0);
1167 return SOUP_MESSAGE_GET_PRIVATE (msg)->msg_flags;
1172 * @SOUP_HTTP_1_0: HTTP 1.0 (RFC 1945)
1173 * @SOUP_HTTP_1_1: HTTP 1.1 (RFC 2616)
1175 * Indicates the HTTP protocol version being used.
1179 * soup_message_set_http_version:
1180 * @msg: a #SoupMessage
1181 * @version: the HTTP version
1183 * Sets the HTTP version on @msg. The default version is
1184 * %SOUP_HTTP_1_1. Setting it to %SOUP_HTTP_1_0 will prevent certain
1185 * functionality from being used.
1188 soup_message_set_http_version (SoupMessage *msg, SoupHTTPVersion version)
1190 SoupMessagePrivate *priv;
1192 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1193 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1195 priv->http_version = version;
1196 if (msg->status_code == SOUP_STATUS_NONE)
1197 priv->orig_http_version = version;
1198 g_object_notify (G_OBJECT (msg), SOUP_MESSAGE_HTTP_VERSION);
1202 * soup_message_get_http_version:
1203 * @msg: a #SoupMessage
1205 * Gets the HTTP version of @msg. This is the minimum of the
1206 * version from the request and the version from the response.
1208 * Return value: the HTTP version
1211 soup_message_get_http_version (SoupMessage *msg)
1213 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_HTTP_1_0);
1215 return SOUP_MESSAGE_GET_PRIVATE (msg)->http_version;
1219 * soup_message_is_keepalive:
1220 * @msg: a #SoupMessage
1222 * Determines whether or not @msg's connection can be kept alive for
1223 * further requests after processing @msg, based on the HTTP version,
1224 * Connection header, etc.
1226 * Return value: %TRUE or %FALSE.
1229 soup_message_is_keepalive (SoupMessage *msg)
1231 const char *c_conn, *s_conn;
1233 c_conn = soup_message_headers_get (msg->request_headers, "Connection");
1234 s_conn = soup_message_headers_get (msg->response_headers, "Connection");
1236 if (msg->status_code == SOUP_STATUS_OK &&
1237 msg->method == SOUP_METHOD_CONNECT)
1240 if (SOUP_MESSAGE_GET_PRIVATE (msg)->http_version == SOUP_HTTP_1_0) {
1241 /* Only persistent if the client requested keepalive
1242 * and the server agreed.
1245 if (!c_conn || !s_conn)
1247 if (soup_header_contains (c_conn, "Keep-Alive") ||
1248 soup_header_contains (s_conn, "Keep-Alive"))
1253 /* Normally persistent unless either side requested otherwise */
1254 if (c_conn && soup_header_contains (c_conn, "close"))
1256 if (s_conn && soup_header_contains (s_conn, "close"))
1259 /* But not if the server sent a terminate-by-EOF response */
1260 if (soup_message_headers_get_encoding (msg->response_headers) == SOUP_ENCODING_EOF)
1268 * soup_message_set_uri:
1269 * @msg: a #SoupMessage
1270 * @uri: the new #SoupURI
1272 * Sets @msg's URI to @uri. If @msg has already been sent and you want
1273 * to re-send it with the new URI, you need to call
1274 * soup_session_requeue_message().
1277 soup_message_set_uri (SoupMessage *msg, SoupURI *uri)
1279 SoupMessagePrivate *priv;
1281 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1282 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1285 soup_uri_free (priv->uri);
1287 g_object_unref (priv->addr);
1290 priv->uri = soup_uri_copy (uri);
1292 g_object_notify (G_OBJECT (msg), SOUP_MESSAGE_URI);
1296 * soup_message_get_uri:
1297 * @msg: a #SoupMessage
1301 * Return value: the URI @msg is targeted for.
1304 soup_message_get_uri (SoupMessage *msg)
1306 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
1308 return SOUP_MESSAGE_GET_PRIVATE (msg)->uri;
1312 * soup_message_get_address:
1313 * @msg: a #SoupMessage
1315 * Gets the address @msg's URI points to. After first setting the
1316 * URI on a message, this will be unresolved, although the message's
1317 * session will resolve it before sending the message.
1319 * Return value: the address @msg's URI points to
1322 soup_message_get_address (SoupMessage *msg)
1324 SoupMessagePrivate *priv;
1326 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
1328 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1330 priv->addr = soup_address_new (priv->uri->host,
1337 * soup_message_set_status:
1338 * @msg: a #SoupMessage
1339 * @status_code: an HTTP status code
1341 * Sets @msg's status code to @status_code. If @status_code is a
1342 * known value, it will also set @msg's reason_phrase.
1345 soup_message_set_status (SoupMessage *msg, guint status_code)
1347 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1348 g_return_if_fail (status_code != 0);
1350 g_free (msg->reason_phrase);
1352 msg->status_code = status_code;
1353 msg->reason_phrase = g_strdup (soup_status_get_phrase (status_code));
1354 g_object_notify (G_OBJECT (msg), SOUP_MESSAGE_STATUS_CODE);
1355 g_object_notify (G_OBJECT (msg), SOUP_MESSAGE_REASON_PHRASE);
1359 * soup_message_set_status_full:
1360 * @msg: a #SoupMessage
1361 * @status_code: an HTTP status code
1362 * @reason_phrase: a description of the status
1364 * Sets @msg's status code and reason phrase.
1367 soup_message_set_status_full (SoupMessage *msg,
1369 const char *reason_phrase)
1371 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1372 g_return_if_fail (status_code != 0);
1373 g_return_if_fail (reason_phrase != NULL);
1375 g_free (msg->reason_phrase);
1377 msg->status_code = status_code;
1378 msg->reason_phrase = g_strdup (reason_phrase);
1379 g_object_notify (G_OBJECT (msg), SOUP_MESSAGE_STATUS_CODE);
1380 g_object_notify (G_OBJECT (msg), SOUP_MESSAGE_REASON_PHRASE);
1384 soup_message_set_io_status (SoupMessage *msg,
1385 SoupMessageIOStatus status)
1387 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1389 priv->io_status = status;
1393 soup_message_get_io_status (SoupMessage *msg)
1395 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1397 return priv->io_status;
1401 * SoupChunkAllocator:
1402 * @msg: the #SoupMessage the chunk is being allocated for
1403 * @max_len: the maximum length that will be read, or 0.
1404 * @user_data: the data passed to soup_message_set_chunk_allocator()
1406 * The prototype for a chunk allocation callback. This should allocate
1407 * a new #SoupBuffer and return it for the I/O layer to read message
1408 * body data off the network into.
1410 * If @max_len is non-0, it indicates the maximum number of bytes that
1411 * could be read, based on what is known about the message size. Note
1412 * that this might be a very large number, and you should not simply
1413 * try to allocate that many bytes blindly. If @max_len is 0, that
1414 * means that libsoup does not know how many bytes remain to be read,
1415 * and the allocator should return a buffer of a size that it finds
1418 * If the allocator returns %NULL, the message will be paused. It is
1419 * up to the application to make sure that it gets unpaused when it
1420 * becomes possible to allocate a new buffer.
1422 * Return value: the new buffer (or %NULL)
1426 * soup_message_set_chunk_allocator:
1427 * @msg: a #SoupMessage
1428 * @allocator: the chunk allocator callback
1429 * @user_data: data to pass to @allocator
1430 * @destroy_notify: destroy notifier to free @user_data when @msg is
1433 * Sets an alternate chunk-allocation function to use when reading
1434 * @msg's body. Every time data is available to read, libsoup will
1435 * call @allocator, which should return a #SoupBuffer. (See
1436 * #SoupChunkAllocator for additional details.) Libsoup will then read
1437 * data from the network into that buffer, and update the buffer's
1438 * %length to indicate how much data it read.
1440 * Generally, a custom chunk allocator would be used in conjunction
1441 * with soup_message_body_set_accumulate() %FALSE and
1442 * #SoupMessage::got_chunk, as part of a strategy to avoid unnecessary
1443 * copying of data. However, you cannot assume that every call to the
1444 * allocator will be followed by a call to your %got_chunk handler; if
1445 * an I/O error occurs, then the buffer will be unreffed without ever
1446 * having been used. If your buffer-allocation strategy requires
1447 * special cleanup, use soup_buffer_new_with_owner() rather than doing
1448 * the cleanup from the %got_chunk handler.
1450 * The other thing to remember when using non-accumulating message
1451 * bodies is that the buffer passed to the %got_chunk handler will be
1452 * unreffed after the handler returns, just as it would be in the
1453 * non-custom-allocated case. If you want to hand the chunk data off
1454 * to some other part of your program to use later, you'll need to ref
1455 * the #SoupBuffer (or its owner, in the soup_buffer_new_with_owner()
1456 * case) to ensure that the data remains valid.
1459 soup_message_set_chunk_allocator (SoupMessage *msg,
1460 SoupChunkAllocator allocator,
1462 GDestroyNotify destroy_notify)
1464 SoupMessagePrivate *priv;
1466 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1468 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1470 if (priv->chunk_allocator_dnotify)
1471 priv->chunk_allocator_dnotify (priv->chunk_allocator_data);
1473 priv->chunk_allocator = allocator;
1474 priv->chunk_allocator_data = user_data;
1475 priv->chunk_allocator_dnotify = destroy_notify;