diff options
Diffstat (limited to 'src/libtipidee')
26 files changed, 285 insertions, 186 deletions
diff --git a/src/libtipidee/deps-lib/tipidee b/src/libtipidee/deps-lib/tipidee index 1a73f52..497b74a 100644 --- a/src/libtipidee/deps-lib/tipidee +++ b/src/libtipidee/deps-lib/tipidee @@ -10,20 +10,16 @@ tipidee_conf_get_responseheaders.o tipidee_conf_get_string.o tipidee_conf_get_uint32.o tipidee_conf_init.o -tipidee_fcgi_beginrequest_body_pack.o -tipidee_fcgi_beginrequest_body_unpack.o -tipidee_fcgi_beginrequest_record_pack.o -tipidee_fcgi_beginrequest_record_unpack.o -tipidee_fcgi_endrequest_body_pack.o -tipidee_fcgi_endrequest_body_unpack.o -tipidee_fcgi_endrequest_record_pack.o -tipidee_fcgi_endrequest_record_unpack.o -tipidee_fcgi_header_pack.o -tipidee_fcgi_header_unpack.o -tipidee_fcgi_unknowntype_body_pack.o -tipidee_fcgi_unknowntype_body_unpack.o -tipidee_fcgi_unknowntype_record_pack.o -tipidee_fcgi_unknowntype_record_unpack.o +tipidee_fcgi_addenv.o +tipidee_fcgi_beginrequest.o +tipidee_fcgi_flush.o +tipidee_fcgi_params.o +tipidee_fcgi_put.o +tipidee_fcgi_putv.o +tipidee_fcgi_read.o +tipidee_fcgi_startread.o +tipidee_fcgi_startwrite.o +tipidee_fcgi_stdin.o tipidee_headers_get_content_length.o tipidee_headers_init.o tipidee_headers_parse.o diff --git a/src/libtipidee/tipidee_fcgi_addenv.c b/src/libtipidee/tipidee_fcgi_addenv.c new file mode 100644 index 0000000..fb9e859 --- /dev/null +++ b/src/libtipidee/tipidee_fcgi_addenv.c @@ -0,0 +1,41 @@ +/* ISC license. */ + +#include <errno.h> + +#include <skalibs/uint32.h> +#include <skalibs/stralloc.h> + +#include <tipidee/fcgi.h> + +static int encode (stralloc *sa, size_t n) +{ + if (n <= 0x7f) + { + char c = n ; + return stralloc_catb(sa, &c, 1) ; + } + else if (n < 0x7ffffffful) + { + char pack[4] ; + uint32_pack_big(pack, n | 0x80000000) ; + return stralloc_catb(sa, pack, 4) ; + } + else return (errno = EOVERFLOW, 0) ; +} + +int tipidee_fcgi_addenvb (stralloc *sa, char const *key, size_t keylen, char const *val, size_t vallen) +{ + size_t start = sa->len ; + if (!encode(sa, keylen)) return 0 ; + if (!encode(sa, vallen) + || !stralloc_catb(sa, key, keylen) + || !stralloc_catb(sa, val, vallen)) goto err ; + if (sa->len > 0x7fff) goto err0 ; + return 1 ; + + err0: + errno = E2BIG ; + err: + sa->len = start ; + return 0 ; +} diff --git a/src/libtipidee/tipidee_fcgi_beginrequest.c b/src/libtipidee/tipidee_fcgi_beginrequest.c new file mode 100644 index 0000000..8a5a9a9 --- /dev/null +++ b/src/libtipidee/tipidee_fcgi_beginrequest.c @@ -0,0 +1,9 @@ +/* ISC license. */ + +#include <tipidee/fcgi.h> + +int tipidee_fcgi_beginrequest (tipidee_fcgi *fc, tain *stamp) +{ + char body[8] = { 0, FCGI_RESPONDER, FCGI_KEEP_CONN, 0, 0, 0, 0, 0 } ; + return tipidee_fcgi_put(fc, FCGI_BEGIN_REQUEST, body, 8, stamp) == 8 ; +} diff --git a/src/libtipidee/tipidee_fcgi_beginrequest_body_pack.c b/src/libtipidee/tipidee_fcgi_beginrequest_body_pack.c deleted file mode 100644 index 400be69..0000000 --- a/src/libtipidee/tipidee_fcgi_beginrequest_body_pack.c +++ /dev/null @@ -1,14 +0,0 @@ -/* ISC license. */ - -#include <string.h> - -#include <skalibs/uint16.h> - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_beginrequest_body_pack (char *s, fcgi_beginrequest_body const *bd) -{ - uint16_pack_big(s, bd->role) ; s += 2 ; - *s++ = bd->flags ; - memset(s, 0, 5) ; -} diff --git a/src/libtipidee/tipidee_fcgi_beginrequest_body_unpack.c b/src/libtipidee/tipidee_fcgi_beginrequest_body_unpack.c deleted file mode 100644 index a729b03..0000000 --- a/src/libtipidee/tipidee_fcgi_beginrequest_body_unpack.c +++ /dev/null @@ -1,14 +0,0 @@ -/* ISC license. */ - -#include <string.h> - -#include <skalibs/uint16.h> - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_beginrequest_body_unpack (char const *s, fcgi_beginrequest_body *bd) -{ - uint16_unpack_big(s, &bd->role) ; s += 2 ; - bd->flags = *s++ ; - memcpy((char *)bd->reserved, s, 5) ; s += 5 ; -} diff --git a/src/libtipidee/tipidee_fcgi_beginrequest_record_pack.c b/src/libtipidee/tipidee_fcgi_beginrequest_record_pack.c deleted file mode 100644 index 3329e8d..0000000 --- a/src/libtipidee/tipidee_fcgi_beginrequest_record_pack.c +++ /dev/null @@ -1,9 +0,0 @@ -/* ISC license. */ - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_beginrequest_record_pack (char *s, fcgi_beginrequest_record const *rec) -{ - tipidee_fcgi_header_pack(s, &rec->header) ; s += 8 ; - tipidee_fcgi_beginrequest_body_pack(s, &rec->body) ; -} diff --git a/src/libtipidee/tipidee_fcgi_beginrequest_record_unpack.c b/src/libtipidee/tipidee_fcgi_beginrequest_record_unpack.c deleted file mode 100644 index c68a08f..0000000 --- a/src/libtipidee/tipidee_fcgi_beginrequest_record_unpack.c +++ /dev/null @@ -1,9 +0,0 @@ -/* ISC license. */ - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_beginrequest_record_unpack (char const *s, fcgi_beginrequest_record *rec) -{ - tipidee_fcgi_header_unpack(s, &rec->header) ; s += 8 ; - tipidee_fcgi_beginrequest_body_unpack(s, &rec->body) ; -} diff --git a/src/libtipidee/tipidee_fcgi_endrequest_body_pack.c b/src/libtipidee/tipidee_fcgi_endrequest_body_pack.c deleted file mode 100644 index d289d6d..0000000 --- a/src/libtipidee/tipidee_fcgi_endrequest_body_pack.c +++ /dev/null @@ -1,14 +0,0 @@ -/* ISC license. */ - -#include <string.h> - -#include <skalibs/uint32.h> - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_endrequest_body_pack (char *s, fcgi_endrequest_body const *bd) -{ - uint32_pack_big(s, bd->appstatus) ; s += 4 ; - *s++ = bd->protostatus ; - memset(s, 0, 3) ; -} diff --git a/src/libtipidee/tipidee_fcgi_endrequest_body_unpack.c b/src/libtipidee/tipidee_fcgi_endrequest_body_unpack.c deleted file mode 100644 index 9f52f18..0000000 --- a/src/libtipidee/tipidee_fcgi_endrequest_body_unpack.c +++ /dev/null @@ -1,14 +0,0 @@ -/* ISC license. */ - -#include <string.h> - -#include <skalibs/uint32.h> - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_endrequest_body_unpack (char const *s, fcgi_endrequest_body *bd) -{ - uint32_unpack_big(s, &bd->appstatus) ; s += 4 ; - bd->protostatus = *s++ ; - memcpy((char *)bd->reserved, s, 3) ; s += 3 ; -} diff --git a/src/libtipidee/tipidee_fcgi_endrequest_record_pack.c b/src/libtipidee/tipidee_fcgi_endrequest_record_pack.c deleted file mode 100644 index 46f3ee9..0000000 --- a/src/libtipidee/tipidee_fcgi_endrequest_record_pack.c +++ /dev/null @@ -1,9 +0,0 @@ -/* ISC license. */ - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_endrequest_record_pack (char *s, fcgi_endrequest_record const *rec) -{ - tipidee_fcgi_header_pack(s, &rec->header) ; s += 8 ; - tipidee_fcgi_endrequest_body_pack(s, &rec->body) ; -} diff --git a/src/libtipidee/tipidee_fcgi_endrequest_record_unpack.c b/src/libtipidee/tipidee_fcgi_endrequest_record_unpack.c deleted file mode 100644 index dc0acd5..0000000 --- a/src/libtipidee/tipidee_fcgi_endrequest_record_unpack.c +++ /dev/null @@ -1,9 +0,0 @@ -/* ISC license. */ - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_endrequest_record_unpack (char const *s, fcgi_endrequest_record *rec) -{ - tipidee_fcgi_header_unpack(s, &rec->header) ; s += 8 ; - tipidee_fcgi_endrequest_body_unpack(s, &rec->body) ; -} diff --git a/src/libtipidee/tipidee_fcgi_flush.c b/src/libtipidee/tipidee_fcgi_flush.c new file mode 100644 index 0000000..f32f8d4 --- /dev/null +++ b/src/libtipidee/tipidee_fcgi_flush.c @@ -0,0 +1,10 @@ +/* ISC license. */ + +#include <skalibs/unix-timed.h> + +#include <tipidee/fcgi.h> + +int tipidee_fcgi_flush (tipidee_fcgi *fc, tain *stamp) +{ + return buffer_timed_flush(&fc->b, &fc->deadline, stamp) ; +} diff --git a/src/libtipidee/tipidee_fcgi_header_pack.c b/src/libtipidee/tipidee_fcgi_header_pack.c deleted file mode 100644 index 2f1a550..0000000 --- a/src/libtipidee/tipidee_fcgi_header_pack.c +++ /dev/null @@ -1,15 +0,0 @@ -/* ISC license. */ - -#include <skalibs/uint16.h> - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_header_pack (char *s, fcgi_header const *hdr) -{ - *s++ = hdr->version ; - *s++ = hdr->type ; - uint16_pack_big(s, hdr->requestid) ; s += 2 ; - uint16_pack_big(s, hdr->len) ; s += 2 ; - *s++ = hdr->padlen ; - *s++ = 0 ; -} diff --git a/src/libtipidee/tipidee_fcgi_header_unpack.c b/src/libtipidee/tipidee_fcgi_header_unpack.c deleted file mode 100644 index 53debd7..0000000 --- a/src/libtipidee/tipidee_fcgi_header_unpack.c +++ /dev/null @@ -1,14 +0,0 @@ -/* ISC license. */ - -#include <skalibs/uint16.h> - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_header_unpack (char const *s, fcgi_header *hdr) -{ - hdr->version = *s++ ; - hdr->type = *s++ ; - uint16_unpack_big(s, &hdr->requestid) ; s += 2 ; - uint16_unpack_big(s, &hdr->len) ; s += 2 ; - hdr->padlen = *s++ ; -} diff --git a/src/libtipidee/tipidee_fcgi_params.c b/src/libtipidee/tipidee_fcgi_params.c new file mode 100644 index 0000000..cac4e50 --- /dev/null +++ b/src/libtipidee/tipidee_fcgi_params.c @@ -0,0 +1,8 @@ +/* ISC license. */ + +#include <tipidee/fcgi.h> + +int tipidee_fcgi_params (tipidee_fcgi *fc, char const *s, size_t len, tain *stamp) +{ + return tipidee_fcgi_put(fc, FCGI_PARAMS, s, len, stamp) == len ; +} diff --git a/src/libtipidee/tipidee_fcgi_put.c b/src/libtipidee/tipidee_fcgi_put.c new file mode 100644 index 0000000..0f4b493 --- /dev/null +++ b/src/libtipidee/tipidee_fcgi_put.c @@ -0,0 +1,34 @@ +/* ISC license. */ + +#include <stdint.h> + +#include <skalibs/uint16.h> +#include <skalibs/buffer.h> +#include <skalibs/unix-timed.h> + +#include <tipidee/fcgi.h> + +static uint16_t putit (tipidee_fcgi *fc, uint8_t rectype, char const *s, uint16_t len, tain *stamp) +{ + char hdr[8] = { 1, (char)rectype, fc->id >> 8, fc->id & 0xff, 0, 0, 0, 0 } ; + uint16_t w ; + uint16_pack_big(hdr + 4, len) ; + w = buffer_timed_put(&fc->b, hdr, 8, &fc->deadline, stamp) ; + if (w < 8) return 0 ; + return buffer_timed_put(&fc->b, s, len, &fc->deadline, stamp) ; +} + +size_t tipidee_fcgi_put (tipidee_fcgi *fc, uint8_t rectype, char const *s, size_t len, tain *stamp) +{ + size_t w = 0 ; + while (len > 0xffffu) + { + uint16_t r = putit(fc, rectype, s, 0xffffu, stamp) ; + w += r ; + if (r < 0xffffu) return w ; + s += 0xffff ; + len -= 0xffff ; + } + w += putit(fc, rectype, s, len, stamp) ; + return w ; +} diff --git a/src/libtipidee/tipidee_fcgi_putv.c b/src/libtipidee/tipidee_fcgi_putv.c new file mode 100644 index 0000000..32f5ba5 --- /dev/null +++ b/src/libtipidee/tipidee_fcgi_putv.c @@ -0,0 +1,47 @@ +/* ISC license. */ + +#include <sys/uio.h> +#include <stdint.h> +#include <errno.h> + +#include <skalibs/uint16.h> +#include <skalibs/buffer.h> +#include <skalibs/siovec.h> +#include <skalibs/unix-timed.h> + +#include <tipidee/fcgi.h> + +static uint16_t putvit (tipidee_fcgi *fc, uint8_t rectype, struct iovec const *v, unsigned int n, uint16_t len, tain *stamp) +{ + char hdr[8] = { 1, (char)rectype, fc->id >> 8, fc->id & 0xff, 0, 0, 0, 0 } ; + uint16_t w ; + uint16_pack_big(hdr + 4, len) ; + w = buffer_timed_put(&fc->b, hdr, 8, &fc->deadline, stamp) ; + if (w < 8) return 0 ; + return buffer_timed_putv(&fc->b, v, n, &fc->deadline, stamp) ; +} + +size_t tipidee_fcgi_putv (tipidee_fcgi *fc, uint8_t rectype, struct iovec const *v, unsigned int n, tain *stamp) +{ + if (!n) return 0 ; + size_t w = 0 ; + unsigned int base = 0 ; + for (unsigned int i = 0 ; i < n ; i++) + if (v[i].iov_len > 0xffffu) return (errno = EMSGSIZE, 0) ; + while (base < n) + { + size_t len = 0 ; + unsigned int i = 0 ; + uint16_t r ; + for (; base + i < n ; i++) + { + if (len + v[base + i].iov_len > 0xffffu) break ; + len += v[base + i].iov_len ; + } + r = putvit(fc, rectype, v + base, i, len, stamp) ; + w += r ; + if (r < len) break ; + base += i ; + } + return w ; +} diff --git a/src/libtipidee/tipidee_fcgi_read.c b/src/libtipidee/tipidee_fcgi_read.c new file mode 100644 index 0000000..9b6dcb0 --- /dev/null +++ b/src/libtipidee/tipidee_fcgi_read.c @@ -0,0 +1,59 @@ +/* ISC license. */ + +#include <stdint.h> +#include <errno.h> + +#include <skalibs/uint16.h> +#include <skalibs/uint32.h> +#include <skalibs/stralloc.h> +#include <skalibs/unix-timed.h> + +#include <tipidee/fcgi.h> + +#include <skalibs/posixishard.h> + +int tipidee_fcgi_read (tipidee_fcgi *fc, uint32_t *status, stralloc *sa, tain *stamp) +{ + int stream ; + uint16_t u ; + char hdr[8] ; + if (buffer_timed_get(&fc->b, hdr, 8, &fc->deadline, stamp) < 8) return -1 ; + if (hdr[0] != 1) return (errno = EPROTO, -1) ; + uint16_unpack_big(hdr + 2, &u) ; + if (u != fc->id) return (errno = EPROTO, -1) ; + uint16_unpack_big(hdr + 4, &u) ; + switch (hdr[1]) + { + case FCGI_STDOUT : stream = 1 ; + case FCGI_STDERR : stream = 2 ; + case FCGI_END_REQUEST : stream = 0 ; + default : return (errno = EPROTO, -1) ; + } + if (stream) + { + if (!stralloc_readyplus(sa, u)) return -1 ; + if (buffer_timed_get(&fc->b, sa->s + sa->len, u, &fc->deadline, stamp) < u) return -1 ; + sa->len += u ; + } + else + { + char endreq[8] ; + if (u != 8) return (errno = EPROTO, -1) ; + if (buffer_timed_get(&fc->b, endreq, 8, &fc->deadline, stamp) < 8) return -1 ; + switch (endreq[4]) + { + case FCGI_REQUEST_COMPLETE : break ; + case FCGI_CANT_MPX_CONN : return (errno = ECONNREFUSED, -1) ; + case FCGI_OVERLOADED : return (errno = ENFILE, -1) ; + case FCGI_UNKNOWN_ROLE : return (errno = EAFNOSUPPORT, -1) ; + default : return (errno = EPROTO, 1) ; + } + uint32_unpack_big(endreq, status) ; + } + if (hdr[6]) + { + char tmp[(uint8_t)hdr[6]] ; + if (buffer_timed_get(&fc->b, tmp, (uint8_t)hdr[6], &fc->deadline, stamp)) return -1 ; + } + return stream ; +} diff --git a/src/libtipidee/tipidee_fcgi_startread.c b/src/libtipidee/tipidee_fcgi_startread.c new file mode 100644 index 0000000..4899e55 --- /dev/null +++ b/src/libtipidee/tipidee_fcgi_startread.c @@ -0,0 +1,12 @@ +/* ISC license. */ + +#include <skalibs/buffer.h> + +#include <tipidee/fcgi.h> + +void tipidee_fcgi_startread (tipidee_fcgi *fc, int fd, pid_t pid, tain const *deadline) +{ + buffer_init(&fc->b, &buffer_read, fd, fc->buf, 4096) ; + fc->deadline = *deadline ; + fc->id = pid & 0xffffu ; fc->id += !fc->id ; +} diff --git a/src/libtipidee/tipidee_fcgi_startwrite.c b/src/libtipidee/tipidee_fcgi_startwrite.c new file mode 100644 index 0000000..12fd628 --- /dev/null +++ b/src/libtipidee/tipidee_fcgi_startwrite.c @@ -0,0 +1,12 @@ +/* ISC license. */ + +#include <skalibs/buffer.h> + +#include <tipidee/fcgi.h> + +void tipidee_fcgi_startwrite (tipidee_fcgi *fc, int fd, pid_t pid, tain const *deadline) +{ + buffer_init(&fc->b, &buffer_write, fd, fc->buf, 4096) ; + fc->deadline = *deadline ; + fc->id = pid & 0xffffu ; fc->id += !fc->id ; +} diff --git a/src/libtipidee/tipidee_fcgi_stdin.c b/src/libtipidee/tipidee_fcgi_stdin.c new file mode 100644 index 0000000..b1a641a --- /dev/null +++ b/src/libtipidee/tipidee_fcgi_stdin.c @@ -0,0 +1,8 @@ +/* ISC license. */ + +#include <tipidee/fcgi.h> + +int tipidee_fcgi_stdin (tipidee_fcgi *fc, char const *s, size_t len, tain *stamp) +{ + return tipidee_fcgi_put(fc, FCGI_STDIN, s, len, stamp) == len ; +} diff --git a/src/libtipidee/tipidee_fcgi_unknowntype_body_pack.c b/src/libtipidee/tipidee_fcgi_unknowntype_body_pack.c deleted file mode 100644 index f99da8f..0000000 --- a/src/libtipidee/tipidee_fcgi_unknowntype_body_pack.c +++ /dev/null @@ -1,11 +0,0 @@ -/* ISC license. */ - -#include <string.h> - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_unknowntype_body_pack (char *s, fcgi_unknowntype_body const *bd) -{ - *s++ = bd->type ; - memset(s, 0, 7) ; -} diff --git a/src/libtipidee/tipidee_fcgi_unknowntype_body_unpack.c b/src/libtipidee/tipidee_fcgi_unknowntype_body_unpack.c deleted file mode 100644 index eb0684c..0000000 --- a/src/libtipidee/tipidee_fcgi_unknowntype_body_unpack.c +++ /dev/null @@ -1,11 +0,0 @@ -/* ISC license. */ - -#include <string.h> - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_unknowntype_body_unpack (char const *s, fcgi_unknowntype_body *bd) -{ - bd->type = *s++ ; - memcpy((char *)bd->reserved, s, 7) ; s += 7 ; -} diff --git a/src/libtipidee/tipidee_fcgi_unknowntype_record_pack.c b/src/libtipidee/tipidee_fcgi_unknowntype_record_pack.c deleted file mode 100644 index 9d962e2..0000000 --- a/src/libtipidee/tipidee_fcgi_unknowntype_record_pack.c +++ /dev/null @@ -1,9 +0,0 @@ -/* ISC license. */ - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_unknowntype_record_pack (char *s, fcgi_unknowntype_record const *rec) -{ - tipidee_fcgi_header_pack(s, &rec->header) ; s += 8 ; - tipidee_fcgi_unknowntype_body_pack(s, &rec->body) ; -} diff --git a/src/libtipidee/tipidee_fcgi_unknowntype_record_unpack.c b/src/libtipidee/tipidee_fcgi_unknowntype_record_unpack.c deleted file mode 100644 index 24c6631..0000000 --- a/src/libtipidee/tipidee_fcgi_unknowntype_record_unpack.c +++ /dev/null @@ -1,9 +0,0 @@ -/* ISC license. */ - -#include <tipidee/fcgi.h> - -void tipidee_fcgi_unknowntype_record_unpack (char const *s, fcgi_unknowntype_record *rec) -{ - tipidee_fcgi_header_unpack(s, &rec->header) ; s += 8 ; - tipidee_fcgi_unknowntype_body_unpack(s, &rec->body) ; -} diff --git a/src/libtipidee/tipidee_headers_parse.c b/src/libtipidee/tipidee_headers_parse.c index 4881633..b621b70 100644 --- a/src/libtipidee/tipidee_headers_parse.c +++ b/src/libtipidee/tipidee_headers_parse.c @@ -9,6 +9,7 @@ #include <skalibs/bytestr.h> #include <skalibs/buffer.h> #include <skalibs/error.h> +#include <skalibs/disize.h> #include <skalibs/avltreen.h> #include <skalibs/unix-timed.h> @@ -71,26 +72,43 @@ states: 4 bits, actions: 7 bits */ -struct tainp_s +struct buffertime_s { + buffer *b ; tain const *deadline ; tain *stamp ; } ; -typedef ssize_t get1_func (buffer *, char *, struct tainp_s *) ; +struct string_s +{ + char const *s ; + size_t len ; + size_t *i ; +} ; + +typedef ssize_t get1_func (char *, void *) ; typedef get1_func *get1_func_ref ; -static ssize_t get1_timed (buffer *b, char *c, struct tainp_s *d) +static ssize_t get1_timed (char *c, void *param) { - return buffer_timed_get(b, c, 1, d->deadline, d->stamp) ; + struct buffertime_s *p = param ; + return buffer_timed_get(p->b, c, 1, p->deadline, p->stamp) ; } -static ssize_t get1_notimed (buffer *b, char *c, struct tainp_s *data) +static ssize_t get1_notimed (char *c, void *param) { - (void)data ; + buffer *b = param ; return buffer_get(b, c, 1) ; } +static ssize_t get1_string (char *c, void *param) +{ + struct string_s *st = param ; + if ((*st->i) >= st->len) return (errno = EAGAIN, -1) ; + *c = st->s[(*st->i)++] ; + return 1 ; +} + static uint8_t cclass (char c) { static uint8_t const ctable[128] = "00000000032001000000000000000000365666665566566566666666664565655666666666666666666666666665556666666666666666666666666666656560" ; @@ -104,7 +122,7 @@ static int needs_processing (char const *s) return 1 ; } -static int tipidee_headers_parse_with (buffer *b, tipidee_headers *hdr, get1_func_ref next, struct tainp_s *data, disize *header, uint32_t *state) +static int tipidee_headers_parse_with (tipidee_headers *hdr, disize *header, uint32_t *state, get1_func_ref next, void *param) { static uint16_t const table[10][8] = { @@ -123,7 +141,7 @@ static int tipidee_headers_parse_with (buffer *b, tipidee_headers *hdr, get1_fun { uint16_t c ; char cur = 0 ; - if ((*next)(b, &cur, data) < 0) + if ((*next)(&cur, param) == -1) return errno == ETIMEDOUT ? 408 : error_isagain(errno) ? -2 : -1 ; c = table[*state][cclass(cur)] ; /* @@ -201,13 +219,19 @@ static int tipidee_headers_parse_with (buffer *b, tipidee_headers *hdr, get1_fun int tipidee_headers_timed_parse (buffer *b, tipidee_headers *hdr, tain const *deadline, tain *stamp) { - struct tainp_s d = { .deadline = deadline, .stamp = stamp } ; + struct buffertime_s bt = { .b = b, .deadline = deadline, .stamp = stamp } ; disize header = DISIZE_ZERO ; uint32_t state = 0 ; - return tipidee_headers_parse_with(b, hdr, &get1_timed, &d, &header, &state) ; + return tipidee_headers_parse_with(hdr, &header, &state, &get1_timed, &bt) ; } int tipidee_headers_parse_nb (buffer *b, tipidee_headers *hdr, disize *header, uint32_t *state) { - return tipidee_headers_parse_with(b, hdr, &get1_notimed, 0, header, state) ; + return tipidee_headers_parse_with(hdr, header, state, &get1_notimed, b) ; +} + +int tipidee_headers_parse_fromstring_nb (char const *s, size_t len, size_t *i, tipidee_headers *hdr, disize *header, uint32_t *state) +{ + struct string_s st = { .s = s, .len = len, .i = i } ; + return tipidee_headers_parse_with(hdr, header, state, &get1_string, &st) ; } |
