source: mainline/uspace/lib/http/receive-buffer.c@ 3ce68b7

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

libhttp: receive headers correctly

  • Property mode set to 100644
File size: 6.0 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 "receive-buffer.h"
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;
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 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
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
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
146/** Receive one character (with buffering) */
147int recv_char(receive_buffer_t *rb, char *c, bool consume)
148{
149 if (rb->out == rb->in) {
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 }
168
169 ssize_t rc = rb->receive(rb->client_data, rb->buffer + rb->in, free);
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
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)
199{
200 char c = 0;
201 int rc = recv_char(rb, &c, false);
202 if (rc != EOK)
203 return rc;
204 if (c != discard)
205 return 0;
206 rc = recv_char(rb, &c, true);
207 if (rc != EOK)
208 return rc;
209 return 1;
210}
211
212/** Receive an end of line, either CR, LF, CRLF or LFCR
213 *
214 * @return number of bytes read (0 if no newline is present in the stream)
215 * or negative error code
216 */
217ssize_t recv_eol(receive_buffer_t *rb)
218{
219 char c = 0;
220 int rc = recv_char(rb, &c, false);
221 if (rc != EOK)
222 return rc;
223
224 if (c != '\r' && c != '\n')
225 return 0;
226
227 rc = recv_char(rb, &c, true);
228 if (rc != EOK)
229 return rc;
230
231 ssize_t rc2 = recv_discard(rb, (c == '\r' ? '\n' : '\r'));
232 if (rc2 < 0)
233 return rc2;
234
235 return 1 + rc2;
236}
237
238/* Receive a single line */
239ssize_t recv_line(receive_buffer_t *rb, char *line, size_t size)
240{
241 size_t written = 0;
242
243 while (written < size) {
244 char c = 0;
245 int rc = recv_char(rb, &c, true);
246 if (rc != EOK)
247 return rc;
248 if (c == '\n') {
249 recv_discard(rb, '\r');
250 line[written++] = 0;
251 return written;
252 }
253 else if (c == '\r') {
254 recv_discard(rb, '\n');
255 line[written++] = 0;
256 return written;
257 }
258 line[written++] = c;
259 }
260
261 return ELIMIT;
262}
263
264/** @}
265 */
Note: See TracBrowser for help on using the repository browser.