Fix http_recv.
[paraslash.git] / http_recv.c
index 7a751e4acbc846bf3feb81e22448f90ec1389002..bedd989ee1f364a751f9e21069f8f2a95c434b94 100644 (file)
@@ -6,6 +6,7 @@
 
 /** \file http_recv.c paraslash's http receiver */
 
+#include <regex.h>
 #include <sys/types.h>
 #include <dirent.h>
 
@@ -20,6 +21,7 @@
 #include "net.h"
 #include "string.h"
 #include "fd.h"
+#include "buffer_tree.h"
 
 /** the output buffer size of the http receiver */
 #define BUFSIZE (32 * 1024)
@@ -95,11 +97,14 @@ static void http_recv_pre_select(struct sched *s, struct task *t)
                para_fd_set(phd->fd, &s->rfds, &s->max_fileno);
 }
 
+#define HTTP_RECV_MAX_PENDING (1024 * 1024)
+#define HTTP_RECV_READ_BUF_SIZE 4096
 
 static void http_recv_post_select(struct sched *s, struct task *t)
 {
        struct receiver_node *rn = container_of(t, struct receiver_node, task);
        struct private_http_recv_data *phd = rn->private_data;
+       struct http_recv_args_info *conf = rn->conf;
 
        if (rn->output_error && *rn->output_error < 0) {
                t->error = *rn->output_error;
@@ -127,18 +132,33 @@ static void http_recv_post_select(struct sched *s, struct task *t)
                }
                return;
        }
-       if (rn->loaded >= BUFSIZE) {
-               t->error = -E_HTTP_RECV_OVERRUN;
+       if (conf->buffer_tree_given) {
+               char *buf;
+               if (btr_bytes_pending(rn->btrn) > HTTP_RECV_MAX_PENDING) {
+                       t->error = -E_HTTP_RECV_OVERRUN;
+                       return;
+               }
+               buf = para_malloc(HTTP_RECV_READ_BUF_SIZE);
+               t->error = recv_bin_buffer(phd->fd, buf, HTTP_RECV_READ_BUF_SIZE);
+               if (t->error == 0)
+                       t->error = -E_RECV_EOF;
+               if (t->error < 0) {
+                       free(buf);
+                       return;
+               }
+               btr_add_output(buf, t->error, rn->btrn);
                return;
        }
+       t->error = -E_HTTP_RECV_OVERRUN;
+       if (rn->loaded >= BUFSIZE)
+               return;
        t->error = recv_bin_buffer(phd->fd, rn->buf + rn->loaded,
                BUFSIZE - rn->loaded);
-       if (t->error > 0) {
-               rn->loaded += t->error;
-               return;
-       }
-       if (!t->error)
+       if (t->error == 0)
                t->error = -E_RECV_EOF;
+       if (t->error < 0)
+               return;
+       rn->loaded += t->error;
 }
 
 static void http_recv_close(struct receiver_node *rn)