diff options
author | Søren Sandmann Pedersen <sandmann@redhat.com> | 2008-11-07 23:34:04 -0500 |
---|---|---|
committer | Søren Sandmann Pedersen <sandmann@redhat.com> | 2008-11-07 23:34:04 -0500 |
commit | 60db6637913fef43c6d1c7e114c0f454c7dbe688 (patch) | |
tree | 3378fdf812fa457ef5ed41f677023876091f6acd | |
parent | 9ba66fc3ae6508f34e236e1e805f8250c77d070c (diff) |
Add lac_connection_printf() as a replacement for _write_cstr()
-rw-r--r-- | src/lac.h | 5 | ||||
-rw-r--r-- | src/lacconnection.c | 19 | ||||
-rw-r--r-- | tests/connection-test.c | 17 | ||||
-rw-r--r-- | tests/tls-test.c | 15 |
4 files changed, 22 insertions, 34 deletions
@@ -294,8 +294,9 @@ gpointer lac_connection_get_data (LacConnection *connection); void lac_connection_write (LacConnection *connection, const char *data, guint len); -void lac_connection_write_cstr (LacConnection *connection, - const char *data); +void lac_connection_write_printf (LacConnection *connection, + const char *format, + ...); void lac_connection_shutdown_write (LacConnection *connection); LacConnection * lac_connection_ref (LacConnection *connection); void lac_connection_unref (LacConnection *connection); diff --git a/src/lacconnection.c b/src/lacconnection.c index c7f7740..1d89af6 100644 --- a/src/lacconnection.c +++ b/src/lacconnection.c @@ -153,18 +153,27 @@ lac_connection_write (LacConnection *connection, } void -lac_connection_write_cstr (LacConnection *connection, - const gchar *data) +lac_connection_write_printf (LacConnection *connection, + const gchar *format, + ...) { gsize len; + char *tmp; + va_list args; g_return_if_fail (connection != NULL); - g_return_if_fail (data != NULL); + g_return_if_fail (format != NULL); + + va_start (args, format); + + tmp = g_strdup_vprintf (format, args); - len = strlen (data); + len = strlen (tmp); if (len > 0) - lac_connection_write (connection, data, len); + lac_connection_write (connection, tmp, len); + + g_free (tmp); } const LacAddress * diff --git a/tests/connection-test.c b/tests/connection-test.c index 5096c93..f29eabe 100644 --- a/tests/connection-test.c +++ b/tests/connection-test.c @@ -25,15 +25,6 @@ GMainLoop *main_loop; int n_connections = 0; static void -connection_write (LacConnection *connection, gchar *str) -{ -#if 0 - g_print ("%s", str); -#endif - lac_connection_write (connection, str, strlen (str)); -} - -static void conn_callback (LacConnection *connection, const LacConnectionEvent *event) { gchar *s; @@ -43,15 +34,13 @@ conn_callback (LacConnection *connection, const LacConnectionEvent *event) { case LAC_CONNECTION_EVENT_CONNECT: g_print ("CONNECT\n"); - connection_write (connection, "GET / HTTP/1.1\r\n"); - connection_write (connection, "Host: "); - connection_write (connection, lac_connection_get_data (connection)); - connection_write (connection, "\r\n\r\n"); + lac_connection_write_printf ( + connection, "GET / HTTP/1.1\r\nHost: %s\r\n\r\n", lac_connection_get_data (connection)); break; case LAC_CONNECTION_EVENT_READ: g_print ("READ (%d bytes)\n", event->read.len); - s = g_new (guchar, event->read.len + 1); + s = g_new (gchar, event->read.len + 1); strncpy (s, event->read.data, event->read.len); s[event->read.len] = '\0'; g_print ("%s\n", s); diff --git a/tests/tls-test.c b/tests/tls-test.c index 8c83edb..368ae0e 100644 --- a/tests/tls-test.c +++ b/tests/tls-test.c @@ -26,15 +26,6 @@ int n_connections = 0; const int port = 443; static void -connection_write (LacConnection *connection, gchar *str) -{ -#if 0 - g_print ("%s", str); -#endif - lac_connection_write (connection, str, strlen (str)); -} - -static void conn_callback (LacConnection *connection, const LacConnectionEvent *event) { gchar *s; @@ -44,10 +35,8 @@ conn_callback (LacConnection *connection, const LacConnectionEvent *event) { case LAC_CONNECTION_EVENT_CONNECT: g_print ("CONNECT\n"); - connection_write (connection, "GET / HTTP/1.1\r\n"); - connection_write (connection, "Host: "); - connection_write (connection, lac_connection_get_data (connection)); - connection_write (connection, "\r\n\r\n"); + lac_connection_write_printf ( + connection, "GET / HTTP/1.1\r\nHost: %s\r\n\r\n", lac_connection_get_data (connection)); break; case LAC_CONNECTION_EVENT_READ: |