diff options
Diffstat (limited to 'libsoup/soup-message-server-io.c')
-rw-r--r-- | libsoup/soup-message-server-io.c | 127 |
1 files changed, 57 insertions, 70 deletions
diff --git a/libsoup/soup-message-server-io.c b/libsoup/soup-message-server-io.c index 79273197..c4b85cbd 100644 --- a/libsoup/soup-message-server-io.c +++ b/libsoup/soup-message-server-io.c @@ -16,85 +16,76 @@ #include "soup-address.h" #include "soup-auth.h" #include "soup-headers.h" -#include "soup-server-message.h" #include "soup-server.h" #include "soup-socket.h" static guint parse_request_headers (SoupMessage *msg, char *headers, guint headers_len, - SoupTransferEncoding *encoding, guint *content_len, - gpointer sock) + SoupEncoding *encoding, gpointer sock) { SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg); - SoupUri *uri; - char *req_path = NULL, *url; - const char *expect, *req_host; - SoupServer *server; - - if (!soup_headers_parse_request (headers, headers_len, - msg->request_headers, - (char **) &msg->method, - &req_path, - &priv->http_version)) - return SOUP_STATUS_BAD_REQUEST; - - expect = soup_message_get_header (msg->request_headers, "Expect"); - if (expect && !strcmp (expect, "100-continue")) - priv->msg_flags |= SOUP_MESSAGE_EXPECT_CONTINUE; + char *req_method, *req_path, *url; + SoupHTTPVersion version; + const char *req_host; + guint status; + SoupURI *uri; + + status = soup_headers_parse_request (headers, headers_len, + msg->request_headers, + &req_method, + &req_path, + &version); + if (!SOUP_STATUS_IS_SUCCESSFUL (status)) + return status; + + g_object_set (G_OBJECT (msg), + SOUP_MESSAGE_METHOD, req_method, + SOUP_MESSAGE_HTTP_VERSION, version, + NULL); + g_free (req_method); /* Handle request body encoding */ - *encoding = soup_message_get_request_encoding (msg, content_len); - if (*encoding == SOUP_TRANSFER_NONE) { - *encoding = SOUP_TRANSFER_CONTENT_LENGTH; - *content_len = 0; - } else if (*encoding == SOUP_TRANSFER_UNKNOWN) { - if (soup_message_get_header (msg->request_headers, "Transfer-Encoding")) + *encoding = soup_message_headers_get_encoding (msg->request_headers); + if (*encoding == SOUP_ENCODING_UNRECOGNIZED) { + if (soup_message_headers_get (msg->request_headers, "Transfer-Encoding")) return SOUP_STATUS_NOT_IMPLEMENTED; else return SOUP_STATUS_BAD_REQUEST; } /* Generate correct context for request */ - server = soup_server_message_get_server (SOUP_SERVER_MESSAGE (msg)); - req_host = soup_message_get_header (msg->request_headers, "Host"); + req_host = soup_message_headers_get (msg->request_headers, "Host"); if (*req_path != '/') { /* Check for absolute URI */ - SoupUri *absolute; - - absolute = soup_uri_new (req_path); - if (absolute) { - url = g_strdup (req_path); - soup_uri_free (absolute); - } else { + uri = soup_uri_new (req_path); + if (!uri) { g_free (req_path); return SOUP_STATUS_BAD_REQUEST; } } else if (req_host) { url = g_strdup_printf ("%s://%s%s", - soup_server_get_protocol (server) == SOUP_PROTOCOL_HTTPS ? "https" : "http", + soup_socket_is_ssl (sock) ? "https" : "http", req_host, req_path); + uri = soup_uri_new (url); + g_free (url); } else if (priv->http_version == SOUP_HTTP_1_0) { /* No Host header, no AbsoluteUri */ SoupAddress *addr = soup_socket_get_local_address (sock); const char *host = soup_address_get_physical (addr); url = g_strdup_printf ("%s://%s:%d%s", - soup_server_get_protocol (server) == SOUP_PROTOCOL_HTTPS ? "https" : "http", - host, soup_server_get_port (server), + soup_socket_is_ssl (sock) ? "https" : "http", + host, soup_address_get_port (addr), req_path); - } else { - g_free (req_path); - return SOUP_STATUS_BAD_REQUEST; - } + uri = soup_uri_new (url); + g_free (url); + } else + uri = NULL; - uri = soup_uri_new (url); - g_free (url); g_free (req_path); - if (!uri) return SOUP_STATUS_BAD_REQUEST; - soup_message_set_uri (msg, uri); soup_uri_free (uri); @@ -102,46 +93,42 @@ parse_request_headers (SoupMessage *msg, char *headers, guint headers_len, } static void -write_header (gpointer name, gpointer value, gpointer headers) +write_header (const char *name, const char *value, gpointer headers) { - g_string_append_printf (headers, "%s: %s\r\n", - (char *)name, (char *)value); + g_string_append_printf (headers, "%s: %s\r\n", name, value); } static void get_response_headers (SoupMessage *msg, GString *headers, - SoupTransferEncoding *encoding, - gpointer user_data) + SoupEncoding *encoding, gpointer user_data) { - SoupServerMessage *smsg = SOUP_SERVER_MESSAGE (msg); - SoupTransferEncoding claimed_encoding; + SoupEncoding claimed_encoding; g_string_append_printf (headers, "HTTP/1.1 %d %s\r\n", msg->status_code, msg->reason_phrase); - soup_message_foreach_header (msg->response_headers, - write_header, headers); - - *encoding = soup_message_get_response_encoding (msg, NULL); - - claimed_encoding = soup_server_message_get_encoding (smsg); - if (claimed_encoding == SOUP_TRANSFER_CONTENT_LENGTH && - !soup_message_get_header (msg->response_headers, "Content-Length")) { - g_string_append_printf (headers, "Content-Length: %d\r\n", - msg->response.length); - } else if (claimed_encoding == SOUP_TRANSFER_CHUNKED) - g_string_append (headers, "Transfer-Encoding: chunked\r\n"); + claimed_encoding = soup_message_headers_get_encoding (msg->response_headers); + if ((msg->method == SOUP_METHOD_HEAD || + msg->status_code == SOUP_STATUS_NO_CONTENT || + msg->status_code == SOUP_STATUS_NOT_MODIFIED || + SOUP_STATUS_IS_INFORMATIONAL (msg->status_code)) || + (msg->method == SOUP_METHOD_CONNECT && + SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))) + *encoding = SOUP_ENCODING_NONE; + else + *encoding = claimed_encoding; + + if (claimed_encoding == SOUP_ENCODING_CONTENT_LENGTH && + !soup_message_headers_get_content_length (msg->response_headers)) { + soup_message_headers_set_content_length (msg->response_headers, + msg->response_body->length); + } + soup_message_headers_foreach (msg->response_headers, + write_header, headers); g_string_append (headers, "\r\n"); } -/** - * soup_message_read_request: - * @req: an empty #SoupServerMessage - * @sock: socket to receive the request on - * - * Begins the process of receiving a request from @sock into @req. - **/ void soup_message_read_request (SoupMessage *req, SoupSocket *sock) { |