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

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

Abstract away the receive buffer

  • Property mode set to 100644
File size: 3.4 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
42#include "receive-buffer.h"
43
44int recv_buffer_init(receive_buffer_t *rb, size_t buffer_size,
45 receive_func_t receive, void *client_data)
46{
47 rb->receive = receive;
48 rb->client_data = client_data;
49
50 rb->in = 0;
51 rb->out = 0;
52 rb->size = buffer_size;
53 rb->buffer = malloc(buffer_size);
54 if (rb->buffer == NULL)
55 return ENOMEM;
56 return EOK;
57}
58
59void recv_buffer_fini(receive_buffer_t *rb)
60{
61 free(rb->buffer);
62}
63
64void recv_reset(receive_buffer_t *rb)
65{
66 rb->in = 0;
67 rb->out = 0;
68}
69
70/** Receive one character (with buffering) */
71int recv_char(receive_buffer_t *rb, char *c, bool consume)
72{
73 if (rb->out == rb->in) {
74 recv_reset(rb);
75
76 ssize_t rc = rb->receive(rb->client_data, rb->buffer, rb->size);
77 if (rc <= 0)
78 return rc;
79
80 rb->in = rc;
81 }
82
83 *c = rb->buffer[rb->out];
84 if (consume)
85 rb->out++;
86 return EOK;
87}
88
89ssize_t recv_buffer(receive_buffer_t *rb, char *buf, size_t buf_size)
90{
91 /* Flush any buffered data*/
92 if (rb->out != rb->in) {
93 size_t size = min(rb->in - rb->out, buf_size);
94 memcpy(buf, rb->buffer + rb->out, size);
95 rb->out += size;
96 return size;
97 }
98
99 return rb->receive(rb->client_data, buf, buf_size);
100}
101
102/** Receive a character and if it is c, discard it from input buffer */
103int recv_discard(receive_buffer_t *rb, char discard)
104{
105 char c = 0;
106 int rc = recv_char(rb, &c, false);
107 if (rc != EOK)
108 return rc;
109 if (c != discard)
110 return EOK;
111 return recv_char(rb, &c, true);
112}
113
114/* Receive a single line */
115ssize_t recv_line(receive_buffer_t *rb, char *line, size_t size)
116{
117 size_t written = 0;
118
119 while (written < size) {
120 char c = 0;
121 int rc = recv_char(rb, &c, true);
122 if (rc != EOK)
123 return rc;
124 if (c == '\n') {
125 recv_discard(rb, '\r');
126 line[written++] = 0;
127 return written;
128 }
129 else if (c == '\r') {
130 recv_discard(rb, '\n');
131 line[written++] = 0;
132 return written;
133 }
134 line[written++] = c;
135 }
136
137 return ELIMIT;
138}
139
140/** @}
141 */
Note: See TracBrowser for help on using the repository browser.