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