loudmouth/lm-proxy.c
changeset 518 cdd6a0c5b439
parent 516 4dd3aa6b83e5
child 607 e2bd14357c9a
--- a/loudmouth/lm-proxy.c	Wed Oct 08 12:44:10 2008 +0200
+++ b/loudmouth/lm-proxy.c	Wed Oct 08 13:11:02 2008 +0200
@@ -31,8 +31,8 @@
  * 
  * connection = lm_connection_new ("myserver");
  * proxy = lm_proxy_new_with_server (LM_PROXY_TYPE_HTTP,
- * 				  "myproxyserver",
- * 				  8080);
+ *                "myproxyserver",
+ *                8080);
  * lm_connection_set_proxy (connection, proxy);
  * ...]]></programlisting></informalexample>
  */
@@ -58,28 +58,28 @@
 #include "lm-utils.h"
 
 struct _LmProxy {
-	LmProxyType  type;
-	gchar       *server;
-	guint        port;
-	gchar       *username;
-	gchar       *password;
-	guint        io_watch;
+    LmProxyType  type;
+    gchar       *server;
+    guint        port;
+    gchar       *username;
+    gchar       *password;
+    guint        io_watch;
 
-        gint         ref_count;
+    gint         ref_count;
 };
 
 static void          proxy_free              (LmProxy       *proxy);
 static gboolean      proxy_http_negotiate    (LmProxy       *proxy,
-					      gint           fd, 
-					      const gchar   *server,
-					      guint          port);
+                                              gint           fd, 
+                                              const gchar   *server,
+                                              guint          port);
 static gboolean      proxy_negotiate         (LmProxy       *proxy,
-					      gint           fd,
-					      const gchar   *server,
-					      guint          port);
+                                              gint           fd,
+                                              const gchar   *server,
+                                              guint          port);
 static gboolean      proxy_http_read_cb      (GIOChannel    *source,
-					      GIOCondition   condition,
-					      gpointer       data);
+                                              GIOCondition   condition,
+                                              gpointer       data);
 static gboolean      proxy_read_cb           (GIOChannel    *source,
                                               GIOCondition   condition,
                                               gpointer       data);
