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-auth.h"
12 #include "soup-marshal.h"
13 #include "soup-message.h"
14 #include "soup-message-private.h"
15 #include "soup-misc.h"
16 #include "soup-server-message.h"
19 G_DEFINE_TYPE (SoupMessage, soup_message, G_TYPE_OBJECT)
38 static guint signals[LAST_SIGNAL] = { 0 };
40 static void wrote_body (SoupMessage *req);
41 static void got_headers (SoupMessage *req);
42 static void got_chunk (SoupMessage *req);
43 static void got_body (SoupMessage *req);
44 static void restarted (SoupMessage *req);
45 static void finished (SoupMessage *req);
46 static void free_chunks (SoupMessage *msg);
49 soup_message_init (SoupMessage *msg)
51 msg->status = SOUP_MESSAGE_STATUS_IDLE;
53 msg->request_headers = g_hash_table_new (soup_str_case_hash,
56 msg->response_headers = g_hash_table_new (soup_str_case_hash,
59 SOUP_MESSAGE_GET_PRIVATE (msg)->http_version = SOUP_HTTP_1_1;
63 finalize (GObject *object)
65 SoupMessage *msg = SOUP_MESSAGE (object);
66 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
68 soup_message_io_stop (msg);
71 soup_uri_free (priv->uri);
74 g_object_unref (priv->auth);
76 g_object_unref (priv->proxy_auth);
78 if (msg->request.owner == SOUP_BUFFER_SYSTEM_OWNED)
79 g_free (msg->request.body);
80 if (msg->response.owner == SOUP_BUFFER_SYSTEM_OWNED)
81 g_free (msg->response.body);
84 soup_message_clear_headers (msg->request_headers);
85 g_hash_table_destroy (msg->request_headers);
87 soup_message_clear_headers (msg->response_headers);
88 g_hash_table_destroy (msg->response_headers);
90 g_slist_foreach (priv->content_handlers, (GFunc) g_free, NULL);
91 g_slist_free (priv->content_handlers);
93 g_free ((char *) msg->reason_phrase);
95 G_OBJECT_CLASS (soup_message_parent_class)->finalize (object);
99 soup_message_class_init (SoupMessageClass *message_class)
101 GObjectClass *object_class = G_OBJECT_CLASS (message_class);
103 g_type_class_add_private (message_class, sizeof (SoupMessagePrivate));
105 /* virtual method definition */
106 message_class->wrote_body = wrote_body;
107 message_class->got_headers = got_headers;
108 message_class->got_chunk = got_chunk;
109 message_class->got_body = got_body;
110 message_class->restarted = restarted;
111 message_class->finished = finished;
113 /* virtual method override */
114 object_class->finalize = finalize;
119 * SoupMessage::wrote-informational:
122 * Emitted immediately after writing a 1xx (Informational)
123 * response for a message.
125 signals[WROTE_INFORMATIONAL] =
126 g_signal_new ("wrote_informational",
127 G_OBJECT_CLASS_TYPE (object_class),
129 G_STRUCT_OFFSET (SoupMessageClass, wrote_informational),
131 soup_marshal_NONE__NONE,
135 * SoupMessage::wrote-headers:
138 * Emitted immediately after writing the headers for a message.
140 signals[WROTE_HEADERS] =
141 g_signal_new ("wrote_headers",
142 G_OBJECT_CLASS_TYPE (object_class),
144 G_STRUCT_OFFSET (SoupMessageClass, wrote_headers),
146 soup_marshal_NONE__NONE,
150 * SoupMessage::wrote-chunk:
153 * Emitted immediately after writing a body chunk for a message.
156 signals[WROTE_CHUNK] =
157 g_signal_new ("wrote_chunk",
158 G_OBJECT_CLASS_TYPE (object_class),
160 G_STRUCT_OFFSET (SoupMessageClass, wrote_chunk),
162 soup_marshal_NONE__NONE,
166 * SoupMessage::wrote-body:
169 * Emitted immediately after writing the complete body for a message.
171 signals[WROTE_BODY] =
172 g_signal_new ("wrote_body",
173 G_OBJECT_CLASS_TYPE (object_class),
175 G_STRUCT_OFFSET (SoupMessageClass, wrote_body),
177 soup_marshal_NONE__NONE,
181 * SoupMessage::got-informational:
184 * Emitted after receiving a 1xx (Informational) response for
187 signals[GOT_INFORMATIONAL] =
188 g_signal_new ("got_informational",
189 G_OBJECT_CLASS_TYPE (object_class),
191 G_STRUCT_OFFSET (SoupMessageClass, got_informational),
193 soup_marshal_NONE__NONE,
197 * SoupMessage::got-headers:
200 * Emitted after receiving all message headers for a message.
202 signals[GOT_HEADERS] =
203 g_signal_new ("got_headers",
204 G_OBJECT_CLASS_TYPE (object_class),
206 G_STRUCT_OFFSET (SoupMessageClass, got_headers),
208 soup_marshal_NONE__NONE,
212 * SoupMessage::got-chunk:
215 * Emitted after receiving a chunk of a message body. Note
216 * that "chunk" in this context means any subpiece of the
217 * body, not necessarily the specific HTTP 1.1 chunks sent by
221 g_signal_new ("got_chunk",
222 G_OBJECT_CLASS_TYPE (object_class),
224 G_STRUCT_OFFSET (SoupMessageClass, got_chunk),
226 soup_marshal_NONE__NONE,
230 * SoupMessage::got-body:
233 * Emitted after receiving the complete message body.
236 g_signal_new ("got_body",
237 G_OBJECT_CLASS_TYPE (object_class),
239 G_STRUCT_OFFSET (SoupMessageClass, got_body),
241 soup_marshal_NONE__NONE,
245 * SoupMessage::restarted:
248 * Emitted when a message is about to be re-queued.
251 g_signal_new ("restarted",
252 G_OBJECT_CLASS_TYPE (object_class),
254 G_STRUCT_OFFSET (SoupMessageClass, restarted),
256 soup_marshal_NONE__NONE,
260 * SoupMessage::finished:
263 * Emitted when all HTTP processing is finished for a message.
264 * (After #read-body for client-side code, or after
265 * #wrote-body for server-side code.)
268 g_signal_new ("finished",
269 G_OBJECT_CLASS_TYPE (object_class),
271 G_STRUCT_OFFSET (SoupMessageClass, finished),
273 soup_marshal_NONE__NONE,
280 * @method: the HTTP method for the created request
281 * @uri_string: the destination endpoint (as a string)
283 * Creates a new empty #SoupMessage, which will connect to @uri
285 * Return value: the new #SoupMessage (or %NULL if @uri could not
289 soup_message_new (const char *method, const char *uri_string)
294 uri = soup_uri_new (uri_string);
303 msg = g_object_new (SOUP_TYPE_MESSAGE, NULL);
304 msg->method = method ? method : SOUP_METHOD_GET;
305 SOUP_MESSAGE_GET_PRIVATE (msg)->uri = uri;
311 * soup_message_new_from_uri:
312 * @method: the HTTP method for the created request
313 * @uri: the destination endpoint (as a #SoupUri)
315 * Creates a new empty #SoupMessage, which will connect to @uri
317 * Return value: the new #SoupMessage
320 soup_message_new_from_uri (const char *method, const SoupUri *uri)
324 msg = g_object_new (SOUP_TYPE_MESSAGE, NULL);
325 msg->method = method ? method : SOUP_METHOD_GET;
326 SOUP_MESSAGE_GET_PRIVATE (msg)->uri = soup_uri_copy (uri);
332 * soup_message_set_request:
334 * @content_type: MIME Content-Type of the body
335 * @req_owner: the #SoupOwnership of the passed data buffer.
336 * @req_body: a data buffer containing the body of the message request.
337 * @req_length: the byte length of @req_body.
339 * Convenience function to set the request body of a #SoupMessage
342 soup_message_set_request (SoupMessage *msg,
343 const char *content_type,
344 SoupOwnership req_owner,
348 g_return_if_fail (SOUP_IS_MESSAGE (msg));
349 g_return_if_fail (content_type != NULL);
350 g_return_if_fail (req_body != NULL || req_length == 0);
352 soup_message_add_header (msg->request_headers,
353 "Content-Type", content_type);
354 msg->request.owner = req_owner;
355 msg->request.body = req_body;
356 msg->request.length = req_length;
360 * soup_message_set_response:
362 * @content_type: MIME Content-Type of the body
363 * @resp_owner: the #SoupOwnership of the passed data buffer.
364 * @resp_body: a data buffer containing the body of the message response.
365 * @resp_length: the byte length of @resp_body.
367 * Convenience function to set the response body of a #SoupMessage
370 soup_message_set_response (SoupMessage *msg,
371 const char *content_type,
372 SoupOwnership resp_owner,
376 g_return_if_fail (SOUP_IS_MESSAGE (msg));
377 g_return_if_fail (content_type != NULL);
378 g_return_if_fail (resp_body != NULL || resp_length == 0);
380 soup_message_add_header (msg->response_headers,
381 "Content-Type", content_type);
382 msg->response.owner = resp_owner;
383 msg->response.body = resp_body;
384 msg->response.length = resp_length;
388 * soup_message_wrote_informational:
389 * @msg: a #SoupMessage
391 * Emits the %wrote_informational signal, indicating that the IO layer
392 * finished writing an informational (1xx) response for @msg.
395 soup_message_wrote_informational (SoupMessage *msg)
397 g_signal_emit (msg, signals[WROTE_INFORMATIONAL], 0);
401 * soup_message_wrote_headers:
402 * @msg: a #SoupMessage
404 * Emits the %wrote_headers signal, indicating that the IO layer
405 * finished writing the (non-informational) headers for @msg.
408 soup_message_wrote_headers (SoupMessage *msg)
410 g_signal_emit (msg, signals[WROTE_HEADERS], 0);
414 * soup_message_wrote_chunk:
415 * @msg: a #SoupMessage
417 * Emits the %wrote_chunk signal, indicating that the IO layer
418 * finished writing a chunk of @msg's body.
421 soup_message_wrote_chunk (SoupMessage *msg)
423 g_signal_emit (msg, signals[WROTE_CHUNK], 0);
427 wrote_body (SoupMessage *req)
430 soup_message_run_handlers (req, SOUP_HANDLER_POST_REQUEST);
431 g_object_unref (req);
435 * soup_message_wrote_body:
436 * @msg: a #SoupMessage
438 * Emits the %wrote_body signal, indicating that the IO layer finished
439 * writing the body for @msg.
442 soup_message_wrote_body (SoupMessage *msg)
444 g_signal_emit (msg, signals[WROTE_BODY], 0);
448 * soup_message_got_informational:
449 * @msg: a #SoupMessage
451 * Emits the %got_informational signal, indicating that the IO layer
452 * read a complete informational (1xx) response for @msg.
455 soup_message_got_informational (SoupMessage *msg)
457 g_signal_emit (msg, signals[GOT_INFORMATIONAL], 0);
461 got_headers (SoupMessage *req)
464 soup_message_run_handlers (req, SOUP_HANDLER_PRE_BODY);
465 if (SOUP_MESSAGE_IS_STARTING (req))
466 g_signal_stop_emission (req, signals[GOT_HEADERS], 0);
467 g_object_unref (req);
471 * soup_message_got_headers:
472 * @msg: a #SoupMessage
474 * Emits the %got_headers signal, indicating that the IO layer
475 * finished reading the (non-informational) headers for @msg.
478 soup_message_got_headers (SoupMessage *msg)
480 g_signal_emit (msg, signals[GOT_HEADERS], 0);
484 got_chunk (SoupMessage *req)
487 soup_message_run_handlers (req, SOUP_HANDLER_BODY_CHUNK);
488 if (SOUP_MESSAGE_IS_STARTING (req))
489 g_signal_stop_emission (req, signals[GOT_CHUNK], 0);
490 g_object_unref (req);
494 * soup_message_got_chunk:
495 * @msg: a #SoupMessage
497 * Emits the %got_chunk signal, indicating that the IO layer finished
498 * reading a chunk of @msg's body.
501 soup_message_got_chunk (SoupMessage *msg)
503 g_signal_emit (msg, signals[GOT_CHUNK], 0);
507 got_body (SoupMessage *req)
510 soup_message_run_handlers (req, SOUP_HANDLER_POST_BODY);
511 if (SOUP_MESSAGE_IS_STARTING (req))
512 g_signal_stop_emission (req, signals[GOT_BODY], 0);
513 g_object_unref (req);
517 * soup_message_got_body:
518 * @msg: a #SoupMessage
520 * Emits the %got_body signal, indicating that the IO layer finished
521 * reading the body for @msg.
524 soup_message_got_body (SoupMessage *msg)
526 g_signal_emit (msg, signals[GOT_BODY], 0);
530 restarted (SoupMessage *req)
532 soup_message_io_stop (req);
536 * soup_message_restarted:
537 * @msg: a #SoupMessage
539 * Emits the %restarted signal, indicating that @msg should be
543 soup_message_restarted (SoupMessage *msg)
545 g_signal_emit (msg, signals[RESTARTED], 0);
549 finished (SoupMessage *req)
551 soup_message_io_stop (req);
552 req->status = SOUP_MESSAGE_STATUS_FINISHED;
556 * soup_message_finished:
557 * @msg: a #SoupMessage
559 * Emits the %finished signal, indicating that @msg has been completely
563 soup_message_finished (SoupMessage *msg)
565 g_signal_emit (msg, signals[FINISHED], 0);
569 free_header_list (gpointer name, gpointer vals, gpointer user_data)
572 g_slist_foreach (vals, (GFunc) g_free, NULL);
579 * soup_message_clear_headers:
580 * @hash: a header table (the %request_headers or %response_headers
581 * field of a #SoupMessage)
586 soup_message_clear_headers (GHashTable *hash)
588 g_return_if_fail (hash != NULL);
590 g_hash_table_foreach_remove (hash, free_header_list, NULL);
594 * soup_message_remove_header:
595 * @hash: a header table (the %request_headers or %response_headers
596 * field of a #SoupMessage)
597 * @name: the header name to remove
599 * Removes @name from @hash. If there are multiple values for @name,
600 * they are all removed.
603 soup_message_remove_header (GHashTable *hash, const char *name)
605 gpointer old_key, old_vals;
607 g_return_if_fail (hash != NULL);
608 g_return_if_fail (name != NULL || name[0] != '\0');
610 if (g_hash_table_lookup_extended (hash, name, &old_key, &old_vals)) {
611 g_hash_table_remove (hash, name);
612 free_header_list (old_key, old_vals, NULL);
617 * soup_message_add_header:
618 * @hash: a header table (the %request_headers or %response_headers
619 * field of a #SoupMessage)
620 * @name: the header name to add
621 * @value: the value of the new header
623 * Adds a header with name @name and value @value to @hash. If there
624 * was already a header with name @name, this one does not replace it,
625 * it is merely added to it.
628 soup_message_add_header (GHashTable *hash, const char *name, const char *value)
632 g_return_if_fail (hash != NULL);
633 g_return_if_fail (name != NULL || name [0] != '\0');
634 g_return_if_fail (value != NULL);
636 old_value = g_hash_table_lookup (hash, name);
639 old_value = g_slist_append (old_value, g_strdup (value));
641 g_hash_table_insert (hash, g_strdup (name),
642 g_slist_append (NULL, g_strdup (value)));
647 * soup_message_get_header:
648 * @hash: a header table (the %request_headers or %response_headers
649 * field of a #SoupMessage)
650 * @name: header name.
652 * Finds the first header in @hash with name @name.
654 * Return value: the header's value or %NULL if not found.
657 soup_message_get_header (GHashTable *hash, const char *name)
661 g_return_val_if_fail (hash != NULL, NULL);
662 g_return_val_if_fail (name != NULL || name [0] != '\0', NULL);
664 vals = g_hash_table_lookup (hash, name);
672 * soup_message_get_header_list:
673 * @hash: a header table (the %request_headers or %response_headers
674 * field of a #SoupMessage)
675 * @name: header name.
677 * Finds all headers in @hash with name @name.
679 * Return value: a (possibly empty) list of values of headers with
680 * name @name. The caller should not modify or free this list.
683 soup_message_get_header_list (GHashTable *hash, const char *name)
685 g_return_val_if_fail (hash != NULL, NULL);
686 g_return_val_if_fail (name != NULL || name [0] != '\0', NULL);
688 return g_hash_table_lookup (hash, name);
694 } SoupMessageForeachHeaderData;
697 foreach_value_in_list (gpointer name, gpointer value, gpointer user_data)
699 GSList *vals = value;
700 SoupMessageForeachHeaderData *data = user_data;
703 (*data->func) (name, vals->data, data->user_data);
709 * soup_message_foreach_header:
710 * @hash: a header table (the %request_headers or %response_headers
711 * field of a #SoupMessage)
712 * @func: callback function to run for each header
713 * @user_data: data to pass to @func
715 * Calls @func once for each header value in @hash. (If there are
716 * headers will multiple values, @func will be called once on each
720 soup_message_foreach_header (GHashTable *hash, GHFunc func, gpointer user_data)
722 SoupMessageForeachHeaderData data;
724 g_return_if_fail (hash != NULL);
725 g_return_if_fail (func != NULL);
728 data.user_data = user_data;
729 g_hash_table_foreach (hash, foreach_value_in_list, &data);
733 * soup_message_set_auth:
734 * @msg: a #SoupMessage
735 * @auth: a #SoupAuth, or %NULL
737 * Sets @msg to authenticate to its destination using @auth, which
738 * must have already been fully authenticated. If @auth is %NULL, @msg
739 * will not authenticate to its destination.
742 soup_message_set_auth (SoupMessage *msg, SoupAuth *auth)
744 SoupMessagePrivate *priv;
747 g_return_if_fail (SOUP_IS_MESSAGE (msg));
748 g_return_if_fail (auth == NULL || SOUP_IS_AUTH (auth));
749 g_return_if_fail (auth == NULL || soup_auth_is_authenticated (auth));
751 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
754 g_object_unref (priv->auth);
755 soup_message_remove_header (msg->request_headers, "Authorization");
761 g_object_ref (priv->auth);
762 token = soup_auth_get_authorization (auth, msg);
763 soup_message_add_header (msg->request_headers, "Authorization", token);
768 * soup_message_get_auth:
769 * @msg: a #SoupMessage
771 * Gets the #SoupAuth used by @msg for authentication.
773 * Return value: the #SoupAuth used by @msg for authentication, or
774 * %NULL if @msg is unauthenticated.
777 soup_message_get_auth (SoupMessage *msg)
779 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
781 return SOUP_MESSAGE_GET_PRIVATE (msg)->auth;
785 * soup_message_set_proxy_auth:
786 * @msg: a #SoupMessage
787 * @auth: a #SoupAuth, or %NULL
789 * Sets @msg to authenticate to its proxy using @auth, which must have
790 * already been fully authenticated. If @auth is %NULL, @msg will not
791 * authenticate to its proxy.
794 soup_message_set_proxy_auth (SoupMessage *msg, SoupAuth *auth)
796 SoupMessagePrivate *priv;
799 g_return_if_fail (SOUP_IS_MESSAGE (msg));
800 g_return_if_fail (auth == NULL || SOUP_IS_AUTH (auth));
801 g_return_if_fail (auth == NULL || soup_auth_is_authenticated (auth));
803 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
805 if (priv->proxy_auth) {
806 g_object_unref (priv->proxy_auth);
807 soup_message_remove_header (msg->request_headers,
808 "Proxy-Authorization");
810 priv->proxy_auth = auth;
811 if (!priv->proxy_auth)
814 g_object_ref (priv->proxy_auth);
815 token = soup_auth_get_authorization (auth, msg);
816 soup_message_add_header (msg->request_headers,
817 "Proxy-Authorization", token);
822 * soup_message_get_proxy_auth:
823 * @msg: a #SoupMessage
825 * Gets the #SoupAuth used by @msg for authentication to its proxy..
827 * Return value: the #SoupAuth used by @msg for authentication to its
828 * proxy, or %NULL if @msg isn't authenticated to its proxy.
831 soup_message_get_proxy_auth (SoupMessage *msg)
833 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
835 return SOUP_MESSAGE_GET_PRIVATE (msg)->proxy_auth;
839 * soup_message_cleanup_response:
840 * @req: a #SoupMessage
842 * Cleans up all response data on @req, so that the request can be sent
843 * again and receive a new response. (Eg, as a result of a redirect or
844 * authorization request.)
847 soup_message_cleanup_response (SoupMessage *req)
849 if (req->response.owner == SOUP_BUFFER_SYSTEM_OWNED)
850 g_free (req->response.body);
852 req->response.owner = 0;
853 req->response.body = NULL;
854 req->response.length = 0;
858 soup_message_clear_headers (req->response_headers);
860 req->status_code = SOUP_STATUS_NONE;
861 if (req->reason_phrase) {
862 g_free ((char *) req->reason_phrase);
863 req->reason_phrase = NULL;
868 * soup_message_set_flags:
869 * @msg: a #SoupMessage
870 * @flags: a set of #SoupMessageFlags values
872 * Sets the specified flags on @msg.
875 soup_message_set_flags (SoupMessage *msg, guint flags)
877 g_return_if_fail (SOUP_IS_MESSAGE (msg));
879 SOUP_MESSAGE_GET_PRIVATE (msg)->msg_flags = flags;
883 * soup_message_get_flags:
884 * @msg: a #SoupMessage
886 * Gets the flags on @msg
888 * Return value: the flags
891 soup_message_get_flags (SoupMessage *msg)
893 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), 0);
895 return SOUP_MESSAGE_GET_PRIVATE (msg)->msg_flags;
899 * soup_message_set_http_version:
900 * @msg: a #SoupMessage
901 * @version: the HTTP version
903 * Sets the HTTP version on @msg. The default version is
904 * %SOUP_HTTP_1_1. Setting it to %SOUP_HTTP_1_0 will prevent certain
905 * functionality from being used.
908 soup_message_set_http_version (SoupMessage *msg, SoupHttpVersion version)
910 g_return_if_fail (SOUP_IS_MESSAGE (msg));
912 SOUP_MESSAGE_GET_PRIVATE (msg)->http_version = version;
916 * soup_message_get_http_version:
917 * @msg: a #SoupMessage
919 * Gets the HTTP version of @msg. This is the minimum of the
920 * version from the request and the version from the response.
922 * Return value: the HTTP version
925 soup_message_get_http_version (SoupMessage *msg)
927 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_HTTP_1_0);
929 return SOUP_MESSAGE_GET_PRIVATE (msg)->http_version;
933 * soup_message_is_keepalive:
934 * @msg: a #SoupMessage
936 * Determines whether or not @msg's connection can be kept alive for
937 * further requests after processing @msg.
939 * Return value: %TRUE or %FALSE.
942 soup_message_is_keepalive (SoupMessage *msg)
944 const char *c_conn, *s_conn;
946 c_conn = soup_message_get_header (msg->request_headers, "Connection");
947 s_conn = soup_message_get_header (msg->response_headers, "Connection");
949 if (msg->status_code == SOUP_STATUS_OK &&
950 soup_method_get_id (msg->method) == SOUP_METHOD_ID_CONNECT)
953 if (SOUP_MESSAGE_GET_PRIVATE (msg)->http_version == SOUP_HTTP_1_0) {
954 /* Only persistent if the client requested keepalive
955 * and the server agreed.
958 if (!c_conn || !s_conn)
960 if (g_ascii_strcasecmp (c_conn, "Keep-Alive") != 0 ||
961 g_ascii_strcasecmp (s_conn, "Keep-Alive") != 0)
966 /* Normally persistent unless either side requested otherwise */
967 if (c_conn && g_ascii_strcasecmp (c_conn, "close") == 0)
969 if (s_conn && g_ascii_strcasecmp (s_conn, "close") == 0)
972 /* But not if the server sent a terminate-by-EOF response */
973 if (soup_message_get_response_encoding (msg, NULL) == SOUP_TRANSFER_EOF)
981 * soup_message_set_uri:
982 * @msg: a #SoupMessage
983 * @uri: the new #SoupUri
985 * Sets @msg's URI to @uri. If @msg has already been sent and you want
986 * to re-send it with the new URI, you need to call
987 * soup_session_requeue_message().
990 soup_message_set_uri (SoupMessage *msg, const SoupUri *uri)
992 SoupMessagePrivate *priv;
994 g_return_if_fail (SOUP_IS_MESSAGE (msg));
995 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
998 soup_uri_free (priv->uri);
999 priv->uri = soup_uri_copy (uri);
1003 * soup_message_get_uri:
1004 * @msg: a #SoupMessage
1008 * Return value: the URI @msg is targeted for.
1011 soup_message_get_uri (SoupMessage *msg)
1013 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
1015 return SOUP_MESSAGE_GET_PRIVATE (msg)->uri;
1019 * soup_message_get_request_encoding:
1020 * @msg: a #SoupMessage
1021 * @content_length: a pointer to store the Content-Length in (or
1024 * Gets @msg's request encoding. For an outgoing (client) request,
1025 * this is only valid after the message has been fully set up (from
1026 * the library's perspective, that means not until the message has
1027 * been queued). For an incoming (server) request, this is valid after
1028 * the request headers have been read and @msg->request_headers filled
1031 * Return value: the request encoding (which cannot be
1032 * %SOUP_TRANSFER_UNKNOWN or %SOUP_TRANSFER_EOF). If it is
1033 * %SOUP_TRANSFER_CONTENT_LENGTH, *@content_length will be set to the
1034 * request body's length.
1036 SoupTransferEncoding
1037 soup_message_get_request_encoding (SoupMessage *msg, guint *content_length)
1039 if (SOUP_IS_SERVER_MESSAGE (msg)) {
1040 const char *enc, *len;
1042 enc = soup_message_get_header (msg->request_headers,
1043 "Transfer-Encoding");
1044 len = soup_message_get_header (msg->request_headers,
1047 if (g_ascii_strcasecmp (enc, "chunked") == 0)
1048 return SOUP_TRANSFER_CHUNKED;
1050 return SOUP_TRANSFER_UNKNOWN;
1052 int lval = atoi (len);
1055 return SOUP_TRANSFER_UNKNOWN;
1058 *content_length = lval;
1059 return SOUP_TRANSFER_CONTENT_LENGTH;
1062 return SOUP_TRANSFER_NONE;
1064 if (msg->request.length) {
1066 *content_length = msg->request.length;
1067 return SOUP_TRANSFER_CONTENT_LENGTH;
1069 return SOUP_TRANSFER_NONE;
1074 * soup_message_get_response_encoding:
1075 * @msg: a #SoupMessage
1076 * @content_length: a pointer to store the Content-Length in (or
1079 * Gets @msg's response encoding. For an outgoing (client) request,
1080 * this is only valid after the response headers have been read and
1081 * @msg->response_headers filled in. For an incoming (server) request,
1082 * this is valid after the server handler has run.
1084 * Note that the returned value is the encoding actually used on the
1085 * wire; this will not agree with the response headers in some cases
1086 * (eg, a HEAD response may have a Content-Length header, but will
1087 * still be considered %SOUP_TRANSFER_NONE by this function).
1089 * Return value: the response encoding (which will not be
1090 * %SOUP_TRANSFER_UNKNOWN). If it is %SOUP_TRANSFER_CONTENT_LENGTH,
1091 * *@content_length will be set to the response body's length.
1093 SoupTransferEncoding
1094 soup_message_get_response_encoding (SoupMessage *msg, guint *content_length)
1096 SoupMethodId method = soup_method_get_id (msg->method);
1098 if (method == SOUP_METHOD_ID_HEAD ||
1099 msg->status_code == SOUP_STATUS_NO_CONTENT ||
1100 msg->status_code == SOUP_STATUS_NOT_MODIFIED ||
1101 SOUP_STATUS_IS_INFORMATIONAL (msg->status_code))
1102 return SOUP_TRANSFER_NONE;
1104 if (SOUP_IS_SERVER_MESSAGE (msg)) {
1105 SoupTransferEncoding enc =
1106 soup_server_message_get_encoding ((SoupServerMessage *)msg);
1107 if (enc == SOUP_TRANSFER_UNKNOWN)
1108 enc = SOUP_TRANSFER_CONTENT_LENGTH;
1109 if (enc == SOUP_TRANSFER_CONTENT_LENGTH && content_length)
1110 *content_length = msg->response.length;
1113 const char *enc, *len;
1115 enc = soup_message_get_header (msg->response_headers,
1116 "Transfer-Encoding");
1117 len = soup_message_get_header (msg->response_headers,
1120 if (g_ascii_strcasecmp (enc, "chunked") == 0)
1121 return SOUP_TRANSFER_CHUNKED;
1123 return SOUP_TRANSFER_UNKNOWN;
1125 int lval = atoi (len);
1128 return SOUP_TRANSFER_UNKNOWN;
1131 *content_length = lval;
1132 return SOUP_TRANSFER_CONTENT_LENGTH;
1134 } else if (method == SOUP_METHOD_ID_CONNECT)
1135 return SOUP_TRANSFER_NONE;
1137 return SOUP_TRANSFER_EOF;
1142 * soup_message_set_status:
1143 * @msg: a #SoupMessage
1144 * @status_code: an HTTP status code
1146 * Sets @msg's status code to @status_code. If @status_code is a
1147 * known value, it will also set @msg's reason_phrase.
1150 soup_message_set_status (SoupMessage *msg, guint status_code)
1152 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1153 g_return_if_fail (status_code != 0);
1155 g_free ((char *) msg->reason_phrase);
1157 msg->status_code = status_code;
1158 msg->reason_phrase = g_strdup (soup_status_get_phrase (status_code));
1162 * soup_message_set_status_full:
1163 * @msg: a #SoupMessage
1164 * @status_code: an HTTP status code
1165 * @reason_phrase: a description of the status
1167 * Sets @msg's status code and reason phrase.
1170 soup_message_set_status_full (SoupMessage *msg,
1172 const char *reason_phrase)
1174 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1175 g_return_if_fail (status_code != 0);
1176 g_return_if_fail (reason_phrase != NULL);
1178 g_free ((char *) msg->reason_phrase);
1180 msg->status_code = status_code;
1181 msg->reason_phrase = g_strdup (reason_phrase);
1186 * soup_message_add_chunk:
1187 * @msg: a #SoupMessage
1188 * @owner: the ownership of @body
1190 * @length: length of @body
1192 * Adds a chunk of response data to @body. (Note that currently
1193 * there is no way to send a request using chunked encoding.)
1196 soup_message_add_chunk (SoupMessage *msg,
1197 SoupOwnership owner,
1201 SoupMessagePrivate *priv;
1202 SoupDataBuffer *chunk;
1204 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1205 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1206 g_return_if_fail (body != NULL || length == 0);
1208 chunk = g_new0 (SoupDataBuffer, 1);
1209 if (owner == SOUP_BUFFER_USER_OWNED) {
1210 chunk->owner = SOUP_BUFFER_SYSTEM_OWNED;
1211 chunk->body = g_memdup (body, length);
1213 chunk->owner = owner;
1214 chunk->body = (char *)body;
1216 chunk->length = length;
1219 priv->last_chunk = g_slist_append (priv->last_chunk, chunk);
1220 priv->last_chunk = priv->last_chunk->next;
1222 priv->chunks = priv->last_chunk =
1223 g_slist_append (NULL, chunk);
1228 * soup_message_add_final_chunk:
1229 * @msg: a #SoupMessage
1231 * Adds a final, empty chunk of response data to @body. This must
1232 * be called after adding the last real chunk, to indicate that
1233 * there is no more data.
1236 soup_message_add_final_chunk (SoupMessage *msg)
1238 soup_message_add_chunk (msg, SOUP_BUFFER_STATIC, NULL, 0);
1242 * soup_message_pop_chunk:
1243 * @msg: a #SoupMessage
1245 * Pops a chunk of response data from @msg's chunk list. The caller
1246 * must free @chunk itself, and must handle the data in @chunk
1247 * according to its %ownership.
1249 * Return value: the chunk, or %NULL if there are no chunks left.
1252 soup_message_pop_chunk (SoupMessage *msg)
1254 SoupMessagePrivate *priv;
1255 SoupDataBuffer *chunk;
1257 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
1258 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1263 chunk = priv->chunks->data;
1264 priv->chunks = g_slist_remove (priv->chunks, chunk);
1266 priv->last_chunk = NULL;
1272 free_chunks (SoupMessage *msg)
1274 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1275 SoupDataBuffer *chunk;
1278 for (ch = priv->chunks; ch; ch = ch->next) {
1281 if (chunk->owner == SOUP_BUFFER_SYSTEM_OWNED)
1282 g_free (chunk->body);
1286 g_slist_free (priv->chunks);
1287 priv->chunks = priv->last_chunk = NULL;