https://roka88.dev/105

RFC7230 - 3.3 Message body

Parser::parseBody()

The presence of a message body in a request is signaled by a Content-Length or Transfer-Encoding header field.

요청 시 메시지에 body가 있으면, Content-Length 또는 Transfer-Encoding 헤더 필드로 표시된다.

if (client.req.headers.find("Content-Length") != client.req.headers.end())
  getBody(client);
else if (client.req.headers["Transfer-Encoding"] == "chunked")
  dechunkBody(client);
else //에러
	client.req.method = "BAD";

RFC7230 - 4.1 Chunked Transfer Coding

The chunked transfer coding wraps the payload body in order to transfer it as a series of chunks, each with its own size indicator, followed by an OPTIONAL trailer containing header fields.  Chunked enables content streams of unknown size to be transferred as a sequence of length-delimited buffers, which enables the sender to retain connection persistence and the recipient to know when it has received the entire message.

청크 전송 코딩은 페이 로드 본체의 각각 자체 크기를 표시하여 전송하며, OPTIONAL 트레일러를 포함하는 헤더 필드 뒤에, 일련의 청크를 순차적으로 전송하기 위해 페이 로드 본체를 감싼다. 청크를 사용하면 알 수 없는 크기의 콘텐츠 스트림을 길이가 제한된 버퍼의 순서로 전송할 수 있으며, 이를 통해 송신자는 커넥션 영속성을 유지하고 수신자는 전체 메시지를 수신한 시점을 알 수 있다.

<aside> 📖 chunked-body   = chunk last-chunk trailer-part CRLF chunk           = chunk-size [ chunk-ext ] CRLF chunk-data CRLF chunk-size    = 1HEXDIG last-chunk    = 1*("0") [ chunk-ext ] CRLF chunk-data     = 1*OCTET ; a sequence of chunk-size octets

</aside>

Parser::getBody() → Parser::findLen()

The chunk-size field is a string of hex digits indicating the size of the chunk-data in octets. A recipient MUST be able to parse and decode the chunked transfer coding.

chunk-size 필드는 octet 단위의 청크 데이터 크기를 나타내는 16진수 문자열이다. 수신자는 청크 분할 전송 코딩을 반드시 구문 분석하고 디코딩 할 수 있어야 한다.(MUST)

to_convert = to_convert.substr(0, to_convert.find("\\r\\n"));
while (to_convert[0] == '\\n')
	to_convert.erase(to_convert.begin());
len = fromHexa(to_convert.c_str()); //16진수 문자열 -> 10진수 정수로 변환