@@ -87,159 +87,159 @@
 static void
 proxy_free (LmProxy *proxy)
 {
-	g_free (proxy->server);
-	g_free (proxy->username);
-	g_free (proxy->password);
+    g_free (proxy->server);
+    g_free (proxy->username);
+    g_free (proxy->password);
 
-	g_free (proxy);
+    g_free (proxy);
 }
 
 static gboolean
 proxy_http_negotiate (LmProxy *proxy, gint fd, const gchar *server, guint port)
 {
-	gchar *str;
+    gchar *str;
 
-	if (proxy->username && proxy->password) {
-		gchar *tmp1;
-		gchar *tmp2;
+    if (proxy->username && proxy->password) {
+        gchar *tmp1;
+        gchar *tmp2;
 
-		tmp1 = g_strdup_printf ("%s:%s",
-					proxy->username, 
-					proxy->password);
-		tmp2 = g_base64_encode ((const guchar *) tmp1, 
-					(gsize) strlen (tmp1));
-		g_free (tmp1);
+        tmp1 = g_strdup_printf ("%s:%s",
+                                proxy->username, 
+                                proxy->password);
+        tmp2 = g_base64_encode ((const guchar *) tmp1, 
+                                (gsize) strlen (tmp1));
+        g_free (tmp1);
 
-		str = g_strdup_printf ("CONNECT %s:%u HTTP/1.1\r\nHost: %s:%u\r\nProxy-Authorization: Basic %s\r\n\r\n",
-				       server, port,
-				       server, port,
-				       tmp2);
-		g_free (tmp2);
-	} else {
-		str = g_strdup_printf ("CONNECT %s:%u HTTP/1.1\r\nHost: %s:%u\r\n\r\n",
-				       server, port, 
-				       server, port);
-	}
+        str = g_strdup_printf ("CONNECT %s:%u HTTP/1.1\r\nHost: %s:%u\r\nProxy-Authorization: Basic %s\r\n\r\n",
+                               server, port,
+                               server, port,
+                               tmp2);
+        g_free (tmp2);
+    } else {
+        str = g_strdup_printf ("CONNECT %s:%u HTTP/1.1\r\nHost: %s:%u\r\n\r\n",
+                               server, port, 
+                               server, port);
+    }
 
-	send (fd, str, strlen (str), 0);
-	g_free (str);
-	return TRUE;
+    send (fd, str, strlen (str), 0);
+    g_free (str);
+    return TRUE;
 }
 
 /* returns TRUE when connected through proxy */
 static gboolean
 proxy_http_read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
 {
-	gchar          buf[512];
-	gsize          bytes_read;
-	GError        *error = NULL;
+    gchar          buf[512];
+    gsize          bytes_read;
+    GError        *error = NULL;
 
-	g_io_channel_read_chars (source, buf, 512, &bytes_read, &error);
+    g_io_channel_read_chars (source, buf, 512, &bytes_read, &error);
 
-	if (bytes_read < 16) {
-		return FALSE;
-	}
+    if (bytes_read < 16) {
+        return FALSE;
+    }
 
-	if (strncmp (buf, "HTTP/1.1 200", 12) != 0 &&
-	    strncmp (buf, "HTTP/1.0 200", 12) != 0) {
-		return FALSE;
-	}
+    if (strncmp (buf, "HTTP/1.1 200", 12) != 0 &&
+        strncmp (buf, "HTTP/1.0 200", 12) != 0) {
+        return FALSE;
+    }
 
-	if (strncmp (buf + (bytes_read - 4), "\r\n\r\n", 4) != 0) {
-		return FALSE;
-	}
+    if (strncmp (buf + (bytes_read - 4), "\r\n\r\n", 4) != 0) {
+        return FALSE;
+    }
 
-	return TRUE;
+    return TRUE;
 }
 
 static gboolean
 proxy_read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
 {
-	LmConnectData *connect_data;
-	LmConnection  *connection;
-	LmProxy       *proxy;
-	gboolean       retval = FALSE;
+    LmConnectData *connect_data;
+    LmConnection  *connection;
+    LmProxy       *proxy;
+    gboolean       retval = FALSE;
 
-	connect_data = (LmConnectData *) data;
-	connection = connect_data->connection;
-	proxy = lm_connection_get_proxy (connection);
+    connect_data = (LmConnectData *) data;
+    connection = connect_data->connection;
+    proxy = lm_connection_get_proxy (connection);
 
-	g_return_val_if_fail (proxy != NULL, FALSE);
+    g_return_val_if_fail (proxy != NULL, FALSE);
 
-	if (lm_connection_is_open (connection)) {
-		return FALSE;
-	}
+    if (lm_connection_is_open (connection)) {
+        return FALSE;
+    }
 
-	switch (lm_proxy_get_type (proxy)) {
-	default:
-	case LM_PROXY_TYPE_NONE:
-		g_assert_not_reached ();
-		break;
-	case LM_PROXY_TYPE_HTTP:
-		retval = proxy_http_read_cb (source, condition, data);
-		break;
-	}
+    switch (lm_proxy_get_type (proxy)) {
+    default:
+    case LM_PROXY_TYPE_NONE:
+        g_assert_not_reached ();
+        break;
+    case LM_PROXY_TYPE_HTTP:
+        retval = proxy_http_read_cb (source, condition, data);
+        break;
+    }
 
-	if (retval == TRUE) {
-		g_source_remove (proxy->io_watch);
-		_lm_old_socket_succeeded ((LmConnectData *) data);
-	}
+    if (retval == TRUE) {
+        g_source_remove (proxy->io_watch);
+        _lm_old_socket_succeeded ((LmConnectData *) data);
+    }
 
-	return FALSE;
+    return FALSE;
 }
 
 gboolean
 proxy_negotiate (LmProxy *proxy, gint fd, const gchar *server, guint port)
 {
-	g_return_val_if_fail (proxy != NULL, FALSE);
+    g_return_val_if_fail (proxy != NULL, FALSE);
 
-	switch (proxy->type) {
-	case LM_PROXY_TYPE_NONE:
-		return TRUE;
-	case LM_PROXY_TYPE_HTTP:
-		return proxy_http_negotiate (proxy, fd, server, port);
-	default:
-		g_assert_not_reached ();
-	}
+    switch (proxy->type) {
+    case LM_PROXY_TYPE_NONE:
+        return TRUE;
+    case LM_PROXY_TYPE_HTTP:
+        return proxy_http_negotiate (proxy, fd, server, port);
+    default:
+        g_assert_not_reached ();
+    }
 
-	return FALSE;
+    return FALSE;
 }
 
 gboolean
 _lm_proxy_connect_cb (GIOChannel *source, GIOCondition condition, gpointer data)
 {
-	LmConnection  *connection;
-	LmConnectData *connect_data;
-	LmProxy       *proxy;
-	int            error;
-	socklen_t      len;
+    LmConnection  *connection;
+    LmConnectData *connect_data;
+    LmProxy       *proxy;
+    int            error;
+    socklen_t      len;
 
-	connect_data = (LmConnectData *) data;
-	connection = connect_data->connection;
-	proxy = lm_connection_get_proxy (connection);
+    connect_data = (LmConnectData *) data;
+    connection = connect_data->connection;
+    proxy = lm_connection_get_proxy (connection);
 
-	g_return_val_if_fail (proxy != NULL, FALSE);
+    g_return_val_if_fail (proxy != NULL, FALSE);
 
-	if (condition == G_IO_ERR) {
-		len = sizeof (error);
-		_lm_sock_get_error (connect_data->fd, &error, &len);
-		_lm_old_socket_failed_with_error (connect_data, error);
-		return FALSE;
-	} else if (condition == G_IO_OUT) {
-		if (!proxy_negotiate (lm_connection_get_proxy (connection), connect_data->fd, lm_connection_get_server (connection), lm_connection_get_port (connection))) {
-			_lm_old_socket_failed (connect_data);
-			return FALSE;
-		}
-			
-		proxy->io_watch = g_io_add_watch (connect_data->io_channel,
-						  G_IO_IN|G_IO_ERR,
-						  (GIOFunc) proxy_read_cb,
-						  connect_data);
-	} else {
-		g_assert_not_reached ();
-	}
+    if (condition == G_IO_ERR) {
+        len = sizeof (error);
+        _lm_sock_get_error (connect_data->fd, &error, &len);
+        _lm_old_socket_failed_with_error (connect_data, error);
+        return FALSE;
+    } else if (condition == G_IO_OUT) {
+        if (!proxy_negotiate (lm_connection_get_proxy (connection), connect_data->fd, lm_connection_get_server (connection), lm_connection_get_port (connection))) {
+            _lm_old_socket_failed (connect_data);
+            return FALSE;
+        }
+            
+        proxy->io_watch = g_io_add_watch (connect_data->io_channel,
+                                          G_IO_IN|G_IO_ERR,
+                                          (GIOFunc) proxy_read_cb,
+                                          connect_data);
+    } else {
+        g_assert_not_reached ();
+    }
 
-	return FALSE;
+    return FALSE;
 }
 
 /**
@@ -254,22 +254,22 @@
 LmProxy * 
 lm_proxy_new (LmProxyType type)
 {
-	LmProxy *proxy;
+    LmProxy *proxy;
 
-	proxy = g_new0 (LmProxy, 1);
-	
-	proxy->ref_count = 1;
-	proxy->type = type;
+    proxy = g_new0 (LmProxy, 1);
+    
+    proxy->ref_count = 1;
+    proxy->type = type;
 
-	switch (proxy->type) {
-	case LM_PROXY_TYPE_HTTP:
-		proxy->port = 8000;
-		break;
-	default:
-		proxy->port = 0;
-	}
+    switch (proxy->type) {
+    case LM_PROXY_TYPE_HTTP:
+        proxy->port = 8000;
+        break;
+    default:
+        proxy->port = 0;
+    }
 
-	return proxy;
+    return proxy;
 }
 
 /**
@@ -285,16 +285,16 @@
  **/
 LmProxy *
 lm_proxy_new_with_server (LmProxyType  type,
-			  const gchar *server,
-			  guint        port)
+                          const gchar *server,
+                          guint        port)
 {
-	LmProxy *proxy;
+    LmProxy *proxy;
 
-	proxy = lm_proxy_new (type);
-	lm_proxy_set_server (proxy, server);
-	lm_proxy_set_port (proxy, port);
+    proxy = lm_proxy_new (type);
+    lm_proxy_set_server (proxy, server);
+    lm_proxy_set_port (proxy, port);
 
-	return proxy;
+    return proxy;
 }
 
 /**
@@ -308,9 +308,9 @@
 LmProxyType
 lm_proxy_get_type (LmProxy *proxy)
 {
-	g_return_val_if_fail (proxy != NULL, LM_PROXY_TYPE_NONE);
+    g_return_val_if_fail (proxy != NULL, LM_PROXY_TYPE_NONE);
 
-	return proxy->type;
+    return proxy->type;
 }
 
 /**
@@ -323,9 +323,9 @@
 void
 lm_proxy_set_type (LmProxy *proxy, LmProxyType type)
 {
-	g_return_if_fail (proxy != NULL);
+    g_return_if_fail (proxy != NULL);
 
-	proxy->type = type;
+    proxy->type = type;
 }
 
 /**
@@ -339,9 +339,9 @@
 const gchar *
 lm_proxy_get_server (LmProxy *proxy)
 {
-	g_return_val_if_fail (proxy != NULL, NULL);
-	
-	return proxy->server;
+    g_return_val_if_fail (proxy != NULL, NULL);
+    
+    return proxy->server;
 }
 
 /**
@@ -354,11 +354,11 @@
 void
 lm_proxy_set_server (LmProxy *proxy, const gchar *server)
 {
-	g_return_if_fail (proxy != NULL);
-	g_return_if_fail (server != NULL);
-	
-	g_free (proxy->server);
-	proxy->server = _lm_utils_hostname_to_punycode (server);
+    g_return_if_fail (proxy != NULL);
+    g_return_if_fail (server != NULL);
+    
+    g_free (proxy->server);
+    proxy->server = _lm_utils_hostname_to_punycode (server);
 }
 
 /**
@@ -372,9 +372,9 @@
 guint
 lm_proxy_get_port (LmProxy *proxy)
 {
-	g_return_val_if_fail (proxy != NULL, 0);
+    g_return_val_if_fail (proxy != NULL, 0);
 
-	return proxy->port;
+    return proxy->port;
 }
 
 /**
@@ -387,9 +387,9 @@
 void
 lm_proxy_set_port (LmProxy *proxy, guint port)
 {
-	g_return_if_fail (proxy != NULL);
-	
-	proxy->port = port;
+    g_return_if_fail (proxy != NULL);
+    
+    proxy->port = port;
 }
 
 /**
@@ -403,9 +403,9 @@
 const gchar *
 lm_proxy_get_username (LmProxy *proxy)
 {
-	g_return_val_if_fail (proxy != NULL, NULL);
-	
-	return proxy->username;
+    g_return_val_if_fail (proxy != NULL, NULL);
+    
+    return proxy->username;
 }
 
 /**
@@ -418,15 +418,15 @@
 void
 lm_proxy_set_username (LmProxy *proxy, const gchar *username)
 {
-	g_return_if_fail (proxy != NULL);
-	
-	g_free (proxy->username);
-	
-	if (username) {
-		proxy->username = g_strdup (username);
-	} else {
-		proxy->username = NULL;
-	}
+    g_return_if_fail (proxy != NULL);
+    
+    g_free (proxy->username);
+    
+    if (username) {
+        proxy->username = g_strdup (username);
+    } else {
+        proxy->username = NULL;
+    }
 }
 /**
  * lm_proxy_get_password:
@@ -439,9 +439,9 @@
 const gchar *
 lm_proxy_get_password (LmProxy *proxy)
 {
-	g_return_val_if_fail (proxy != NULL, NULL);
-	
-	return proxy->password;
+    g_return_val_if_fail (proxy != NULL, NULL);
+    
+    return proxy->password;
 }
 
 /**
@@ -454,15 +454,15 @@
 void
 lm_proxy_set_password (LmProxy *proxy, const gchar *password)
 {
-	g_return_if_fail (proxy != NULL);
-	
-	g_free (proxy->password);
+    g_return_if_fail (proxy != NULL);
+    
+    g_free (proxy->password);
 
-	if (password) {
-		proxy->password = g_strdup (password);
-	} else {
-		proxy->password = NULL;
-	}
+    if (password) {
+        proxy->password = g_strdup (password);
+    } else {
+        proxy->password = NULL;
+    }
 }
 
 /**
@@ -476,10 +476,10 @@
 LmProxy *
 lm_proxy_ref (LmProxy *proxy)
 {
-	g_return_val_if_fail (proxy != NULL, NULL);
-	
-	proxy->ref_count++;
-	return proxy;
+    g_return_val_if_fail (proxy != NULL, NULL);
+    
+    proxy->ref_count++;
+    return proxy;
 }
 
 /**
@@ -492,11 +492,11 @@
 void
 lm_proxy_unref (LmProxy *proxy)
 {
-	g_return_if_fail (proxy != NULL);
-	
-	proxy->ref_count--;
+    g_return_if_fail (proxy != NULL);
+    
+    proxy->ref_count--;
 
-	if (proxy->ref_count == 0) {
-		proxy_free (proxy);
-        }
+    if (proxy->ref_count == 0) {
+        proxy_free (proxy);
+    }
 }