本篇內(nèi)容主要講解“web容器是怎么解析http報(bào)文的”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“web容器是怎么解析http報(bào)文的”吧!
成都創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括衡水網(wǎng)站建設(shè)、衡水網(wǎng)站制作、衡水網(wǎng)頁(yè)制作以及衡水網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,衡水網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到衡水省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
http報(bào)文其實(shí)就是一定規(guī)則的字符串,那么解析它們,就是解析字符串,看看是否滿足http協(xié)議約定的規(guī)則。
start-line: 起始行,描述請(qǐng)求或響應(yīng)的基本信息 *( header-field CRLF ): 頭 CRLF [message-body]: 消息body,實(shí)際傳輸?shù)臄?shù)據(jù)
以下代碼都是jetty9.4.12版本
如何解析這么長(zhǎng)的字符串呢,jetty是通過(guò)狀態(tài)機(jī)來(lái)實(shí)現(xiàn)的。具體可以看下org.eclipse.jetty.http.HttpParse類
public enum State { START, METHOD, ![](/upload/otherpic51/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 }
總共分成了21種狀態(tài),然后進(jìn)行狀態(tài)間的流轉(zhuǎn)。在parseNext方法中分別對(duì)起始行 -> header -> body content分別解析
public boolean parseNext(ByteBuffer buffer) { try { // Start a request/response if (_state==State.START) { // 快速判斷 if (quickStart(buffer)) return true; } // Request/response line 轉(zhuǎn)換 if (_state.ordinal()>= State.START.ordinal() && _state.ordinal()<State.HEADER.ordinal()) { if (parseLine(buffer)) return true; } // headers轉(zhuǎn)換 if (_state== State.HEADER) { if (parseFields(buffer)) return true; } // content轉(zhuǎn)換 if (_state.ordinal()>= State.CONTENT.ordinal() && _state.ordinal()<State.TRAILER.ordinal()) { // Handle HEAD response if (_responseStatus>0 && _headResponse) { setState(State.END); return handleContentMessage(); } else { if (parseContent(buffer)) return true; } } return false; }
整體有三條路徑
開(kāi)始 -> start-line -> header -> 結(jié)束
開(kāi)始 -> start-line -> header -> content -> 結(jié)束
開(kāi)始 -> start-line -> header -> chunk-content -> 結(jié)束
start-line = request-line(請(qǐng)求起始行)/(響應(yīng)起始行)status-line
請(qǐng)求報(bào)文解析狀態(tài)遷移
請(qǐng)求行:START -> METHOD -> SPACE1 -> URI -> SPACE2 -> REQUEST_VERSION
響應(yīng)報(bào)文解析狀態(tài)遷移
響應(yīng)行:START -> RESPONSE_VERSION -> SPACE1 -> STATUS -> SPACE2 -> REASON
HEADER 的狀態(tài)只有一種了,在jetty的老版本中還區(qū)分了HEADER_IN_NAM, HEADER_VALUE, HEADER_IN_VALUE等,9.4中都去除了。為了提高匹配效率,jetty使用了Trie樹(shù)快速匹配header頭。
static { CACHE.put(new HttpField(HttpHeader.CONNECTION,HttpHeaderValue.CLOSE)); CACHE.put(new HttpField(HttpHeader.CONNECTION,HttpHeaderValue.KEEP_ALIVE)); // 以下省略了很多了通用header頭
請(qǐng)求體:
CONTENT -> END,這種是普通的帶Content-Length頭的報(bào)文,HttpParser一直運(yùn)行CONTENT狀態(tài),直到最后ContentLength達(dá)到了指定的數(shù)量,則進(jìn)入END狀態(tài)
chunked分塊傳輸?shù)臄?shù)據(jù)
CHUNKED_CONTENT -> CHUNK_SIZE -> CHUNK -> CHUNK_END -> END
undertow是另一種web容器,它的處理方式與jetty有什么不同呢
狀態(tài)機(jī)種類不一樣了,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;
具體處理流程在HttpRequestParser抽象類中
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 // 快速處理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); } } // 處理path handlePath(buffer, currentState, builder); // 處理版本 if (failed) { handleHttpVersion(buffer, currentState, builder); handleAfterVersion(buffer, currentState); } // 處理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); }
與jetty不同的是對(duì)content的處理,在header處理完以后,將數(shù)據(jù)放到io.undertow.server.HttpServerExchange,然后根據(jù)類型,有不同的content讀取方式,比如處理固定長(zhǎng)度的,F(xiàn)ixedLengthStreamSourceConduit。
到此,相信大家對(duì)“web容器是怎么解析http報(bào)文的”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
本文名稱:web容器是怎么解析http報(bào)文的
文章鏈接:http://jinyejixie.com/article36/gceepg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、關(guān)鍵詞優(yōu)化、自適應(yīng)網(wǎng)站、網(wǎng)站內(nèi)鏈、微信小程序、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)