summaryrefslogtreecommitdiff
path: root/src/libtipidee/tipidee_util_chunked_read.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-09-21 02:18:35 +0000
committerLaurent Bercot <ska@appnovation.com>2023-09-21 02:18:35 +0000
commit3d334dca671898241732dbc0ef6838b768308da7 (patch)
tree0cac6b60ea1356455fef2553e41105f3c68bce9d /src/libtipidee/tipidee_util_chunked_read.c
parent6be5496f8a5660875c5f45f915210f69496d231b (diff)
downloadtipidee-3d334dca671898241732dbc0ef6838b768308da7.tar.xz
Implement If-Modified-Since
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libtipidee/tipidee_util_chunked_read.c')
-rw-r--r--src/libtipidee/tipidee_util_chunked_read.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/libtipidee/tipidee_util_chunked_read.c b/src/libtipidee/tipidee_util_chunked_read.c
new file mode 100644
index 0000000..850ffae
--- /dev/null
+++ b/src/libtipidee/tipidee_util_chunked_read.c
@@ -0,0 +1,41 @@
+/* ISC license. */
+
+#include <string.h>
+#include <errno.h>
+
+#include <skalibs/types.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/unix-timed.h>
+
+#include <tipidee/util.h>
+
+#include <skalibs/posixishard.h>
+
+int tipidee_util_chunked_read (buffer *b, stralloc *sa, size_t maxlen, tain const *deadline, tain *stamp)
+{
+ char line[512] ;
+ for (;;)
+ {
+ size_t chunklen, pos, w = 0 ;
+ ssize_t r = timed_getlnmax(b, line, 512, &w, '\n', deadline, stamp) ;
+ if (r < 0) return 0 ;
+ if (!r) return (errno = EPIPE, 0) ;
+ pos = size_scan(line, &chunklen) ;
+ if (!pos) return (errno = EPROTO, 0) ;
+ if (!memchr("\r\n; \t", line[pos], 5)) return (errno = EPROTO, 0) ;
+ if (!chunklen) break ;
+ if (sa->len + chunklen > maxlen) return (errno = EMSGSIZE, 0) ;
+ if (!stralloc_readyplus(sa, chunklen)) return 0 ;
+ if (buffer_timed_get(b, sa->s + sa->len, chunklen, deadline, stamp) < chunklen) return 0 ;
+ sa->len += chunklen ;
+ }
+ for (;;)
+ {
+ size_t w = 0 ;
+ ssize_t r = timed_getlnmax(b, line, 512, &w, '\n', deadline, stamp) ;
+ if (r < 0) return 0 ;
+ if (!r) return (errno = EPIPE, 0) ;
+ if (w == 1 || (w == 2 && line[0] == '\r')) break ;
+ }
+ return 1 ;
+}