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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ee1c2d9 was 408424e, checked in by Martin Sucha <sucha14@…>, 12 years ago

libhttp: reimplement receiving status

  • Property mode set to 100644
File size: 6.8 KB
RevLine 
[ff364f1]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>
[3ce68b7]41#include <adt/list.h>
[ff364f1]42
[b623b68]43#include <http/receive-buffer.h>
[ff364f1]44
45int 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;
[3ce68b7]54
55 list_initialize(&rb->marks);
56
[ff364f1]57 rb->buffer = malloc(buffer_size);
58 if (rb->buffer == NULL)
59 return ENOMEM;
60 return EOK;
61}
62
[3ce68b7]63static ssize_t dummy_receive(void *unused, void *buf, size_t buf_size)
64{
65 return 0;
66}
67
68int recv_buffer_init_const(receive_buffer_t *rb, void *buf, size_t size)
69{
70 int rc = recv_buffer_init(rb, size, dummy_receive, NULL);
71 if (rc != EOK)
72 return rc;
73
74 memcpy(rb->buffer, buf, size);
75 rb->in = size;
76 return EOK;
77}
78
[ff364f1]79void recv_buffer_fini(receive_buffer_t *rb)
80{
81 free(rb->buffer);
82}
83
84void recv_reset(receive_buffer_t *rb)
85{
86 rb->in = 0;
87 rb->out = 0;
88}
89
[3ce68b7]90void recv_mark(receive_buffer_t *rb, receive_buffer_mark_t *mark)
91{
92 link_initialize(&mark->link);
93 list_append(&mark->link, &rb->marks);
94 recv_mark_update(rb, mark);
95}
96
97void recv_unmark(receive_buffer_t *rb, receive_buffer_mark_t *mark)
98{
99 list_remove(&mark->link);
100}
101
102void recv_mark_update(receive_buffer_t *rb, receive_buffer_mark_t *mark)
103{
104 mark->offset = rb->out;
105}
106
107int recv_cut(receive_buffer_t *rb, receive_buffer_mark_t *a, receive_buffer_mark_t *b, void **out_buf, size_t *out_size)
108{
109 if (a->offset > b->offset)
110 return EINVAL;
111
112 size_t size = b->offset - a->offset;
113 void *buf = malloc(size);
114 if (buf == NULL)
115 return ENOMEM;
116
117 memcpy(buf, rb->buffer + a->offset, size);
118 *out_buf = buf;
119 *out_size = size;
120 return EOK;
121}
122
123int recv_cut_str(receive_buffer_t *rb, receive_buffer_mark_t *a, receive_buffer_mark_t *b, char **out_buf)
124{
125 if (a->offset > b->offset)
126 return EINVAL;
127
128 size_t size = b->offset - a->offset;
129 char *buf = malloc(size + 1);
130 if (buf == NULL)
131 return ENOMEM;
132
133 memcpy(buf, rb->buffer + a->offset, size);
134 buf[size] = 0;
135 for (size_t i = 0; i < size; i++) {
136 if (buf[i] == 0) {
137 free(buf);
138 return EIO;
139 }
140 }
141 *out_buf = buf;
142 return EOK;
143}
144
145
[ff364f1]146/** Receive one character (with buffering) */
147int recv_char(receive_buffer_t *rb, char *c, bool consume)
148{
149 if (rb->out == rb->in) {
[3ce68b7]150 size_t free = rb->size - rb->in;
151 if (free == 0) {
152 size_t min_mark = rb->size;
153 list_foreach(rb->marks, link, receive_buffer_mark_t, mark) {
154 min_mark = min(min_mark, mark->offset);
155 }
156
157 if (min_mark == 0)
158 return ELIMIT;
159
160 size_t new_in = rb->in - min_mark;
161 memmove(rb->buffer, rb->buffer + min_mark, new_in);
162 rb->out = rb->in = new_in;
163 free = rb->size - rb->in;
164 list_foreach(rb->marks, link, receive_buffer_mark_t, mark) {
165 mark->offset -= min_mark;
166 }
167 }
[ff364f1]168
[3ce68b7]169 ssize_t rc = rb->receive(rb->client_data, rb->buffer + rb->in, free);
[ff364f1]170 if (rc <= 0)
171 return rc;
172
173 rb->in = rc;
174 }
175
176 *c = rb->buffer[rb->out];
177 if (consume)
178 rb->out++;
179 return EOK;
180}
181
182ssize_t recv_buffer(receive_buffer_t *rb, char *buf, size_t buf_size)
183{
184 /* Flush any buffered data*/
185 if (rb->out != rb->in) {
186 size_t size = min(rb->in - rb->out, buf_size);
187 memcpy(buf, rb->buffer + rb->out, size);
188 rb->out += size;
189 return size;
190 }
191
192 return rb->receive(rb->client_data, buf, buf_size);
193}
194
[3ce68b7]195/** Receive a character and if it is c, discard it from input buffer
196 * @return number of characters discarded (0 or 1) or negative error code
197 */
198ssize_t recv_discard(receive_buffer_t *rb, char discard)
[ff364f1]199{
200 char c = 0;
201 int rc = recv_char(rb, &c, false);
202 if (rc != EOK)
203 return rc;
204 if (c != discard)
[3ce68b7]205 return 0;
206 rc = recv_char(rb, &c, true);
207 if (rc != EOK)
208 return rc;
209 return 1;
210}
211
[408424e]212/** Receive a prefix of constant string discard and return number of bytes read
213 * @return number of characters discarded or negative error code
214 */
215ssize_t recv_discard_str(receive_buffer_t *rb, const char *discard)
216{
217 size_t discarded = 0;
218 while (*discard) {
219 ssize_t rc = recv_discard(rb, *discard);
220 if (rc < 0)
221 return rc;
222 if (rc == 0)
223 break;
224 discarded++;
225 discard++;
226 }
227 return discarded;
228}
229
230ssize_t recv_while(receive_buffer_t *rb, char_class_func_t class)
231{
232 size_t received = 0;
233
234 while (true) {
235 char c = 0;
236 int rc = recv_char(rb, &c, false);
237 if (rc != EOK)
238 return rc;
239
240 if (!class(c))
241 break;
242
243 rc = recv_char(rb, &c, true);
244 if (rc != EOK)
245 return rc;
246
247 received++;
248 }
249
250 return received;
251}
252
[3ce68b7]253/** Receive an end of line, either CR, LF, CRLF or LFCR
254 *
255 * @return number of bytes read (0 if no newline is present in the stream)
256 * or negative error code
257 */
258ssize_t recv_eol(receive_buffer_t *rb)
259{
260 char c = 0;
261 int rc = recv_char(rb, &c, false);
262 if (rc != EOK)
263 return rc;
264
265 if (c != '\r' && c != '\n')
266 return 0;
267
268 rc = recv_char(rb, &c, true);
269 if (rc != EOK)
270 return rc;
271
272 ssize_t rc2 = recv_discard(rb, (c == '\r' ? '\n' : '\r'));
273 if (rc2 < 0)
274 return rc2;
275
276 return 1 + rc2;
[ff364f1]277}
278
279/* Receive a single line */
280ssize_t recv_line(receive_buffer_t *rb, char *line, size_t size)
281{
282 size_t written = 0;
283
284 while (written < size) {
285 char c = 0;
286 int rc = recv_char(rb, &c, true);
287 if (rc != EOK)
288 return rc;
289 if (c == '\n') {
290 recv_discard(rb, '\r');
291 line[written++] = 0;
292 return written;
293 }
294 else if (c == '\r') {
295 recv_discard(rb, '\n');
296 line[written++] = 0;
297 return written;
298 }
299 line[written++] = c;
300 }
301
302 return ELIMIT;
303}
304
305/** @}
306 */
Note: See TracBrowser for help on using the repository browser.