Fri, 13 Jan 2017 09:53:55 +0100
fixes memory leaks in request_stat_path and send_cgi
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> | |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
35 | #include <sys/uio.h> |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
36 | #include <sys/uio.h> |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
37 | |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
38 | #if defined(LINUX) || defined(SOLARIS) |
54 | 39 | #include <sys/sendfile.h> |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
40 | #define WS_SENDFILE |
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
41 | #elif defined(BSD) |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
42 | #if defined(__NetBSD__) || defined(__OpenBSD__) |
112 | 43 | #define net_sys_sendfile net_fallback_sendfile |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
44 | #else |
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
45 | #define WS_SENDFILE |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
46 | #endif |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
47 | #else |
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
48 | #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
|
49 | #endif |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
50 | |
54 | 51 | #include "../daemon/vfs.h" |
1 | 52 | #include "io.h" |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
53 | #include "pool.h" |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
54 | #include "ucx/utils.h" |
1 | 55 | |
56 | IOStream native_io_funcs = { | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
57 | (io_write_f)net_sys_write, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
58 | (io_writev_f)net_sys_writev, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
59 | (io_read_f)net_sys_read, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
60 | (io_sendfile_f)net_sys_sendfile, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
61 | (io_close_f)net_sys_close, |
54 | 62 | NULL |
1 | 63 | }; |
64 | ||
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
65 | IOStream http_io_funcs = { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
66 | (io_write_f)net_http_write, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
67 | (io_writev_f)net_http_writev, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
68 | (io_read_f)net_http_read, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
69 | (io_sendfile_f)net_http_sendfile, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
70 | (io_close_f)net_http_close, |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
71 | (io_finish_f)net_http_finish |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
72 | }; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
73 | |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
74 | IOStream ssl_io_funcs = { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
75 | (io_write_f)net_ssl_write, |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
76 | (io_writev_f)net_ssl_writev, |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
77 | (io_read_f)net_ssl_read, |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
78 | NULL, |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
79 | (io_close_f)net_ssl_close, |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
80 | (io_finish_f)net_ssl_finish |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
81 | }; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
82 | |
1 | 83 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
84 | /* |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
85 | * SysStream implementation |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
86 | */ |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
87 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
88 | IOStream* sysstream_new(pool_handle_t *pool, int fd) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
89 | SysStream *st = pool_malloc(pool, sizeof(SysStream)); |
1 | 90 | st->st = native_io_funcs; |
91 | st->fd = fd; | |
92 | return (IOStream*)st; | |
93 | } | |
94 | ||
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
95 | #ifdef XP_UNIX |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
96 | ssize_t net_sys_write(SysStream *st, void *buf, size_t nbytes) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
97 | return write(st->fd, buf, nbytes); |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
98 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
99 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
100 | ssize_t net_sys_writev(SysStream *st, struct iovec *iovec, int iovcnt) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
101 | return writev(st->fd, iovec, iovcnt); |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
102 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
103 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
104 | ssize_t net_sys_read(SysStream *st, void *buf, size_t nbytes) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
105 | return read(st->fd, buf, nbytes); |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
106 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
107 | |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
108 | #ifdef WS_SENDFILE |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
109 | ssize_t net_sys_sendfile(SysStream *st, sendfiledata *sfd) { |
54 | 110 | ssize_t ret = 0; |
111 | off_t fileoffset = sfd->offset; | |
112 | if(sfd->fd->fd != -1) { | |
71 | 113 | #ifdef BSD |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
114 | struct iovec hdvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
115 | hdvec.iov_base = (void*)sfd->header; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
116 | hdvec.iov_len = sfd->hlen; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
117 | struct iovec trvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
118 | trvec.iov_base = (void*)sfd->trailer; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
119 | trvec.iov_len = sfd->tlen; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
120 | struct sf_hdtr hdtr; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
121 | hdtr.headers = &hdvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
122 | hdtr.hdr_cnt = 1; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
123 | hdtr.trailers = &trvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
124 | hdtr.trl_cnt = 1; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
125 | |
70
4e6e812c1d97
fixed some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
126 | off_t len = sfd->len; |
71 | 127 | #ifdef OSX |
128 | ret = sendfile(sfd->fd->fd, st->fd, fileoffset, &len, &hdtr, 0); | |
129 | #else // BSD | |
130 | ret = sendfile( | |
131 | sfd->fd->fd, | |
132 | st->fd, | |
133 | fileoffset, | |
134 | sfd->len, | |
135 | &hdtr, | |
136 | NULL, | |
137 | 0); | |
138 | #endif | |
139 | #else // Solaris/Linux | |
104
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
140 | if(sfd->header) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
141 | ret += write(st->fd, sfd->header, sfd->hlen); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
142 | } |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
143 | ret += sendfile(st->fd, sfd->fd->fd, &fileoffset, sfd->len); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
144 | if(sfd->trailer) { |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
145 | ret += write(st->fd, sfd->trailer, sfd->tlen); |
a8acbb12f27c
implemented range requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
82
diff
changeset
|
146 | } |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
147 | #endif |
54 | 148 | } else { |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
149 | return net_fallback_sendfile((IOStream*)st, sfd); |
54 | 150 | } |
151 | ||
152 | return ret; | |
153 | } | |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
154 | #endif |
54 | 155 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
156 | void net_sys_close(SysStream *st) { |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
157 | close(st->fd); |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
158 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
159 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
160 | #elif defined(XP_WIN32) |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
161 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
162 | ssize_t net_sys_write(SysStream *st, void *buf, size_t nbytes) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
163 | int ret = send(st->socket, buf, nbytes, 0); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
164 | if(ret == SOCKET_ERROR) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
165 | return IO_ERROR; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
166 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
167 | return ret; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
168 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
169 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
170 | ssize_t net_sys_writev(SysStream *st, struct iovec *iovec, int iovcnt) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
171 | // TODO |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
172 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
173 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
174 | ssize_t net_sys_read(SysStream *st, void *buf, size_t nbytes) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
175 | int ret = recv(st->socket, buf, nbytes, 0); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
176 | if(ret == SOCKET_ERROR) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
177 | return IO_ERROR; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
178 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
179 | return ret; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
180 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
181 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
182 | ssize_t net_sys_sendfile(SysStream *st, sendfiledata *sfd) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
183 | // TODO |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
184 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
185 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
186 | void net_sys_close(SysStream *st) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
187 | closesocket(st->socket); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
188 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
189 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
190 | #endif |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
191 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
192 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
193 | /* |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
194 | * HttpStream implementation |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
195 | */ |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
196 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
197 | IOStream* httpstream_new(pool_handle_t *pool, IOStream *fd) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
198 | HttpStream *st = pool_malloc(pool, sizeof(HttpStream)); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
199 | st->st = http_io_funcs; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
200 | st->fd = fd; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
201 | st->max_read = 0; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
202 | st->read = 0; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
203 | st->chunked_enc = WS_FALSE; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
204 | st->buffered = WS_FALSE; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
205 | return (IOStream*)st; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
206 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
207 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
208 | ssize_t net_http_write(HttpStream *st, void *buf, size_t nbytes) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
209 | IOStream *fd = st->fd; |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
210 | if(st->chunked_enc) { |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
211 | // TODO: on some plattforms iov_len is smaller than size_t |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
212 | struct iovec io[3]; |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
213 | char chunk_len[16]; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
214 | io[0].iov_base = chunk_len; |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
215 | io[0].iov_len = snprintf(chunk_len, 16, "%zx\r\n", nbytes); |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
216 | io[1].iov_base = buf; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
217 | io[1].iov_len = nbytes; |
126
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
218 | io[2].iov_base = "\r\n"; |
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
219 | io[2].iov_len = 2; |
631aaa01b2b5
fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
112
diff
changeset
|
220 | ssize_t r = fd->writev(fd, io, 3); |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
221 | return r - io[0].iov_len; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
222 | } else { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
223 | return fd->write(fd, buf, nbytes); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
224 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
225 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
226 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
227 | ssize_t net_http_writev(HttpStream *st, struct iovec *iovec, int iovcnt) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
228 | IOStream *fd = st->fd; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
229 | if(st->chunked_enc) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
230 | struct iovec *io = calloc(iovcnt + 1, sizeof(struct iovec)); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
231 | char chunk_len[16]; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
232 | io[0].iov_base = chunk_len; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
233 | size_t len = 0; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
234 | for(int i=0;i<iovcnt;i++) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
235 | len += iovec[i].iov_len; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
236 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
237 | 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
|
238 | memcpy(io + 1, iovec, iovcnt * sizeof(struct iovec)); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
239 | ssize_t r = fd->writev(fd, io, iovcnt + 1); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
240 | return r - io[0].iov_len; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
241 | } else { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
242 | return fd->writev(fd, iovec, iovcnt); |
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 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
245 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
246 | ssize_t net_http_read(HttpStream *st, void *buf, size_t nbytes) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
247 | if(st->max_read != 0 && st->read >= st->max_read) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
248 | return 0; |
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 | ssize_t r = st->fd->read(st->fd, buf, nbytes); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
251 | st->read += r; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
252 | return r; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
253 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
254 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
255 | ssize_t net_http_sendfile(HttpStream *st, sendfiledata *sfd) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
256 | ssize_t ret = 0; |
112 | 257 | // TODO: support chunked transfer encoding |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
258 | if(st->fd->sendfile) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
259 | ret = st->fd->sendfile(st->fd, sfd); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
260 | } else { |
112 | 261 | ret = net_fallback_sendfile((IOStream*)st, sfd); |
110
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 | return ret; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
265 | } |
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 | void net_http_close(HttpStream *st) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
268 | st->fd->close(st->fd); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
269 | } |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
270 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
271 | void net_http_finish(HttpStream *st) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
272 | if(st->chunked_enc) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
273 | 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
|
274 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
275 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
276 | |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
277 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
278 | /* |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
279 | * SSLStream implementation |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
280 | */ |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
281 | |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
282 | 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
|
283 | SSLStream *st = pool_malloc(pool, sizeof(SSLStream)); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
284 | st->st = ssl_io_funcs; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
285 | st->ssl = ssl; |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
286 | st->error = 0; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
287 | return (IOStream*)st; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
288 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
289 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
290 | ssize_t net_ssl_write(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
|
291 | 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
|
292 | if(ret <= 0) { |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
293 | 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
|
294 | } |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
295 | return ret; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
296 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
297 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
298 | 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
|
299 | ssize_t r = 0; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
300 | for(int i=0;i<iovcnt;i++) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
301 | 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
|
302 | if(ret <= 0) { |
133
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
303 | st->error = SSL_get_error(st->ssl, ret); |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
304 | return 0; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
305 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
306 | r += ret; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
307 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
308 | return r; |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
309 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
310 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
311 | 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
|
312 | 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
|
313 | if(ret <= 0) { |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
314 | 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
|
315 | } |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
316 | return ret; |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
317 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
318 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
319 | 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
|
320 | int ret = SSL_shutdown(st->ssl); |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
321 | if(ret != 1) { |
87b405d61f64
improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
126
diff
changeset
|
322 | 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
|
323 | } |
106
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
324 | close(SSL_get_fd(st->ssl)); |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
325 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
326 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
327 | void net_ssl_finish(SSLStream *st) { |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
328 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
329 | } |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
330 | |
b122f34ddc80
added minimal ssl support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
104
diff
changeset
|
331 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
332 | /* -------------------- public nsapi network functions -------------------- */ |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
333 | |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
334 | 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
|
335 | ssize_t r = ((IOStream*)fd)->read(fd, buf, nbytes); |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
336 | if(r == 0) { |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
337 | return IO_EOF; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
338 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
339 | return r; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
340 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
341 | |
9
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
342 | ssize_t net_write(SYS_NETFD fd, void *buf, size_t nbytes) { |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
343 | ssize_t r = ((IOStream*)fd)->write(fd, buf, nbytes); |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
344 | if(r < 0) { |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
345 | return IO_ERROR; |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
346 | } |
9
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
347 | return r; |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
348 | } |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
349 | |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
350 | 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
|
351 | ssize_t r = ((IOStream*)fd)->writev(fd, iovec, iovcnt); |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
352 | if(r < 0) { |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
353 | return IO_ERROR; |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
354 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
355 | return r; |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
356 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
357 | |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
358 | ssize_t net_printf(SYS_NETFD fd, char *format, ...) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
359 | va_list arg; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
360 | va_start(arg, format); |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
361 | sstr_t buf = ucx_vasprintf(ucx_default_allocator(), format, arg); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
362 | ssize_t r = net_write(fd, buf.ptr, buf.length); |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
363 | 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
|
364 | va_end(arg); |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
365 | return r; |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
366 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
367 | |
54 | 368 | ssize_t net_sendfile(SYS_NETFD fd, sendfiledata *sfd) { |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
369 | IOStream *out = fd; |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
370 | if(out->sendfile && sfd->fd && sfd->fd->fd != -1) { |
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
371 | ssize_t r = out->sendfile(fd, sfd); |
54 | 372 | if(r < 0) { |
373 | return IO_ERROR; | |
374 | } | |
375 | } else { | |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
376 | // stream/file does not support sendfile |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
377 | // do regular copy |
112 | 378 | return net_fallback_sendfile(out, sfd); |
54 | 379 | } |
380 | return IO_ERROR; | |
381 | } | |
382 | ||
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
383 | // private |
112 | 384 | 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
|
385 | char *buf = malloc(4096); |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
386 | if(!buf) { |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
387 | // TODO: out of memory error |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
388 | return IO_ERROR; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
389 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
390 | char *header = (char*)sfd->header; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
391 | int hlen = sfd->hlen; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
392 | char *trailer = (char*)sfd->trailer; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
393 | int tlen = sfd->tlen; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
394 | if(header == NULL) { |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
395 | hlen = 0; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
396 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
397 | if(trailer == NULL) { |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
398 | tlen = 0; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
399 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
400 | |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
401 | ssize_t r; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
402 | while(hlen > 0) { |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
403 | 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
|
404 | header += r; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
405 | hlen -= r; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
406 | if(r <= 0) { |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
407 | free(buf); |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
408 | return IO_ERROR; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
409 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
410 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
411 | |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
412 | 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
|
413 | free(buf); |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
414 | return IO_ERROR; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
415 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
416 | |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
417 | size_t length = sfd->len; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
418 | while(length > 0) { |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
419 | 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
|
420 | break; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
421 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
422 | char *write_buf = buf; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
423 | while(r > 0) { |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
424 | ssize_t w = fd->write(fd, write_buf, r); |
111
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
425 | r -= w; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
426 | length -= w; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
427 | write_buf += w; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
428 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
429 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
430 | free(buf); |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
431 | if(length > 0) { |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
432 | return IO_ERROR; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
433 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
434 | |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
435 | while(tlen > 0) { |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
436 | 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
|
437 | trailer += r; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
438 | tlen -= r; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
439 | if(r <= 0) { |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
440 | return IO_ERROR; |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
441 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
442 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
443 | |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
444 | 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
|
445 | } |
c93be34fde76
fixed NetBSD build and an uninitialized struct member
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
446 | |
82
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
447 | int net_flush(SYS_NETFD sd) { |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
448 | // TODO: implement |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
449 | return 0; |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
450 | } |
32
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
451 | |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
452 | void net_close(SYS_NETFD fd) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
453 | ((IOStream*)fd)->close(fd); |
32
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
454 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
455 | |
110
43a746e905f6
refactored IO system
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
106
diff
changeset
|
456 | // private |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
457 | void net_finish(SYS_NETFD fd) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
458 | ((IOStream*)fd)->finish(fd); |
32
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
459 | } |