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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 959b7ec 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
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
[b7fd2a0]45errno_t recv_buffer_init(receive_buffer_t *rb, size_t buffer_size,
[ff364f1]46 receive_func_t receive, void *client_data)
47{
48 rb->receive = receive;
49 rb->client_data = client_data;
[a35b458]50
[ff364f1]51 rb->in = 0;
52 rb->out = 0;
53 rb->size = buffer_size;
[a35b458]54
[3ce68b7]55 list_initialize(&rb->marks);
[a35b458]56
[ff364f1]57 rb->buffer = malloc(buffer_size);
58 if (rb->buffer == NULL)
59 return ENOMEM;
60 return EOK;
61}
62
[b7fd2a0]63static errno_t dummy_receive(void *unused, void *buf, size_t buf_size,
[9a09212]64 size_t *nrecv)
[3ce68b7]65{
[9a09212]66 *nrecv = 0;
67 return EOK;
[3ce68b7]68}
69
[b7fd2a0]70errno_t recv_buffer_init_const(receive_buffer_t *rb, void *buf, size_t size)
[3ce68b7]71{
[b7fd2a0]72 errno_t rc = recv_buffer_init(rb, size, dummy_receive, NULL);
[3ce68b7]73 if (rc != EOK)
74 return rc;
[a35b458]75
[3ce68b7]76 memcpy(rb->buffer, buf, size);
77 rb->in = size;
78 return EOK;
79}
80
[ff364f1]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
[3ce68b7]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
[b7fd2a0]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)
[3ce68b7]110{
111 if (a->offset > b->offset)
112 return EINVAL;
[a35b458]113
[3ce68b7]114 size_t size = b->offset - a->offset;
115 void *buf = malloc(size);
116 if (buf == NULL)
117 return ENOMEM;
[a35b458]118
[3ce68b7]119 memcpy(buf, rb->buffer + a->offset, size);
120 *out_buf = buf;
121 *out_size = size;
122 return EOK;
123}
124
[b7fd2a0]125errno_t recv_cut_str(receive_buffer_t *rb, receive_buffer_mark_t *a, receive_buffer_mark_t *b, char **out_buf)
[3ce68b7]126{
127 if (a->offset > b->offset)
128 return EINVAL;
[a35b458]129
[3ce68b7]130 size_t size = b->offset - a->offset;
131 char *buf = malloc(size + 1);
132 if (buf == NULL)
133 return ENOMEM;
[a35b458]134
[3ce68b7]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
[ff364f1]147/** Receive one character (with buffering) */
[b7fd2a0]148errno_t recv_char(receive_buffer_t *rb, char *c, bool consume)
[ff364f1]149{
150 if (rb->out == rb->in) {
[3ce68b7]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 }
[a35b458]157
[3ce68b7]158 if (min_mark == 0)
159 return ELIMIT;
[a35b458]160
[3ce68b7]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 }
[a35b458]169
[9a09212]170 size_t nrecv;
[b7fd2a0]171 errno_t rc = rb->receive(rb->client_data, rb->buffer + rb->in, free, &nrecv);
[9a09212]172 if (rc != EOK)
[ff364f1]173 return rc;
[a35b458]174
[9a09212]175 rb->in = nrecv;
[ff364f1]176 }
[a35b458]177
[ff364f1]178 *c = rb->buffer[rb->out];
179 if (consume)
180 rb->out++;
181 return EOK;
182}
183
[b7fd2a0]184errno_t recv_buffer(receive_buffer_t *rb, char *buf, size_t buf_size,
[9a09212]185 size_t *nrecv)
[ff364f1]186{
[9a09212]187 /* Flush any buffered data */
[ff364f1]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;
[9a09212]192 *nrecv = size;
193 return EOK;
[ff364f1]194 }
[a35b458]195
[9a09212]196 return rb->receive(rb->client_data, buf, buf_size, nrecv);
[ff364f1]197}
198
[3ce68b7]199/** Receive a character and if it is c, discard it from input buffer
[9a09212]200 * @param ndisc Place to store number of characters discarded
[cde999a]201 * @return EOK or an error code
[3ce68b7]202 */
[b7fd2a0]203errno_t recv_discard(receive_buffer_t *rb, char discard, size_t *ndisc)
[ff364f1]204{
205 char c = 0;
[b7fd2a0]206 errno_t rc = recv_char(rb, &c, false);
[ff364f1]207 if (rc != EOK)
208 return rc;
[9a09212]209 if (c != discard) {
210 *ndisc = 0;
211 return EOK;
212 }
[3ce68b7]213 rc = recv_char(rb, &c, true);
214 if (rc != EOK)
215 return rc;
[9a09212]216 *ndisc = 1;
217 return EOK;
[3ce68b7]218}
219
[408424e]220/** Receive a prefix of constant string discard and return number of bytes read
[9a09212]221 * @param ndisc Place to store number of characters discarded
[cde999a]222 * @return EOK or an error code
[408424e]223 */
[b7fd2a0]224errno_t recv_discard_str(receive_buffer_t *rb, const char *discard, size_t *ndisc)
[408424e]225{
226 size_t discarded = 0;
227 while (*discard) {
[9a09212]228 size_t nd;
[b7fd2a0]229 errno_t rc = recv_discard(rb, *discard, &nd);
[9a09212]230 if (rc != EOK)
[408424e]231 return rc;
[9a09212]232 if (nd == 0)
[408424e]233 break;
[9a09212]234 discarded += nd;
[408424e]235 discard++;
236 }
[9a09212]237
238 *ndisc = discarded;
239 return EOK;
[408424e]240}
241
[b7fd2a0]242errno_t recv_while(receive_buffer_t *rb, char_class_func_t class)
[408424e]243{
244 while (true) {
245 char c = 0;
[b7fd2a0]246 errno_t rc = recv_char(rb, &c, false);
[408424e]247 if (rc != EOK)
248 return rc;
[a35b458]249
[408424e]250 if (!class(c))
251 break;
[a35b458]252
[408424e]253 rc = recv_char(rb, &c, true);
254 if (rc != EOK)
255 return rc;
256 }
[a35b458]257
[9a09212]258 return EOK;
[408424e]259}
260
[3ce68b7]261/** Receive an end of line, either CR, LF, CRLF or LFCR
262 *
[9a09212]263 * @param nrecv Place to store number of bytes received (zero if
264 * no newline is present in the stream)
[cde999a]265 * @return EOK on success or an error code
[3ce68b7]266 */
[b7fd2a0]267errno_t recv_eol(receive_buffer_t *rb, size_t *nrecv)
[3ce68b7]268{
269 char c = 0;
[b7fd2a0]270 errno_t rc = recv_char(rb, &c, false);
[3ce68b7]271 if (rc != EOK)
272 return rc;
[a35b458]273
[9a09212]274 if (c != '\r' && c != '\n') {
275 *nrecv = 0;
276 return EOK;
277 }
[a35b458]278
[3ce68b7]279 rc = recv_char(rb, &c, true);
280 if (rc != EOK)
281 return rc;
[a35b458]282
[9a09212]283 size_t nr;
284 rc = recv_discard(rb, (c == '\r' ? '\n' : '\r'), &nr);
285 if (rc != EOK)
286 return rc;
[a35b458]287
[9a09212]288 *nrecv = 1 + nr;
289 return EOK;
[ff364f1]290}
291
292/* Receive a single line */
[b7fd2a0]293errno_t recv_line(receive_buffer_t *rb, char *line, size_t size, size_t *nrecv)
[ff364f1]294{
295 size_t written = 0;
[9a09212]296 size_t nr;
[a35b458]297
[ff364f1]298 while (written < size) {
299 char c = 0;
[b7fd2a0]300 errno_t rc = recv_char(rb, &c, true);
[ff364f1]301 if (rc != EOK)
302 return rc;
303 if (c == '\n') {
[9a09212]304 rc = recv_discard(rb, '\r', &nr);
305 if (rc != EOK)
306 return rc;
307
[ff364f1]308 line[written++] = 0;
[9a09212]309 *nrecv = written;
310 return EOK;
[1433ecda]311 } else if (c == '\r') {
[9a09212]312 rc = recv_discard(rb, '\n', &nr);
313 if (rc != EOK)
314 return rc;
[ff364f1]315 line[written++] = 0;
[9a09212]316 *nrecv = written;
317 return EOK;
[ff364f1]318 }
319 line[written++] = c;
320 }
[a35b458]321
[ff364f1]322 return ELIMIT;
323}
324
325/** @}
326 */
Note: See TracBrowser for help on using the repository browser.