diff --git a/libsoup/soup-message-io.c b/libsoup/soup-message-io.c
index fb505fa1..4d58d239 100644
--- a/libsoup/soup-message-io.c
+++ b/libsoup/soup-message-io.c
@@ -209,6 +209,8 @@ soup_message_io_steal (SoupMessage *msg)
 	return iostream;
 }
 
+#define LIMITHEADERSIZE (64*1024)
+
 static gboolean
 read_headers (SoupMessage *msg, gboolean blocking,
 	      GCancellable *cancellable, GError **error)
@@ -254,6 +256,14 @@ read_headers (SoupMessage *msg, gboolean blocking,
 				break;
 			}
 		}
+        
+        if (io->read_header_buf->len > LIMITHEADERSIZE) {
+            soup_message_set_status (msg, SOUP_STATUS_MALFORMED);
+			g_set_error_literal (error, G_IO_ERROR,
+					     G_IO_ERROR_PARTIAL_INPUT,
+					     _("Header too big"));
+            return FALSE;
+        }
 	}
 
 	io->read_header_buf->data[io->read_header_buf->len] = '\0';
