soup-auth-manager: add soup_auth_manager_use_auth()
[platform/upstream/libsoup.git] / libsoup / soup-message-server-io.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * soup-message-server-io.c: server-side request/response
4  *
5  * Copyright (C) 2000-2003, Ximian, Inc.
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <string.h>
13
14 #include <glib/gi18n-lib.h>
15
16 #include "soup.h"
17 #include "soup-message-private.h"
18 #include "soup-misc-private.h"
19
20 static guint
21 parse_request_headers (SoupMessage *msg, char *headers, guint headers_len,
22                        SoupEncoding *encoding, gpointer sock, GError **error)
23 {
24         SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
25         char *req_method, *req_path, *url;
26         SoupHTTPVersion version;
27         const char *req_host;
28         guint status;
29         SoupURI *uri;
30
31         status = soup_headers_parse_request (headers, headers_len,
32                                              msg->request_headers,
33                                              &req_method,
34                                              &req_path,
35                                              &version);
36         if (!SOUP_STATUS_IS_SUCCESSFUL (status)) {
37                 if (status == SOUP_STATUS_MALFORMED) {
38                         g_set_error_literal (error, SOUP_REQUEST_ERROR,
39                                              SOUP_REQUEST_ERROR_PARSING,
40                                              _("Could not parse HTTP request"));
41                 }
42                 return status;
43         }
44
45         g_object_set (G_OBJECT (msg),
46                       SOUP_MESSAGE_METHOD, req_method,
47                       SOUP_MESSAGE_HTTP_VERSION, version,
48                       NULL);
49         g_free (req_method);
50
51         /* Handle request body encoding */
52         *encoding = soup_message_headers_get_encoding (msg->request_headers);
53         if (*encoding == SOUP_ENCODING_UNRECOGNIZED) {
54                 if (soup_message_headers_get_list (msg->request_headers, "Transfer-Encoding"))
55                         return SOUP_STATUS_NOT_IMPLEMENTED;
56                 else
57                         return SOUP_STATUS_BAD_REQUEST;
58         }
59
60         /* Generate correct context for request */
61         req_host = soup_message_headers_get_one (msg->request_headers, "Host");
62         if (req_host && strchr (req_host, '/')) {
63                 g_free (req_path);
64                 return SOUP_STATUS_BAD_REQUEST;
65         }
66
67         if (!strcmp (req_path, "*") && req_host) {
68                 /* Eg, "OPTIONS * HTTP/1.1" */
69                 url = g_strdup_printf ("%s://%s",
70                                        soup_socket_is_ssl (sock) ? "https" : "http",
71                                        req_host);
72                 uri = soup_uri_new (url);
73                 if (uri)
74                         soup_uri_set_path (uri, "*");
75                 g_free (url);
76         } else if (*req_path != '/') {
77                 /* Must be an absolute URI */
78                 uri = soup_uri_new (req_path);
79         } else if (req_host) {
80                 url = g_strdup_printf ("%s://%s%s",
81                                        soup_socket_is_ssl (sock) ? "https" : "http",
82                                        req_host, req_path);
83                 uri = soup_uri_new (url);
84                 g_free (url);
85         } else if (priv->http_version == SOUP_HTTP_1_0) {
86                 /* No Host header, no AbsoluteUri */
87                 SoupAddress *addr = soup_socket_get_local_address (sock);
88
89                 uri = soup_uri_new (NULL);
90                 soup_uri_set_scheme (uri, soup_socket_is_ssl (sock) ?
91                                      SOUP_URI_SCHEME_HTTPS :
92                                      SOUP_URI_SCHEME_HTTP);
93                 soup_uri_set_host (uri, soup_address_get_physical (addr));
94                 soup_uri_set_port (uri, soup_address_get_port (addr));
95                 soup_uri_set_path (uri, req_path);
96         } else
97                 uri = NULL;
98
99         g_free (req_path);
100
101         if (!SOUP_URI_VALID_FOR_HTTP (uri)) {
102                 /* certainly not "a valid host on the server" (RFC2616 5.2.3)
103                  * SOUP_URI_VALID_FOR_HTTP also guards against uri == NULL
104                  */
105                 if (uri)
106                         soup_uri_free (uri);
107                 return SOUP_STATUS_BAD_REQUEST;
108         }
109
110         soup_message_set_uri (msg, uri);
111         soup_uri_free (uri);
112
113         return SOUP_STATUS_OK;
114 }
115
116 static void
117 handle_partial_get (SoupMessage *msg)
118 {
119         SoupRange *ranges;
120         int nranges;
121         SoupBuffer *full_response;
122
123         /* Make sure the message is set up right for us to return a
124          * partial response; it has to be a GET, the status must be
125          * 200 OK (and in particular, NOT already 206 Partial
126          * Content), and the SoupServer must have already filled in
127          * the response body
128          */
129         if (msg->method != SOUP_METHOD_GET ||
130             msg->status_code != SOUP_STATUS_OK ||
131             soup_message_headers_get_encoding (msg->response_headers) !=
132             SOUP_ENCODING_CONTENT_LENGTH ||
133             msg->response_body->length == 0 ||
134             !soup_message_body_get_accumulate (msg->response_body))
135                 return;
136
137         /* Oh, and there has to have been a valid Range header on the
138          * request, of course.
139          */
140         if (!soup_message_headers_get_ranges (msg->request_headers,
141                                               msg->response_body->length,
142                                               &ranges, &nranges))
143                 return;
144
145         full_response = soup_message_body_flatten (msg->response_body);
146         if (!full_response) {
147                 soup_message_headers_free_ranges (msg->request_headers, ranges);
148                 return;
149         }
150
151         soup_message_set_status (msg, SOUP_STATUS_PARTIAL_CONTENT);
152         soup_message_body_truncate (msg->response_body);
153
154         if (nranges == 1) {
155                 SoupBuffer *range_buf;
156
157                 /* Single range, so just set Content-Range and fix the body. */
158
159                 soup_message_headers_set_content_range (msg->response_headers,
160                                                         ranges[0].start,
161                                                         ranges[0].end,
162                                                         full_response->length);
163                 range_buf = soup_buffer_new_subbuffer (full_response,
164                                                        ranges[0].start,
165                                                        ranges[0].end - ranges[0].start + 1);
166                 soup_message_body_append_buffer (msg->response_body, range_buf);
167                 soup_buffer_free (range_buf);
168         } else {
169                 SoupMultipart *multipart;
170                 SoupMessageHeaders *part_headers;
171                 SoupBuffer *part_body;
172                 const char *content_type;
173                 int i;
174
175                 /* Multiple ranges, so build a multipart/byteranges response
176                  * to replace msg->response_body with.
177                  */
178
179                 multipart = soup_multipart_new ("multipart/byteranges");
180                 content_type = soup_message_headers_get_one (msg->response_headers,
181                                                              "Content-Type");
182                 for (i = 0; i < nranges; i++) {
183                         part_headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_MULTIPART);
184                         if (content_type) {
185                                 soup_message_headers_append (part_headers,
186                                                              "Content-Type",
187                                                              content_type);
188                         }
189                         soup_message_headers_set_content_range (part_headers,
190                                                                 ranges[i].start,
191                                                                 ranges[i].end,
192                                                                 full_response->length);
193                         part_body = soup_buffer_new_subbuffer (full_response,
194                                                                ranges[i].start,
195                                                                ranges[i].end - ranges[i].start + 1);
196                         soup_multipart_append_part (multipart, part_headers,
197                                                     part_body);
198                         soup_message_headers_free (part_headers);
199                         soup_buffer_free (part_body);
200                 }
201
202                 soup_multipart_to_message (multipart, msg->response_headers,
203                                            msg->response_body);
204                 soup_multipart_free (multipart);
205         }
206
207         soup_buffer_free (full_response);
208         soup_message_headers_free_ranges (msg->request_headers, ranges);
209 }
210
211 static void
212 get_response_headers (SoupMessage *msg, GString *headers,
213                       SoupEncoding *encoding, gpointer user_data)
214 {
215         SoupEncoding claimed_encoding;
216         SoupMessageHeadersIter iter;
217         const char *name, *value;
218
219         handle_partial_get (msg);
220
221         g_string_append_printf (headers, "HTTP/1.%c %d %s\r\n",
222                                 soup_message_get_http_version (msg) == SOUP_HTTP_1_0 ? '0' : '1',
223                                 msg->status_code, msg->reason_phrase);
224
225         claimed_encoding = soup_message_headers_get_encoding (msg->response_headers);
226         if ((msg->method == SOUP_METHOD_HEAD ||
227              msg->status_code  == SOUP_STATUS_NO_CONTENT ||
228              msg->status_code  == SOUP_STATUS_NOT_MODIFIED ||
229              SOUP_STATUS_IS_INFORMATIONAL (msg->status_code)) ||
230             (msg->method == SOUP_METHOD_CONNECT &&
231              SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)))
232                 *encoding = SOUP_ENCODING_NONE;
233         else
234                 *encoding = claimed_encoding;
235
236         if (claimed_encoding == SOUP_ENCODING_CONTENT_LENGTH &&
237             !soup_message_headers_get_content_length (msg->response_headers)) {
238                 soup_message_headers_set_content_length (msg->response_headers,
239                                                          msg->response_body->length);
240         }
241
242         soup_message_headers_iter_init (&iter, msg->response_headers);
243         while (soup_message_headers_iter_next (&iter, &name, &value))
244                 g_string_append_printf (headers, "%s: %s\r\n", name, value);
245         g_string_append (headers, "\r\n");
246 }
247
248 void
249 soup_message_read_request (SoupMessage               *msg,
250                            SoupSocket                *sock,
251                            SoupMessageCompletionFn    completion_cb,
252                            gpointer                   user_data)
253 {
254         GMainContext *async_context;
255         GIOStream *iostream;
256
257         g_object_get (sock,
258                       SOUP_SOCKET_ASYNC_CONTEXT, &async_context,
259                       NULL);
260         if (!async_context)
261                 async_context = g_main_context_ref (g_main_context_default ());
262
263         iostream = soup_socket_get_iostream (sock);
264
265         soup_message_io_server (msg, iostream, async_context,
266                                 get_response_headers,
267                                 parse_request_headers,
268                                 sock,
269                                 completion_cb, user_data);
270         if (async_context)
271                 g_main_context_unref (async_context);
272 }