Changes in uspace/lib/http/src/receive-buffer.c [408424e:cde999a] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/http/src/receive-buffer.c
r408424e rcde999a 61 61 } 62 62 63 static ssize_t dummy_receive(void *unused, void *buf, size_t buf_size) 64 { 65 return 0; 63 static int dummy_receive(void *unused, void *buf, size_t buf_size, 64 size_t *nrecv) 65 { 66 *nrecv = 0; 67 return EOK; 66 68 } 67 69 … … 167 169 } 168 170 169 ssize_t rc = rb->receive(rb->client_data, rb->buffer + rb->in, free); 170 if (rc <= 0) 171 size_t nrecv; 172 int rc = rb->receive(rb->client_data, rb->buffer + rb->in, free, &nrecv); 173 if (rc != EOK) 171 174 return rc; 172 175 173 rb->in = rc;176 rb->in = nrecv; 174 177 } 175 178 … … 180 183 } 181 184 182 ssize_t recv_buffer(receive_buffer_t *rb, char *buf, size_t buf_size) 183 { 184 /* Flush any buffered data*/ 185 int recv_buffer(receive_buffer_t *rb, char *buf, size_t buf_size, 186 size_t *nrecv) 187 { 188 /* Flush any buffered data */ 185 189 if (rb->out != rb->in) { 186 190 size_t size = min(rb->in - rb->out, buf_size); 187 191 memcpy(buf, rb->buffer + rb->out, size); 188 192 rb->out += size; 189 return size; 190 } 191 192 return rb->receive(rb->client_data, buf, buf_size); 193 *nrecv = size; 194 return EOK; 195 } 196 197 return rb->receive(rb->client_data, buf, buf_size, nrecv); 193 198 } 194 199 195 200 /** 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 */ 198 ssize_t recv_discard(receive_buffer_t *rb, char discard) 201 * @param ndisc Place to store number of characters discarded 202 * @return EOK or an error code 203 */ 204 int recv_discard(receive_buffer_t *rb, char discard, size_t *ndisc) 199 205 { 200 206 char c = 0; … … 202 208 if (rc != EOK) 203 209 return rc; 204 if (c != discard) 205 return 0; 210 if (c != discard) { 211 *ndisc = 0; 212 return EOK; 213 } 206 214 rc = recv_char(rb, &c, true); 207 215 if (rc != EOK) 208 216 return rc; 209 return 1; 217 *ndisc = 1; 218 return EOK; 210 219 } 211 220 212 221 /** Receive a prefix of constant string discard and return number of bytes read 213 * @return number of characters discarded or negative error code 214 */ 215 ssize_t recv_discard_str(receive_buffer_t *rb, const char *discard) 222 * @param ndisc Place to store number of characters discarded 223 * @return EOK or an error code 224 */ 225 int recv_discard_str(receive_buffer_t *rb, const char *discard, size_t *ndisc) 216 226 { 217 227 size_t discarded = 0; 218 228 while (*discard) { 219 ssize_t rc = recv_discard(rb, *discard); 220 if (rc < 0) 221 return rc; 222 if (rc == 0) 229 size_t nd; 230 int rc = recv_discard(rb, *discard, &nd); 231 if (rc != EOK) 232 return rc; 233 if (nd == 0) 223 234 break; 224 discarded ++;235 discarded += nd; 225 236 discard++; 226 237 } 227 return discarded; 228 } 229 230 ssize_t recv_while(receive_buffer_t *rb, char_class_func_t class) 231 { 232 size_t received = 0; 233 238 239 *ndisc = discarded; 240 return EOK; 241 } 242 243 int recv_while(receive_buffer_t *rb, char_class_func_t class) 244 { 234 245 while (true) { 235 246 char c = 0; … … 244 255 if (rc != EOK) 245 256 return rc; 246 247 received++; 248 } 249 250 return received; 257 } 258 259 return EOK; 251 260 } 252 261 253 262 /** Receive an end of line, either CR, LF, CRLF or LFCR 254 263 * 255 * @return number of bytes read (0 if no newline is present in the stream) 256 * or negative error code 257 */ 258 ssize_t recv_eol(receive_buffer_t *rb) 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 int recv_eol(receive_buffer_t *rb, size_t *nrecv) 259 269 { 260 270 char c = 0; … … 263 273 return rc; 264 274 265 if (c != '\r' && c != '\n') 266 return 0; 275 if (c != '\r' && c != '\n') { 276 *nrecv = 0; 277 return EOK; 278 } 267 279 268 280 rc = recv_char(rb, &c, true); … … 270 282 return rc; 271 283 272 ssize_t rc2 = recv_discard(rb, (c == '\r' ? '\n' : '\r')); 273 if (rc2 < 0) 274 return rc2; 275 276 return 1 + rc2; 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; 277 291 } 278 292 279 293 /* Receive a single line */ 280 ssize_t recv_line(receive_buffer_t *rb, char *line, size_t size)294 int recv_line(receive_buffer_t *rb, char *line, size_t size, size_t *nrecv) 281 295 { 282 296 size_t written = 0; 297 size_t nr; 283 298 284 299 while (written < size) { … … 288 303 return rc; 289 304 if (c == '\n') { 290 recv_discard(rb, '\r'); 305 rc = recv_discard(rb, '\r', &nr); 306 if (rc != EOK) 307 return rc; 308 291 309 line[written++] = 0; 292 return written; 310 *nrecv = written; 311 return EOK; 293 312 } 294 313 else if (c == '\r') { 295 recv_discard(rb, '\n'); 314 rc = recv_discard(rb, '\n', &nr); 315 if (rc != EOK) 316 return rc; 296 317 line[written++] = 0; 297 return written; 318 *nrecv = written; 319 return EOK; 298 320 } 299 321 line[written++] = c;
Note:
See TracChangeset
for help on using the changeset viewer.