diff options
Diffstat (limited to 'src/libtipidee')
-rw-r--r-- | src/libtipidee/deps-lib/tipidee | 7 | ||||
-rw-r--r-- | src/libtipidee/tipidee_response_error_nofile.c | 12 | ||||
-rw-r--r-- | src/libtipidee/tipidee_response_error_nofile_G.c | 4 | ||||
-rw-r--r-- | src/libtipidee/tipidee_response_file.c | 17 | ||||
-rw-r--r-- | src/libtipidee/tipidee_response_file_G.c | 4 | ||||
-rw-r--r-- | src/libtipidee/tipidee_response_header_end.c | 10 | ||||
-rw-r--r-- | src/libtipidee/tipidee_response_header_write.c | 21 | ||||
-rw-r--r-- | src/libtipidee/tipidee_response_header_writeall.c | 11 | ||||
-rw-r--r-- | src/libtipidee/tipidee_response_partial.c | 29 | ||||
-rw-r--r-- | src/libtipidee/tipidee_response_partial_G.c | 12 | ||||
-rw-r--r-- | src/libtipidee/tipidee_util_parse_range.c | 49 |
11 files changed, 146 insertions, 30 deletions
diff --git a/src/libtipidee/deps-lib/tipidee b/src/libtipidee/deps-lib/tipidee index 985be7e..33f9f2f 100644 --- a/src/libtipidee/deps-lib/tipidee +++ b/src/libtipidee/deps-lib/tipidee @@ -16,8 +16,8 @@ tipidee_headers_parse.o tipidee_headers_search.o tipidee_log_answer.o tipidee_log_exit.o -tipidee_log_resource.o tipidee_log_request.o +tipidee_log_resource.o tipidee_log_start.o tipidee_method.o tipidee_response_error_nofile.o @@ -27,16 +27,21 @@ tipidee_response_file_G.o tipidee_response_header_date.o tipidee_response_header_date_G.o tipidee_response_header_date_fmt.o +tipidee_response_header_end.o tipidee_response_header_lastmodified.o tipidee_response_header_preparebuiltin.o +tipidee_response_header_write.o tipidee_response_header_writeall.o tipidee_response_header_writeall_G.o tipidee_response_header_writemerge.o tipidee_response_header_writemerge_G.o +tipidee_response_partial.o +tipidee_response_partial_G.o tipidee_response_status.o tipidee_rql_read.o tipidee_uri_parse.o tipidee_util_chunked_read.o tipidee_util_defaulttext.o tipidee_util_httpdate.o +tipidee_util_parse_range.o -lskarnet diff --git a/src/libtipidee/tipidee_response_error_nofile.c b/src/libtipidee/tipidee_response_error_nofile.c index 8a7cd53..10da032 100644 --- a/src/libtipidee/tipidee_response_error_nofile.c +++ b/src/libtipidee/tipidee_response_error_nofile.c @@ -8,19 +8,21 @@ #include <tipidee/method.h> #include <tipidee/response.h> -size_t tipidee_response_error_nofile (buffer *b, tipidee_rql const *rql, unsigned int status, char const *reason, char const *text, tipidee_response_header const *rhdr, uint32_t rhdrn, uint32_t options, tain const *stamp) +size_t tipidee_response_error_nofile (buffer *b, tipidee_rql const *rql, unsigned int status, char const *reason, char const *text, tipidee_response_header const *rhdr, uint32_t rhdrn, tipidee_response_header const *plus, uint32_t plusn, uint32_t options, tain const *stamp) { static char const txt1[] = "<html>\n<head><title>" ; static char const txt2[] = "</title></head>\n<body>\n<h1> " ; static char const txt3[] = " </h1>\n<p>\n" ; static char const txt4[] = "\n</p>\n</body>\n</html>\n" ; char fmt[SIZE_FMT] ; + tipidee_response_header v[2] = { { .key = "Content-Type", .value = "text/html; charset=UTF-8", .options = 0 }, { .key = "Content-Length", .value = fmt, .options = 0 } } ; size_t n = tipidee_response_status(b, rql, status, reason) ; n += tipidee_response_header_writeall(b, rhdr, rhdrn, options, stamp) ; - n += buffer_putsnoflush(b, "Content-Type: text/html; charset=UTF-8\r\n") ; - n += buffer_putsnoflush(b, "Content-Length: ") ; - n += buffer_putnoflush(b, fmt, size_fmt(fmt, text ? sizeof(txt1) + sizeof(txt2) + sizeof(txt3) + sizeof(txt4) - 4 + 2 * strlen(reason) + strlen(text) : 0)) ; - n += buffer_putnoflush(b, "\r\n\r\n", 4) ; + if (plusn) n += tipidee_response_header_write(b, plus, plusn) ; + fmt[size_fmt(fmt, text ? sizeof(txt1) + sizeof(txt2) + sizeof(txt3) + sizeof(txt4) - 4 + 2 * strlen(reason) + strlen(text) : 0)] = 0 ; + n += tipidee_response_header_write(b, v, 2) ; + + n += buffer_putnoflush(b, "\r\n", 2) ; if (text && rql->m != TIPIDEE_METHOD_HEAD) { n += buffer_putsnoflush(b, txt1) ; diff --git a/src/libtipidee/tipidee_response_error_nofile_G.c b/src/libtipidee/tipidee_response_error_nofile_G.c index d7239cc..e6bdce5 100644 --- a/src/libtipidee/tipidee_response_error_nofile_G.c +++ b/src/libtipidee/tipidee_response_error_nofile_G.c @@ -4,9 +4,9 @@ #include <tipidee/response.h> -size_t tipidee_response_error_nofile_G (buffer *b, tipidee_rql const *rql, unsigned int status, char const *reason, char const *text, tipidee_response_header const *rhdr, uint32_t rhdrn, uint32_t options) +size_t tipidee_response_error_nofile_G (buffer *b, tipidee_rql const *rql, unsigned int status, char const *reason, char const *text, tipidee_response_header const *rhdr, uint32_t rhdrn, tipidee_response_header const *plus, uint32_t plusn, uint32_t options) { tain wstamp ; tain_wallclock_read(&wstamp) ; - return tipidee_response_error_nofile(b, rql, status, reason, text, rhdr, rhdrn, options, &wstamp) ; + return tipidee_response_error_nofile(b, rql, status, reason, text, rhdr, rhdrn, plus, plusn, options, &wstamp) ; } diff --git a/src/libtipidee/tipidee_response_file.c b/src/libtipidee/tipidee_response_file.c index f2021ee..b64d086 100644 --- a/src/libtipidee/tipidee_response_file.c +++ b/src/libtipidee/tipidee_response_file.c @@ -2,18 +2,15 @@ #include <stddef.h> -#include <skalibs/types.h> +#include <skalibs/uint64.h> #include <skalibs/buffer.h> -#include <tipidee/conf.h> -#include <tipidee/method.h> -#include <tipidee/rql.h> #include <tipidee/response.h> -#include <tipidee/util.h> -size_t tipidee_response_file (buffer *b, tipidee_rql const *rql, unsigned int status, char const *reason, struct stat const *st, char const *ct, tipidee_response_header const *rhdr, uint32_t rhdrn, uint32_t options, tain const *stamp) +size_t tipidee_response_file (buffer *b, tipidee_rql const *rql, unsigned int status, char const *reason, struct stat const *st, uint64_t cl, char const *ct, tipidee_response_header const *rhdr, uint32_t rhdrn, uint32_t options, tain const *stamp) { char fmt[128] ; + tipidee_response_header v[2] = { { .key = "Content-Type", .value = ct, .options = 0 }, { .key = "Content-Length", .value = fmt, .options = 0 } } ; size_t n = tipidee_response_status(b, rql, status, reason) ; n += tipidee_response_header_writeall(b, rhdr, rhdrn, options & 1, stamp) ; if (options & 2) @@ -21,11 +18,7 @@ size_t tipidee_response_file (buffer *b, tipidee_rql const *rql, unsigned int st size_t l = tipidee_response_header_lastmodified(fmt, 128, st) ; if (l) n += buffer_putnoflush(b, fmt, l) ; } - n += buffer_putsnoflush(b, "Content-Type: ") ; - n += buffer_putsnoflush(b, ct) ; - n += buffer_putsnoflush(b, "\r\nContent-Length: ") ; - fmt[uint64_fmt(fmt, st->st_size)] = 0 ; - n += buffer_putsnoflush(b, fmt) ; - n += buffer_putnoflush(b, "\r\n\r\n", 4) ; + fmt[uint64_fmt(fmt, cl)] = 0 ; + n += tipidee_response_header_write(b, v, 2) ; return n ; } diff --git a/src/libtipidee/tipidee_response_file_G.c b/src/libtipidee/tipidee_response_file_G.c index 8c08c98..06af667 100644 --- a/src/libtipidee/tipidee_response_file_G.c +++ b/src/libtipidee/tipidee_response_file_G.c @@ -4,9 +4,9 @@ #include <tipidee/response.h> -size_t tipidee_response_file_G (buffer *b, tipidee_rql const *rql, unsigned int status, char const *reason, struct stat const *st, char const *ct, tipidee_response_header const *rhdr, uint32_t rhdrn, uint32_t options) +size_t tipidee_response_file_G (buffer *b, tipidee_rql const *rql, unsigned int status, char const *reason, struct stat const *st, uint64_t cl, char const *ct, tipidee_response_header const *rhdr, uint32_t rhdrn, uint32_t options) { tain wstamp ; tain_wallclock_read(&wstamp) ; - return tipidee_response_file(b, rql, status, reason, st, ct, rhdr, rhdrn, options, &wstamp) ; + return tipidee_response_file(b, rql, status, reason, st, cl, ct, rhdr, rhdrn, options, &wstamp) ; } diff --git a/src/libtipidee/tipidee_response_header_end.c b/src/libtipidee/tipidee_response_header_end.c new file mode 100644 index 0000000..ddd8575 --- /dev/null +++ b/src/libtipidee/tipidee_response_header_end.c @@ -0,0 +1,10 @@ +/* ISC license. */ + +#include <skalibs/buffer.h> + +#include <tipidee/response.h> + +size_t tipidee_response_header_end (buffer *b) +{ + return buffer_put(b, "\r\n", 2) ; +} diff --git a/src/libtipidee/tipidee_response_header_write.c b/src/libtipidee/tipidee_response_header_write.c new file mode 100644 index 0000000..6c8edd9 --- /dev/null +++ b/src/libtipidee/tipidee_response_header_write.c @@ -0,0 +1,21 @@ +/* ISC license. */ + +#include <stddef.h> + +#include <skalibs/buffer.h> + +#include <tipidee/response.h> + +size_t tipidee_response_header_write (buffer *b, tipidee_response_header const *hdr, uint32_t n) +{ + size_t m = 0 ; + for (uint32_t i = 0 ; i < n ; i++) + { + if (!hdr[i].value) continue ; + m += buffer_putsnoflush(b, hdr[i].key) ; + m += buffer_putnoflush(b, ": ", 2) ; + m += buffer_putsnoflush(b, hdr[i].value) ; + m += buffer_putnoflush(b, "\r\n", 2) ; + } + return m ; +} diff --git a/src/libtipidee/tipidee_response_header_writeall.c b/src/libtipidee/tipidee_response_header_writeall.c index b94578d..36bc9bf 100644 --- a/src/libtipidee/tipidee_response_header_writeall.c +++ b/src/libtipidee/tipidee_response_header_writeall.c @@ -1,5 +1,7 @@ /* ISC license. */ +#include <stddef.h> + #include <skalibs/buffer.h> #include <tipidee/response.h> @@ -9,13 +11,6 @@ size_t tipidee_response_header_writeall (buffer *b, tipidee_response_header cons char fmt[128] ; size_t m = buffer_putnoflush(b, fmt, tipidee_response_header_date(fmt, 128, stamp)) ; if (options & 1) m += buffer_putsnoflush(b, "Connection: close\r\n") ; - for (uint32_t i = 0 ; i < rhdrn ; i++) - { - if (!rhdr[i].value) continue ; - m += buffer_putsnoflush(b, rhdr[i].key) ; - m += buffer_putnoflush(b, ": ", 2) ; - m += buffer_putsnoflush(b, rhdr[i].value) ; - m += buffer_putnoflush(b, "\r\n", 2) ; - } + m += tipidee_response_header_write(b, rhdr, rhdrn) ; return m ; } diff --git a/src/libtipidee/tipidee_response_partial.c b/src/libtipidee/tipidee_response_partial.c new file mode 100644 index 0000000..4411896 --- /dev/null +++ b/src/libtipidee/tipidee_response_partial.c @@ -0,0 +1,29 @@ +/* ISC license. */ + +#include <stddef.h> + +#include <skalibs/uint64.h> +#include <skalibs/buffer.h> + +#include <tipidee/util.h> +#include <tipidee/response.h> + +size_t tipidee_response_partial (buffer *b, tipidee_rql const *rql, struct stat const *st, uint64_t start, uint64_t len, char const *ct, tipidee_response_header const *rhdr, uint32_t rhdrn, uint32_t options, tain const *stamp) +{ + tipidee_defaulttext dt ; + size_t n ; + char fmt[UINT64_FMT] ; + if (!tipidee_util_defaulttext(206, &dt)) return 0 ; + n = tipidee_response_file(b, rql, 206, dt.reason, st, len, ct, rhdr, rhdrn, options, stamp) ; + if (len) + { + n += buffer_putsnoflush(b, "Content-Range: bytes ") ; + n += buffer_putnoflush(b, fmt, uint64_fmt(fmt, start)) ; + n += buffer_putnoflush(b, "-", 1) ; + n += buffer_putnoflush(b, fmt, uint64_fmt(fmt, start + len - 1)) ; + n += buffer_putnoflush(b, "/", 1) ; + n += buffer_putnoflush(b, fmt, uint64_fmt(fmt, st->st_size)) ; + n += buffer_putnoflush(b, "\r\n", 2) ; + } + return n ; +} diff --git a/src/libtipidee/tipidee_response_partial_G.c b/src/libtipidee/tipidee_response_partial_G.c new file mode 100644 index 0000000..d9c5d78 --- /dev/null +++ b/src/libtipidee/tipidee_response_partial_G.c @@ -0,0 +1,12 @@ +/* ISC license. */ + +#include <skalibs/tai.h> + +#include <tipidee/response.h> + +size_t tipidee_response_partial_G (buffer *b, tipidee_rql const *rql, struct stat const *st, uint64_t start, uint64_t len, char const *ct, tipidee_response_header const *rhdr, uint32_t rhdrn, uint32_t options) +{ + tain wstamp ; + tain_wallclock_read(&wstamp) ; + return tipidee_response_partial(b, rql, st, start, len, ct, rhdr, rhdrn, options, &wstamp) ; +} diff --git a/src/libtipidee/tipidee_util_parse_range.c b/src/libtipidee/tipidee_util_parse_range.c new file mode 100644 index 0000000..e096fda --- /dev/null +++ b/src/libtipidee/tipidee_util_parse_range.c @@ -0,0 +1,49 @@ +/* ISC license. */ + +#include <string.h> + +#include <skalibs/uint64.h> + +#include <tipidee/util.h> + +int tipidee_util_parse_range (char const *s, off_t max, uint64_t *start, uint64_t *len) +{ + if (strncmp(s, "bytes=", 6)) return -1 ; + s += 6 ; + if (*s == '-') + { + uint64_t n ; + size_t m = uint64_scan(++s, &n) ; + if (!m) return -1 ; + s += m ; + if (*s && *s != ',') return -1 ; + if (n > max) return -1 ; + *start = max - n ; + *len = n ; + return 1 ; + } + else + { + uint64_t beg ; + uint64_t n ; + size_t m = uint64_scan(s, &beg) ; + if (!m) return -1 ; + s += m ; + if (*s++ != '-') return -1 ; + if (beg >= max) return -1 ; + if (!*s || *s == ',') + { + *start = beg ; + *len = max - beg ; + return 1 ; + } + m = uint64_scan(s, &n) ; + if (!m) return -1 ; + s += m ; + if (*s && *s != ',') return -1 ; + if (n >= max || n < beg) return -1 ; + *start = beg ; + *len = n + 1 - beg ; + return 1 ; + } +} |