| 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 |
|
|---|
| 45 | errno_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 |
|
|---|
| 63 | static 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 |
|
|---|
| 70 | errno_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 |
|
|---|
| 81 | void recv_buffer_fini(receive_buffer_t *rb)
|
|---|
| 82 | {
|
|---|
| 83 | free(rb->buffer);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | void recv_reset(receive_buffer_t *rb)
|
|---|
| 87 | {
|
|---|
| 88 | rb->in = 0;
|
|---|
| 89 | rb->out = 0;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | void 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 |
|
|---|
| 99 | void recv_unmark(receive_buffer_t *rb, receive_buffer_mark_t *mark)
|
|---|
| 100 | {
|
|---|
| 101 | list_remove(&mark->link);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | void recv_mark_update(receive_buffer_t *rb, receive_buffer_mark_t *mark)
|
|---|
| 105 | {
|
|---|
| 106 | mark->offset = rb->out;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | errno_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 |
|
|---|
| 125 | errno_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 |
|
|---|
| 148 | /** Receive one character (with buffering) */
|
|---|
| 149 | errno_t recv_char(receive_buffer_t *rb, char *c, bool consume)
|
|---|
| 150 | {
|
|---|
| 151 | if (rb->out == rb->in) {
|
|---|
| 152 | size_t free = rb->size - rb->in;
|
|---|
| 153 | if (free == 0) {
|
|---|
| 154 | size_t min_mark = rb->size;
|
|---|
| 155 | list_foreach(rb->marks, link, receive_buffer_mark_t, mark) {
|
|---|
| 156 | min_mark = min(min_mark, mark->offset);
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | if (min_mark == 0)
|
|---|
| 160 | return ELIMIT;
|
|---|
| 161 |
|
|---|
| 162 | size_t new_in = rb->in - min_mark;
|
|---|
| 163 | memmove(rb->buffer, rb->buffer + min_mark, new_in);
|
|---|
| 164 | rb->out = rb->in = new_in;
|
|---|
| 165 | free = rb->size - rb->in;
|
|---|
| 166 | list_foreach(rb->marks, link, receive_buffer_mark_t, mark) {
|
|---|
| 167 | mark->offset -= min_mark;
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | size_t nrecv;
|
|---|
| 172 | errno_t rc = rb->receive(rb->client_data, rb->buffer + rb->in, free, &nrecv);
|
|---|
| 173 | if (rc != EOK)
|
|---|
| 174 | return rc;
|
|---|
| 175 |
|
|---|
| 176 | rb->in = nrecv;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | *c = rb->buffer[rb->out];
|
|---|
| 180 | if (consume)
|
|---|
| 181 | rb->out++;
|
|---|
| 182 | return EOK;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | errno_t recv_buffer(receive_buffer_t *rb, char *buf, size_t buf_size,
|
|---|
| 186 | size_t *nrecv)
|
|---|
| 187 | {
|
|---|
| 188 | /* Flush any buffered data */
|
|---|
| 189 | if (rb->out != rb->in) {
|
|---|
| 190 | size_t size = min(rb->in - rb->out, buf_size);
|
|---|
| 191 | memcpy(buf, rb->buffer + rb->out, size);
|
|---|
| 192 | rb->out += size;
|
|---|
| 193 | *nrecv = size;
|
|---|
| 194 | return EOK;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | return rb->receive(rb->client_data, buf, buf_size, nrecv);
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /** Receive a character and if it is c, discard it from input buffer
|
|---|
| 201 | * @param ndisc Place to store number of characters discarded
|
|---|
| 202 | * @return EOK or an error code
|
|---|
| 203 | */
|
|---|
| 204 | errno_t recv_discard(receive_buffer_t *rb, char discard, size_t *ndisc)
|
|---|
| 205 | {
|
|---|
| 206 | char c = 0;
|
|---|
| 207 | errno_t rc = recv_char(rb, &c, false);
|
|---|
| 208 | if (rc != EOK)
|
|---|
| 209 | return rc;
|
|---|
| 210 | if (c != discard) {
|
|---|
| 211 | *ndisc = 0;
|
|---|
| 212 | return EOK;
|
|---|
| 213 | }
|
|---|
| 214 | rc = recv_char(rb, &c, true);
|
|---|
| 215 | if (rc != EOK)
|
|---|
| 216 | return rc;
|
|---|
| 217 | *ndisc = 1;
|
|---|
| 218 | return EOK;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /** Receive a prefix of constant string discard and return number of bytes read
|
|---|
| 222 | * @param ndisc Place to store number of characters discarded
|
|---|
| 223 | * @return EOK or an error code
|
|---|
| 224 | */
|
|---|
| 225 | errno_t recv_discard_str(receive_buffer_t *rb, const char *discard, size_t *ndisc)
|
|---|
| 226 | {
|
|---|
| 227 | size_t discarded = 0;
|
|---|
| 228 | while (*discard) {
|
|---|
| 229 | size_t nd;
|
|---|
| 230 | errno_t rc = recv_discard(rb, *discard, &nd);
|
|---|
| 231 | if (rc != EOK)
|
|---|
| 232 | return rc;
|
|---|
| 233 | if (nd == 0)
|
|---|
| 234 | break;
|
|---|
| 235 | discarded += nd;
|
|---|
| 236 | discard++;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | *ndisc = discarded;
|
|---|
| 240 | return EOK;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | errno_t recv_while(receive_buffer_t *rb, char_class_func_t class)
|
|---|
| 244 | {
|
|---|
| 245 | while (true) {
|
|---|
| 246 | char c = 0;
|
|---|
| 247 | errno_t rc = recv_char(rb, &c, false);
|
|---|
| 248 | if (rc != EOK)
|
|---|
| 249 | return rc;
|
|---|
| 250 |
|
|---|
| 251 | if (!class(c))
|
|---|
| 252 | break;
|
|---|
| 253 |
|
|---|
| 254 | rc = recv_char(rb, &c, true);
|
|---|
| 255 | if (rc != EOK)
|
|---|
| 256 | return rc;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | return EOK;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /** Receive an end of line, either CR, LF, CRLF or LFCR
|
|---|
| 263 | *
|
|---|
| 264 | * @param nrecv Place to store number of bytes received (zero if
|
|---|
| 265 | * no newline is present in the stream)
|
|---|
| 266 | * @return EOK on success or an error code
|
|---|
| 267 | */
|
|---|
| 268 | errno_t recv_eol(receive_buffer_t *rb, size_t *nrecv)
|
|---|
| 269 | {
|
|---|
| 270 | char c = 0;
|
|---|
| 271 | errno_t rc = recv_char(rb, &c, false);
|
|---|
| 272 | if (rc != EOK)
|
|---|
| 273 | return rc;
|
|---|
| 274 |
|
|---|
| 275 | if (c != '\r' && c != '\n') {
|
|---|
| 276 | *nrecv = 0;
|
|---|
| 277 | return EOK;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | rc = recv_char(rb, &c, true);
|
|---|
| 281 | if (rc != EOK)
|
|---|
| 282 | return rc;
|
|---|
| 283 |
|
|---|
| 284 | size_t nr;
|
|---|
| 285 | rc = recv_discard(rb, (c == '\r' ? '\n' : '\r'), &nr);
|
|---|
| 286 | if (rc != EOK)
|
|---|
| 287 | return rc;
|
|---|
| 288 |
|
|---|
| 289 | *nrecv = 1 + nr;
|
|---|
| 290 | return EOK;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | /* Receive a single line */
|
|---|
| 294 | errno_t recv_line(receive_buffer_t *rb, char *line, size_t size, size_t *nrecv)
|
|---|
| 295 | {
|
|---|
| 296 | size_t written = 0;
|
|---|
| 297 | size_t nr;
|
|---|
| 298 |
|
|---|
| 299 | while (written < size) {
|
|---|
| 300 | char c = 0;
|
|---|
| 301 | errno_t rc = recv_char(rb, &c, true);
|
|---|
| 302 | if (rc != EOK)
|
|---|
| 303 | return rc;
|
|---|
| 304 | if (c == '\n') {
|
|---|
| 305 | rc = recv_discard(rb, '\r', &nr);
|
|---|
| 306 | if (rc != EOK)
|
|---|
| 307 | return rc;
|
|---|
| 308 |
|
|---|
| 309 | line[written++] = 0;
|
|---|
| 310 | *nrecv = written;
|
|---|
| 311 | return EOK;
|
|---|
| 312 | } else if (c == '\r') {
|
|---|
| 313 | rc = recv_discard(rb, '\n', &nr);
|
|---|
| 314 | if (rc != EOK)
|
|---|
| 315 | return rc;
|
|---|
| 316 | line[written++] = 0;
|
|---|
| 317 | *nrecv = written;
|
|---|
| 318 | return EOK;
|
|---|
| 319 | }
|
|---|
| 320 | line[written++] = c;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | return ELIMIT;
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | /** @}
|
|---|
| 327 | */
|
|---|