diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | multipart-replace-http-server.c | 12 | ||||
-rw-r--r-- | multipart-replace.c | 15 |
3 files changed, 19 insertions, 10 deletions
@@ -3,4 +3,4 @@ /multipart-replace-http-server *.o -.*.mk
\ No newline at end of file +.*.mk diff --git a/multipart-replace-http-server.c b/multipart-replace-http-server.c index a6a7870..2b30e0f 100644 --- a/multipart-replace-http-server.c +++ b/multipart-replace-http-server.c @@ -153,12 +153,12 @@ int sockstream_listen(const char *type, const char *addr) { struct addrinfo *ai_addr = NULL; struct addrinfo ai_hints = { 0 }; - char *col = strrchr(addr, ':'); + char *host = strdupa(addr); + char *col = strrchr(host, ':'); if (col == NULL) { errno = EINVAL; return -EAI_SYSTEM; } - char *host = strdupa(addr); char *port = &col[1]; *col = '\0'; if (host[0] == '\0') @@ -169,7 +169,9 @@ int sockstream_listen(const char *type, const char *addr) { ai_hints.ai_flags = AI_PASSIVE; int r = getaddrinfo(host, port, &ai_hints, &ai_addr); - if (r != 0) + if (r < 0) + return r; + if (r > 0) return -r; int sock = socket(ai_addr->ai_family, ai_addr->ai_socktype, ai_addr->ai_protocol); @@ -264,7 +266,7 @@ void usage() { } int main(int argc, char *argv[]) { - if (strcmp(argv[1], "-h") == 0) { + if (argc >=2 && strcmp(argv[1], "-h") == 0) { usage(); return EXIT_SUCCESS; } @@ -281,7 +283,7 @@ int main(int argc, char *argv[]) { error(1, 0, "Opening socket %s %s: %s", argv[1], argv[2], gai_strerror(-sock)); } - for (int i = 2; i < argc; i++) + for (int i = 3; i < argc; i++) file_add(argv[i]); signal(SIGPIPE, SIG_IGN); diff --git a/multipart-replace.c b/multipart-replace.c index 30ae7bb..5f0ca4e 100644 --- a/multipart-replace.c +++ b/multipart-replace.c @@ -38,7 +38,6 @@ ssize_t safe_atoi(char *str) { return atoi(str); } -#define rgetline() do { if ((line_len = getline(&line_buf, &line_cap, stream)) < 0) { error(0, 0, "source hung up"); return; } } while(0) void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const char *boundary) { FILE *stream = fdopen(fd, "r"); boundary = boundary_line(boundary); @@ -52,7 +51,11 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const content_length = -1; /* scan for the first non-empty line */ do { - rgetline(); + line_len = getline(&line_buf, &line_cap, stream); + if (line_len < 0) { + error(0, 0, "source hung up"); + return; + } } while (strcmp(line_buf, "\r\n") == 0); /* make sure it matches the boundary separator */ if (strcmp(line_buf, boundary) != 0) { @@ -61,7 +64,11 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const } /* read the frame header (MIME headers) */ do { - rgetline(); + line_len = getline(&line_buf, &line_cap, stream); + if (line_len < 0) { + error(0, 0, "source hung up"); + return; + } /* 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,7 +87,7 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const /* 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, 1, stream) != 1) { + if (fread(&s->back->buf[s->back->len], content_length, 1, stream) != 1) { error(0, ferror(stream), "fread(%zd)", s->back->len); return; } |