Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am writing a simple HTTP client in .NET for learning purposes. I am using the .NET Socket class, which ultimately uses Winsock. I do not want to use the WebRequest, HttpWebRequest, or HttpClient classes, as they use WinINet, which I do not want to use as I am doing this for my own understanding of how HTTP works.

I am wondering how to determine when an HTTP response is finished. By reading the HTTP/1.1 specification (RFC 2616), I think the following pseudocode is how to determine when an HTTP response is finished.

parse HTTP headers
if parse not successful:
    throw error
if HTTP version is 1.1 and Transfer-encoding is chunked:
    parse first line of each chunk as an ASCII hexadecimal, the chunk size
    if parse not successful:
        throw error
    read each chunk until chunk size 0
else if Content-Length is specified:
    read Content-Length number of bytes
else:
    throw error

Is this a more-or-less correct approach?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
289 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your understanding is mostly correct, with some minor corrections, per RFC 2616 Section 4.4:

Read and parse HTTP headers
if not successful:
    throw error
if response can contain message body:
    if HTTP version is 1.1+ and Transfer-encoding is not identity:
        while true:
            read line, extract delimited ASCII hexadecimal, the chunk size
            if not successful:
                throw error
             if chunk size is 0:
                break while loop
             read chunk size number of bytes
        read and parse trailing HTTP headers
    else if Content-Length is specified:
        read Content-Length number of bytes
    else if Content-Type is "multipart/byteranges":
        read and parse MIME-encoded chunks until terminating MIME boundary is reached
    else:
        read until connection is closed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...