SoupRequest: Return better GErrors on parsing failures
authorDan Winship <danw@gnome.org>
Sat, 14 Jul 2012 13:24:29 +0000 (09:24 -0400)
committerDan Winship <danw@gnome.org>
Mon, 10 Dec 2012 16:14:57 +0000 (17:14 +0100)
In cases where the SoupMessage API would return SOUP_STATUS_MALFORMED,
return a clearer GError in the SoupRequest API.

libsoup/soup-message-client-io.c
libsoup/soup-message-io.c
libsoup/soup-message-private.h
libsoup/soup-message-server-io.c
libsoup/soup-session.c
libsoup/soup-session.h
po/POTFILES.in

index 06fe5cc..1ee4cbb 100644 (file)
@@ -11,6 +11,8 @@
 
 #include <string.h>
 
+#include <glib/gi18n-lib.h>
+
 #include "soup.h"
 #include "soup-connection.h"
 #include "soup-message-private.h"
@@ -21,7 +23,8 @@ static guint
 parse_response_headers (SoupMessage *req,
                        char *headers, guint headers_len,
                        SoupEncoding *encoding,
-                       gpointer user_data)
+                       gpointer user_data,
+                       GError **error)
 {
        SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (req);
        SoupHTTPVersion version;
@@ -32,8 +35,12 @@ parse_response_headers (SoupMessage *req,
                                          req->response_headers,
                                          &version,
                                          &req->status_code,
-                                         &req->reason_phrase))
+                                         &req->reason_phrase)) {
+               g_set_error_literal (error, SOUP_REQUEST_ERROR,
+                                    SOUP_REQUEST_ERROR_PARSING,
+                                    _("Could not parse HTTP response"));
                return SOUP_STATUS_MALFORMED;
+       }
 
        g_object_notify (G_OBJECT (req), SOUP_MESSAGE_STATUS_CODE);
        g_object_notify (G_OBJECT (req), SOUP_MESSAGE_REASON_PHRASE);
@@ -53,8 +60,12 @@ parse_response_headers (SoupMessage *req,
        else
                *encoding = soup_message_headers_get_encoding (req->response_headers);
 
-       if (*encoding == SOUP_ENCODING_UNRECOGNIZED)
+       if (*encoding == SOUP_ENCODING_UNRECOGNIZED) {
+               g_set_error_literal (error, SOUP_REQUEST_ERROR,
+                                    SOUP_REQUEST_ERROR_ENCODING,
+                                    _("Unrecognized HTTP response encoding"));
                return SOUP_STATUS_MALFORMED;
+       }
 
        return SOUP_STATUS_OK;
 }
index d38d743..03f8c0a 100644 (file)
@@ -503,7 +503,7 @@ io_read (SoupMessage *msg, GCancellable *cancellable, GError **error)
                status = io->parse_headers_cb (msg, (char *)io->read_header_buf->data,
                                               io->read_header_buf->len,
                                               &io->read_encoding,
-                                              io->header_data);
+                                              io->header_data, error);
                g_byte_array_set_size (io->read_header_buf, 0);
 
                if (status != SOUP_STATUS_OK) {
index 8665007..f688515 100644 (file)
@@ -52,7 +52,8 @@ typedef guint    (*SoupMessageParseHeadersFn)(SoupMessage      *msg,
                                              char             *headers,
                                              guint             header_len,
                                              SoupEncoding     *encoding,
-                                             gpointer          user_data);
+                                             gpointer          user_data,
+                                             GError          **error);
 typedef void     (*SoupMessageCompletionFn)  (SoupMessage      *msg,
                                              gpointer          user_data);
 
index a53e5b7..e85896b 100644 (file)
 
 #include <string.h>
 
+#include <glib/gi18n-lib.h>
+
 #include "soup.h"
 #include "soup-message-private.h"
 #include "soup-misc-private.h"
 
 static guint
 parse_request_headers (SoupMessage *msg, char *headers, guint headers_len,
-                      SoupEncoding *encoding, gpointer sock)
+                      SoupEncoding *encoding, gpointer sock, GError **error)
 {
        SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
        char *req_method, *req_path, *url;
@@ -31,8 +33,14 @@ parse_request_headers (SoupMessage *msg, char *headers, guint headers_len,
                                             &req_method,
                                             &req_path,
                                             &version);
-       if (!SOUP_STATUS_IS_SUCCESSFUL (status))
+       if (!SOUP_STATUS_IS_SUCCESSFUL (status)) {
+               if (status == SOUP_STATUS_MALFORMED) {
+                       g_set_error_literal (error, SOUP_REQUEST_ERROR,
+                                            SOUP_REQUEST_ERROR_PARSING,
+                                            _("Could not parse HTTP request"));
+               }
                return status;
+       }
 
        g_object_set (G_OBJECT (msg),
                      SOUP_MESSAGE_METHOD, req_method,
index 20d1c06..85d705a 100644 (file)
@@ -3816,6 +3816,10 @@ soup_session_request_uri (SoupSession *session, SoupURI *uri,
  * @SOUP_REQUEST_ERROR_BAD_URI: the URI could not be parsed
  * @SOUP_REQUEST_ERROR_UNSUPPORTED_URI_SCHEME: the URI scheme is not
  *   supported by this #SoupSession
+ * @SOUP_REQUEST_ERROR_PARSING: the server's response could not
+ *   be parsed
+ * @SOUP_REQUEST_ERROR_ENCODING: the server's response was in an
+ *   unsupported format
  *
  * A #SoupRequest error.
  *
index acc12a9..0390862 100644 (file)
@@ -163,7 +163,9 @@ GQuark soup_request_error_quark (void);
 
 typedef enum {
        SOUP_REQUEST_ERROR_BAD_URI,
-       SOUP_REQUEST_ERROR_UNSUPPORTED_URI_SCHEME
+       SOUP_REQUEST_ERROR_UNSUPPORTED_URI_SCHEME,
+       SOUP_REQUEST_ERROR_PARSING,
+       SOUP_REQUEST_ERROR_ENCODING
 } SoupRequestError;
 
 G_END_DECLS
index ba5d82d..fff1f0e 100644 (file)
@@ -1,6 +1,8 @@
 libsoup/soup-body-input-stream.c
 libsoup/soup-converter-wrapper.c
+libsoup/soup-message-client-io.c
 libsoup/soup-message-io.c
+libsoup/soup-message-server-io.c
 libsoup/soup-request.c
 libsoup/soup-session.c
 libsoup/soup-tld.c