[libsoup] SoupRequest: Return better GErrors on parsing failures



commit fea7925589f6b768252b07c9df8f44ee70bca032
Author: Dan Winship <danw gnome org>
Date:   Sat Jul 14 09:24:29 2012 -0400

    SoupRequest: Return better GErrors on parsing failures
    
    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 |   17 ++++++++++++++---
 libsoup/soup-message-io.c        |    2 +-
 libsoup/soup-message-private.h   |    3 ++-
 libsoup/soup-message-server-io.c |   12 ++++++++++--
 libsoup/soup-session.c           |    4 ++++
 libsoup/soup-session.h           |    4 +++-
 po/POTFILES.in                   |    2 ++
 7 files changed, 36 insertions(+), 8 deletions(-)
---
diff --git a/libsoup/soup-message-client-io.c b/libsoup/soup-message-client-io.c
index 06fe5cc..1ee4cbb 100644
--- a/libsoup/soup-message-client-io.c
+++ b/libsoup/soup-message-client-io.c
@@ -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;
 }
diff --git a/libsoup/soup-message-io.c b/libsoup/soup-message-io.c
index d38d743..03f8c0a 100644
--- a/libsoup/soup-message-io.c
+++ b/libsoup/soup-message-io.c
@@ -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) {
diff --git a/libsoup/soup-message-private.h b/libsoup/soup-message-private.h
index 8665007..f688515 100644
--- a/libsoup/soup-message-private.h
+++ b/libsoup/soup-message-private.h
@@ -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);
 
diff --git a/libsoup/soup-message-server-io.c b/libsoup/soup-message-server-io.c
index a53e5b7..e85896b 100644
--- a/libsoup/soup-message-server-io.c
+++ b/libsoup/soup-message-server-io.c
@@ -11,13 +11,15 @@
 
 #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,
diff --git a/libsoup/soup-session.c b/libsoup/soup-session.c
index 20d1c06..85d705a 100644
--- a/libsoup/soup-session.c
+++ b/libsoup/soup-session.c
@@ -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.
  *
diff --git a/libsoup/soup-session.h b/libsoup/soup-session.h
index acc12a9..0390862 100644
--- a/libsoup/soup-session.h
+++ b/libsoup/soup-session.h
@@ -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
diff --git a/po/POTFILES.in b/po/POTFILES.in
index ba5d82d..fff1f0e 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -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



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]