Thu, 23 May 2024 15:13:03 +0200
fix build
1 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
44
3da1f7b6847f
added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
41
diff
changeset
|
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
1 | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
41
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
29 | #ifdef __gnu_linux__ |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
30 | #define _GNU_SOURCE |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
31 | #endif |
bb7a1f5a8b48
added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
32
diff
changeset
|
32 | |
1 | 33 | #include <unistd.h> |
34 | #include <stdlib.h> | |
147
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
35 | |
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
36 | #ifdef XP_UNIX |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
37 | #include <sys/uio.h> |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
38 | #include <sys/uio.h> |
147
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
39 | #endif |
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
40 | |
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
41 | #ifdef XP_WIN32 |
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
42 | |
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
43 | #endif |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
44 | |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
45 | #if defined(LINUX) || defined(SOLARIS) |
54 | 46 | #include <sys/sendfile.h> |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
47 | #define WS_SENDFILE |
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
48 | #elif defined(BSD) |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
49 | #if defined(__NetBSD__) || defined(__OpenBSD__) |
112 | 50 | #define net_sys_sendfile net_fallback_sendfile |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
51 | #else |
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
52 | #define WS_SENDFILE |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
53 | #endif |
147
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
54 | #endif |
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
55 | |
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
56 | #ifdef WS_SENDFILE |
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
57 | #define NET_SYS_SENDFILE net_sys_sendfile |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
58 | #else |
147
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
59 | #define NET_SYS_SENDFILE net_fallback_sendfile |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
60 | #endif |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
61 | |
147
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
62 | |
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
63 | |
54 | 64 | #include "../daemon/vfs.h" |
1 | 65 | #include "io.h" |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
66 | #include "pool.h" |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
67 | #include "../daemon/netsite.h" |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
68 | #include "../daemon/event.h" |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
407
diff
changeset
|
69 | #include "cx/utils.h" |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
407
diff
changeset
|
70 | #include <cx/printf.h> |
1 | 71 | |
72 | IOStream native_io_funcs = { | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
73 | (io_write_f)net_sys_write, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
74 | (io_writev_f)net_sys_writev, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
75 | (io_read_f)net_sys_read, |
147
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
76 | (io_sendfile_f)NET_SYS_SENDFILE, |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
77 | (io_close_f)net_sys_close, |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
78 | NULL, |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
79 | (io_setmode_f)net_sys_setmode, |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
80 | (io_poll_f)net_sys_poll, |
513
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
81 | 0, |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
82 | 0 |
1 | 83 | }; |
84 | ||
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
85 | IOStream http_io_funcs = { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
86 | (io_write_f)net_http_write, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
87 | (io_writev_f)net_http_writev, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
88 | (io_read_f)net_http_read, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
89 | (io_sendfile_f)net_http_sendfile, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
90 | (io_close_f)net_http_close, |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
91 | (io_finish_f)net_http_finish, |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
92 | (io_setmode_f)net_http_setmode, |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
93 | (io_poll_f)net_http_poll, |
513
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
94 | 0, |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
95 | IO_STREAM_TYPE_HTTP |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
96 | }; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
97 | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
98 | IOStream ssl_io_funcs = { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
99 | (io_write_f)net_ssl_write, |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
100 | (io_writev_f)net_ssl_writev, |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
101 | (io_read_f)net_ssl_read, |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
102 | NULL, |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
103 | (io_close_f)net_ssl_close, |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
104 | (io_finish_f)net_ssl_finish, |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
105 | (io_setmode_f)net_ssl_setmode, |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
106 | (io_poll_f)net_ssl_poll, |
513
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
107 | 0, |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
108 | IO_STREAM_TYPE_SSL |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
109 | }; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
110 | |
513
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
111 | static int net_write_max_attempts = 16384; |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
112 | |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
113 | void io_set_max_writes(int n) { |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
114 | net_write_max_attempts = 1; |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
115 | } |
1 | 116 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
117 | /* |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
118 | * Sysstream implementation |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
119 | */ |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
120 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
121 | IOStream* Sysstream_new(pool_handle_t *pool, SYS_SOCKET fd) { |
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
122 | Sysstream *st = pool_malloc(pool, sizeof(Sysstream)); |
1 | 123 | st->st = native_io_funcs; |
124 | st->fd = fd; | |
125 | return (IOStream*)st; | |
126 | } | |
127 | ||
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
128 | #ifdef XP_UNIX |
498
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
129 | ssize_t net_sys_write(Sysstream *st, const void *buf, size_t nbytes) { |
502
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
130 | ssize_t r = write(st->fd, buf, nbytes); |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
131 | st->st.io_errno = errno; |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
132 | return r; |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
133 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
134 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
135 | ssize_t net_sys_writev(Sysstream *st, struct iovec *iovec, int iovcnt) { |
502
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
136 | ssize_t r = writev(st->fd, iovec, iovcnt); |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
137 | st->st.io_errno = errno; |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
138 | return r; |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
139 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
140 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
141 | ssize_t net_sys_read(Sysstream *st, void *buf, size_t nbytes) { |
502
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
142 | ssize_t r = read(st->fd, buf, nbytes); |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
143 | st->st.io_errno = errno; |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
144 | return r; |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
145 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
146 | |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
147 | #ifdef WS_SENDFILE |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
148 | ssize_t net_sys_sendfile(Sysstream *st, sendfiledata *sfd) { |
54 | 149 | ssize_t ret = 0; |
150 | off_t fileoffset = sfd->offset; | |
151 | if(sfd->fd->fd != -1) { | |
71 | 152 | #ifdef BSD |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
153 | struct iovec hdvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
154 | hdvec.iov_base = (void*)sfd->header; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
155 | hdvec.iov_len = sfd->hlen; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
156 | struct iovec trvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
157 | trvec.iov_base = (void*)sfd->trailer; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
158 | trvec.iov_len = sfd->tlen; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
159 | struct sf_hdtr hdtr; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
160 | hdtr.headers = &hdvec; |
493
56cf890dd9ed
fix net_sendfile not counting number of written bytes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
430
diff
changeset
|
161 | hdtr.hdr_cnt = sfd->hlen > 0 ? 1 : 0; |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
162 | hdtr.trailers = &trvec; |
493
56cf890dd9ed
fix net_sendfile not counting number of written bytes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
430
diff
changeset
|
163 | hdtr.trl_cnt = sfd->tlen > 0 ? 1 : 0; |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
164 | |
70
4e6e812c1d97
fixed some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
165 | off_t len = sfd->len; |
71 | 166 | #ifdef OSX |
167 | ret = sendfile(sfd->fd->fd, st->fd, fileoffset, &len, &hdtr, 0); | |
168 | #else // BSD | |
169 | ret = sendfile( | |
170 | sfd->fd->fd, | |
171 | st->fd, | |
172 | fileoffset, | |
173 | sfd->len, | |
174 | &hdtr, | |
175 | NULL, | |
176 | 0); | |
177 | #endif | |
493
56cf890dd9ed
fix net_sendfile not counting number of written bytes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
430
diff
changeset
|
178 | if(ret == 0) { |
56cf890dd9ed
fix net_sendfile not counting number of written bytes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
430
diff
changeset
|
179 | ret = sfd->hlen + sfd->tlen + sfd->len; |
56cf890dd9ed
fix net_sendfile not counting number of written bytes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
430
diff
changeset
|
180 | } |
56cf890dd9ed
fix net_sendfile not counting number of written bytes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
430
diff
changeset
|
181 | |
71 | 182 | #else // Solaris/Linux |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
183 | if(sfd->header) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
184 | ret += write(st->fd, sfd->header, sfd->hlen); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
185 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
186 | ret += sendfile(st->fd, sfd->fd->fd, &fileoffset, sfd->len); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
187 | if(sfd->trailer) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
188 | ret += write(st->fd, sfd->trailer, sfd->tlen); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
189 | } |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
190 | #endif |
54 | 191 | } else { |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
192 | return net_fallback_sendfile((IOStream*)st, sfd); |
54 | 193 | } |
502
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
194 | st->st.io_errno = errno; |
54 | 195 | return ret; |
196 | } | |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
197 | #endif |
54 | 198 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
199 | void net_sys_close(Sysstream *st) { |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
200 | system_close(st->fd); |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
201 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
202 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
203 | void net_sys_setmode(Sysstream *st, int mode) { |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
204 | int flags; |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
205 | if (-1 == (flags = fcntl(st->fd, F_GETFL, 0))) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
206 | flags = 0; |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
207 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
208 | if(mode == IO_MODE_BLOCKING) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
209 | if (fcntl(st->fd, F_SETFL, flags & ~O_NONBLOCK) != 0) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
210 | perror("fcntl"); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
211 | // TODO: error |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
212 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
213 | } else if(mode == IO_MODE_NONBLOCKING) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
214 | if (fcntl(st->fd, F_SETFL, flags | O_NONBLOCK) != 0) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
215 | perror("fcntl"); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
216 | // TODO: error |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
217 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
218 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
219 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
220 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
221 | int net_sys_poll(Sysstream *st, EventHandler *ev, int events, Event *cb) { |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
222 | switch(events) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
223 | default: return -1; |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
224 | case IO_POLL_NONE: return ev_remove_poll(ev, st->fd); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
225 | case IO_POLL_IN: return ev_pollin(ev, st->fd, cb); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
226 | case IO_POLL_OUT: return ev_pollout(ev, st->fd, cb); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
227 | case IO_POLL_IN | IO_POLL_OUT: return -1; // TODO: implement |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
228 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
229 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
230 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
231 | #elif defined(XP_WIN32) |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
232 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
233 | ssize_t net_sys_write(Sysstream *st, void *buf, size_t nbytes) { |
147
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
234 | int ret = send(st->fd, buf, nbytes, 0); |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
235 | if(ret == SOCKET_ERROR) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
236 | return IO_ERROR; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
237 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
238 | return ret; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
239 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
240 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
241 | ssize_t net_sys_writev(Sysstream *st, struct iovec *iovec, int iovcnt) { |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
242 | // TODO |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
243 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
244 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
245 | ssize_t net_sys_read(Sysstream *st, void *buf, size_t nbytes) { |
147
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
246 | int ret = recv(st->fd, buf, nbytes, 0); |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
247 | if(ret == SOCKET_ERROR) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
248 | return IO_ERROR; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
249 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
250 | return ret; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
251 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
252 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
253 | ssize_t net_sys_sendfile(Sysstream *st, sendfiledata *sfd) { |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
254 | // TODO |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
255 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
256 | |
430
83560f32e7d5
refactor send_cgi into non-blocking SAF (wip)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
257 | void net_sys_close(Sysstream *st) { |
147
d050449c3b9e
ported io.c and some headers to windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
142
diff
changeset
|
258 | closesocket(st->fd); |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
259 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
260 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
261 | #endif |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
262 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
263 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
264 | /* |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
265 | * HttpStream implementation |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
266 | */ |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
267 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
268 | IOStream* httpstream_new(pool_handle_t *pool, IOStream *fd) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
269 | HttpStream *st = pool_malloc(pool, sizeof(HttpStream)); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
270 | st->st = http_io_funcs; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
271 | st->fd = fd; |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
407
diff
changeset
|
272 | st->written = 0; |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
273 | st->max_read = 0; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
274 | st->read = 0; |
354
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
275 | st->read_total = 0; |
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
276 | st->readbuf = NULL; |
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
277 | st->bufsize = 0; |
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
278 | st->buflen = NULL; |
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
279 | st->bufpos = NULL; |
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
280 | st->chunk_buf_pos = 0; |
498
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
281 | st->current_chunk_length = 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
282 | st->current_chunk_pos = 0; |
500
077aa138e8fb
fix uninitialized memory in http stream, when chunked transfer encoding is used
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
498
diff
changeset
|
283 | st->current_trailer = 0; |
498
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
284 | st->write_chunk_buf_len = 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
285 | st->write_chunk_buf_pos = 0; |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
286 | st->chunked_enc = WS_FALSE; |
354
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
287 | st->read_eof = WS_TRUE; |
361
570026d3a685
make sure the http stream is finished if headers are sent
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
288 | st->write_eof = WS_FALSE; |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
289 | return (IOStream*)st; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
290 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
291 | |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
292 | int httpstream_enable_chunked_read(IOStream *st, char *buffer, size_t bufsize, int *cursize, int *pos) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
293 | if(st->read != (io_read_f)net_http_read) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
294 | log_ereport(LOG_FAILURE, "%s", "httpstream_enable_chunked_read: IOStream is not an HttpStream"); |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
295 | return 1; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
296 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
297 | st->read = (io_read_f)net_http_read_chunked; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
298 | HttpStream *http = (HttpStream*)st; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
299 | http->max_read = 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
300 | http->read = 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
301 | http->readbuf = buffer; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
302 | http->bufsize = bufsize; |
336
fb75473fecca
store HttpStream buflen as pointer (referencing NSAPISession cursize)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
335
diff
changeset
|
303 | http->buflen = cursize; |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
304 | http->bufpos = pos; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
305 | http->chunk_buf_pos = 0; |
354
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
306 | http->read_eof = WS_FALSE; |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
307 | return 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
308 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
309 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
310 | int httpstream_enable_chunked_write(IOStream *st) { |
513
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
311 | if(st->type != IO_STREAM_TYPE_HTTP) { |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
312 | log_ereport(LOG_FAILURE, "%s", "httpstream_enable_chunked_write: IOStream is not an HttpStream"); |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
313 | return 1; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
314 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
315 | HttpStream *http = (HttpStream*)st; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
316 | http->chunked_enc = WS_TRUE; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
317 | return 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
318 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
319 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
320 | int httpstream_set_max_read(IOStream *st, int64_t maxread) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
321 | if(st->write != (io_write_f)net_http_write) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
322 | log_ereport(LOG_FAILURE, "%s", "httpstream_set_max_read: IOStream is not an HttpStream"); |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
323 | return 1; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
324 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
325 | HttpStream *http = (HttpStream*)st; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
326 | http->max_read = maxread; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
327 | return 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
328 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
329 | |
354
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
330 | WSBool httpstream_eof(IOStream *st) { |
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
331 | HttpStream *http = (HttpStream*)st; |
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
332 | return http->read_eof; |
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
333 | } |
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
334 | |
407
78af44222463
close connection if content-length != number of bytes written
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
406
diff
changeset
|
335 | int64_t httpstream_written(IOStream *st) { |
78af44222463
close connection if content-length != number of bytes written
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
406
diff
changeset
|
336 | HttpStream *http = (HttpStream*)st; |
78af44222463
close connection if content-length != number of bytes written
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
406
diff
changeset
|
337 | return http->written; |
78af44222463
close connection if content-length != number of bytes written
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
406
diff
changeset
|
338 | } |
78af44222463
close connection if content-length != number of bytes written
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
406
diff
changeset
|
339 | |
498
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
340 | /* |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
341 | * iovec callback func |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
342 | * returns number of payload bytes written (number of bytes returned back to the net_write caller) |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
343 | */ |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
344 | typedef ssize_t(*writeop_finish_func)(HttpStream *st, char *base, size_t len, size_t written, void *udata); |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
345 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
346 | static ssize_t httpstream_finish_prev_header(HttpStream *st, char *base, size_t len, size_t written, void *udata) { |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
347 | st->write_chunk_buf_pos += written; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
348 | if(st->write_chunk_buf_pos == st->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:
493
diff
changeset
|
349 | st->write_chunk_buf_len = 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
350 | st->write_chunk_buf_pos = 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
351 | } |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
352 | return 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
353 | } |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
354 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
355 | static ssize_t httpstream_finish_data(HttpStream *st, char *base, size_t len, size_t written, void *udata) { |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
356 | st->current_chunk_pos += written; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
357 | if(st->current_chunk_pos == st->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:
493
diff
changeset
|
358 | st->current_chunk_length = 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
359 | st->current_chunk_pos = 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
360 | st->current_trailer = 2; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
361 | } |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
362 | return written; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
363 | } |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
364 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
365 | static ssize_t httpstream_finish_new_header(HttpStream *st, char *base, size_t len, size_t written, void *udata) { |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
366 | size_t *chunk_len = udata; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
367 | st->current_chunk_length = *chunk_len; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
368 | st->current_chunk_pos = 0; // new chunk started |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
369 | if(written < len) { |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
370 | st->write_chunk_buf_len = len-written; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
371 | st->write_chunk_buf_pos = 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
372 | memcpy(st->write_chunk_buf + st->write_chunk_buf_pos, base+written, st->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:
493
diff
changeset
|
373 | } else { |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
374 | st->write_chunk_buf_len = 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
375 | st->write_chunk_buf_pos = 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
376 | } |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
377 | return 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
378 | } |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
379 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
380 | static ssize_t httpstream_finish_trailer(HttpStream *st, char *base, size_t len, size_t written, void *udata) { |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
381 | st->current_trailer -= written; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
382 | return 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
383 | } |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
384 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
385 | ssize_t net_http_write(HttpStream *st, const void *buf, size_t nbytes) { |
502
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
386 | st->st.io_errno = 0; |
361
570026d3a685
make sure the http stream is finished if headers are sent
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
387 | if(st->write_eof) return 0; |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
388 | IOStream *fd = st->fd; |
498
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
389 | if(!st->chunked_enc) { |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
390 | ssize_t w = fd->write(fd, buf, nbytes); |
406
e5d96f6b9306
store number of written bytes in the HttpStream
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
383
diff
changeset
|
391 | st->written += w > 0 ? w : 0; |
e5d96f6b9306
store number of written bytes in the HttpStream
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
383
diff
changeset
|
392 | return w; |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
393 | } else { |
498
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
394 | struct iovec io[8]; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
395 | writeop_finish_func io_finished[8]; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
396 | void *io_finished_udata[8]; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
397 | int iovec_len = 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
398 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
399 | char *str_crlf = "\r\n"; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
400 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
401 | size_t prev_chunk_len = st->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:
493
diff
changeset
|
402 | size_t new_chunk_len = 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
403 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
404 | // was the previous chunk header completely sent? |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
405 | if(st->write_chunk_buf_len > 0) { |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
406 | io[0].iov_base = &st->write_chunk_buf[st->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:
493
diff
changeset
|
407 | io[0].iov_len = st->write_chunk_buf_len - st->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:
493
diff
changeset
|
408 | io_finished[0] = httpstream_finish_prev_header; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
409 | io_finished_udata[0] = &prev_chunk_len; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
410 | iovec_len++; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
411 | } |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
412 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
413 | // was the previous chunk payload completely sent? |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
414 | if(st->current_chunk_length != 0) { |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
415 | size_t chunk_remaining = st->current_chunk_length - st->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:
493
diff
changeset
|
416 | size_t prev_nbytes = chunk_remaining > nbytes ? nbytes : chunk_remaining; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
417 | io[iovec_len].iov_base = (char*)buf; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
418 | io[iovec_len].iov_len = prev_nbytes; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
419 | io_finished[iovec_len] = httpstream_finish_data; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
420 | buf = ((char*)buf) + prev_nbytes; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
421 | nbytes -= prev_nbytes; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
422 | iovec_len++; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
423 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
424 | io[iovec_len].iov_base = str_crlf; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
425 | io[iovec_len].iov_len = 2; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
426 | io_finished[iovec_len] = httpstream_finish_trailer; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
427 | iovec_len++; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
428 | } else if(st->current_trailer > 0) { |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
429 | io[iovec_len].iov_base = str_crlf + 2 - st->current_trailer; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
430 | io[iovec_len].iov_len = st->current_trailer; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
431 | io_finished[iovec_len] = httpstream_finish_trailer; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
432 | iovec_len++; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
433 | } |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
434 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
435 | // TODO: on some plattforms iov_len is smaller than size_t |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
436 | // if nbytes > INT_MAX, it should be devided into multiple |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
437 | // iovec entries |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
438 | char chunk_len[16]; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
439 | if(nbytes > 0) { |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
440 | new_chunk_len = nbytes; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
441 | io[iovec_len].iov_base = chunk_len; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
442 | io[iovec_len].iov_len = snprintf(chunk_len, 16, "%zx\r\n", nbytes); |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
443 | io_finished[iovec_len] = httpstream_finish_new_header; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
444 | io_finished_udata[iovec_len] = &new_chunk_len; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
445 | iovec_len++; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
446 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
447 | io[iovec_len].iov_base = (char*)buf; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
448 | io[iovec_len].iov_len = nbytes; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
449 | io_finished[iovec_len] = httpstream_finish_data; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
450 | iovec_len++; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
451 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
452 | io[iovec_len].iov_base = str_crlf; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
453 | io[iovec_len].iov_len = 2; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
454 | io_finished[iovec_len] = httpstream_finish_trailer; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
455 | iovec_len++; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
456 | } |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
457 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
458 | ssize_t wv = fd->writev(fd, io, iovec_len); |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
459 | if(wv <= 0) { |
502
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
460 | st->st.io_errno = net_errno(st->fd); |
498
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
461 | return wv; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
462 | } |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
463 | |
502
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
464 | ssize_t ret_w = 0; |
498
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
465 | int i = 0; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
466 | while(wv > 0) { |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
467 | char *base = io[i].iov_base; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
468 | size_t len = io[i].iov_len; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
469 | size_t wlen = wv > len ? len : wv; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
470 | ret_w += io_finished[i](st, base, len, wlen, io_finished_udata[i]); |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
471 | wv -= wlen; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
472 | i++; |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
473 | } |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
474 | |
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
475 | st->written += ret_w; |
502
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
476 | if(ret_w == 0) { |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
477 | st->st.io_errno = EWOULDBLOCK; // not sure if this is really correct |
513
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
478 | //ret_w = -1; |
502
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
479 | } |
498
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
480 | return ret_w; |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
481 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
482 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
483 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
484 | ssize_t net_http_writev(HttpStream *st, struct iovec *iovec, int iovcnt) { |
361
570026d3a685
make sure the http stream is finished if headers are sent
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
485 | if(st->write_eof) return 0; |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
486 | IOStream *fd = st->fd; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
487 | if(st->chunked_enc) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
488 | struct iovec *io = calloc(iovcnt + 1, sizeof(struct iovec)); |
251
f727a21497bb
add basic PUT implementation and tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
235
diff
changeset
|
489 | if(!io) { |
f727a21497bb
add basic PUT implementation and tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
235
diff
changeset
|
490 | return 0; |
f727a21497bb
add basic PUT implementation and tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
235
diff
changeset
|
491 | } |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
492 | char chunk_len[16]; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
493 | io[0].iov_base = chunk_len; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
494 | size_t len = 0; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
495 | for(int i=0;i<iovcnt;i++) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
496 | len += iovec[i].iov_len; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
497 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
498 | io[0].iov_len = snprintf(chunk_len, 16, "\r\n%zx\r\n", len); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
499 | memcpy(io + 1, iovec, iovcnt * sizeof(struct iovec)); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
500 | ssize_t r = fd->writev(fd, io, iovcnt + 1); |
251
f727a21497bb
add basic PUT implementation and tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
235
diff
changeset
|
501 | |
f727a21497bb
add basic PUT implementation and tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
235
diff
changeset
|
502 | ssize_t ret = r - io[0].iov_len; |
f727a21497bb
add basic PUT implementation and tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
235
diff
changeset
|
503 | free(io); |
406
e5d96f6b9306
store number of written bytes in the HttpStream
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
383
diff
changeset
|
504 | st->written += ret; |
251
f727a21497bb
add basic PUT implementation and tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
235
diff
changeset
|
505 | return ret; |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
506 | } else { |
406
e5d96f6b9306
store number of written bytes in the HttpStream
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
383
diff
changeset
|
507 | ssize_t w = fd->writev(fd, iovec, iovcnt); |
e5d96f6b9306
store number of written bytes in the HttpStream
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
383
diff
changeset
|
508 | st->written += w; |
e5d96f6b9306
store number of written bytes in the HttpStream
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
383
diff
changeset
|
509 | return w; |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
510 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
511 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
512 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
513 | ssize_t net_http_read(HttpStream *st, void *buf, size_t nbytes) { |
332
6f82ede01e1c
simplify request input buffer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
251
diff
changeset
|
514 | if(st->read >= st->max_read) { |
354
017eda1be105
add missing HttpStream initializations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
336
diff
changeset
|
515 | st->read_eof = WS_TRUE; |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
516 | return 0; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
517 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
518 | ssize_t r = st->fd->read(st->fd, buf, nbytes); |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
519 | if(r < 0) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
520 | st->st.io_errno = st->fd->io_errno; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
521 | } |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
522 | st->read += r; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
523 | return r; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
524 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
525 | |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
526 | #define BUF_UNNEEDED_DIFF 64 |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
527 | /* |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
528 | * read from st->chunk_buf first, read from st->fd if perform_io is true |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
529 | */ |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
530 | static ssize_t net_http_read_buffered(HttpStream *st, char *buf, size_t nbytes, WSBool read_data, WSBool *perform_io) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
531 | ssize_t r = 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
532 | |
334
a55491f66003
fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
333
diff
changeset
|
533 | //memset(buf, 'x', nbytes); |
a55491f66003
fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
333
diff
changeset
|
534 | //char *orig_buf = buf; |
a55491f66003
fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
333
diff
changeset
|
535 | |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
536 | // copy available data from st->readbuf to buf |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
537 | int pos = *st->bufpos; |
336
fb75473fecca
store HttpStream buflen as pointer (referencing NSAPISession cursize)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
335
diff
changeset
|
538 | size_t buf_available = *st->buflen - pos; |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
539 | if(buf_available) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
540 | size_t cplen = buf_available > nbytes ? nbytes : buf_available; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
541 | if(read_data) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
542 | // if we read data (and not a chunk header), we limit the |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
543 | // amount of bytes we copy |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
544 | size_t chunk_available = st->max_read - st->read; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
545 | cplen = cplen > chunk_available ? chunk_available : cplen; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
546 | st->read += cplen; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
547 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
548 | memcpy(buf, st->readbuf + pos, cplen); |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
549 | *st->bufpos += cplen; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
550 | r += cplen; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
551 | buf += cplen; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
552 | nbytes -= cplen; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
553 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
554 | |
335 | 555 | // maybe perform IO and refill the read buffer |
556 | // if we read data (read_data == true), make sure not to perform IO, | |
557 | // when a chunk is completed | |
558 | // | |
559 | // if we read a chunk header (read_data == false) it is very important | |
560 | // to not perform IO, if we have previously copied data from readbuf | |
561 | // this ensures we never override non-chunk-header data | |
334
a55491f66003
fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
333
diff
changeset
|
562 | if(*perform_io && ((read_data && nbytes > 0 && st->max_read - st->read) || (!read_data && r == 0))) { |
336
fb75473fecca
store HttpStream buflen as pointer (referencing NSAPISession cursize)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
335
diff
changeset
|
563 | if(*st->buflen - *st->bufpos > 0) { |
334
a55491f66003
fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
333
diff
changeset
|
564 | printf("todo: fix, should not happen, remove later\n"); |
a55491f66003
fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
333
diff
changeset
|
565 | } |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
566 | // fill buffer again |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
567 | ssize_t rlen = st->fd->read(st->fd, st->readbuf, st->bufsize); |
336
fb75473fecca
store HttpStream buflen as pointer (referencing NSAPISession cursize)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
335
diff
changeset
|
568 | *st->buflen = rlen; |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
569 | *st->bufpos = 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
570 | *perform_io = WS_FALSE; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
571 | if(rlen < 0) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
572 | st->st.io_errno = st->fd->io_errno; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
573 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
574 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
575 | if(rlen > 0) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
576 | // call func again to get data from buffer (no IO will be performed) |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
577 | r += net_http_read_buffered(st, buf, nbytes, read_data, perform_io); |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
578 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
579 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
580 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
581 | return r; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
582 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
583 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
584 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
585 | /* |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
586 | * parses a chunk header |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
587 | * the chunk length is stored in chunklen |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
588 | * return: 0 if the data is incomplete |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
589 | * -1 if an error occured |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
590 | * >0 chunk header length |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
591 | */ |
498
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
592 | int http_stream_parse_chunk_header(char *str, int len, WSBool first, int64_t *chunklen) { |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
593 | char *hdr_start = NULL; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
594 | char *hdr_end = NULL; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
595 | int i = 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
596 | if(first) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
597 | hdr_start = str; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
598 | } else { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
599 | if(len < 3) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
600 | return 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
601 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
602 | if(str[0] == '\r' && str[1] == '\n') { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
603 | hdr_start = str+2; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
604 | i = 2; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
605 | } else if(str[0] == '\n') { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
606 | hdr_start = str+1; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
607 | i = 1; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
608 | } else { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
609 | return -1; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
610 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
611 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
612 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
613 | for(;i<len;i++) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
614 | char c = str[i]; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
615 | if(c == '\r' || c == '\n') { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
616 | hdr_end = str+i; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
617 | break; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
618 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
619 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
620 | if(!hdr_end || i == len) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
621 | return 0; // incomplete |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
622 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
623 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
624 | if(*hdr_end == '\r') { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
625 | // we also need '\n' |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
626 | if(hdr_end[1] != '\n') { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
627 | return -1; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
628 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
629 | i++; // '\n' found |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
630 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
631 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
632 | // parse |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
633 | char save_c = *hdr_end; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
634 | *hdr_end = '\0'; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
635 | char *end; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
636 | int64_t clen; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
637 | errno = 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
638 | clen = strtoll(hdr_start, &end, 16); |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
639 | *hdr_end = save_c; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
640 | if(errno) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
641 | return -1; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
642 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
643 | i++; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
644 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
645 | if(clen == 0) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
646 | // chunk length of 0 indicates the end |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
647 | // an additional \r\n is required (we also accept \n) |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
648 | if(i >= len) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
649 | return 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
650 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
651 | if(str[i] == '\n') { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
652 | i++; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
653 | } else if(str[i] == '\r') { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
654 | if(++i >= len) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
655 | return 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
656 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
657 | if(str[i] == '\n') { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
658 | i++; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
659 | } else { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
660 | return -1; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
661 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
662 | } else { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
663 | return -1; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
664 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
665 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
666 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
667 | *chunklen = clen; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
668 | return i; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
669 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
670 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
671 | ssize_t net_http_read_chunked(HttpStream *st, void *buf, size_t nbytes) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
672 | if(st->read_eof) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
673 | return 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
674 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
675 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
676 | char *rbuf = buf; // buffer pos |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
677 | size_t rd = 0; // number of bytes read |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
678 | size_t rbuflen = nbytes; // number of bytes until end of buf |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
679 | WSBool perform_io = WS_TRUE; // we do only 1 read before we abort |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
680 | while(rd < nbytes && (perform_io || (st->max_read - st->read) > 0)) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
681 | // how many bytes are available in the current chunk |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
682 | size_t chunk_available = st->max_read - st->read; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
683 | if(chunk_available > 0) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
684 | ssize_t r = net_http_read_buffered(st, rbuf, rbuflen, TRUE, &perform_io); |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
685 | if(r == 0) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
686 | break; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
687 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
688 | rd += r; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
689 | st->read_total += r; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
690 | rbuf += r; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
691 | rbuflen -= r; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
692 | } else { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
693 | int chunkbuf_avail = HTTP_STREAM_CBUF_SIZE - st->chunk_buf_pos; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
694 | if(chunkbuf_avail == 0) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
695 | // for some reason HTTP_STREAM_CBUF_SIZE is not enough |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
696 | // to store the chunk header |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
697 | // this indicates that something has gone wrong (or this is an attack) |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
698 | st->read_eof = WS_TRUE; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
699 | return -1; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
700 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
701 | // fill st->chunk_buf |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
702 | ssize_t r = net_http_read_buffered(st, &st->chunk_buf[st->chunk_buf_pos], chunkbuf_avail, FALSE, &perform_io); |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
703 | if(r == 0) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
704 | break; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
705 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
706 | int chunkbuf_len = st->chunk_buf_pos + r; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
707 | int64_t chunklen; |
498
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
708 | int ret = http_stream_parse_chunk_header(st->chunk_buf, chunkbuf_len, st->read_total > 0 ? FALSE : TRUE, &chunklen); |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
709 | if(ret == 0) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
710 | // incomplete chunk header |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
711 | st->chunk_buf_pos = chunkbuf_len; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
712 | } else if(ret < 0) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
713 | // error |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
714 | st->read_eof = WS_TRUE; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
715 | return -1; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
716 | } else if(ret > 0) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
717 | st->max_read = chunklen; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
718 | st->read = 0; |
335 | 719 | int remaining_len = chunkbuf_len - ret; |
720 | if(remaining_len > 0) { | |
721 | // we have read more into chunk_buf than the chunk_header | |
722 | // it is save to just move bufpos back | |
723 | *st->bufpos -= remaining_len; | |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
724 | } |
334
a55491f66003
fix and simplify chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
333
diff
changeset
|
725 | //st->remaining_len = chunkbuf_len - ret; |
333
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
726 | st->chunk_buf_pos = 0; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
727 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
728 | if(chunklen == 0) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
729 | st->read_eof = WS_TRUE; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
730 | break; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
731 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
732 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
733 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
734 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
735 | if(!perform_io && rd == 0) { |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
736 | perform_io = WS_TRUE; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
737 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
738 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
739 | |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
740 | return rd; |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
741 | } |
bb536d4bc174
first semi-functional implementation of chunked transfer encoding for request bodies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
332
diff
changeset
|
742 | |
361
570026d3a685
make sure the http stream is finished if headers are sent
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
743 | ssize_t net_http_sendfile(HttpStream *st, sendfiledata *sfd) { |
570026d3a685
make sure the http stream is finished if headers are sent
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
744 | if(st->write_eof) return 0; |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
745 | ssize_t ret = 0; |
112 | 746 | // TODO: support chunked transfer encoding |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
747 | if(st->fd->sendfile) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
748 | ret = st->fd->sendfile(st->fd, sfd); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
749 | } else { |
112 | 750 | ret = net_fallback_sendfile((IOStream*)st, sfd); |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
751 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
752 | |
493
56cf890dd9ed
fix net_sendfile not counting number of written bytes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
430
diff
changeset
|
753 | st->written += ret > 0 ? ret : 0; |
56cf890dd9ed
fix net_sendfile not counting number of written bytes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
430
diff
changeset
|
754 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
755 | return ret; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
756 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
757 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
758 | void net_http_close(HttpStream *st) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
759 | st->fd->close(st->fd); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
760 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
761 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
762 | void net_http_finish(HttpStream *st) { |
361
570026d3a685
make sure the http stream is finished if headers are sent
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
763 | if(st->chunked_enc && !st->write_eof) { |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
764 | st->fd->write(st->fd, "0\r\n\r\n", 5); |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
765 | } |
361
570026d3a685
make sure the http stream is finished if headers are sent
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
354
diff
changeset
|
766 | st->write_eof = WS_TRUE; |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
767 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
768 | |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
769 | 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
|
770 | st->fd->setmode(st->fd, mode); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
771 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
772 | |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
773 | int net_http_poll(HttpStream *st, EventHandler *ev, int events, Event *cb) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
774 | return st->fd->poll(st->fd, ev, events, cb); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
775 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
776 | |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
777 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
778 | /* |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
779 | * SSLStream implementation |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
780 | */ |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
781 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
782 | IOStream* sslstream_new(pool_handle_t *pool, SSL *ssl) { |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
783 | SSLStream *st = pool_malloc(pool, sizeof(SSLStream)); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
784 | st->st = ssl_io_funcs; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
785 | st->ssl = ssl; |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
786 | st->error = 0; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
787 | return (IOStream*)st; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
788 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
789 | |
498
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
790 | ssize_t net_ssl_write(SSLStream *st, const void *buf, size_t nbytes) { |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
791 | int ret = SSL_write(st->ssl, buf, nbytes); |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
792 | if(ret <= 0) { |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
793 | st->error = SSL_get_error(st->ssl, ret); |
502
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
794 | if(st->error == SSL_ERROR_WANT_WRITE || st->error == SSL_ERROR_WANT_READ) { |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
795 | st->st.io_errno = EWOULDBLOCK; |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
796 | } else { |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
797 | st->st.io_errno = -1; |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
798 | } |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
799 | ret = -1; |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
800 | } |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
801 | return ret; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
802 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
803 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
804 | ssize_t net_ssl_writev(SSLStream *st, struct iovec *iovec, int iovcnt) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
805 | ssize_t r = 0; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
806 | for(int i=0;i<iovcnt;i++) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
807 | int ret = SSL_write(st->ssl, iovec[i].iov_base, iovec[i].iov_len); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
808 | if(ret <= 0) { |
502
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
809 | if(r == 0) { |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
810 | st->error = SSL_get_error(st->ssl, ret); |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
811 | if(st->error == SSL_ERROR_WANT_WRITE || st->error == SSL_ERROR_WANT_READ) { |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
812 | st->st.io_errno = EWOULDBLOCK; |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
813 | } else { |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
814 | st->st.io_errno = -1; |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
815 | } |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
816 | } |
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
817 | break; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
818 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
819 | r += ret; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
820 | } |
502
11ac3761c0e3
fix non-blocking CGI handler and non-blocking SSL-IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
500
diff
changeset
|
821 | return r == 0 ? -1 : r; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
822 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
823 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
824 | ssize_t net_ssl_read(SSLStream *st, void *buf, size_t nbytes) { |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
825 | int ret = SSL_read(st->ssl, buf, nbytes); |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
826 | if(ret <= 0) { |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
827 | st->error = SSL_get_error(st->ssl, ret); |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
828 | } |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
829 | return ret; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
830 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
831 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
832 | void net_ssl_close(SSLStream *st) { |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
833 | int ret = SSL_shutdown(st->ssl); |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
834 | if(ret != 1) { |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
835 | st->error = SSL_get_error(st->ssl, ret); |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
836 | } |
171
af7e2d80dee6
adds wrapper for close syscall with error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
837 | system_close(SSL_get_fd(st->ssl)); |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
838 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
839 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
840 | void net_ssl_finish(SSLStream *st) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
841 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
842 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
843 | |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
844 | 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
|
845 | int flags; |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
846 | if (-1 == (flags = fcntl(SSL_get_fd(st->ssl), F_GETFL, 0))) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
847 | flags = 0; |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
848 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
849 | if(mode == IO_MODE_BLOCKING) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
850 | if (fcntl(SSL_get_fd(st->ssl), F_SETFL, flags & ~O_NONBLOCK) != 0) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
851 | perror("fcntl"); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
852 | // TODO: error |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
853 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
854 | } else if(mode == IO_MODE_NONBLOCKING) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
855 | if (fcntl(SSL_get_fd(st->ssl), F_SETFL, flags | O_NONBLOCK) != 0) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
856 | perror("fcntl"); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
857 | // TODO: error |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
858 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
859 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
860 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
861 | |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
862 | int net_ssl_poll(SSLStream *st, EventHandler *ev, int events, Event *cb) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
863 | int fd = SSL_get_fd(st->ssl); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
864 | switch(events) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
865 | default: return -1; |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
866 | case IO_POLL_NONE: return ev_remove_poll(ev, fd); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
867 | case IO_POLL_IN: return ev_pollin(ev, fd, cb); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
868 | case IO_POLL_OUT: return ev_pollout(ev, fd, cb); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
869 | case IO_POLL_IN | IO_POLL_OUT: return -1; // TODO: implement |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
870 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
871 | } |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
872 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
873 | /* -------------------- public nsapi network functions -------------------- */ |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
874 | |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
875 | ssize_t net_read(SYS_NETFD fd, void *buf, size_t nbytes) { |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
876 | ssize_t r = ((IOStream*)fd)->read(fd, buf, nbytes); |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
877 | if(r == 0) { |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
878 | return IO_EOF; |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
879 | } else if(r < 0) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
880 | ((IOStream*)fd)->io_errno = errno; |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
881 | return IO_ERROR; |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
882 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
883 | return r; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
884 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
885 | |
498
0d80f8a2b29f
fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
493
diff
changeset
|
886 | ssize_t net_write(SYS_NETFD fd, const void *buf, size_t nbytes) { |
513
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
887 | size_t w = 0; |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
888 | size_t remaining = nbytes; |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
889 | const char *cbuf = buf; |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
890 | ssize_t r = 0; |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
891 | int attempts = 0; |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
892 | while(w < nbytes && attempts < net_write_max_attempts) { |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
893 | r = ((IOStream*)fd)->write(fd, cbuf, remaining); |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
894 | if(r <= 0) { |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
895 | break; |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
896 | } |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
897 | w += r; |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
898 | cbuf += r; |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
899 | remaining -= r; |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
900 | attempts++; |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
901 | } |
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
902 | if(r < 0 && w == 0) { |
9
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
903 | return IO_ERROR; |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
904 | } |
513
9a49c245a49c
change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
502
diff
changeset
|
905 | return w; |
9
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
906 | } |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
907 | |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
908 | ssize_t net_writev(SYS_NETFD fd, struct iovec *iovec, int iovcnt) { |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
909 | ssize_t r = ((IOStream*)fd)->writev(fd, iovec, iovcnt); |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
910 | if(r < 0) { |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
911 | ((IOStream*)fd)->io_errno = errno; |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
912 | return IO_ERROR; |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
913 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
914 | return r; |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
915 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
916 | |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
917 | ssize_t net_printf(SYS_NETFD fd, char *format, ...) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
918 | va_list arg; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
919 | va_start(arg, format); |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
407
diff
changeset
|
920 | cxmutstr buf = cx_vasprintf_a(cxDefaultAllocator, format, arg); |
383
a5698877d14a
fix net_printf not working with empty strings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
361
diff
changeset
|
921 | ssize_t r = buf.length > 0 ? net_write(fd, buf.ptr, buf.length) : 0; |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
922 | free(buf.ptr); |
142
55298bc9ed28
adds new a pathcheck saf and improves content type matchin and improves content type matching
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
923 | va_end(arg); |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
924 | if(r < 0) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
925 | ((IOStream*)fd)->io_errno = errno; |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
926 | } |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
927 | return r; |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
928 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
929 | |
54 | 930 | ssize_t net_sendfile(SYS_NETFD fd, sendfiledata *sfd) { |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
931 | IOStream *out = fd; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
932 | if(out->sendfile && sfd->fd && sfd->fd->fd != -1) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
933 | ssize_t r = out->sendfile(fd, sfd); |
54 | 934 | if(r < 0) { |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
935 | out->io_errno = errno; |
54 | 936 | return IO_ERROR; |
937 | } | |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
938 | return r; |
54 | 939 | } else { |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
940 | // stream/file does not support sendfile |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
941 | // do regular copy |
112 | 942 | return net_fallback_sendfile(out, sfd); |
54 | 943 | } |
944 | } | |
945 | ||
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
946 | // private |
112 | 947 | ssize_t net_fallback_sendfile(IOStream *fd, sendfiledata *sfd) { |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
948 | char *buf = malloc(4096); |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
949 | if(!buf) { |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
950 | // TODO: out of memory error |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
951 | return IO_ERROR; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
952 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
953 | char *header = (char*)sfd->header; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
954 | int hlen = sfd->hlen; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
955 | char *trailer = (char*)sfd->trailer; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
956 | int tlen = sfd->tlen; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
957 | if(header == NULL) { |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
958 | hlen = 0; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
959 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
960 | if(trailer == NULL) { |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
961 | tlen = 0; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
962 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
963 | |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
964 | ssize_t r; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
965 | while(hlen > 0) { |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
966 | r = fd->write(fd, header, hlen); |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
967 | header += r; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
968 | hlen -= r; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
969 | if(r <= 0) { |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
970 | free(buf); |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
971 | fd->io_errno = errno; |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
972 | return IO_ERROR; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
973 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
974 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
975 | |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
976 | if(system_lseek(sfd->fd, sfd->offset, SEEK_SET) == -1) { |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
977 | free(buf); |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
978 | fd->io_errno = errno; |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
979 | return IO_ERROR; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
980 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
981 | |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
982 | size_t length = sfd->len; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
983 | while(length > 0) { |
205
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
984 | // TODO: remove |
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
985 | if(length > sfd->len) { |
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
986 | log_ereport(LOG_WARN, "net_fallback_sendfile: length > sfd->len: %zu > %zu", length, sfd->len); |
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
987 | free(buf); |
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
988 | return IO_ERROR; |
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
989 | } |
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
990 | |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
991 | if((r = system_fread(sfd->fd, buf, 4096)) <= 0) { |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
992 | break; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
993 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
994 | char *write_buf = buf; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
995 | while(r > 0) { |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
996 | ssize_t w = fd->write(fd, write_buf, r); |
205
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
997 | // TODO: remove |
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
998 | if(w > r) { |
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
999 | log_ereport(LOG_WARN, "net_fallback_sendfile: w > r, %zd > %zd", w, r); |
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
1000 | w = 0; |
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
1001 | } |
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
1002 | |
203
7fe53d5d587c
fix error handling in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
193
diff
changeset
|
1003 | if(w <= 0) { |
205
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
1004 | free(buf); |
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
1005 | fd->io_errno = errno; |
b8515afa450a
improve error handling and add some debug code in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
203
diff
changeset
|
1006 | return IO_ERROR; |
203
7fe53d5d587c
fix error handling in net_fallback_sendfile
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
193
diff
changeset
|
1007 | } |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1008 | r -= w; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1009 | length -= w; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1010 | write_buf += w; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1011 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1012 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1013 | free(buf); |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1014 | if(length > 0) { |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
1015 | fd->io_errno = errno; |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1016 | return IO_ERROR; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1017 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1018 | |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1019 | while(tlen > 0) { |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1020 | r = fd->write(fd, trailer, tlen); |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1021 | trailer += r; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1022 | tlen -= r; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1023 | if(r <= 0) { |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
1024 | fd->io_errno = errno; |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1025 | return IO_ERROR; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1026 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1027 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1028 | |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1029 | return sfd->hlen + sfd->len + sfd->tlen; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1030 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1031 | |
82
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
1032 | int net_flush(SYS_NETFD sd) { |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
1033 | // TODO: implement |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
1034 | return 0; |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
1035 | } |
32
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
1036 | |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
1037 | void net_close(SYS_NETFD fd) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
1038 | ((IOStream*)fd)->close(fd); |
32
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
1039 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
1040 | |
172
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
1041 | int net_setnonblock(SYS_NETFD fd, int nonblock) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
1042 | ((IOStream*)fd)->setmode( |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
1043 | fd, |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
1044 | nonblock ? IO_MODE_NONBLOCKING : IO_MODE_BLOCKING); |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
1045 | return 0; |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
1046 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
1047 | |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
1048 | int net_errno(SYS_NETFD fd) { |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
1049 | return ((IOStream*)fd)->io_errno; |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
1050 | } |
5580517faafc
adds public aio and poll api and asynchronous send_range function
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
147
diff
changeset
|
1051 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
1052 | // private |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
1053 | void net_finish(SYS_NETFD fd) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
1054 | ((IOStream*)fd)->finish(fd); |
32
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
1055 | } |