Sat, 17 Oct 2015 23:05:23 +0200
added date header to response
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> |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
37 | #ifndef BSD |
54 | 38 | #include <sys/sendfile.h> |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
39 | #endif |
32
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
40 | #include <limits.h> /* asprintf */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
41 | |
54 | 42 | #include "../daemon/vfs.h" |
1 | 43 | #include "io.h" |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
44 | #include "pool.h" |
1 | 45 | |
46 | IOStream native_io_funcs = { | |
47 | system_write, | |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
48 | system_writev, |
54 | 49 | system_read, |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
50 | NULL, |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
51 | NULL, |
54 | 52 | NULL |
1 | 53 | }; |
54 | ||
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
55 | IOStream net_io_funcs = { |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
56 | (io_write_f)net_stream_write, |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
57 | (io_writev_f)net_stream_writev, |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
58 | (io_read_f)net_stream_read, |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
59 | (io_sendfile_f)net_stream_sendfile, |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
60 | (io_close_f)net_stream_close, |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
61 | (io_finish_f)net_stream_finish |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
62 | }; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
63 | |
1 | 64 | |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
65 | IOStream* stream_new_from_fd(pool_handle_t *pool, int fd) { |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
66 | SystemIOStream *st = pool_malloc(pool, sizeof(SystemIOStream)); |
1 | 67 | st->st = native_io_funcs; |
68 | st->fd = fd; | |
69 | return (IOStream*)st; | |
70 | } | |
71 | ||
72 | ssize_t system_write(IOStream *st, void *buf, size_t nbytes) { | |
73 | return write(((SystemIOStream*)st)->fd, buf, nbytes); | |
74 | } | |
75 | ||
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
76 | ssize_t system_writev(IOStream *st, struct iovec *iovec, int iovcnt) { |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
77 | return writev(((SystemIOStream*)st)->fd, iovec, iovcnt); |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
78 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
79 | |
1 | 80 | ssize_t system_read(IOStream *st, void *buf, size_t nbytes) { |
81 | return read(((SystemIOStream*)st)->fd, buf, nbytes); | |
82 | } | |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
83 | |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
84 | |
46
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
85 | IOStream* net_stream_from_fd(pool_handle_t *pool, int fd) { |
636e05eb48f6
cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
44
diff
changeset
|
86 | NetIOStream *st = pool_malloc(pool, sizeof(NetIOStream)); |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
87 | st->st = net_io_funcs; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
88 | st->fd = fd; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
89 | st->max_read = 0; |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
90 | st->read = 0; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
91 | st->chunked_enc = 0; |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
92 | st->buffered = 0; |
25
5dee29c7c530
Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
23
diff
changeset
|
93 | return (IOStream*)st; |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
94 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
95 | |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
96 | ssize_t net_stream_write(NetIOStream *st, void *buf, size_t nbytes) { |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
97 | if(st->chunked_enc) { |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
98 | // TODO: on some plattforms iov_len is smaller than size_t |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
99 | struct iovec io[2]; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
100 | char chunk_len[16]; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
101 | io[0].iov_base = chunk_len; |
71 | 102 | io[0].iov_len = snprintf(chunk_len, 16, "\r\n%zx\r\n", nbytes); |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
103 | io[1].iov_base = buf; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
104 | io[1].iov_len = nbytes; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
105 | ssize_t r = writev(st->fd, io, 2); |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
106 | return r - io[0].iov_len; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
107 | } else { |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
108 | return write(st->fd, buf, nbytes); |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
109 | } |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
110 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
111 | |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
112 | ssize_t net_stream_writev(NetIOStream *st, struct iovec *iovec, int iovcnt) { |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
113 | if(st->chunked_enc) { |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
114 | struct iovec *io = calloc(iovcnt + 1, sizeof(struct iovec)); |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
115 | char chunk_len[16]; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
116 | io[0].iov_base = chunk_len; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
117 | size_t len = 0; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
118 | for(int i=0;i<iovcnt;i++) { |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
119 | len += iovec[i].iov_len; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
120 | } |
71 | 121 | io[0].iov_len = snprintf(chunk_len, 16, "\r\n%zx\r\n", len); |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
122 | memcpy(io + 1, iovec, iovcnt * sizeof(struct iovec)); |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
123 | ssize_t r = writev(st->fd, io, iovcnt + 1); |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
124 | return r - io[0].iov_len; |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
125 | } else { |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
126 | return writev(st->fd, iovec, iovcnt); |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
127 | } |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
128 | } |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
129 | |
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
130 | ssize_t net_stream_read(NetIOStream *st, void *buf, size_t nbytes) { |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
131 | if(st->max_read != 0 && st->read >= st->max_read) { |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
132 | return 0; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
133 | } |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
134 | ssize_t r = read(st->fd, buf, nbytes); |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
135 | st->read += r; |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
136 | return r; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
137 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
138 | |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
139 | ssize_t net_stream_sendfile(NetIOStream *st, sendfiledata *sfd) { |
54 | 140 | // TODO: header and trailer |
141 | ssize_t ret = 0; | |
142 | off_t fileoffset = sfd->offset; | |
143 | if(sfd->fd->fd != -1) { | |
71 | 144 | #ifdef BSD |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
145 | struct iovec hdvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
146 | hdvec.iov_base = (void*)sfd->header; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
147 | hdvec.iov_len = sfd->hlen; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
148 | struct iovec trvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
149 | trvec.iov_base = (void*)sfd->trailer; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
150 | trvec.iov_len = sfd->tlen; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
151 | struct sf_hdtr hdtr; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
152 | hdtr.headers = &hdvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
153 | hdtr.hdr_cnt = 1; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
154 | hdtr.trailers = &trvec; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
155 | hdtr.trl_cnt = 1; |
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
156 | |
70
4e6e812c1d97
fixed some warnings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
69
diff
changeset
|
157 | off_t len = sfd->len; |
71 | 158 | #ifdef OSX |
159 | ret = sendfile(sfd->fd->fd, st->fd, fileoffset, &len, &hdtr, 0); | |
160 | #else // BSD | |
161 | ret = sendfile( | |
162 | sfd->fd->fd, | |
163 | st->fd, | |
164 | fileoffset, | |
165 | sfd->len, | |
166 | &hdtr, | |
167 | NULL, | |
168 | 0); | |
169 | #endif | |
170 | #else // Solaris/Linux | |
65
14722c5f8856
added chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
64
diff
changeset
|
171 | ret = sendfile(st->fd, sfd->fd->fd, &fileoffset, sfd->len); |
69
4a10bc0ee80d
compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
66
diff
changeset
|
172 | #endif |
54 | 173 | } else { |
174 | // TODO: regular copy | |
175 | fprintf(stderr, "sendfile not implemented for SYS_FILE\n"); | |
176 | } | |
177 | ||
178 | return ret; | |
179 | } | |
180 | ||
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
181 | void net_stream_close(NetIOStream *st) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
182 | close(st->fd); |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
183 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
184 | |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
185 | void net_stream_finish(NetIOStream *st) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
186 | if(st->chunked_enc) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
187 | write(st->fd, "0\r\n\r\n", 5); |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
188 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
189 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
190 | |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
191 | |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
192 | 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
|
193 | ssize_t r = ((IOStream*)fd)->read(fd, buf, nbytes); |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
194 | if(r == 0) { |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
195 | return IO_EOF; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
196 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
197 | return r; |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
198 | } |
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
199 | |
9
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
200 | 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
|
201 | ssize_t r = ((IOStream*)fd)->write(fd, buf, nbytes); |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
202 | if(r < 0) { |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
203 | return IO_ERROR; |
20
7b235fa88008
Some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
14
diff
changeset
|
204 | } |
9
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
205 | return r; |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
206 | } |
30e51941a673
Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
8
diff
changeset
|
207 | |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
208 | 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
|
209 | ssize_t r = ((IOStream*)fd)->writev(fd, iovec, iovcnt); |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
210 | if(r < 0) { |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
211 | return IO_ERROR; |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
212 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
213 | return r; |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
214 | } |
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
215 | |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
216 | ssize_t net_printf(SYS_NETFD fd, char *format, ...) { |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
217 | va_list arg; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
218 | va_start(arg, format); |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
219 | char *buf; |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
220 | ssize_t len = vasprintf(&buf, format, arg); |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
221 | ssize_t r = net_write(fd, buf, len); |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
222 | free(buf); |
64
c7f5b062e622
added net_writev
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
54
diff
changeset
|
223 | return r; |
23
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
224 | } |
a2c8fc23c90e
Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
20
diff
changeset
|
225 | |
54 | 226 | ssize_t net_sendfile(SYS_NETFD fd, sendfiledata *sfd) { |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
227 | NetIOStream *out = fd; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
228 | if(out->st.sendfile && sfd->fd && sfd->fd->fd != -1) { |
54 | 229 | ssize_t r = ((IOStream*)fd)->sendfile(fd, sfd); |
230 | if(r < 0) { | |
231 | return IO_ERROR; | |
232 | } | |
233 | } else { | |
66
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
234 | // stream/file does not support sendfile |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
235 | // do regular copy |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
236 | char *buf = malloc(4096); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
237 | if(!buf) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
238 | // TODO: out of memory error |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
239 | return IO_ERROR; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
240 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
241 | char *header = (char*)sfd->header; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
242 | int hlen = sfd->hlen; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
243 | char *trailer = (char*)sfd->trailer; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
244 | int tlen = sfd->tlen; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
245 | if(header == NULL) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
246 | hlen = 0; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
247 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
248 | if(trailer == NULL) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
249 | tlen = 0; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
250 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
251 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
252 | ssize_t r; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
253 | while(hlen > 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
254 | r = out->st.write(fd, header, hlen); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
255 | header += r; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
256 | hlen -= r; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
257 | if(r <= 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
258 | free(buf); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
259 | return IO_ERROR; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
260 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
261 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
262 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
263 | if(system_lseek(sfd->fd, sfd->offset, SEEK_SET) == -1) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
264 | free(buf); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
265 | return IO_ERROR; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
266 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
267 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
268 | size_t length = sfd->len; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
269 | while(length > 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
270 | if((r = system_fread(sfd->fd, buf, 4096)) <= 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
271 | break; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
272 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
273 | char *write_buf = buf; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
274 | while(r > 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
275 | ssize_t w = out->st.write(fd, write_buf, r); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
276 | r -= w; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
277 | length -= w; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
278 | write_buf += w; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
279 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
280 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
281 | free(buf); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
282 | if(length > 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
283 | return IO_ERROR; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
284 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
285 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
286 | while(tlen > 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
287 | r = out->st.write(fd, trailer, tlen); |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
288 | trailer += r; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
289 | tlen -= r; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
290 | if(r <= 0) { |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
291 | return IO_ERROR; |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
292 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
293 | } |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
294 | |
74babc0082b7
added authentication cache
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
295 | return sfd->hlen + sfd->len + sfd->tlen; |
54 | 296 | } |
297 | return IO_ERROR; | |
298 | } | |
299 | ||
82
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
300 | int net_flush(SYS_NETFD sd) { |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
301 | // TODO: implement |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
302 | return 0; |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
303 | } |
740cfd9dd443
added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
80
diff
changeset
|
304 | |
80
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
305 | void net_close(SYS_NETFD fd) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
306 | ((IOStream*)fd)->close(fd); |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
307 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
308 | |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
309 | void net_finish(SYS_NETFD fd) { |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
310 | ((IOStream*)fd)->finish(fd); |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
311 | } |
0de4a90979e1
fixed chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
71
diff
changeset
|
312 | |
8
f4d56bf9de40
Added request body reader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
4
diff
changeset
|
313 | |
4
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
314 | /* iovec buffer */ |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
315 | iovec_buf_t *iovec_buf_create(pool_handle_t *pool) { |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
316 | iovec_buf_t *buf = pool_malloc(pool, sizeof(iovec_buf_t)); |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
317 | |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
318 | buf->pool = pool; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
319 | buf->iov = pool_calloc(pool, 32, sizeof(struct iovec)); |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
320 | buf->maxiovec = 32; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
321 | buf->iovctn = 0; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
322 | |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
323 | return buf; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
324 | } |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
325 | |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
326 | void iovec_buf_write(iovec_buf_t *io, void *buf, size_t nbyte) { |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
327 | if(io->iovctn >= io->maxiovec) { |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
328 | io->iov = pool_realloc( |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
329 | io->pool, |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
330 | io->iov, |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
331 | (io->maxiovec + 16) * sizeof(struct iovec)); |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
332 | } |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
333 | |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
334 | io->iov[io->iovctn].iov_base = buf; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
335 | io->iov[io->iovctn].iov_len = nbyte; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
336 | io->iovctn++; |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
337 | } |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
338 | |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
339 | ssize_t iovec_buf_flush(iovec_buf_t *io, int fd) { |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
340 | return writev(fd, io->iov, io->iovctn); |
998844b5ed25
Added some protocol functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
341 | } |
32
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
342 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
343 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
344 | /* TODO: add asprintf to new file */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
345 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
346 | /* |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
347 | * asprintf implementation for Solaris 10 |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
348 | * source from OpenSolaris |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
349 | * file: /onnv/onnv-gate/usr/src/lib/libc/port/print/asprintf.c |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
350 | */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
351 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
352 | /* |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
353 | * Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
354 | * Use is subject to license terms. |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
355 | */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
356 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
357 | /* |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
358 | * Copyright (c) 2004 Darren Tucker. |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
359 | * |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
360 | * Based originally on asprintf.c from OpenBSD: |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
361 | * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
362 | * |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
363 | * Permission to use, copy, modify, and distribute this software for any |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
364 | * purpose with or without fee is hereby granted, provided that the above |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
365 | * copyright notice and this permission notice appear in all copies. |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
366 | * |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
367 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
368 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
369 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
370 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
371 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
372 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
373 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
374 | */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
375 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
376 | #ifdef __SunOS_5_10 |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
377 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
378 | #define INIT_SZ 128 |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
379 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
380 | /* VARARGS2 */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
381 | int |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
382 | vasprintf(char **str, const char *format, va_list ap) |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
383 | { |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
384 | char string[INIT_SZ]; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
385 | char *newstr; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
386 | int ret; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
387 | size_t len; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
388 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
389 | *str = NULL; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
390 | ret = vsnprintf(string, INIT_SZ, format, ap); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
391 | if (ret < 0) /* retain the value of errno from vsnprintf() */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
392 | return (-1); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
393 | if (ret < INIT_SZ) { |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
394 | len = ret + 1; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
395 | if ((newstr = malloc(len)) == NULL) |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
396 | return (-1); /* retain errno from malloc() */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
397 | (void) strlcpy(newstr, string, len); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
398 | *str = newstr; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
399 | return (ret); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
400 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
401 | /* |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
402 | * We will perform this loop more than once only if some other |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
403 | * thread modifies one of the vasprintf() arguments after our |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
404 | * previous call to vsnprintf(). |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
405 | */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
406 | for (;;) { |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
407 | if (ret == INT_MAX) { /* Bad length */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
408 | errno = ENOMEM; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
409 | return (-1); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
410 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
411 | len = ret + 1; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
412 | if ((newstr = malloc(len)) == NULL) |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
413 | return (-1); /* retain errno from malloc() */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
414 | ret = vsnprintf(newstr, len, format, ap); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
415 | if (ret < 0) { /* retain errno from vsnprintf() */ |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
416 | free(newstr); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
417 | return (-1); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
418 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
419 | if (ret < len) { |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
420 | *str = newstr; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
421 | return (ret); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
422 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
423 | free(newstr); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
424 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
425 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
426 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
427 | int |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
428 | asprintf(char **str, const char *format, ...) |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
429 | { |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
430 | va_list ap; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
431 | int ret; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
432 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
433 | *str = NULL; |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
434 | va_start(ap, format); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
435 | ret = vasprintf(str, format, ap); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
436 | va_end(ap); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
437 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
438 | return (ret); |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
439 | } |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
440 | |
ebba53de8b18
added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
25
diff
changeset
|
441 | #endif |