16.03.2017 Views

Learning HTTP/2

akamai-learning-http2-preview-edition

akamai-learning-http2-preview-edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Frames<br />

As mentioned before, <strong>HTTP</strong>/2 is a framed protocol. Framing is a method for wrapping<br />

all the important stuff in a way that makes it easy for consumers of the protocol<br />

to read, parse, and create. In contrast, <strong>HTTP</strong>/1 is not framed but is rather text delimited.<br />

Look at the following simple example:<br />

GET / <strong>HTTP</strong>/1.1 <br />

Host: www.example.com <br />

Connection: keep-alive <br />

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 <br />

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4)... <br />

Accept-Encoding: gzip, deflate, sdch <br />

Accept-Language: en-US,en;q=0.8 <br />

Cookie: pfy_cbc_lb=p-browse-w; customerZipCode=99912|N; ltc=%20;... <br />

<br />

Parsing something like this is not rocket science but it tends to be slow and error<br />

prone. You need to keep reading bytes until you get to a delimiter, in this case,<br />

while also accounting for all of the less spec compliant clients that just send . A<br />

state machine looks something like this:<br />

loop<br />

while( ! CRLF )<br />

read bytes<br />

end while<br />

if first line<br />

parse line as the Request-Line<br />

else if line is empty<br />

break out of the loop # We are done<br />

else if line starts with non-whitespace<br />

parse the header line into a key/value pair<br />

else if line starts with space<br />

add the continuation header to the previous header<br />

end if<br />

end loop<br />

# Now go on to ready the request/response based on whatever was<br />

# in the Transfer-encoding header<br />

Writing this code is very doable and has been done countless times. The problems<br />

with parsing an h1 request/response are:<br />

• You can only have one request/response on the wire at a time. You have to parse<br />

until done.<br />

• It is unclear how much memory the parsing will take. What buffer are you reading<br />

a line into? What happens if that line it too long? Grow and reallocate?<br />

28 | Chapter 3: The Protocol

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!