Changeset ff364f1 in mainline for uspace/lib/http/http.c


Ignore:
Timestamp:
2013-09-21T10:13:41Z (12 years ago)
Author:
Martin Sucha <sucha14@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0005b63
Parents:
f9a2831
Message:

Abstract away the receive buffer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/http/http.c

    rf9a2831 rff364f1  
    4343
    4444#include "http.h"
     45#include "receive-buffer.h"
    4546
    46 void recv_reset(http_t *http)
     47static ssize_t http_receive(void *client_data, void *buf, size_t buf_size)
    4748{
    48         http->recv_buffer_in = 0;
    49         http->recv_buffer_out = 0;
    50 }
    51 
    52 /** Receive one character (with buffering) */
    53 int recv_char(http_t *http, char *c, bool consume)
    54 {
    55         if (http->recv_buffer_out == http->recv_buffer_in) {
    56                 recv_reset(http);
    57                
    58                 ssize_t rc = recv(http->conn_sd, http->recv_buffer, http->buffer_size, 0);
    59                 if (rc <= 0)
    60                         return rc;
    61                
    62                 http->recv_buffer_in = rc;
    63         }
    64        
    65         *c = http->recv_buffer[http->recv_buffer_out];
    66         if (consume)
    67                 http->recv_buffer_out++;
    68         return EOK;
    69 }
    70 
    71 ssize_t recv_buffer(http_t *http, char *buf, size_t buf_size)
    72 {
    73         /* Flush any buffered data*/
    74         if (http->recv_buffer_out != http->recv_buffer_in) {
    75                 size_t size = min(http->recv_buffer_in - http->recv_buffer_out, buf_size);
    76                 memcpy(buf, http->recv_buffer + http->recv_buffer_out, size);
    77                 http->recv_buffer_out += size;
    78                 return size;
    79         }
    80        
     49        http_t *http = client_data;
    8150        return recv(http->conn_sd, buf, buf_size, 0);
    82 }
    83 
    84 /** Receive a character and if it is c, discard it from input buffer */
    85 int recv_discard(http_t *http, char discard)
    86 {
    87         char c = 0;
    88         int rc = recv_char(http, &c, false);
    89         if (rc != EOK)
    90                 return rc;
    91         if (c != discard)
    92                 return EOK;
    93         return recv_char(http, &c, true);
    94 }
    95 
    96 /* Receive a single line */
    97 ssize_t recv_line(http_t *http, char *line, size_t size)
    98 {
    99         size_t written = 0;
    100        
    101         while (written < size) {
    102                 char c = 0;
    103                 int rc = recv_char(http, &c, true);
    104                 if (rc != EOK)
    105                         return rc;
    106                 if (c == '\n') {
    107                         recv_discard(http, '\r');
    108                         line[written++] = 0;
    109                         return written;
    110                 }
    111                 else if (c == '\r') {
    112                         recv_discard(http, '\n');
    113                         line[written++] = 0;
    114                         return written;
    115                 }
    116                 line[written++] = c;
    117         }
    118        
    119         return ELIMIT;
    12051}
    12152
     
    13465       
    13566        http->buffer_size = 4096;
    136         http->recv_buffer = malloc(http->buffer_size);
    137         if (http->recv_buffer == NULL) {
    138                 free(http->host);
     67        int rc = recv_buffer_init(&http->recv_buffer, http->buffer_size,
     68            http_receive, http);
     69        if (rc != EOK) {
    13970                free(http);
    14071                return NULL;
     
    198129void http_destroy(http_t *http)
    199130{
    200         free(http->recv_buffer);
     131        recv_buffer_fini(&http->recv_buffer);
    201132        free(http);
    202133}
Note: See TracChangeset for help on using the changeset viewer.