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");
760 g_object_ref (priv->auth);
761 token = soup_auth_get_authorization (auth, msg);
762 soup_message_add_header (msg->request_headers, "Authorization", token);
767 * soup_message_get_auth:
768 * @msg: a #SoupMessage
770 * Gets the #SoupAuth used by @msg for authentication.
772 * Return value: the #SoupAuth used by @msg for authentication, or
773 * %NULL if @msg is unauthenticated.
776 soup_message_get_auth (SoupMessage *msg)
778 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
780 return SOUP_MESSAGE_GET_PRIVATE (msg)->auth;
784 * soup_message_set_proxy_auth:
785 * @msg: a #SoupMessage
786 * @auth: a #SoupAuth, or %NULL
788 * Sets @msg to authenticate to its proxy using @auth, which must have
789 * already been fully authenticated. If @auth is %NULL, @msg will not
790 * authenticate to its proxy.
793 soup_message_set_proxy_auth (SoupMessage *msg, SoupAuth *auth)
795 SoupMessagePrivate *priv;
798 g_return_if_fail (SOUP_IS_MESSAGE (msg));
799 g_return_if_fail (auth == NULL || SOUP_IS_AUTH (auth));
800 g_return_if_fail (auth == NULL || soup_auth_is_authenticated (auth));
802 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
804 if (priv->proxy_auth)
805 g_object_unref (priv->proxy_auth);
806 soup_message_remove_header (msg->request_headers,
807 "Proxy-Authorization");
808 priv->proxy_auth = auth;
809 if (!priv->proxy_auth)
812 g_object_ref (priv->proxy_auth);
813 token = soup_auth_get_authorization (auth, msg);
814 soup_message_add_header (msg->request_headers,
815 "Proxy-Authorization", token);
820 * soup_message_get_proxy_auth:
821 * @msg: a #SoupMessage
823 * Gets the #SoupAuth used by @msg for authentication to its proxy..
825 * Return value: the #SoupAuth used by @msg for authentication to its
826 * proxy, or %NULL if @msg isn't authenticated to its proxy.
829 soup_message_get_proxy_auth (SoupMessage *msg)
831 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
833 return SOUP_MESSAGE_GET_PRIVATE (msg)->proxy_auth;
837 * soup_message_cleanup_response:
838 * @req: a #SoupMessage
840 * Cleans up all response data on @req, so that the request can be sent
841 * again and receive a new response. (Eg, as a result of a redirect or
842 * authorization request.)
845 soup_message_cleanup_response (SoupMessage *req)
847 if (req->response.owner == SOUP_BUFFER_SYSTEM_OWNED)
848 g_free (req->response.body);
850 req->response.owner = 0;
851 req->response.body = NULL;
852 req->response.length = 0;
856 soup_message_clear_headers (req->response_headers);
858 req->status_code = SOUP_STATUS_NONE;
859 if (req->reason_phrase) {
860 g_free ((char *) req->reason_phrase);
861 req->reason_phrase = NULL;
866 * soup_message_set_flags:
867 * @msg: a #SoupMessage
868 * @flags: a set of #SoupMessageFlags values
870 * Sets the specified flags on @msg.
873 soup_message_set_flags (SoupMessage *msg, guint flags)
875 g_return_if_fail (SOUP_IS_MESSAGE (msg));
877 SOUP_MESSAGE_GET_PRIVATE (msg)->msg_flags = flags;
881 * soup_message_get_flags:
882 * @msg: a #SoupMessage
884 * Gets the flags on @msg
886 * Return value: the flags
889 soup_message_get_flags (SoupMessage *msg)
891 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), 0);
893 return SOUP_MESSAGE_GET_PRIVATE (msg)->msg_flags;
897 * soup_message_set_http_version:
898 * @msg: a #SoupMessage
899 * @version: the HTTP version
901 * Sets the HTTP version on @msg. The default version is
902 * %SOUP_HTTP_1_1. Setting it to %SOUP_HTTP_1_0 will prevent certain
903 * functionality from being used.
906 soup_message_set_http_version (SoupMessage *msg, SoupHttpVersion version)
908 g_return_if_fail (SOUP_IS_MESSAGE (msg));
910 SOUP_MESSAGE_GET_PRIVATE (msg)->http_version = version;
914 * soup_message_get_http_version:
915 * @msg: a #SoupMessage
917 * Gets the HTTP version of @msg. This is the minimum of the
918 * version from the request and the version from the response.
920 * Return value: the HTTP version
923 soup_message_get_http_version (SoupMessage *msg)
925 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), SOUP_HTTP_1_0);
927 return SOUP_MESSAGE_GET_PRIVATE (msg)->http_version;
931 * soup_message_is_keepalive:
932 * @msg: a #SoupMessage
934 * Determines whether or not @msg's connection can be kept alive for
935 * further requests after processing @msg.
937 * Return value: %TRUE or %FALSE.
940 soup_message_is_keepalive (SoupMessage *msg)
942 const char *c_conn, *s_conn;
944 c_conn = soup_message_get_header (msg->request_headers, "Connection");
945 s_conn = soup_message_get_header (msg->response_headers, "Connection");
947 if (msg->status_code == SOUP_STATUS_OK &&
948 soup_method_get_id (msg->method) == SOUP_METHOD_ID_CONNECT)
951 if (SOUP_MESSAGE_GET_PRIVATE (msg)->http_version == SOUP_HTTP_1_0) {
952 /* Only persistent if the client requested keepalive
953 * and the server agreed.
956 if (!c_conn || !s_conn)
958 if (g_ascii_strcasecmp (c_conn, "Keep-Alive") != 0 ||
959 g_ascii_strcasecmp (s_conn, "Keep-Alive") != 0)
964 /* Normally persistent unless either side requested otherwise */
965 if (c_conn && g_ascii_strcasecmp (c_conn, "close") == 0)
967 if (s_conn && g_ascii_strcasecmp (s_conn, "close") == 0)
970 /* But not if the server sent a terminate-by-EOF response */
971 if (soup_message_get_response_encoding (msg, NULL) == SOUP_TRANSFER_EOF)
979 * soup_message_set_uri:
980 * @msg: a #SoupMessage
981 * @uri: the new #SoupUri
983 * Sets @msg's URI to @uri. If @msg has already been sent and you want
984 * to re-send it with the new URI, you need to call
985 * soup_session_requeue_message().
988 soup_message_set_uri (SoupMessage *msg, const SoupUri *uri)
990 SoupMessagePrivate *priv;
992 g_return_if_fail (SOUP_IS_MESSAGE (msg));
993 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
996 soup_uri_free (priv->uri);
997 priv->uri = soup_uri_copy (uri);
1001 * soup_message_get_uri:
1002 * @msg: a #SoupMessage
1006 * Return value: the URI @msg is targeted for.
1009 soup_message_get_uri (SoupMessage *msg)
1011 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
1013 return SOUP_MESSAGE_GET_PRIVATE (msg)->uri;
1017 * soup_message_get_request_encoding:
1018 * @msg: a #SoupMessage
1019 * @content_length: a pointer to store the Content-Length in (or
1022 * Gets @msg's request encoding. For an outgoing (client) request,
1023 * this is only valid after the message has been fully set up (from
1024 * the library's perspective, that means not until the message has
1025 * been queued). For an incoming (server) request, this is valid after
1026 * the request headers have been read and @msg->request_headers filled
1029 * Return value: the request encoding (which cannot be
1030 * %SOUP_TRANSFER_UNKNOWN or %SOUP_TRANSFER_EOF). If it is
1031 * %SOUP_TRANSFER_CONTENT_LENGTH, *@content_length will be set to the
1032 * request body's length.
1034 SoupTransferEncoding
1035 soup_message_get_request_encoding (SoupMessage *msg, guint *content_length)
1037 if (SOUP_IS_SERVER_MESSAGE (msg)) {
1038 const char *enc, *len;
1040 enc = soup_message_get_header (msg->request_headers,
1041 "Transfer-Encoding");
1042 len = soup_message_get_header (msg->request_headers,
1045 if (g_ascii_strcasecmp (enc, "chunked") == 0)
1046 return SOUP_TRANSFER_CHUNKED;
1048 return SOUP_TRANSFER_UNKNOWN;
1050 int lval = atoi (len);
1053 return SOUP_TRANSFER_UNKNOWN;
1056 *content_length = lval;
1057 return SOUP_TRANSFER_CONTENT_LENGTH;
1060 return SOUP_TRANSFER_NONE;
1062 if (msg->request.length) {
1064 *content_length = msg->request.length;
1065 return SOUP_TRANSFER_CONTENT_LENGTH;
1067 return SOUP_TRANSFER_NONE;
1072 * soup_message_get_response_encoding:
1073 * @msg: a #SoupMessage
1074 * @content_length: a pointer to store the Content-Length in (or
1077 * Gets @msg's response encoding. For an outgoing (client) request,
1078 * this is only valid after the response headers have been read and
1079 * @msg->response_headers filled in. For an incoming (server) request,
1080 * this is valid after the server handler has run.
1082 * Note that the returned value is the encoding actually used on the
1083 * wire; this will not agree with the response headers in some cases
1084 * (eg, a HEAD response may have a Content-Length header, but will
1085 * still be considered %SOUP_TRANSFER_NONE by this function).
1087 * Return value: the response encoding (which will not be
1088 * %SOUP_TRANSFER_UNKNOWN). If it is %SOUP_TRANSFER_CONTENT_LENGTH,
1089 * *@content_length will be set to the response body's length.
1091 SoupTransferEncoding
1092 soup_message_get_response_encoding (SoupMessage *msg, guint *content_length)
1094 SoupMethodId method = soup_method_get_id (msg->method);
1096 if (method == SOUP_METHOD_ID_HEAD ||
1097 msg->status_code == SOUP_STATUS_NO_CONTENT ||
1098 msg->status_code == SOUP_STATUS_NOT_MODIFIED ||
1099 SOUP_STATUS_IS_INFORMATIONAL (msg->status_code))
1100 return SOUP_TRANSFER_NONE;
1102 if (SOUP_IS_SERVER_MESSAGE (msg)) {
1103 SoupTransferEncoding enc =
1104 soup_server_message_get_encoding ((SoupServerMessage *)msg);
1105 if (enc == SOUP_TRANSFER_UNKNOWN)
1106 enc = SOUP_TRANSFER_CONTENT_LENGTH;
1107 if (enc == SOUP_TRANSFER_CONTENT_LENGTH && content_length)
1108 *content_length = msg->response.length;
1111 const char *enc, *len;
1113 enc = soup_message_get_header (msg->response_headers,
1114 "Transfer-Encoding");
1115 len = soup_message_get_header (msg->response_headers,
1118 if (g_ascii_strcasecmp (enc, "chunked") == 0)
1119 return SOUP_TRANSFER_CHUNKED;
1121 return SOUP_TRANSFER_UNKNOWN;
1123 int lval = atoi (len);
1126 return SOUP_TRANSFER_UNKNOWN;
1129 *content_length = lval;
1130 return SOUP_TRANSFER_CONTENT_LENGTH;
1132 } else if (method == SOUP_METHOD_ID_CONNECT)
1133 return SOUP_TRANSFER_NONE;
1135 return SOUP_TRANSFER_EOF;
1140 * soup_message_set_status:
1141 * @msg: a #SoupMessage
1142 * @status_code: an HTTP status code
1144 * Sets @msg's status code to @status_code. If @status_code is a
1145 * known value, it will also set @msg's reason_phrase.
1148 soup_message_set_status (SoupMessage *msg, guint status_code)
1150 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1151 g_return_if_fail (status_code != 0);
1153 g_free ((char *) msg->reason_phrase);
1155 msg->status_code = status_code;
1156 msg->reason_phrase = g_strdup (soup_status_get_phrase (status_code));
1160 * soup_message_set_status_full:
1161 * @msg: a #SoupMessage
1162 * @status_code: an HTTP status code
1163 * @reason_phrase: a description of the status
1165 * Sets @msg's status code and reason phrase.
1168 soup_message_set_status_full (SoupMessage *msg,
1170 const char *reason_phrase)
1172 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1173 g_return_if_fail (status_code != 0);
1174 g_return_if_fail (reason_phrase != NULL);
1176 g_free ((char *) msg->reason_phrase);
1178 msg->status_code = status_code;
1179 msg->reason_phrase = g_strdup (reason_phrase);
1184 * soup_message_add_chunk:
1185 * @msg: a #SoupMessage
1186 * @owner: the ownership of @body
1188 * @length: length of @body
1190 * Adds a chunk of response data to @body. (Note that currently
1191 * there is no way to send a request using chunked encoding.)
1194 soup_message_add_chunk (SoupMessage *msg,
1195 SoupOwnership owner,
1199 SoupMessagePrivate *priv;
1200 SoupDataBuffer *chunk;
1202 g_return_if_fail (SOUP_IS_MESSAGE (msg));
1203 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1204 g_return_if_fail (body != NULL || length == 0);
1206 chunk = g_new0 (SoupDataBuffer, 1);
1207 if (owner == SOUP_BUFFER_USER_OWNED) {
1208 chunk->owner = SOUP_BUFFER_SYSTEM_OWNED;
1209 chunk->body = g_memdup (body, length);
1211 chunk->owner = owner;
1212 chunk->body = (char *)body;
1214 chunk->length = length;
1217 priv->last_chunk = g_slist_append (priv->last_chunk, chunk);
1218 priv->last_chunk = priv->last_chunk->next;
1220 priv->chunks = priv->last_chunk =
1221 g_slist_append (NULL, chunk);
1226 * soup_message_add_final_chunk:
1227 * @msg: a #SoupMessage
1229 * Adds a final, empty chunk of response data to @body. This must
1230 * be called after adding the last real chunk, to indicate that
1231 * there is no more data.
1234 soup_message_add_final_chunk (SoupMessage *msg)
1236 soup_message_add_chunk (msg, SOUP_BUFFER_STATIC, NULL, 0);
1240 * soup_message_pop_chunk:
1241 * @msg: a #SoupMessage
1243 * Pops a chunk of response data from @msg's chunk list. The caller
1244 * must free @chunk itself, and must handle the data in @chunk
1245 * according to its %ownership.
1247 * Return value: the chunk, or %NULL if there are no chunks left.
1250 soup_message_pop_chunk (SoupMessage *msg)
1252 SoupMessagePrivate *priv;
1253 SoupDataBuffer *chunk;
1255 g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
1256 priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1261 chunk = priv->chunks->data;
1262 priv->chunks = g_slist_remove (priv->chunks, chunk);
1264 priv->last_chunk = NULL;
1270 free_chunks (SoupMessage *msg)
1272 SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
1273 SoupDataBuffer *chunk;
1276 for (ch = priv->chunks; ch; ch = ch->next) {
1279 if (chunk->owner == SOUP_BUFFER_SYSTEM_OWNED)
1280 g_free (chunk->body);
1284 g_slist_free (priv->chunks);
1285 priv->chunks = priv->last_chunk = NULL;