source: mainline/uspace/lib/http/src/receive-buffer.c@ 8bf9058

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8bf9058 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 7.2 KB
Line 
1/*
2 * Copyright (c) 2012 Jiri Svoboda
3 * Copyright (c) 2013 Martin Sucha
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - 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 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup http
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <stdlib.h>
38#include <str.h>
39#include <errno.h>
40#include <macros.h>
41#include <adt/list.h>
42
43#include <http/receive-buffer.h>
44
45errno_t recv_buffer_init(receive_buffer_t *rb, size_t buffer_size,
46 receive_func_t receive, void *client_data)
47{
48 rb->receive = receive;
49 rb->client_data = client_data;
50
51 rb->in = 0;
52 rb->out = 0;
53 rb->size = buffer_size;
54
55 list_initialize(&rb->marks);
56
57 rb->buffer = malloc(buffer_size);
58 if (rb->buffer == NULL)
59 return ENOMEM;
60 return EOK;
61}
62
63static errno_t dummy_receive(void *unused, void *buf, size_t buf_size,
64 size_t *nrecv)
65{
66 *nrecv = 0;
67 return EOK;
68}
69
70errno_t recv_buffer_init_const(receive_buffer_t *rb, void *buf, size_t size)
71{
72 errno_t rc = recv_buffer_init(rb, size, dummy_receive, NULL);
73 if (rc != EOK)
74 return rc;
75
76 memcpy(rb->buffer, buf, size);
77 rb->in = size;
78 return EOK;
79}
80
81void recv_buffer_fini(receive_buffer_t *rb)
82{
83 free(rb->buffer);
84}
85
86void recv_reset(receive_buffer_t *rb)
87{
88 rb->in = 0;
89 rb->out = 0;
90}
91
92void recv_mark(receive_buffer_t *rb, receive_buffer_mark_t *mark)
93{
94 link_initialize(&mark->link);
95 list_append(&mark->link, &rb->marks);
96 recv_mark_update(rb, mark);
97}
98
99void recv_unmark(receive_buffer_t *rb, receive_buffer_mark_t *mark)
100{
101 list_remove(&mark->link);
102}
103
104void recv_mark_update(receive_buffer_t *rb, receive_buffer_mark_t *mark)
105{
106 mark->offset = rb->out;
107}
108
109errno_t recv_cut(receive_buffer_t *rb, receive_buffer_mark_t *a, receive_buffer_mark_t *b, void **out_buf, size_t *out_size)
110{
111 if (a->offset > b->offset)
112 return EINVAL;
113
114 size_t size = b->offset - a->offset;
115 void *buf = malloc(size);
116 if (buf == NULL)
117 return ENOMEM;
118
119 memcpy(buf, rb->buffer + a->offset, size);
120 *out_buf = buf;
121 *out_size = size;
122 return EOK;
123}
124
125errno_t recv_cut_str(receive_buffer_t *rb, receive_buffer_mark_t *a, receive_buffer_mark_t *b, char **out_buf)
126{
127 if (a->offset > b->offset)
128 return EINVAL;
129
130 size_t size = b->offset - a->offset;
131 char *buf = malloc(size + 1);
132 if (buf == NULL)
133 return ENOMEM;
134
135 memcpy(buf, rb->buffer + a->offset, size);
136 buf[size] = 0;
137 for (size_t i = 0; i < size; i++) {
138 if (buf[i] == 0) {
139 free(buf);
140 return EIO;
141 }
142 }
143 *out_buf = buf;
144 return EOK;
145}
146
147/** Receive one character (with buffering) */
148errno_t recv_char(receive_buffer_t *rb, char *c, bool consume)
149{
150 if (rb->out == rb->in) {
151 size_t free = rb->size - rb->in;
152 if (free == 0) {
153 size_t min_mark = rb->size;
154 list_foreach(rb->marks, link, receive_buffer_mark_t, mark) {
155 min_mark = min(min_mark, mark->offset);
156 }
157
158 if (min_mark == 0)
159 return ELIMIT;
160
161 size_t new_in = rb->in - min_mark;
162 memmove(rb->buffer, rb->buffer + min_mark, new_in);
163 rb->out = rb->in = new_in;
164 free = rb->size - rb->in;
165 list_foreach(rb->marks, link, receive_buffer_mark_t, mark) {
166 mark->offset -= min_mark;
167 }
168 }
169
170 size_t nrecv;
171 errno_t rc = rb->receive(rb->client_data, rb->buffer + rb->in, free, &nrecv);
172 if (rc != EOK)
173 return rc;
174
175 rb->in = nrecv;
176 }
177
178 *c = rb->buffer[rb->out];
179 if (consume)
180 rb->out++;
181 return EOK;
182}
183
184errno_t recv_buffer(receive_buffer_t *rb, char *buf, size_t buf_size,
185 size_t *nrecv)
186{
187 /* Flush any buffered data */
188 if (rb->out != rb->in) {
189 size_t size = min(rb->in - rb->out, buf_size);
190 memcpy(buf, rb->buffer + rb->out, size);
191 rb->out += size;
192 *nrecv = size;
193 return EOK;
194 }
195
196 return rb->receive(rb->client_data, buf, buf_size, nrecv);
197}
198
199/** Receive a character and if it is c, discard it from input buffer
200 * @param ndisc Place to store number of characters discarded
201 * @return EOK or an error code
202 */
203errno_t recv_discard(receive_buffer_t *rb, char discard, size_t *ndisc)
204{
205 char c = 0;
206 errno_t rc = recv_char(rb, &c, false);
207 if (rc != EOK)
208 return rc;
209 if (c != discard) {
210 *ndisc = 0;
211 return EOK;
212 }
213 rc = recv_char(rb, &c, true);
214 if (rc != EOK)
215 return rc;
216 *ndisc = 1;
217 return EOK;
218}
219
220/** Receive a prefix of constant string discard and return number of bytes read
221 * @param ndisc Place to store number of characters discarded
222 * @return EOK or an error code
223 */
224errno_t recv_discard_str(receive_buffer_t *rb, const char *discard, size_t *ndisc)
225{
226 size_t discarded = 0;
227 while (*discard) {
228 size_t nd;
229 errno_t rc = recv_discard(rb, *discard, &nd);
230 if (rc != EOK)
231 return rc;
232 if (nd == 0)
233 break;
234 discarded += nd;
235 discard++;
236 }
237
238 *ndisc = discarded;
239 return EOK;
240}
241
242errno_t recv_while(receive_buffer_t *rb, char_class_func_t class)
243{
244 while (true) {
245 char c = 0;
246 errno_t rc = recv_char(rb, &c, false);
247 if (rc != EOK)
248 return rc;
249
250 if (!class(c))
251 break;
252
253 rc = recv_char(rb, &c, true);
254 if (rc != EOK)
255 return rc;
256 }
257
258 return EOK;
259}
260
261/** Receive an end of line, either CR, LF, CRLF or LFCR
262 *
263 * @param nrecv Place to store number of bytes received (zero if
264 * no newline is present in the stream)
265 * @return EOK on success or an error code
266 */
267errno_t recv_eol(receive_buffer_t *rb, size_t *nrecv)
268{
269 char c = 0;
270 errno_t rc = recv_char(rb, &c, false);
271 if (rc != EOK)
272 return rc;
273
274 if (c != '\r' && c != '\n') {
275 *nrecv = 0;
276 return EOK;
277 }
278
279 rc = recv_char(rb, &c, true);
280 if (rc != EOK)
281 return rc;
282
283 size_t nr;
284 rc = recv_discard(rb, (c == '\r' ? '\n' : '\r'), &nr);
285 if (rc != EOK)
286 return rc;
287
288 *nrecv = 1 + nr;
289 return EOK;
290}
291
292/* Receive a single line */
293errno_t recv_line(receive_buffer_t *rb, char *line, size_t size, size_t *nrecv)
294{
295 size_t written = 0;
296 size_t nr;
297
298 while (written < size) {
299 char c = 0;
300 errno_t rc = recv_char(rb, &c, true);
301 if (rc != EOK)
302 return rc;
303 if (c == '\n') {
304 rc = recv_discard(rb, '\r', &nr);
305 if (rc != EOK)
306 return rc;
307
308 line[written++] = 0;
309 *nrecv = written;
310 return EOK;
311 } else if (c == '\r') {
312 rc = recv_discard(rb, '\n', &nr);
313 if (rc != EOK)
314 return rc;
315 line[written++] = 0;
316 *nrecv = written;
317 return EOK;
318 }
319 line[written++] = c;
320 }
321
322 return ELIMIT;
323}
324
325/** @}
326 */
Note: See TracBrowser for help on using the repository browser.