詳解http報文(2)-web容器是如何解析http報文的

摘要

詳解http報文一文中,詳細介紹了http報文的文本結構。那麼做爲服務端,web容器是如何解析http報文的呢?本文以jetty和undertow容器爲例,來解析web容器是如何處理http報文的。html

在前文中咱們從概覽中能夠了解到,http報文其實就是必定規則的字符串,那麼解析它們,就是解析字符串,看看是否知足http協議約定的規則。java

start-line: 起始行,描述請求或響應的基本信息

*( header-field CRLF ): 頭

CRLF

[message-body]: 消息body,實際傳輸的數據

複製代碼

jetty

如下代碼都是jetty9.4.12版本web

如何解析這麼長的字符串呢,jetty是經過狀態機來實現的。具體能夠看下org.eclipse.jetty.http.HttpParsebash

public enum State
    {
        START,
        METHOD,
        
![](https://user-gold-cdn.xitu.io/2019/10/9/16db0a55c99520eb?w=1202&h=630&f=png&s=101034),
        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種狀態,而後進行狀態間的流轉。在parseNext方法中分別對起始行 -> header -> body content分別解析eclipse

public boolean parseNext(ByteBuffer buffer)
    {
        try
        {
            // Start a request/response
            if (_state==State.START)
            {
                // 快速判斷
                if (quickStart(buffer))
                    return true;
            }

            // Request/response line 轉換
            if (_state.ordinal()>= State.START.ordinal() && _state.ordinal()<State.HEADER.ordinal())
            {
                if (parseLine(buffer))
                    return true;
            }

            // headers轉換
            if (_state== State.HEADER)
            {
                if (parseFields(buffer))
                    return true;
            }

            // content轉換
            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;
    }
複製代碼

總體流程

總體有三條路徑ide

  1. 開始 -> start-line -> header -> 結束
  2. 開始 -> start-line -> header -> content -> 結束
  3. 開始 -> start-line -> header -> chunk-content -> 結束

起始行

start-line = request-line(請求起始行)/(響應起始行)status-linepost

  1. 請求報文解析狀態遷移 請求行:START -> METHOD -> SPACE1 -> URI -> SPACE2 -> REQUEST_VERSIONui

  2. 響應報文解析狀態遷移 響應行:START -> RESPONSE_VERSION -> SPACE1 -> STATUS -> SPACE2 -> REASONspa

header 頭

HEADER 的狀態只有一種了,在jetty的老版本中還區分了HEADER_IN_NAM, HEADER_VALUE, HEADER_IN_VALUE等,9.4中都去除了。爲了提升匹配效率,jetty使用了Trie樹快速匹配header頭。.net

static
    {
        CACHE.put(new HttpField(HttpHeader.CONNECTION,HttpHeaderValue.CLOSE));
        CACHE.put(new HttpField(HttpHeader.CONNECTION,HttpHeaderValue.KEEP_ALIVE));
      // 如下省略了不少了通用header頭
複製代碼

content

請求體:

  1. CONTENT -> END,這種是普通的帶Content-Length頭的報文,HttpParser一直運行CONTENT狀態,直到最後ContentLength達到了指定的數量,則進入END狀態
  2. chunked分塊傳輸的數據 CHUNKED_CONTENT -> CHUNK_SIZE -> CHUNK -> CHUNK_END -> END

undertow

undertow是另外一種web容器,它的處理方式與jetty有什麼不一樣呢 狀態機種類不同了,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不一樣的是對content的處理,在header處理完之後,將數據放到io.undertow.server.HttpServerExchange,而後根據類型,有不一樣的content讀取方式,好比處理固定長度的,FixedLengthStreamSourceConduit

關注公衆號【方丈的寺院】,第一時間收到文章的更新,與方丈一塊兒開始技術修行之路

在這裏插入圖片描述

參考

www.blogjava.net/DLevin/arch…

www.ph0ly.com/2018/10/06/…

webtide.com/http-traile…

undertow.io/undertow-do…

相關文章
相關標籤/搜索