diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-08-31 18:06:54 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-08-31 18:06:54 +0200 |
commit | 49355f01096cee9ebaed4b4c6273b36e4aea50df (patch) | |
tree | 61d4d9e62657fad01a56c78051597ba9f35c9fd6 | |
parent | 56761cf66047107f3459424d8b37bb41d40ccdf4 (diff) |
Use GDataInputStream in the httpd service.
-rw-r--r-- | httpd.c | 67 |
1 files changed, 29 insertions, 38 deletions
@@ -20,53 +20,39 @@ static gboolean service_incoming(GSocketService *service, g_warning("g_socket_connection_get_socket() returned NULL"); return FALSE; } + GDataInputStream *input = g_data_input_stream_new(g_io_stream_get_input_stream((GIOStream*)connection)); + g_data_input_stream_set_newline_type(input, G_DATA_STREAM_NEWLINE_TYPE_CR_LF); GString *string = g_string_new(NULL); - gboolean done = FALSE; - gssize tot = 0; - - while(done == FALSE) { - gchar buffer[0x400]; - gssize len = g_socket_receive(socket, buffer, 0x400, NULL, &error); - if(len == -1) { - g_warning(error->message); - g_error_free(error); - g_string_free(string, TRUE); - return FALSE; + gchar *line; + gsize size; + gchar *path = NULL; + + while(g_socket_is_connected(socket) == TRUE && + (line = g_data_input_stream_read_line(input, &size, NULL, &error)) != NULL) { + if(strlen(line) == 0) { + break; } - tot += len; - g_string_append_len(string, buffer, len); - /* TODO: find a more sane way to stop reading */ - if(string->len >= 4 && g_strcmp0(string->str + string->len - 4, "\r\n\r\n") == 0) { - done = TRUE; + if(path == NULL) { + gchar **firstline = g_strsplit(line, " ", 0); + /* this sets the first character of the last string in + * firstline to \0 (HTTP version) */ + firstline[g_strv_length(firstline)-1] = '\0'; + /* now join from the second string to get the request path */ + gchar *path_escaped = g_strjoinv(" ", firstline + 1); + path = g_uri_unescape_string(path_escaped, NULL); + g_free(path_escaped); + } else { + g_string_append(string, line); } } - g_debug("read %lu bytes", tot); - g_debug("HTTP header:\n%s", string->str); - /* split headers */ - gchar **data = g_strsplit(string->str, "\r\n", 0); + g_object_unref(input); + /* not yet used */ g_string_free(string, TRUE); - gchar **firstline = g_strsplit(data[0], " ", 0); - /* this sets the first character of the last string in - * firstline to \0 (HTTP version) */ - firstline[g_strv_length(firstline)-1] = '\0'; - /* now join from the second string to get the request path */ - gchar *path_escaped = g_strjoinv(" ", firstline + 1); - gchar *path = g_uri_unescape_string(path_escaped, NULL); - g_free(path_escaped); - - g_strfreev(firstline); - - for(gint i = 1; strlen(data[i]); i++) { - g_debug("data[%d] = %s", i, data[i]); - } - - g_strfreev(data); - httpd_commands_handle(connection, path); g_free(path); @@ -88,7 +74,12 @@ gboolean httpd_start() { ss = g_threaded_socket_service_new(10); - g_socket_listener_add_inet_port((GSocketListener*)ss, port, NULL, NULL); + if(g_socket_listener_add_inet_port((GSocketListener*)ss, port, NULL, NULL) == FALSE) { + g_warning("httpd: failed to set port"); + return FALSE; + } else { + g_debug("started httpd"); + } g_signal_connect(ss, "incoming", (GCallback)service_incoming, NULL); g_socket_service_start(ss); |