Index: uspace/lib/http/Makefile
===================================================================
--- uspace/lib/http/Makefile	(revision f9a28312911b00b866d97c0f8c9ab3ea4555edaf)
+++ uspace/lib/http/Makefile	(revision ff364f125938ece58e318df9f0e2415746e266ae)
@@ -37,5 +37,6 @@
 	headers.c \
 	request.c \
-	response.c
+	response.c \
+	receive-buffer.c
 
 include $(USPACE_PREFIX)/Makefile.common
Index: uspace/lib/http/http.c
===================================================================
--- uspace/lib/http/http.c	(revision f9a28312911b00b866d97c0f8c9ab3ea4555edaf)
+++ uspace/lib/http/http.c	(revision ff364f125938ece58e318df9f0e2415746e266ae)
@@ -43,79 +43,10 @@
 
 #include "http.h"
+#include "receive-buffer.h"
 
-void recv_reset(http_t *http)
+static ssize_t http_receive(void *client_data, void *buf, size_t buf_size)
 {
-	http->recv_buffer_in = 0;
-	http->recv_buffer_out = 0;
-}
-
-/** Receive one character (with buffering) */
-int recv_char(http_t *http, char *c, bool consume)
-{
-	if (http->recv_buffer_out == http->recv_buffer_in) {
-		recv_reset(http);
-		
-		ssize_t rc = recv(http->conn_sd, http->recv_buffer, http->buffer_size, 0);
-		if (rc <= 0)
-			return rc;
-		
-		http->recv_buffer_in = rc;
-	}
-	
-	*c = http->recv_buffer[http->recv_buffer_out];
-	if (consume)
-		http->recv_buffer_out++;
-	return EOK;
-}
-
-ssize_t recv_buffer(http_t *http, char *buf, size_t buf_size)
-{
-	/* Flush any buffered data*/
-	if (http->recv_buffer_out != http->recv_buffer_in) {
-		size_t size = min(http->recv_buffer_in - http->recv_buffer_out, buf_size);
-		memcpy(buf, http->recv_buffer + http->recv_buffer_out, size);
-		http->recv_buffer_out += size;
-		return size;
-	}
-	
+	http_t *http = client_data;
 	return recv(http->conn_sd, buf, buf_size, 0);
-}
-
-/** Receive a character and if it is c, discard it from input buffer */
-int recv_discard(http_t *http, char discard)
-{
-	char c = 0;
-	int rc = recv_char(http, &c, false);
-	if (rc != EOK)
-		return rc;
-	if (c != discard)
-		return EOK;
-	return recv_char(http, &c, true);
-}
-
-/* Receive a single line */
-ssize_t recv_line(http_t *http, char *line, size_t size)
-{
-	size_t written = 0;
-	
-	while (written < size) {
-		char c = 0;
-		int rc = recv_char(http, &c, true);
-		if (rc != EOK)
-			return rc;
-		if (c == '\n') {
-			recv_discard(http, '\r');
-			line[written++] = 0;
-			return written;
-		}
-		else if (c == '\r') {
-			recv_discard(http, '\n');
-			line[written++] = 0;
-			return written;
-		}
-		line[written++] = c;
-	}
-	
-	return ELIMIT;
 }
 
@@ -134,7 +65,7 @@
 	
 	http->buffer_size = 4096;
