Search code examples
javasocketshttpnetwork-programmingtheory

End of HTTP header


I'm trying to build a program which will identify the end of a header. Now I am aware of that http headers end with an empty line as said in Detect end of http get request header (java), however, to my understanding that is not always the case.

E.g. in multipart you have a content-type with a boundary, and in that boundary there is an empty space, example

Reading line: POST /hello:hello HTTP/1.1
Reading line: bla: bla
Reading line: Content-Type: multipart/form-data; boundary=--------------------------276559868742390689469124
Reading line: cache-control: no-cache
Reading line: Postman-Token: be8db757-81f4-49db-9187-30d66d0dd3d5
Reading line: User-Agent: PostmanRuntime/7.1.5
Reading line: Accept: */*
Reading line: Host: localhost:8080
Reading line: accept-encoding: gzip, deflate
Reading line: content-length: 498
Reading line: Connection: keep-alive
Reading line: 
Reading line: ----------------------------276559868742390689469124
Reading line: Content-Disposition: form-data; name=""; filename="TimeComplexity.txt"
Reading line: Content-Type: text/plain

so obviously looking for an empty space does not seem to work every time. What I have noticed though is that the Content-Type followed by a space always seems to be the end of the HTTP header, is that correct? and if not, what other solutions are there? except for extracting the content-length and extract the content before the header, it seems.. slow. Could also mention that I'm trying to parse the message in java, but I wouldn't mind examples in other languages if you are pasting any code, however, I seek to understand it theoretically, I believe I can manage the rest.

Thanks in advance!


Solution

  • HTTP headers may be in any order. Each header ends with CRLF, and after the last header there is a single CRLF creating an empty line. That signals that there are no more headers. The rest is the HTTP message body. See RFC 2616 for the HTTP protocol spec.

    Your last HTTP header is the Connection header. The following body data (498 bytes in size) is MIME data in multipart/form-data format. MIME looks similar to HTTP, in that it also has headers and body separated by an empty line. But MIME also has boundaries separating different sections of MIME data, and each section has its own headers and body. See RFCs 2045 - 2047, 7578 and other MIME related RFCs.