diff options
Diffstat (limited to 'src/multipart-replace.c')
-rw-r--r-- | src/multipart-replace.c | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/src/multipart-replace.c b/src/multipart-replace.c index b1fe662..fa893f2 100644 --- a/src/multipart-replace.c +++ b/src/multipart-replace.c @@ -27,7 +27,25 @@ #include "multipart-replace.h" #include "util.h" -#define error(stat, errnum, ...) do { error(0, errnum, __VA_ARGS__); goto end; } while(0) +#define error(stat, errnum, ...) do { \ + error(0, errnum, __VA_ARGS__); \ + ret = stat; \ + goto end; \ + } while(0) +#define stdioerror(stream, name) do { \ + if (feof(stream)) { \ + error(0, 0, "%s: EOF", name); \ + } else { \ + int err = ferror(stream); \ + if (err != 0) { \ + error(0, err, "%s", name); \ + } else { \ + error(0, errno, "%s: umm... what? Not EOF, but also not error. Here's errno", name); \ + } \ + ret = EXIT_FAILURE; \ + } \ + goto end; \ + } while(0) static char *boundary_line(const char *old) { @@ -57,7 +75,8 @@ ssize_t safe_atoi(char *str) { return atoi(str); } -void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const char *_boundary) { +int multipart_replace_reader(struct multipart_replace_stream *s, int fd, const char *_boundary) { + int ret = 0; FILE *stream = fdopen(fd, "r"); char *boundary = boundary_line(_boundary); @@ -66,33 +85,23 @@ 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 (running) { content_length = -1; /* scan for the first non-empty line */ do { line_len = getline(&line_buf, &line_cap, stream); if (line_len < 0) - error(EXIT_FAILURE, ferror(stream), "source hung up"); + stdioerror(stream, "src"); } while (strcmp(line_buf, "\r\n") == 0); /* make sure it matches the boundary separator */ - 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); - } + 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(EXIT_FAILURE, ferror(stream), "source hung up"); + stdioerror(stream, "src"); /* 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); @@ -110,7 +119,7 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const 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(EXIT_FALURE, ferror(stream), "fread(%zd)", s->back->len); + stdioerror(stream, "src"); s->back->len += content_length; /* swap the frames */ @@ -123,10 +132,13 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const } end: free(boundary); + free(line_buf); fclose(stream); + return ret; } -void multipart_replace_writer(struct multipart_replace_stream *s, int fd, const char *_boundary) { +int multipart_replace_writer(struct multipart_replace_stream *s, int fd, const char *_boundary) { + int ret = 0; FILE *stream = fdopen(fd, "w"); struct frame myframe = { 0 }; long lastframe = 0; @@ -145,12 +157,12 @@ void multipart_replace_writer(struct multipart_replace_stream *s, int fd, const /* send the frame to the client */ if (fwrite(boundary, boundary_len, 1, stream) < 1) - error(EXIT_FAILURE, ferror(stream), "fwrite(boundary)"); + stdioerror(stream, "dst <- boundary"); if (fwrite(myframe.buf, myframe.len, 1, stream) < 1) - error(EXIT_FAILURE, ferror(stream), "fwrite(frame.buf)"); + stdioerror(stream, "dst <- frame"); /* send a blank line for pleasantness */ if (fwrite("\r\n", 2, 1, stream) < 1) - error(EXIT_FAILURE, ferror(stream), "fwrite(\"\\r\\n\")"); + stdioerror(stream, "dst <- nl"); fflush(stream); /* poll until there's a new frame */ @@ -166,6 +178,7 @@ void multipart_replace_writer(struct multipart_replace_stream *s, int fd, const free(boundary); free(myframe.buf); fclose(stream); + return ret; } void init_multipart_replace_stream(struct multipart_replace_stream *s) { |