Changeset ff364f1 in mainline for uspace/lib/http/http.c
- Timestamp:
- 2013-09-21T10:13:41Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 0005b63
- Parents:
- f9a2831
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/http/http.c
rf9a2831 rff364f1 43 43 44 44 #include "http.h" 45 #include "receive-buffer.h" 45 46 46 void recv_reset(http_t *http)47 static ssize_t http_receive(void *client_data, void *buf, size_t buf_size) 47 48 { 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; 81 50 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;120 51 } 121 52 … … 134 65 135 66 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) { 139 70 free(http); 140 71 return NULL; … … 198 129 void http_destroy(http_t *http) 199 130 { 200 free(http->recv_buffer);131 recv_buffer_fini(&http->recv_buffer); 201 132 free(http); 202 133 }
Note:
See TracChangeset
for help on using the changeset viewer.