Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does the web container parse http messages

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how the web container parses http messages". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how the web container parses http messages.

Abstract

Http messages are actually regular strings, so parsing them is parsing strings to see if they meet the rules of the http protocol.

Start-line: starting line, describing the basic information of the request or response * (header-field CRLF): header CRLF [message-body]: message body, actual transmitted data jetty

The following code is the jetty9.4.12 version

How to parse such a long string? jetty is implemented through a state machine. For more information, take a look at the org.eclipse.jetty.http.HttpParse class.

Public enum State {START, METHOD,! [] (https://img2018.cnblogs.com/blog/1147363/201910/1147363-20191009220439773-204646534.png), SPACE1, STATUS, URI, SPACE2, REQUEST_VERSION, REASON, PROXY, HEADER, CONTENT, EOF_CONTENT, CHUNKED_CONTENT, CHUNK_SIZE CHUNK_PARAMS, CHUNK, TRAILER, END, CLOSE, / / The associated stream/endpoint should be closed CLOSED / / The associated stream/endpoint is at EOF}

A total of 21 states are divided, and then the transfer between states is carried out. Parse the starting line-> header-> body content respectively in the parseNext method

Public boolean parseNext (ByteBuffer buffer) {try {/ / Start a request/response if (_ state==State.START) {/ / quickly determine if (quickStart (buffer)) return true } / / Request/response line transform if (_ state.ordinal () > = State.START.ordinal () & & _ state.ordinal () = State.CONTENT.ordinal () & & _ state.ordinal () 0 & & _ headResponse) {setState (State.END); return handleContentMessage () } else {if (parseContent (buffer)) return true;}} return false;} overall process

There are three paths as a whole.

Start-> start-line-> header-> end

Start-> start-line-> header-> content-> end

Start-> start-line-> header-> chunk-content-> end

Start line

Start-line = request-line (request start line) / (response start line) status-line

Request message parsing status migration

Request line: START-> METHOD-> SPACE1-> URI-> SPACE2-> REQUEST_VERSION

Response message parsing state migration

Response line: START-> RESPONSE_VERSION-> SPACE1-> STATUS-> SPACE2-> REASON

Header header

There is only one state for HEADER, and HEADER_IN_NAM, HEADER_VALUE, HEADER_IN_VALUE, etc., were distinguished in the old version of jetty, and all of them were removed in 9.4. In order to improve the matching efficiency, jetty uses Trie tree to quickly match headers.

Static {CACHE.put (new HttpField (HttpHeader.CONNECTION,HttpHeaderValue.CLOSE)); CACHE.put (new HttpField (HttpHeader.CONNECTION,HttpHeaderValue.KEEP_ALIVE)); / / A lot of generic header header content are omitted below

Request body:

CONTENT-> END, which is an ordinary message with a Content-Length header. HttpParser runs CONTENT status until the specified number of ContentLength is reached, and then enters END state.

Data transmitted in blocks by chunked

CHUNKED_CONTENT-> CHUNK_SIZE-> CHUNK-> CHUNK_END-> END

Undertow

Undertow is another web container. What's the difference between it and jetty?

The type of state machine is different, io.undertow.util.HttpString.ParseState

Public static final int VERB = 0; public static final int PATH = 1; public static final int PATH_PARAMETERS = 2; public static final int QUERY_PARAMETERS = 3; public static final int VERSION = 4; public static final int AFTER_VERSION = 5; public static final int HEADER = 6; public static final int HEADER_VALUE = 7; public static final int PARSE_COMPLETE = 8

The concrete processing flow is in the HttpRequestParser abstract class.

Public void handle (ByteBuffer buffer, final ParseState currentState, final HttpServerExchange builder) throws BadRequestException {if (currentState.state = = ParseState.VERB) {/ / fast path, we assume that it will parse fully so we avoid all the if statements / / Quick processing GET final int position = buffer.position () If (buffer.remaining () > 3 & & buffer.get (position) = ='G' & & buffer.get (position + 1) ='E' & & buffer.get (position + 2) = ='T' & & buffer.get (position + 3) = ='') {buffer.position (position + 4) Builder.setRequestMethod (Methods.GET); currentState.state = ParseState.PATH;} else {try {handleHttpVerb (buffer, currentState, builder);} catch (IllegalArgumentException e) {throw new BadRequestException (e) }} / / processing path handlePath (buffer, currentState, builder); / / processing version if (failed) {handleHttpVersion (buffer, currentState, builder); handleAfterVersion (buffer, currentState) } / / deal with header while (currentState.state! = ParseState.PARSE_COMPLETE & & buffer.hasRemaining ()) {handleHeader (buffer, currentState, builder); if (currentState.state = = ParseState.HEADER_VALUE) {handleHeaderValue (buffer, currentState, builder);}} return } handleStateful (buffer, currentState, builder);}

Different from jetty is the processing of content, after header processing, put the data into io.undertow.server.HttpServerExchange, and then according to the type, there are different ways to read content, such as dealing with fixed-length, FixedLengthStreamSourceConduit.

At this point, I believe you have a deeper understanding of "how the web container parses http messages". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report