diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2023-12-20 14:08:25 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2023-12-20 14:08:25 +0000 |
commit | 51d0fdce74aeb23ef28336037b2d35aa4c955cee (patch) | |
tree | 9c178c8e6756ec770b85efd54235cd7c2a97888f /src/tipideed | |
parent | d2959364ef8d6bc948474a8facf497788b4e768b (diff) | |
download | tipidee-51d0fdce74aeb23ef28336037b2d35aa4c955cee.tar.xz |
Major refactors; implement ranges
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/tipideed')
-rw-r--r-- | src/tipideed/cgi.c | 28 | ||||
-rw-r--r-- | src/tipideed/deps-exe/tipideed | 3 | ||||
-rw-r--r-- | src/tipideed/errors.c (renamed from src/tipideed/responses.c) | 64 | ||||
-rw-r--r-- | src/tipideed/redirection.c | 29 | ||||
-rw-r--r-- | src/tipideed/regular.c | 28 | ||||
-rw-r--r-- | src/tipideed/send_file.c | 74 | ||||
-rw-r--r-- | src/tipideed/tipideed-internal.h | 27 | ||||
-rw-r--r-- | src/tipideed/tipideed.c | 16 |
8 files changed, 209 insertions, 60 deletions
diff --git a/src/tipideed/cgi.c b/src/tipideed/cgi.c index 343f7af..14da139 100644 --- a/src/tipideed/cgi.c +++ b/src/tipideed/cgi.c @@ -25,13 +25,14 @@ #include <tipidee/tipidee.h> #include "tipideed-internal.h" +#define dienomem(rql, docroot) die500sys(rql, 111, (docroot), "stralloc_catb") + static void addenv_ (tipidee_rql const *rql, char const *docroot, char const *k, char const *v, int slash) { if (!stralloc_cats(&g.sa, k) || !stralloc_catb(&g.sa, "=/", 1 + !!slash) || !stralloc_cats(&g.sa, v) - || !stralloc_0(&g.sa)) - die500sys(rql, 111, docroot, "stralloc_catb") ; + || !stralloc_0(&g.sa)) dienomem(rql, docroot) ; } #define addenv(rql, d, k, v) addenv_(rql, d, k, (v), 0) @@ -40,8 +41,24 @@ static void addenv_ (tipidee_rql const *rql, char const *docroot, char const *k, static void delenv (tipidee_rql const *rql, char const *docroot, char const *k) { if (!stralloc_cats(&g.sa, k) - || !stralloc_0(&g.sa)) - die500sys(rql, 111, docroot, "stralloc_catb") ; + || !stralloc_0(&g.sa)) dienomem(rql, docroot) ; +} + +static void addrequesturi (tipidee_rql const *rql, char const *docroot, char const *script, char const *infopath) +{ + if (!stralloc_cats(&g.sa, "REQUEST_URI=") + || !stralloc_cats(&g.sa, script)) dienomem(rql, docroot) ; + if (infopath) + { + if (!stralloc_catb(&g.sa, "/", 1) + || !stralloc_cats(&g.sa, infopath)) dienomem(rql, docroot) ; + } + if (rql->uri.query) + { + if (!stralloc_catb(&g.sa, "?", 1) + || !stralloc_cats(&g.sa, rql->uri.query)) dienomem(rql, docroot) ; + } + if (!stralloc_0(&g.sa)) dienomem(rql, docroot) ; } static inline void modify_env (tipidee_rql const *rql, char const *docroot, tipidee_headers const *hdr, size_t cl, char const *script, char const *infopath) @@ -61,13 +78,14 @@ static inline void modify_env (tipidee_rql const *rql, char const *docroot, tipi if (rql->uri.query) addenv(rql, docroot, "QUERY_STRING", rql->uri.query) ; else delenv(rql, docroot, "QUERY_STRING") ; addenv(rql, docroot, "SCRIPT_NAME", script) ; + addrequesturi(rql, docroot, script, infopath) ; addenv(rql, docroot, "SERVER_NAME", rql->uri.host) ; { char proto[9] = "HTTP/1.1" ; if (!rql->http_minor) proto[7] = '0' ; addenv(rql, docroot, "SERVER_PROTOCOL", proto) ; } - + for (size_t i = 0 ; i < hdr->n ; i++) { char const *key = hdr->buf + hdr->list[i].left ; diff --git a/src/tipideed/deps-exe/tipideed b/src/tipideed/deps-exe/tipideed index 8e5263e..ac5d4a3 100644 --- a/src/tipideed/deps-exe/tipideed +++ b/src/tipideed/deps-exe/tipideed @@ -1,8 +1,9 @@ cgi.o +errors.o harden.o options.o regular.o -responses.o +redirection.o send_file.o trace.o util.o diff --git a/src/tipideed/responses.c b/src/tipideed/errors.c index 6e32ea9..9b00f3b 100644 --- a/src/tipideed/responses.c +++ b/src/tipideed/errors.c @@ -21,22 +21,28 @@ #include "tipideed-internal.h" -void response_error_early (tipidee_rql const *rql, unsigned int status, char const *reason, char const *text, uint32_t options) +void response_error_early_plus (tipidee_rql const *rql, unsigned int status, char const *reason, char const *text, tipidee_response_header const *v, uint32_t n, uint32_t options) { tain deadline ; - tipidee_response_error_nofile_G(buffer_1, rql, status, reason, text, g.rhdr, g.rhdrn, options & 1 || !g.cont) ; + tipidee_response_error_nofile_G(buffer_1, rql, status, reason, text, g.rhdr, g.rhdrn, v, n, options & 1 || !g.cont) ; tain_add_g(&deadline, &g.writetto) ; if (!buffer_timed_flush_g(buffer_1, &deadline)) strerr_diefu1sys(111, "write to stdout") ; } -void response_error_early_and_exit (tipidee_rql const *rql, unsigned int status, char const *reason, char const *text) +void response_error_early_plus_and_exit (tipidee_rql const *rql, unsigned int status, char const *reason, char const *text, tipidee_response_header const *v, uint32_t n) { - response_error_early(rql, status, reason, text, 1) ; + response_error_early_plus(rql, status, reason, text, v, n, 1) ; log_and_exit(1) ; } -void response_error (tipidee_rql const *rql, char const *docroot, unsigned int status, uint32_t options) +void eexit_405 (tipidee_rql const *rql) +{ + static tipidee_response_header const allowpost = { .key = "Allow", .value = "GET, HEAD, POST", .options = 0 } ; + response_error_early_plus_and_exit(rql, 405, "Method Not Allowed", "Invalid method for the resource.", &allowpost, 1) ; +} + +void response_error_plus (tipidee_rql const *rql, char const *docroot, unsigned int status, tipidee_response_header const *plus, uint32_t plusn, uint32_t options) { tain deadline ; tipidee_defaulttext dt ; @@ -85,7 +91,9 @@ void response_error (tipidee_rql const *rql, char const *docroot, unsigned int s } else { - tipidee_response_file_G(buffer_1, rql, status, dt.reason, &st, tipidee_conf_get_content_type(&g.conf, g.sa.s + pos), g.rhdr, g.rhdrn, options) ; + tipidee_response_file_G(buffer_1, rql, status, dt.reason, &st, st.st_size, tipidee_conf_get_content_type(&g.conf, g.sa.s + pos), g.rhdr, g.rhdrn, options) ; + tipidee_response_header_write(buffer_1, plus, plusn) ; + tipidee_response_header_end(buffer_1) ; tipidee_log_answer(g.logv, rql, status, st.st_size) ; send_file(fd, st.st_size, g.sa.s + pos) ; fd_close(fd) ; @@ -95,56 +103,38 @@ void response_error (tipidee_rql const *rql, char const *docroot, unsigned int s } } - tipidee_response_error_nofile_G(buffer_1, rql, status, dt.reason, dt.text, g.rhdr, g.rhdrn, options & 1 || !g.cont) ; + tipidee_response_error_nofile_G(buffer_1, rql, status, dt.reason, dt.text, g.rhdr, g.rhdrn, plus, plusn, options & 1 || !g.cont) ; tipidee_log_answer(g.logv, rql, status, 0) ; tain_add_g(&deadline, &g.writetto) ; if (!buffer_timed_flush_g(buffer_1, &deadline)) strerr_diefu1sys(111, "write to stdout") ; } -void response_error_and_exit (tipidee_rql const *rql, char const *docroot, unsigned int status) +void response_error_plus_and_exit (tipidee_rql const *rql, char const *docroot, unsigned int status, tipidee_response_header const *plus, uint32_t plusn) { - response_error(rql, docroot, status, 1) ; + response_error_plus(rql, docroot, status, plus, plusn, 1) ; log_and_exit(0) ; } -void response_error_and_die (tipidee_rql const *rql, int e, char const *docroot, unsigned int status, char const *const *v, unsigned int n, uint32_t options) +void response_error_plus_and_die (tipidee_rql const *rql, int e, char const *docroot, unsigned int status, tipidee_response_header const *plus, uint32_t plusn, char const *const *v, unsigned int n, uint32_t options) { int serr = errno ; - response_error(rql, docroot, status, options | 1) ; + response_error_plus(rql, docroot, status, plus, plusn, options | 1) ; errno = serr ; if (options & 1) strerr_dievsys(e, v, n) ; else strerr_diev(e, v, n) ; } -void exit_405_ (tipidee_rql const *rql, uint32_t options) +void exit_405 (tipidee_rql const *rql, char const *docroot, uint32_t options) { - tain deadline ; - tipidee_response_status(buffer_1, rql, 405, "Method Not Allowed") ; - tipidee_response_header_writeall_G(buffer_1, g.rhdr, g.rhdrn, 1) ; - buffer_putsnoflush(buffer_1, "Allow: GET, HEAD") ; - if (options & 1) buffer_putsnoflush(buffer_1, ", POST") ; - buffer_putnoflush(buffer_1, "\r\n\r\n", 4) ; - if (!(options & 2)) tipidee_log_answer(g.logv, rql, 405, 0) ; - tain_add_g(&deadline, &g.writetto) ; - if (!buffer_timed_flush_g(buffer_1, &deadline)) - strerr_diefu1sys(111, "write to stdout") ; - log_and_exit(0) ; + tipidee_response_header hd = { .key = "Allow", .value = options & 1 ? "GET, HEAD, POST" : "GET, HEAD", .options = 0 } ; + response_error_plus_and_exit(rql, docroot, 405, &hd, 1) ; } -void respond_30x (tipidee_rql const *rql, tipidee_redirection const *rd) +void respond_416 (tipidee_rql const *rql, char const *docroot, uint64_t size) { - static unsigned int const status[4] = { 307, 308, 302, 301 } ; - static char const *const reason[4] = { "Temporary Redirect", "Permanent Redirect", "Found", "Moved Permanently" } ; - tain deadline ; - tipidee_response_status(buffer_1, rql, status[rd->type], reason[rd->type]) ; - tipidee_response_header_writeall_G(buffer_1, g.rhdr, g.rhdrn, 0) ; - buffer_putsnoflush(buffer_1, "Content-Length: 0\r\nLocation: ") ; - buffer_putsnoflush(buffer_1, rd->location) ; - if (rd->sub) buffer_putsnoflush(buffer_1, rd->sub) ; - buffer_putnoflush(buffer_1, "\r\n\r\n", 4) ; - tipidee_log_answer(g.logv, rql, status[rd->type], 0) ; - tain_add_g(&deadline, &g.writetto) ; - if (!buffer_timed_flush_g(buffer_1, &deadline)) - strerr_diefu1sys(111, "write to stdout") ; + char buf[8 + UINT64_FMT] = "bytes */" ; + tipidee_response_header cr = { .key = "Content-Range", .value = buf, .options = 0 } ; + buf[8 + uint64_fmt(buf + 8, size)] = 0 ; + response_error_plus(rql, docroot, 416, &cr, 1, 0) ; } diff --git a/src/tipideed/redirection.c b/src/tipideed/redirection.c new file mode 100644 index 0000000..e525e66 --- /dev/null +++ b/src/tipideed/redirection.c @@ -0,0 +1,29 @@ +/* ISC license. */ + +#include <skalibs/buffer.h> +#include <skalibs/strerr.h> +#include <skalibs/tai.h> +#include <skalibs/unix-timed.h> + +#include <tipidee/log.h> +#include <tipidee/util.h> +#include <tipidee/response.h> +#include "tipideed-internal.h" + +void respond_30x (tipidee_rql const *rql, tipidee_redirection const *rd) +{ + static unsigned int const status[4] = { 307, 308, 302, 301 } ; + tipidee_defaulttext dt ; + tain deadline ; + tipidee_util_defaulttext(status[rd->type], &dt) ; + tipidee_response_status(buffer_1, rql, status[rd->type], dt.reason) ; + tipidee_response_header_writeall_G(buffer_1, g.rhdr, g.rhdrn, 0) ; + buffer_putsnoflush(buffer_1, "Content-Length: 0\r\nLocation: ") ; + buffer_putsnoflush(buffer_1, rd->location) ; + if (rd->sub) buffer_putsnoflush(buffer_1, rd->sub) ; + buffer_putnoflush(buffer_1, "\r\n\r\n", 4) ; + tipidee_log_answer(g.logv, rql, status[rd->type], 0) ; + tain_add_g(&deadline, &g.writetto) ; + if (!buffer_timed_flush_g(buffer_1, &deadline)) + strerr_diefu1sys(111, "write to stdout") ; +} diff --git a/src/tipideed/regular.c b/src/tipideed/regular.c index 67fba35..3d82750 100644 --- a/src/tipideed/regular.c +++ b/src/tipideed/regular.c @@ -22,7 +22,8 @@ int respond_regular (tipidee_rql const *rql, char const *docroot, char const *fn if (rql->m == TIPIDEE_METHOD_HEAD) { tain deadline ; - tipidee_response_file_G(buffer_1, rql, 200, "OK", st, ra->content_type, g.rhdr, g.rhdrn, 2 | !g.cont) ; + tipidee_response_file_G(buffer_1, rql, 200, "OK", st, st->st_size, ra->content_type, g.rhdr, g.rhdrn, 2 | !g.cont) ; + tipidee_response_header_end(buffer_1) ; tipidee_log_answer(g.logv, rql, 200, st->st_size) ; tain_add_g(&deadline, &g.writetto) ; if (!buffer_timed_flush_g(buffer_1, &deadline)) @@ -40,7 +41,8 @@ int respond_regular (tipidee_rql const *rql, char const *docroot, char const *fn } else die500sys(rql, 111, docroot, "open ", fn) ; } - tipidee_response_file_G(buffer_1, rql, 200, "OK", st, ra->content_type, g.rhdr, g.rhdrn, 2 | !g.cont) ; + tipidee_response_file_G(buffer_1, rql, 200, "OK", st, st->st_size, ra->content_type, g.rhdr, g.rhdrn, 2 | !g.cont) ; + tipidee_response_header_end(buffer_1) ; tipidee_log_answer(g.logv, rql, 200, st->st_size) ; send_file(fd, st->st_size, fn) ; fd_close(fd) ; @@ -58,10 +60,30 @@ int respond_304 (tipidee_rql const *rql, char const *fn, struct stat const *st) size_t l = tipidee_response_header_lastmodified(fmt, 128, st) ; if (l) buffer_putnoflush(buffer_1, fmt, l) ; } - buffer_putnoflush(buffer_1, "\r\n", 2) ; + tipidee_response_header_end(buffer_1) ; tipidee_log_answer(g.logv, rql, 304, 0) ; tain_add_g(&deadline, &g.writetto) ; if (!buffer_timed_flush_g(buffer_1, &deadline)) strerr_diefu1sys(111, "write to stdout") ; return 0 ; } + +int respond_partial (tipidee_rql const *rql, char const *docroot, char const *fn, struct stat const *st, uint64_t start, uint64_t len, tipidee_resattr const *ra) +{ + int fd = open_read(fn) ; + if (fd == -1) + { + if (errno == EACCES) + { + respond_403(rql, docroot) ; + return 0 ; + } + else die500sys(rql, 111, docroot, "open ", fn) ; + } + tipidee_response_partial_G(buffer_1, rql, st, start, len, ra->content_type, g.rhdr, g.rhdrn, 2 | !g.cont) ; + tipidee_response_header_end(buffer_1) ; + tipidee_log_answer(g.logv, rql, 206, len) ; + send_file_range(fd, start, len, fn) ; + fd_close(fd) ; + return 0 ; +} diff --git a/src/tipideed/send_file.c b/src/tipideed/send_file.c index 6d97167..bcbd1c7 100644 --- a/src/tipideed/send_file.c +++ b/src/tipideed/send_file.c @@ -2,6 +2,66 @@ #include <skalibs/sysdeps.h> +#ifdef SKALIBS_HASSENDFILE + +#include <sys/types.h> +#include <sys/sendfile.h> +#include <limits.h> + +#include <skalibs/uint64.h> +#include <skalibs/strerr.h> +#include <skalibs/tai.h> +#include <skalibs/unix-timed.h> + +#include "tipideed-internal.h" + +void init_splice_pipe (void) +{ +} + +struct sendfile_s +{ + int fd ; + off_t pos ; + uint64_t n ; +} ; + +static int s_getfd (void *p) +{ + (void)p ; + return 1 ; +} + +static int s_isnonempty (void *p) +{ + struct sendfile_s *sf = p ; + return !!sf->n ; +} + +static int s_flush (void *p) +{ + struct sendfile_s *sf = p ; + while (sf->n) + { + ssize_t r = sendfile(1, sf->fd, &sf->pos, sf->n > SSIZE_MAX ? SSIZE_MAX : sf->n) ; + if (r == -1) return 0 ; + sf->n -= r ; + } + return 1 ; +} + +void send_file_range (int fd, uint64_t offset, uint64_t n, char const *fn) +{ + struct sendfile_s sf = { .pos = offset, .n = n, .fd = fd } ; + tain deadline ; + tain_add_g(&deadline, &g.writetto) ; + if (!buffer_timed_flush_g(buffer_1, &deadline)) + strerr_diefu2sys(111, "write", " to stdout") ; + if (!timed_flush_g(&sf, &s_getfd, &s_isnonempty, &s_flush, &deadline)) + strerr_diefu3sys(111, "sendfile ", fn, " to stdout") ; +} + +#else #ifdef SKALIBS_HASSPLICE #include <skalibs/nonposix.h> @@ -10,7 +70,9 @@ #include <stdint.h> #include <unistd.h> +#include <skalibs/uint64.h> #include <skalibs/strerr.h> +#include <skalibs/tai.h> #include <skalibs/djbunix.h> #include <skalibs/unix-timed.h> @@ -53,7 +115,7 @@ static int flush (void *b) return 1 ; } -void send_file (int fd, uint64_t n, char const *fn) +void send_file_range (int fd, uint64_t offset, uint64_t n, char const *fn) { tain deadline ; struct spliceinfo_s si = { .last = 0 } ; @@ -62,7 +124,7 @@ void send_file (int fd, uint64_t n, char const *fn) strerr_diefu2sys(111, "write", " to stdout") ; while (n) { - si.n = splice(fd, 0, g.p[1], 0, n, 0) ; + si.n = splice(fd, &offset, g.p[1], 0, n, 0) ; if (si.n == -1) strerr_diefu2sys(111, "read from ", fn) ; else if (!si.n) strerr_diefu3x(111, "serve ", fn, ": file was truncated") ; else if (si.n > n) @@ -81,7 +143,10 @@ void send_file (int fd, uint64_t n, char const *fn) #else #include <sys/uio.h> +#include <unistd.h> +#include <stdint.h> +#include <skalibs/uint64.h> #include <skalibs/allreadwrite.h> #include <skalibs/buffer.h> #include <skalibs/strerr.h> @@ -94,12 +159,14 @@ void init_splice_pipe (void) { } -void send_file (int fd, uint64_t n, char const *fn) +void send_file_range (int fd, uint64_t offset, uint64_t n, char const *fn) { tain deadline ; struct iovec v[2] ; ssize_t r ; if (!n) goto flushit ; /* I know, I know, but do-while SUCKS */ + if (offset && lseek(fd, offset, SEEK_SET) == -1) + strerr_diefu2sys(111, "lseek on ", fn) ; fillit: buffer_wpeek(buffer_1, v) ; r = allreadv(fd, v, 2) ; @@ -120,3 +187,4 @@ void send_file (int fd, uint64_t n, char const *fn) } #endif +#endif diff --git a/src/tipideed/tipideed-internal.h b/src/tipideed/tipideed-internal.h index 8631861..4b35efa 100644 --- a/src/tipideed/tipideed-internal.h +++ b/src/tipideed/tipideed-internal.h @@ -79,28 +79,33 @@ extern void tipideed_harden (unsigned int) ; /* Responses */ -extern void response_error_early (tipidee_rql const *, unsigned int, char const *, char const *, uint32_t) ; /* set bit 0 for Connection: close */ -extern void response_error_early_and_exit (tipidee_rql const *, unsigned int, char const *, char const *) gccattr_noreturn ; -extern void exit_405_ (tipidee_rql const *, uint32_t) gccattr_noreturn ; /* set bit 0 for Allow: POST, bit 1 for preexit */ +#define response_error_early(rql, status, reason, text, options) response_error_early_plus(rql, status, reason, text, 0, 0, options) +extern void response_error_early_plus (tipidee_rql const *, unsigned int, char const *, char const *, tipidee_response_header const *, uint32_t, uint32_t) ; +#define response_error_early_and_exit(rql, status, reason, text) response_error_early_plus_and_exit(rql, status, reason, (text), 0, 0) +extern void response_error_early_plus_and_exit (tipidee_rql const *, unsigned int, char const *, char const *, tipidee_response_header const *, uint32_t) gccattr_noreturn ; +extern void eexit_405 (tipidee_rql const *) gccattr_noreturn ; #define eexit_400(r, s) response_error_early_and_exit(r, 400, "Bad Request", s) -#define eexit_405(r) exit_405_((r), 3) #define eexit_408(r) response_error_early_and_exit((r), 408, "Request Timeout", 0) #define eexit_413(r, s) response_error_early_and_exit(r, 413, "Request Entity Too Large", s) #define eexit_501(r, s) response_error_early_and_exit(r, 501, "Not Implemented", s) +#define response_error(rql, docroot, status, options) response_error_plus(rql, docroot, status, 0, 0, options) +extern void response_error_plus (tipidee_rql const *, char const *, unsigned int, tipidee_response_header const *, uint32_t, uint32_t) ; /* set bit 0 for Connection: close */ +#define response_error_and_exit(rql, docroot, status) response_error_plus_and_exit(rql, docroot, (status), 0, 0) +extern void response_error_plus_and_exit (tipidee_rql const *, char const *, unsigned int, tipidee_response_header const *, uint32_t) gccattr_noreturn ; +#define response_error_and_die(rql, e, docroot, status, v, n, options) response_error_plus_and_die(rql, e, docroot, status, 0, 0, v, n, options) +extern void response_error_plus_and_die (tipidee_rql const *, int, char const *, unsigned int, tipidee_response_header const *, uint32_t, char const *const *, unsigned int, uint32_t) gccattr_noreturn ; /* set bit 0 for diesys */ -extern void response_error (tipidee_rql const *, char const *, unsigned int, uint32_t) ; /* set bit 0 for Connection: close */ -extern void response_error_and_exit (tipidee_rql const *, char const *, unsigned int) gccattr_noreturn ; -extern void response_error_and_die (tipidee_rql const *, int, char const *, unsigned int, char const *const *, unsigned int, uint32_t) gccattr_noreturn ; /* set bit 0 for diesys */ - +extern void exit_405 (tipidee_rql const *, char const *, uint32_t) gccattr_noreturn ; #define exit_400(r, d) response_error_and_exit(r, (d), 400) -#define exit_405(r) exit_405_((r), 0) #define exit_408(r, d) response_error_and_exit(r, (d), 408) #define exit_413(r, d) response_error_and_exit(r, (d), 413) #define exit_501(r, d) response_error_and_exit(r, (d), 501) extern void respond_30x (tipidee_rql const *, tipidee_redirection const *) ; +extern void respond_416 (tipidee_rql const *, char const *, uint64_t) ; + #define respond_403(r, d) response_error(r, (d), 403, 0) #define respond_404(r, d) response_error(r, (d), 404, 0) #define respond_414(r, d) response_error(r, (d), 414, 0) @@ -126,12 +131,14 @@ extern int respond_options (tipidee_rql const *, uint32_t) ; /* send_file */ extern void init_splice_pipe (void) ; -extern void send_file (int, uint64_t, char const *) ; +extern void send_file_range (int, uint64_t, uint64_t, char const *) ; +#define send_file(fd, n, fn) send_file_range(fd, 0, n, fn) /* regular */ extern int respond_regular (tipidee_rql const *, char const *, char const *, struct stat const *, tipidee_resattr const *) ; +extern int respond_partial (tipidee_rql const *, char const *, char const *, struct stat const *, uint64_t, uint64_t, tipidee_resattr const *) ; extern int respond_304 (tipidee_rql const *, char const *, struct stat const *) ; diff --git a/src/tipideed/tipideed.c b/src/tipideed/tipideed.c index 8323db1..69280f6 100644 --- a/src/tipideed/tipideed.c +++ b/src/tipideed/tipideed.c @@ -11,6 +11,7 @@ #include <skalibs/posixplz.h> #include <skalibs/env.h> #include <skalibs/uint16.h> +#include <skalibs/uint64.h> #include <skalibs/types.h> #include <skalibs/bytestr.h> #include <skalibs/sgetopt.h> @@ -286,7 +287,7 @@ static inline int serve (tipidee_rql *rql, char const *docroot, char *uribuf, ti if (!ra.flags & TIPIDEE_RA_FLAG_CGI) { if (infopath) { respond_404(rql, docroot) ; return 0 ; } - if (rql->m == TIPIDEE_METHOD_POST) exit_405(rql) ; + if (rql->m == TIPIDEE_METHOD_POST) exit_405(rql, docroot, 0) ; } if (rql->m == TIPIDEE_METHOD_OPTIONS) @@ -306,6 +307,19 @@ static inline int serve (tipidee_rql *rql, char const *docroot, char *uribuf, ti && tain_less(&actual, &wanted)) return respond_304(rql, fn, &st) ; } + + if (rql->m == TIPIDEE_METHOD_GET) + { + infopath = tipidee_headers_search(hdr, "Range") ; + if (infopath) + { + uint64_t start, len ; + int r = tipidee_util_parse_range(infopath, st.st_size, &start, &len) ; + if (r > 0) return respond_partial(rql, docroot, fn, &st, start, len, &ra) ; + if (r < 0) { respond_416(rql, docroot, st.st_size) ; return 0 ; } + } + } + return respond_regular(rql, docroot, fn, &st, &ra) ; } |