-	http->recv_buffer = malloc(http->buffer_size);
-	if (http->recv_buffer == NULL) {
-		free(http->host);
+	int rc = recv_buffer_init(&http->recv_buffer, http->buffer_size,
+	    http_receive, http);
+	if (rc != EOK) {
 		free(http);
 		return NULL;
@@ -198,5 +129,5 @@
 void http_destroy(http_t *http)
 {
-	free(http->recv_buffer);
+	recv_buffer_fini(&http->recv_buffer);
 	free(http);
 }
Index: uspace/lib/http/http.h
===================================================================
--- uspace/lib/http/http.h	(revision f9a28312911b00b866d97c0f8c9ab3ea4555edaf)
+++ uspace/lib/http/http.h	(revision ff364f125938ece58e318df9f0e2415746e266ae)
@@ -41,4 +41,6 @@
 #include <inet/addr.h>
 
+#include "receive-buffer.h"
+
 typedef struct {
 	char *host;
@@ -50,7 +52,5 @@
 	
 	size_t buffer_size;
-	char *recv_buffer;
-	size_t recv_buffer_in;
-	size_t recv_buffer_out;
+	receive_buffer_t recv_buffer;
 } http_t;
 
@@ -98,10 +98,4 @@
 extern void http_destroy(http_t *);
 
-extern void recv_reset(http_t *);
-extern int recv_char(http_t *, char *, bool);
-extern ssize_t recv_buffer(http_t *, char *, size_t);
-extern int recv_discard(http_t *, char);
-extern ssize_t recv_line(http_t *, char *, size_t);
-
 #endif
 
Index: uspace/lib/http/receive-buffer.c
===================================================================
--- uspace/lib/http/receive-buffer.c	(revision ff364f125938ece58e318df9f0e2415746e266ae)
+++ uspace/lib/http/receive-buffer.c	(revision ff364f125938ece58e318df9f0e2415746e266ae)
@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 2012 Jiri Svoboda
+ * Copyright (c) 2013 Martin Sucha
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup http
+ * @{
+ */
+/**
+ * @file
+ */
+
+#include <stdlib.h>
+#include <str.h>
+#include <errno.h>
+#include <macros.h>
+
+#include "receive-buffer.h"
+
+int recv_buffer_init(receive_buffer_t *rb, size_t buffer_size,
+    receive_func_t receive, void *client_data)
+{
+	rb->receive = receive;
+	rb->client_data = client_data;
+	
+	rb->in = 0;
+	rb->out = 0;
+	rb->size = buffer_size;
+	rb->buffer = malloc(buffer_size);
+	if (rb->buffer == NULL)
+		return ENOMEM;
+	return EOK;
+}
+
+void recv_buffer_fini(receive_buffer_t *rb)
+{
+	free(rb->buffer);
+}
+
+void recv_reset(receive_buffer_t *rb)
+{
+	rb->in = 0;
+	rb->out = 0;
+}
+
+/** Receive one character (with buffering) */
+int recv_char(receive_buffer_t *rb, char *c, bool consume)
+{
+	if (rb->out == rb->in) {
+		recv_reset(rb);
+		
+		ssize_t rc = rb->receive(rb->client_data, rb->buffer, rb->size);
+		if (rc <= 0)
+			return rc;
+		
+		rb->in = rc;
+	}
+	
+	*c = rb->buffer[rb->out];
+	if (consume)
+		rb->out++;
+	return EOK;
+}
+
+ssize_t recv_buffer(receive_buffer_t *rb, char *buf, size_t buf_size)
+{
+	/* Flush any buffered data*/
+	if (rb->out != rb->in) {
+		size_t size = min(rb->in - rb->out, buf_size);
+		memcpy(buf, rb->buffer + rb->out, size);
+		rb->out += size;
+		return size;
+	}
+	
+	return rb->receive(rb->client_data, buf, buf_size);
+}
+
+/** Receive a character and if it is c, discard it from input buffer */
+int recv_discard(receive_buffer_t *rb, char discard)
+{
+	char c = 0;
+	int rc = recv_char(rb, &c, false);
+	if (rc != EOK)
+		return rc;
+	if (c != discard)
+		return EOK;
+	return recv_char(rb, &c, true);
+}
+
+/* Receive a single line */
+ssize_t recv_line(receive_buffer_t *rb, char *line, size_t size)
+{
+	size_t written = 0;
+	
+	while (written < size) {
+		char c = 0;
+		int rc = recv_char(rb, &c, true);
+		if (rc != EOK)
+			return rc;
+		if (c == '\n') {
+			recv_discard(rb, '\r');
+			line[written++] = 0;
+			return written;
+		}
+		else if (c == '\r') {
+			recv_discard(rb, '\n');
+			line[written++] = 0;
+			return written;
+		}
+		line[written++] = c;
+	}
+	
+	return ELIMIT;
+}
+
+/** @}
+ */
Index: uspace/lib/http/receive-buffer.h
===================================================================
--- uspace/lib/http/receive-buffer.h	(revision ff364f125938ece58e318df9f0e2415746e266ae)
+++ uspace/lib/http/receive-buffer.h	(revision ff364f125938ece58e318df9f0e2415746e266ae)
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2013 Martin Sucha
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup http
+ * @{
+ */
+/**
+ * @file
+ */
+
+#ifndef HTTP_RECEIVE_BUFFER_H_
+#define HTTP_RECEIVE_BUFFER_H_
+
+
+/** Receive data.
+ *
+ * @param client_data client data
+ * @param buf buffer to store the data
+ * @param buf_size buffer size
+ * @return number of bytes received or negative error code
+ */
+typedef ssize_t (*receive_func_t)(void *, void *, size_t);
+
+typedef struct {
+	size_t size;
+	char *buffer;
+	size_t in;
+	size_t out;
+	
+	void *client_data;
+	receive_func_t receive;
+} receive_buffer_t;
+
+extern int recv_buffer_init(receive_buffer_t *, size_t, receive_func_t, void *);
+extern void recv_buffer_fini(receive_buffer_t *);
+extern void recv_reset(receive_buffer_t *);
+extern int recv_char(receive_buffer_t *, char *, bool);
+extern ssize_t recv_buffer(receive_buffer_t *, char *, size_t);
+extern int recv_discard(receive_buffer_t *, char);
+extern ssize_t recv_line(receive_buffer_t *, char *, size_t);
+
+
+
+#endif
+
+/** @}
+ */
Index: uspace/lib/http/response.c
===================================================================
--- uspace/lib/http/response.c	(revision f9a28312911b00b866d97c0f8c9ab3ea4555edaf)
+++ uspace/lib/http/response.c	(revision ff364f125938ece58e318df9f0e2415746e266ae)
@@ -106,5 +106,5 @@
 	}
 	
-	int rc = recv_line(http, line, http->buffer_size);
+	int rc = recv_line(&http->recv_buffer, line, http->buffer_size);
 	if (rc < 0)
 		goto error;
@@ -116,5 +116,5 @@
 	
 	while (true) {
-		rc = recv_line(http, line, http->buffer_size);
+		rc = recv_line(&http->recv_buffer, line, http->buffer_size);
 		if (rc < 0)
 			goto error;
@@ -150,5 +150,5 @@
 int http_receive_body(http_t *http, void *buf, size_t buf_size)
 {
-	return recv_buffer(http, buf, buf_size);
+	return recv_buffer(&http->recv_buffer, buf, buf_size);
 }
 
