MCPcopy Create free account
hub / github.com/ClassiCube/ClassiCube / HttpClient_Process

Function HttpClient_Process

src/Http_Worker.c:476–608  ·  view source on GitHub ↗

https://httpwg.org/specs/rfc7230.html */

Source from the content-addressed store, hash-verified

474
475/* https://httpwg.org/specs/rfc7230.html */
476static cc_result HttpClient_Process(struct HttpClientState* state, char* buffer, int total) {
477 struct HttpRequest* req = state->req;
478 cc_uint32 left, avail, read;
479 int offset = 0, chunkLen, ok;
480
481 while (offset < total) {
482 switch (state->state) {
483 case HTTP_RESPONSE_STATE_INITIAL:
484 state->state = HTTP_RESPONSE_STATE_HEADER;
485 break;
486
487 case HTTP_RESPONSE_STATE_HEADER:
488 {
489 for (; offset < total;)
490 {
491 char c = buffer[offset++];
492 if (c == '\r') continue;
493 if (c != '\n') {
494 /* Warn when a header would be truncated */
495 if (state->header.length == HTTP_HEADER_MAX_LENGTH)
496 return HTTP_ERR_TRUNCATED;
497
498 String_Append(&state->header, c);
499 continue;
500 }
501
502 /* Zero length header = end of message headers */
503 if (state->header.length == 0) {
504 state->state = HttpClient_BeginBody(req, state);
505
506 /* The rest of the request body is just content/data */
507 if (state->state == HTTP_RESPONSE_STATE_DATA) {
508 state->dataLeft = req->contentLength;
509 ok = Http_BufferExpand(req, state->dataLeft);
510 if (!ok) return ERR_OUT_OF_MEMORY;
511 }
512 break;
513 }
514
515 Http_ParseHeader(state->req, &state->header);
516 HttpClient_ParseHeader(state, &state->header);
517 state->header.length = 0;
518 }
519 }
520 break;
521
522 case HTTP_RESPONSE_STATE_DATA:
523 {
524 left = total - offset;
525 avail = state->dataLeft;
526 read = min(left, avail);
527
528 /* TODO figure out why this bug happens */
529 if (!req->data) Process_Abort("Http state broken, please report this");
530
531 Mem_Copy(req->data + req->size, buffer + offset, read);
532 Http_BufferExpanded(req, read);
533

Callers 1

HttpClient_ParseResponseFunction · 0.85

Calls 8

String_AppendFunction · 0.85
HttpClient_BeginBodyFunction · 0.85
Http_BufferExpandFunction · 0.85
Http_ParseHeaderFunction · 0.85
HttpClient_ParseHeaderFunction · 0.85
Mem_CopyFunction · 0.85
Http_BufferExpandedFunction · 0.85

Tested by

no test coverage detected