summaryrefslogtreecommitdiff
path: root/multipart-replace.c
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-03-22 23:26:29 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-03-22 23:26:29 -0400
commitd3a6398406f1a3f222c7ca00d4e4608267148456 (patch)
treef6ae31c550d45d9e95a4a5df69d1a44aea118adf /multipart-replace.c
parente90c4a77b7783468048bc4bc49eb0423ae676a78 (diff)
stuff
Diffstat (limited to 'multipart-replace.c')
-rw-r--r--multipart-replace.c61
1 files changed, 30 insertions, 31 deletions
diff --git a/multipart-replace.c b/multipart-replace.c
index 78732e6..a10ba4d 100644
--- a/multipart-replace.c
+++ b/multipart-replace.c
@@ -10,6 +10,8 @@
#include "multipart-replace.h"
#include "util.h"
+#define error(stat, errnum, ...) do { error(0, errnum, __VA_ARGS__); return; } while(0)
+
static
char *boundary_line(const char *old) {
size_t len = strlen(old);
@@ -47,28 +49,33 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const
ssize_t line_len = 0;
ssize_t content_length = -1;
+ bool first = true;
while (1) {
content_length = -1;
/* scan for the first non-empty line */
do {
line_len = getline(&line_buf, &line_cap, stream);
- if (line_len < 0) {
- error(0, ferror(stream), "source hung up");
- return;
- }
+ if (line_len < 0)
+ error(EXIT_FAILURE, ferror(stream), "source hung up");
} while (strcmp(line_buf, "\r\n") == 0);
/* make sure it matches the boundary separator */
- if (strcmp(line_buf, boundary) != 0) {
- error(0, 0, "line does not match boundary: \"%s\"", line_buf);
- return;
+ if (first) {
+ while (strcmp(line_buf, boundary) != 0) {
+ line_len = getline(&line_buf, &line_cap, stream);
+ if (line_len < 0)
+ error(EXIT_FAILURE, ferror(stream), "source hung up");
+ }
+ first = false;
+ } else {
+ if (strcmp(line_buf, boundary) != 0)
+ error(EXIT_FAILURE, 0, "line does not match boundary: \"%s\"", line_buf);
}
/* read the frame header (MIME headers) */
+ s->back->len = 0;
do {
line_len = getline(&line_buf, &line_cap, stream);
- if (line_len < 0) {
- error(0, ferror(stream), "source hung up");
- return;
- }
+ if (line_len < 0)
+ error(EXIT_FAILURE, ferror(stream), "source hung up");
/* append the line to the frame contents */
if ((ssize_t)s->back->cap < s->back->len + line_len)
s->back->buf = xrealloc(s->back->buf, s->back->cap = s->back->len + line_len);
@@ -80,17 +87,13 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const
}
} while (strcmp(line_buf, "\r\n") != 0);
/* make sure that it included a Content-length header */
- if (content_length < 0) {
- error(0, 0, "did not get frame length");
- return;
- }
+ if (content_length < 0)
+ error(EXIT_FAILURE, 0, "did not get frame length");
/* read the frame contents */
if ((ssize_t)s->back->cap < s->back->len + content_length)
s->back->buf = xrealloc(s->back->buf, s->back->cap = s->back->len + content_length);
- if (fread(&s->back->buf[s->back->len], content_length, 1, stream) != 1) {
- error(0, ferror(stream), "fread(%zd)", s->back->len);
- return;
- }
+ if (fread(&s->back->buf[s->back->len], content_length, 1, stream) != 1)
+ error(EXIT_FALURE, ferror(stream), "fread(%zd)", s->back->len);
s->back->len += content_length;
/* swap the frames */
@@ -100,6 +103,8 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const
s->back = tmp;
s->framecount++;
pthread_rwlock_unlock(&s->frontlock);
+
+ //debug("got frame on %d", fd);
}
}
@@ -128,19 +133,13 @@ void multipart_replace_writer(struct multipart_replace_stream *s, int fd, const
pthread_rwlock_unlock(&s->frontlock);
/* send the frame to the client */
- if (fwrite(boundary, boundary_len, 1, stream) < 1) {
- error(0, ferror(stream), "fwrite(boundary)");
- return;
- }
- if (fwrite(myframe.buf, myframe.len, 1, stream) < 1) {
- error(0, ferror(stream), "fwrite(frame.buf)");
- return;
- }
+ if (fwrite(boundary, boundary_len, 1, stream) < 1)
+ error(EXIT_FAILURE, ferror(stream), "fwrite(boundary)");
+ if (fwrite(myframe.buf, myframe.len, 1, stream) < 1)
+ error(EXIT_FAILURE, ferror(stream), "fwrite(frame.buf)");
/* send a blank line for pleasantness */
- if (fwrite("\r\n", 2, 1, stream) < 1) {
- error(0, ferror(stream), "fwrite(\"\\r\\n\")");
- return;
- }
+ if (fwrite("\r\n", 2, 1, stream) < 1)
+ error(EXIT_FAILURE, ferror(stream), "fwrite(\"\\r\\n\")");
fflush(stream);
}
}