[libgdata] core: Allow HTTPS port to be overriden using the LIBGDATA_HTTPS_PORT envvar



commit 8803a876ade32dfc4428bdac8c48a722e1caa7ad
Author: Philip Withnall <philip tecnocode co uk>
Date:   Sun Jul 7 17:48:10 2013 +0100

    core: Allow HTTPS port to be overriden using the LIBGDATA_HTTPS_PORT envvar
    
    This will be useful for unit testing, allowing network traffic to be
    redirected to a local unit testing server.

 gdata/gdata-client-login-authorizer.c |    6 ++++-
 gdata/gdata-download-stream.c         |    6 ++++-
 gdata/gdata-oauth1-authorizer.c       |   13 ++++++++-
 gdata/gdata-private.h                 |    1 +
 gdata/gdata-service.c                 |   42 +++++++++++++++++++++++++++++++-
 gdata/gdata-upload-stream.c           |    6 ++++-
 6 files changed, 67 insertions(+), 7 deletions(-)
---
diff --git a/gdata/gdata-client-login-authorizer.c b/gdata/gdata-client-login-authorizer.c
index c074bd8..29f17a1 100644
--- a/gdata/gdata-client-login-authorizer.c
+++ b/gdata/gdata-client-login-authorizer.c
@@ -667,6 +667,7 @@ authenticate (GDataClientLoginAuthorizer *self, GDataAuthorizationDomain *domain
        const gchar *service_name;
        guint status;
        GDataSecureString auth_token;
+       SoupURI *_uri;
 
        /* Prepare the request.
         * NOTE: At this point, our non-pageable password is copied into a pageable HTTP request structure. 
We can't do much about this
@@ -686,7 +687,10 @@ authenticate (GDataClientLoginAuthorizer *self, GDataAuthorizationDomain *domain
        g_free (captcha_answer);
 
        /* Build the message */
-       message = soup_message_new (SOUP_METHOD_POST, "https://www.google.com/accounts/ClientLogin";);
+       _uri = soup_uri_new ("https://www.google.com/accounts/ClientLogin";);
+       soup_uri_set_port (_uri, _gdata_service_get_https_port ());
+       message = soup_message_new_from_uri (SOUP_METHOD_POST, _uri);
+       soup_uri_free (_uri);
        soup_message_set_request (message, "application/x-www-form-urlencoded", SOUP_MEMORY_TAKE, 
request_body, strlen (request_body));
 
        /* Send the message */
diff --git a/gdata/gdata-download-stream.c b/gdata/gdata-download-stream.c
index e4c05aa..f76a59e 100644
--- a/gdata/gdata-download-stream.c
+++ b/gdata/gdata-download-stream.c
@@ -337,6 +337,7 @@ gdata_download_stream_constructor (GType type, guint n_construct_params, GObject
        GDataDownloadStreamPrivate *priv;
        GDataServiceClass *klass;
        GObject *object;
+       SoupURI *_uri;
 
        /* Chain up to the parent class */
        object = G_OBJECT_CLASS (gdata_download_stream_parent_class)->constructor (type, n_construct_params, 
construct_params);
@@ -354,7 +355,10 @@ gdata_download_stream_constructor (GType type, guint n_construct_params, GObject
        g_cancellable_connect (priv->cancellable, (GCallback) cancellable_cancel_cb, 
priv->network_cancellable, NULL);
 
        /* Build the message */
-       priv->message = soup_message_new (SOUP_METHOD_GET, priv->download_uri);
+       _uri = soup_uri_new (priv->download_uri);
+       soup_uri_set_port (_uri, _gdata_service_get_https_port ());
+       priv->message = soup_message_new_from_uri (SOUP_METHOD_GET, _uri);
+       soup_uri_free (_uri);
 
        /* Make sure the headers are set */
        klass = GDATA_SERVICE_GET_CLASS (priv->service);
diff --git a/gdata/gdata-oauth1-authorizer.c b/gdata/gdata-oauth1-authorizer.c
index 23e5a10..6cb702c 100644
--- a/gdata/gdata-oauth1-authorizer.c
+++ b/gdata/gdata-oauth1-authorizer.c
@@ -709,6 +709,7 @@ gdata_oauth1_authorizer_request_authentication_uri (GDataOAuth1Authorizer *self,
        GDataAuthorizationDomain *domain;
        GHashTable *response_details;
        const gchar *callback_uri, *_token, *_token_secret, *callback_confirmed;
+       SoupURI *_uri;
 
        g_return_val_if_fail (GDATA_IS_OAUTH1_AUTHORIZER (self), NULL);
        g_return_val_if_fail (token != NULL, NULL);
@@ -756,7 +757,11 @@ gdata_oauth1_authorizer_request_authentication_uri (GDataOAuth1Authorizer *self,
        request_body = soup_form_encode_hash (parameters);
 
        /* Build the message */
-       message = soup_message_new (SOUP_METHOD_POST, "https://www.google.com/accounts/OAuthGetRequestToken";);
+       _uri = soup_uri_new ("https://www.google.com/accounts/OAuthGetRequestToken";);
+       soup_uri_set_port (_uri, _gdata_service_get_https_port ());
+       message = soup_message_new_from_uri (SOUP_METHOD_POST, _uri);
+       soup_uri_free (_uri);
+
        soup_message_set_request (message, "application/x-www-form-urlencoded", SOUP_MEMORY_TAKE, 
request_body, strlen (request_body));
 
        sign_message (self, message, NULL, NULL, parameters);
@@ -1007,6 +1012,7 @@ gdata_oauth1_authorizer_request_authorization (GDataOAuth1Authorizer *self, cons
        GHashTable *parameters;
        GHashTable *response_details;
        const gchar *_token, *_token_secret;
+       SoupURI *_uri;
 
        g_return_val_if_fail (GDATA_IS_OAUTH1_AUTHORIZER (self), FALSE);
        g_return_val_if_fail (token != NULL && *token != '\0', FALSE);
@@ -1029,7 +1035,10 @@ gdata_oauth1_authorizer_request_authorization (GDataOAuth1Authorizer *self, cons
        request_body = soup_form_encode_hash (parameters);
 
        /* Build the message */
-       message = soup_message_new (SOUP_METHOD_POST, "https://www.google.com/accounts/OAuthGetAccessToken";);
+       _uri = soup_uri_new ("https://www.google.com/accounts/OAuthGetAccessToken";);
+       soup_uri_set_port (_uri, _gdata_service_get_https_port ());
+       message = soup_message_new_from_uri (SOUP_METHOD_POST, _uri);
+       soup_uri_free (_uri);
        soup_message_set_request (message, "application/x-www-form-urlencoded", SOUP_MEMORY_TAKE, 
request_body, strlen (request_body));
 
        sign_message (self, message, token, token_secret, parameters);
diff --git a/gdata/gdata-private.h b/gdata/gdata-private.h
index 11a3796..73905f4 100644
--- a/gdata/gdata-private.h
+++ b/gdata/gdata-private.h
@@ -58,6 +58,7 @@ G_GNUC_INTERNAL SoupMessage *_gdata_service_query (GDataService *self, GDataAuth
                                                    GCancellable *cancellable, GError **error) 
G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
 G_GNUC_INTERNAL const gchar *_gdata_service_get_scheme (void) G_GNUC_CONST;
 G_GNUC_INTERNAL gchar *_gdata_service_build_uri (const gchar *format, ...) G_GNUC_PRINTF (1, 2) 
G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
+G_GNUC_INTERNAL guint _gdata_service_get_https_port (void);
 G_GNUC_INTERNAL gchar *_gdata_service_fix_uri_scheme (const gchar *uri) G_GNUC_WARN_UNUSED_RESULT 
G_GNUC_MALLOC;
 G_GNUC_INTERNAL GDataLogLevel _gdata_service_get_log_level (void) G_GNUC_CONST;
 G_GNUC_INTERNAL SoupSession *_gdata_service_build_session (void) G_GNUC_WARN_UNUSED_RESULT G_GNUC_MALLOC;
diff --git a/gdata/gdata-service.c b/gdata/gdata-service.c
index 928ca40..7ae2462 100644
--- a/gdata/gdata-service.c
+++ b/gdata/gdata-service.c
@@ -539,9 +539,13 @@ _gdata_service_build_message (GDataService *self, GDataAuthorizationDomain *doma
 {
        SoupMessage *message;
        GDataServiceClass *klass;
+       SoupURI *_uri;
 
-       /* Create the message */
-       message = soup_message_new (method, uri);
+       /* Create the message. Allow changing the HTTPS port just for testing. */
+       _uri = soup_uri_new (uri);
+       soup_uri_set_port (_uri, _gdata_service_get_https_port ());
+       message = soup_message_new_from_uri (method, _uri);
+       soup_uri_free (_uri);
 
        /* Make sure subclasses set their headers */
        klass = GDATA_SERVICE_GET_CLASS (self);
@@ -1959,6 +1963,40 @@ _gdata_service_fix_uri_scheme (const gchar *uri)
        return g_strdup (uri);
 }
 
+/**
+ * _gdata_service_get_https_port:
+ *
+ * Gets the destination TCP/IP port number which libgdata should use for all outbound HTTPS traffic.
+ * This defaults to 443, but may be overridden using the <code class="literal">LIBGDATA_HTTPS_PORT</code>
+ * environment variable. This is intended to allow network traffic to be redirected to a local server for
+ * unit testing, with a listening port above 1024 so the tests don't need root privileges.
+ *
+ * The value returned by this function may change at any time (e.g. between unit tests), so callers must not 
cache the result.
+ *
+ * Return value: port number to use for HTTPS traffic
+ */
+guint
+_gdata_service_get_https_port (void)
+{
+       const gchar *port_string;
+
+       /* Allow changing the HTTPS port just for testing. */
+       port_string = g_getenv ("LIBGDATA_HTTPS_PORT");
+       if (port_string != NULL) {
+               const gchar *end;
+
+               guint64 port = g_ascii_strtoull (port_string, (gchar **) &end, 10);
+
+               if (port != 0 && *end == '\0') {
+                       g_debug ("Overriding message port to %" G_GUINT64_FORMAT ".", port);
+                       return port;
+               }
+       }
+
+       /* Return the default. */
+       return 443;
+}
+
 /*
  * debug_handler:
  *
diff --git a/gdata/gdata-upload-stream.c b/gdata/gdata-upload-stream.c
index 8add121..69a347e 100644
--- a/gdata/gdata-upload-stream.c
+++ b/gdata/gdata-upload-stream.c
@@ -409,9 +409,13 @@ static SoupMessage *
 build_message (GDataUploadStream *self, const gchar *method, const gchar *upload_uri)
 {
        SoupMessage *new_message;
+       SoupURI *_uri;
 
        /* Build the message */
-       new_message = soup_message_new (method, upload_uri);
+       _uri = soup_uri_new (upload_uri);
+       soup_uri_set_port (_uri, _gdata_service_get_https_port ());
+       new_message = soup_message_new_from_uri (method, _uri);
+       soup_uri_free (_uri);
 
        /* We don't want to accumulate chunks */
        soup_message_body_set_accumulate (new_message->request_body, FALSE);


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