在详解http报文一文中,详细介绍了http报文的文本结构。那么做为服务端,web容器是如何解析http报文的呢?本文以jetty和undertow容器为例,来解析web容器是如何处理http报文的。html
在前文中咱们从概览中能够了解到,http报文其实就是必定规则的字符串,那么解析它们,就是解析字符串,看看是否知足http协议约定的规则。java
start-line: 起始行,描述请求或响应的基本信息
*( header-field CRLF ): 头
CRLF
[message-body]: 消息body,实际传输的数据
复制代码
如下代码都是jetty9.4.12版本web
如何解析这么长的字符串呢,jetty是经过状态机来实现的。具体能够看下org.eclipse.jetty.http.HttpParse
类bash
public enum State
{
START,
METHOD,
,
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
start-line = request-line(请求起始行)/(响应起始行)status-linepost
请求报文解析状态迁移 请求行:START -> METHOD -> SPACE1 -> URI -> SPACE2 -> REQUEST_VERSIONui
响应报文解析状态迁移 响应行:START -> RESPONSE_VERSION -> SPACE1 -> STATUS -> SPACE2 -> REASONspa
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头
复制代码
请求体:
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
。
关注公众号【方丈的寺院】,第一时间收到文章的更新,与方丈一块儿开始技术修行之路