MCPcopy Create free account
hub / github.com/amcewen/HttpClient / readHeader

Method readHeader

HttpClient.cpp:487–562  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

485}
486
487int HttpClient::readHeader()
488{
489 char c = read();
490
491 if (endOfHeadersReached())
492 {
493 // We've passed the headers, but rather than return an error, we'll just
494 // act as a slightly less efficient version of read()
495 return c;
496 }
497
498 // Whilst reading out the headers to whoever wants them, we'll keep an
499 // eye out for the "Content-Length" header
500 switch(iState)
501 {
502 case eStatusCodeRead:
503 // We're at the start of a line, or somewhere in the middle of reading
504 // the Content-Length prefix
505 if (*iContentLengthPtr == c)
506 {
507 // This character matches, just move along
508 iContentLengthPtr++;
509 if (*iContentLengthPtr == '\0')
510 {
511 // We've reached the end of the prefix
512 iState = eReadingContentLength;
513 // Just in case we get multiple Content-Length headers, this
514 // will ensure we just get the value of the last one
515 iContentLength = 0;
516 }
517 }
518 else if ((iContentLengthPtr == kContentLengthPrefix) && (c == '\r'))
519 {
520 // We've found a '\r' at the start of a line, so this is probably
521 // the end of the headers
522 iState = eLineStartingCRFound;
523 }
524 else
525 {
526 // This isn't the Content-Length header, skip to the end of the line
527 iState = eSkipToEndOfHeader;
528 }
529 break;
530 case eReadingContentLength:
531 if (isdigit(c))
532 {
533 iContentLength = iContentLength*10 + (c - '0');
534 }
535 else
536 {
537 // We've reached the end of the content length
538 // We could sanity check it here or double-check for "\r\n"
539 // rather than anything else, but let's be lenient
540 iState = eSkipToEndOfHeader;
541 }
542 break;
543 case eLineStartingCRFound:
544 if (c == '\n')

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected