src/server/util/io.h

Sun, 22 Feb 2026 13:19:12 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 22 Feb 2026 13:19:12 +0100
changeset 699
d794871da099
parent 697
3ddfd45d4e47
permissions
-rw-r--r--

implement reverse_proxy_service location and url parameters

1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 32
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #ifndef IOSTREAM_H
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #define IOSTREAM_H
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
32 #include <openssl/ssl.h>
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
33 #include "../public/nsapi.h"
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
34 #include "systems.h"
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
35
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
36 #ifdef _WIN32
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
37 #include <windows.h>
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
38 #include <winsock2.h>
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
39 #endif
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
40
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #ifdef __cplusplus
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 extern "C" {
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #endif
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
147
d050449c3b9e ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
45 #ifdef XP_UNIX
d050449c3b9e ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
46 #define SYS_SOCKET int
d050449c3b9e ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
47 #elif defined(XP_WIN32)
d050449c3b9e ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
48 #define SYS_SOCKET SOCKET
d050449c3b9e ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
49 #endif
d050449c3b9e ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
50
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
51 #define IO_MODE_BLOCKING 0
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
52 #define IO_MODE_NONBLOCKING 1
697
3ddfd45d4e47 add raw mode for http streams
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 684
diff changeset
53 #define IO_MODE_RAW 2
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
54
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
55 #define IO_POLL_NONE 0
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
56 #define IO_POLL_IN 1
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
57 #define IO_POLL_OUT 2
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
58
513
9a49c245a49c change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 498
diff changeset
59 #define IO_STREAM_TYPE_HTTP 0x48545450
9a49c245a49c change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 498
diff changeset
60 #define IO_STREAM_TYPE_SSL 0x53534C
9a49c245a49c change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 498
diff changeset
61
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
62 typedef struct IOStream IOStream;
430
83560f32e7d5 refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
63 typedef struct Sysstream Sysstream;
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
64 typedef struct HttpStream HttpStream;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
498
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
66 typedef ssize_t(*io_write_f)(IOStream *, const void *, size_t);
64
c7f5b062e622 added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
67 typedef ssize_t(*io_writev_f)(IOStream *, struct iovec *, int);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 typedef ssize_t(*io_read_f)(IOStream *, void *, size_t);
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
69 typedef ssize_t(*io_sendfile_f)(IOStream *, sendfiledata *);
80
0de4a90979e1 fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
70 typedef void(*io_close_f)(IOStream *);
0de4a90979e1 fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
71 typedef void(*io_finish_f)(IOStream *);
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
72 typedef void(*io_setmode_f)(IOStream *, int);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
73 typedef int(*io_poll_f)(IOStream *, EventHandler *, int, Event *);
684
48da20bde908 refactor http client io to use an HttpStream for reading the response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 683
diff changeset
74 typedef void (*io_free_f)(IOStream *);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
76 struct IOStream {
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
77 io_write_f write;
64
c7f5b062e622 added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 54
diff changeset
78 io_writev_f writev;
54
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
79 io_read_f read;
3a1d5a52adfc new vfs api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
80 io_sendfile_f sendfile;
80
0de4a90979e1 fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
81 io_close_f close;
0de4a90979e1 fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
82 io_finish_f finish;
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
83 io_setmode_f setmode;
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
84 io_poll_f poll;
684
48da20bde908 refactor http client io to use an HttpStream for reading the response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 683
diff changeset
85 io_free_f free;
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
86 int io_errno;
513
9a49c245a49c change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 498
diff changeset
87 unsigned int type;
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 };
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89
430
83560f32e7d5 refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
90 struct Sysstream {
8
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
91 IOStream st;
684
48da20bde908 refactor http client io to use an HttpStream for reading the response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 683
diff changeset
92 pool_handle_t *pool;
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
93 #ifdef XP_UNIX
8
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
94 int fd;
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
95 #elif defined(XP_WIN32)
147
d050449c3b9e ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
96 SOCKET fd;
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
97 #endif
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
98 };
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
99
333
bb536d4bc174 first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
100 #define HTTP_STREAM_CBUF_SIZE 16
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
101 struct HttpStream {
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
102 IOStream st;
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
103 IOStream *fd;
684
48da20bde908 refactor http client io to use an HttpStream for reading the response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 683
diff changeset
104 pool_handle_t *pool;
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
105
406
e5d96f6b9306 store number of written bytes in the HttpStream
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
106 uint64_t written;
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
107 /*
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
108 * content-length or current chunk size
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
109 */
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
110 uint64_t max_read;
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
111 /*
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
112 * total bytes read (with content-length) or bytes read of current chunk
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
113 */
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
114 uint64_t read;
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
115 /*
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
116 * total bytes read with chunked transfer encoding
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
117 */
333
bb536d4bc174 first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
118 uint64_t read_total;
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
119 /*
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
120 * read buffer (used only with chunked transfer encoding)
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
121 */
333
bb536d4bc174 first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
122 char *readbuf;
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
123 /*
337
d934c2eb6a76 remove redundant comments
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 336
diff changeset
124 * readbuf allocated size
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
125 */
337
d934c2eb6a76 remove redundant comments
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 336
diff changeset
126 size_t bufsize;
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
127 /*
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
128 * number of bytes currently stored in readbuf
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
129 */
337
d934c2eb6a76 remove redundant comments
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 336
diff changeset
130 int *buflen;
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
131 /*
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
132 * current position in the read buffer
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
133 */
337
d934c2eb6a76 remove redundant comments
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 336
diff changeset
134 int *bufpos;
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
135 /*
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
136 * current chunk_buf position
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
137 */
333
bb536d4bc174 first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
138 int chunk_buf_pos;
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
139 /*
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
140 * buffer used only for parsing chunk headers
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
141 */
333
bb536d4bc174 first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
142 char chunk_buf[HTTP_STREAM_CBUF_SIZE];
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
143 /*
335
5234c57b8759 code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 334
diff changeset
144 * chunked transfer encoding for write enabled?
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
145 */
335
5234c57b8759 code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 334
diff changeset
146 WSBool chunked_enc;
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
147 /*
498
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
148 * current chunk size (set after the header is sent)
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
149 */
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
150 size_t current_chunk_length;
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
151 /*
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
152 * current chunk position
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
153 */
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
154 size_t current_chunk_pos;
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
155 /*
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
156 * missing trailer before new data
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
157 * 0: no trailer
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
158 * 2: crlf
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
159 * 1: lf
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
160 */
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
161 int current_trailer;
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
162 /*
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
163 * write chunk header buffer
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
164 */
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
165 char write_chunk_buf[HTTP_STREAM_CBUF_SIZE];
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
166 /*
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
167 * chunk header buffer length
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
168 * only used when the chunk header was completely sent
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
169 * must be 0 before payload data is sent
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
170 */
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
171 int write_chunk_buf_len;
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
172 /*
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
173 * current write_chunk_buf position (if remaining != 0)
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
174 */
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
175 int write_chunk_buf_pos;
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
176 /*
335
5234c57b8759 code cleanup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 334
diff changeset
177 * end of file indicator (read)
334
a55491f66003 fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 333
diff changeset
178 */
333
bb536d4bc174 first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
179 WSBool read_eof;
361
570026d3a685 make sure the http stream is finished if headers are sent
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
180 /*
570026d3a685 make sure the http stream is finished if headers are sent
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
181 * end of file indicator (write)
570026d3a685 make sure the http stream is finished if headers are sent
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
182 */
570026d3a685 make sure the http stream is finished if headers are sent
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 354
diff changeset
183 WSBool write_eof;
697
3ddfd45d4e47 add raw mode for http streams
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 684
diff changeset
184 /*
3ddfd45d4e47 add raw mode for http streams
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 684
diff changeset
185 * raw socket IO
3ddfd45d4e47 add raw mode for http streams
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 684
diff changeset
186 */
3ddfd45d4e47 add raw mode for http streams
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 684
diff changeset
187 WSBool raw;
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
188 };
8
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
189
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
190 typedef struct SSLStream {
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
191 IOStream st;
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 112
diff changeset
192 SSL *ssl;
684
48da20bde908 refactor http client io to use an HttpStream for reading the response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 683
diff changeset
193 pool_handle_t *pool;
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 112
diff changeset
194 int error;
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
195 } SSLStream;
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
196
513
9a49c245a49c change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 498
diff changeset
197 void io_set_max_writes(int n);
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
198
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
199 /* system stream */
430
83560f32e7d5 refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
200 IOStream* Sysstream_new(pool_handle_t *pool, SYS_SOCKET fd);
684
48da20bde908 refactor http client io to use an HttpStream for reading the response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 683
diff changeset
201 void sysstream_free(IOStream *st);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202
498
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
203 ssize_t net_sys_write(Sysstream *st, const void *buf, size_t nbytes);
430
83560f32e7d5 refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
204 ssize_t net_sys_writev(Sysstream *st, struct iovec *iovec, int iovcnt);
83560f32e7d5 refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
205 ssize_t net_sys_read(Sysstream *st, void *buf, size_t nbytes);
83560f32e7d5 refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
206 ssize_t net_sys_sendfile(Sysstream *st, sendfiledata *sfd);
83560f32e7d5 refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
207 void net_sys_close(Sysstream *st);
83560f32e7d5 refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
208 void net_sys_setmode(Sysstream *st, int mode);
83560f32e7d5 refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
209 int net_sys_poll(Sysstream *st, EventHandler *ev, int events, Event *cb);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
211 /* http stream */
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
212 IOStream* httpstream_new(pool_handle_t *pool, IOStream *fd);
684
48da20bde908 refactor http client io to use an HttpStream for reading the response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 683
diff changeset
213 void httpstream_free(IOStream *st);
8
f4d56bf9de40 Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
214
333
bb536d4bc174 first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
215 int httpstream_enable_chunked_read(IOStream *st, char *buffer, size_t bufsize, int *cursize, int *pos);
683
db37761a8494 enable buffered reader for request bodies with fixed content-length
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 513
diff changeset
216 int httpstream_enable_buffered_read(IOStream *st, char *buffer, size_t bufsize, int *cursize, int *pos);
333
bb536d4bc174 first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
217 int httpstream_enable_chunked_write(IOStream *st);
bb536d4bc174 first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
218 int httpstream_set_max_read(IOStream *st, int64_t maxread);
354
017eda1be105 add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
219 WSBool httpstream_eof(IOStream *st);
407
78af44222463 close connection if content-length != number of bytes written
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 406
diff changeset
220 int64_t httpstream_written(IOStream *st);
333
bb536d4bc174 first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
221
498
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
222 ssize_t net_http_write(HttpStream *st, const void *buf, size_t nbytes);
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
223 ssize_t net_http_writev(HttpStream *st, struct iovec *iovec, int iovcnt);
697
3ddfd45d4e47 add raw mode for http streams
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 684
diff changeset
224 ssize_t net_http_write_raw(HttpStream *st, const void *buf, size_t nbytes);
3ddfd45d4e47 add raw mode for http streams
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 684
diff changeset
225 ssize_t net_http_writev_raw(HttpStream *st, struct iovec *iovec, int iovcnt);
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
226 ssize_t net_http_read(HttpStream *st, void *buf, size_t nbytes);
683
db37761a8494 enable buffered reader for request bodies with fixed content-length
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 513
diff changeset
227 ssize_t net_http_read_buffered(HttpStream *st, void *buf, size_t nbytes);
333
bb536d4bc174 first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
228 ssize_t net_http_read_chunked(HttpStream *st, void *buf, size_t nbytes);
697
3ddfd45d4e47 add raw mode for http streams
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 684
diff changeset
229 ssize_t net_http_read_raw(HttpStream *st, void *buf, size_t nbytes);
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
230 ssize_t net_http_sendfile(HttpStream *st, sendfiledata *sfd);
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
231 void net_http_close(HttpStream *st);
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
232 void net_http_finish(HttpStream *st);
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
233 void net_http_setmode(HttpStream *st, int mode);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
234 int net_http_poll(HttpStream *st, EventHandler *ev, int events, Event *cb);
4
998844b5ed25 Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
235
498
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
236 int http_stream_parse_chunk_header(char *str, int len, WSBool first, int64_t *chunklen);
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
237
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
238 /* ssl stream */
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
239 IOStream* sslstream_new(pool_handle_t *pool, SSL *ssl);
684
48da20bde908 refactor http client io to use an HttpStream for reading the response
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 683
diff changeset
240 void sslstream_free(IOStream *st);
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
241
498
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 430
diff changeset
242 ssize_t net_ssl_write(SSLStream *st, const void *buf, size_t nbytes);
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
243 ssize_t net_ssl_writev(SSLStream *st, struct iovec *iovec, int iovcnt);
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
244 ssize_t net_ssl_read(SSLStream *st, void *buf, size_t nbytes);
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
245 void net_ssl_close(SSLStream *st);
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
246 void net_ssl_finish(SSLStream *st);
172
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
247 void net_ssl_setmode(SSLStream *st, int mode);
5580517faafc adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 147
diff changeset
248 int net_ssl_poll(SSLStream *st, EventHandler *ev, int events, Event *cb);
106
b122f34ddc80 added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 80
diff changeset
249
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
250 /* net_ functions */
112
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 110 111
diff changeset
251 ssize_t net_fallback_sendfile(IOStream *fd, sendfiledata *sfd);
110
43a746e905f6 refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 106
diff changeset
252 void net_finish(SYS_NETFD fd);
32
ebba53de8b18 added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
253
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 #ifdef __cplusplus
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 #endif
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 #endif /* IOSTREAM_H */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259

mercurial