一、概述
个人是个实践型人员,所以打算看着jetty源码,从头开始组装Jetty。
首先从github.com里找到jetty-project项目,用git下载源码,本文以9.3.x为例。
首先Jetty作为一个web server,必然需要支持HTTP。
查看Jetty-http项目下http包下一共有下列几个类:
接口:
HttpContent
HttpFieldPreEncoder
HttpParser.HttpHandler
HttpParser.RequestHandler
HttpParser.ResponseHandler
HttpTokens
类:
DateGenerator
DateParser
HttpPostHttpField
Http1FieldPreEncoder
HttpCookie
HttpField
HttpField.IntValueHttpField
HttpField.LongValueHttpField
HttpFields
HttpGenerator
HttpParser
HttpStatus
HttpURI
MetaData
MetaData.Request
MetaData.Response
MimeTypes
PathMap
PathMap.MappedEntry
PathMap.PathSet
PreEncodedHttpField
ResourceHttpContent
枚举类:
HttpGenerator.Result
HttpGenerator.State
HttpHeader
HttpHeaderValue
HttpMethod
HttpParser.State
HttpScheme
HttpStatus.Code
HttpTokens.EndOfContent
HttpVersion
MimeTypes.Type
异常类:
BadMessageException
上述类里,需要关注的有下面几个基础类,分别进行解说。
Http协议由请求消息和响应消息组成,其中请求消息由请求行、首部行(消息报头)、实体主体组成;而响应消息由状态行、首部行、实体主体组成。围绕这些我们需要研究的类有HttpContent、HttpCookie、HttpField、HttpFields、HttpStatus等。
二、类分析
2.1 接口
public interface HttpContent
{
HttpField getContentType();
String getContentTypeValue();
String getCharacterEncoding();
Type getMimeType();
HttpField getContentEncoding();
String getContentEncodingValue();
HttpField getContentLength();
long getContentLengthValue();
HttpField getLastModified();
String getLastModifiedValue();
HttpField getETag();
String getETagValue();
ByteBuffer getIndirectBuffer();
ByteBuffer getDirectBuffer();
Resource getResource();
InputStream getInputStream() throws IOException;
ReadableByteChannel getReadableByteChannel() throws IOException;
void release();
HttpContent getGzipContent();
public interface Factory
{
HttpContent getContent(String path) throws IOException;
}
}
HttpContent接口定义了一系列实体主体部分的操作。
并且本接口涉及到HttpField,查看HttpField源码。
public class HttpField
{
private final static String __zeroquality="q=0";
private final HttpHeader _header;
private final String _name;
private final String _value;
// cached hashcode for case insensitive name
private int hash = 0;
public HttpField(HttpHeader header, String name, String value)
{
_header = header;
_name = name;
_value = value;
}
public HttpField(HttpHeader header, String value)
{
this(header,header.asString(),value);
}
public HttpField(HttpHeader header, HttpHeaderValue value)
{
this(header,header.asString(),value.asString());
}
public HttpField(String name, String value)
{
this(HttpHeader.CACHE.get(name),name,value);
}
//header、name的getter方法,value的int、long、String、String[]getter方法。
/* value是否包含search----------------------------- */
/** Look for a value in a possible multi valued field
* @param search Values to search for
* @return True iff the value is contained in the field value entirely or
* as an element of a quoted comma separated list. List element parameters (eg qualities) are ignored,
* except if they are q=0, in which case the item itself is ignored.
*/
public boolean contains(String search){
...
}
@Override
public String toString()
{
String v=getValue();
return getName() + ": " + (v==null?"":v);
}
//header是否和field同名
public boolean isSameName(HttpField field)
{
if (field==null)
return false;
if (field==this)
return true;
if (_header!=null && _header==field.getHeader())
return true;
if (_name.equalsIgnoreCase(field.getName()))
return true;
return false;
}
private int nameHashCode()
{
int h = this.hash;
int len = _name.length();
if (h == 0 && len > 0)
{
for (int i = 0; i < len; i++)
{
// simple case insensitive hash
char c = _name.charAt(i);
// assuming us-ascii (per last paragraph on http://tools.ietf.org/html/rfc7230#section-3.2.4)
if ((c >= 'a' && c <= 'z'))
c -= 0x20;
h = 31 * h + c;
}
this.hash = h;
}
return h;
}
@Override
public int hashCode()
{
if (_header==null)
return _value.hashCode() ^ nameHashCode();
return _value.hashCode() ^ _header.hashCode();
}
@Override
public boolean equals(Object o)
{
if (o==this)
return true;
if (!(o instanceof HttpField))
return false;
HttpField field=(HttpField)o;
if (_header!=field.getHeader())
return false;
if (!_name.equalsIgnoreCase(field.getName()))
return false;
if (_value==null && field.getValue()!=null)
return false;
return Objects.equals(_value,field.getValue());
}
//内部类
public static class IntValueHttpField extends HttpField
{
private final int _int;
//构造方法和_int的getter
}
public static class LongValueHttpField extends HttpField
{
private final long _long;
//与IntValueHttpField相同
}
}
HttpField包含header、name和value属性。可以看成一个有header的Map类。
HttpHeader是个枚举类,主要由General Fields、Entity Fields、Request Fields、Response Fields、Other Fields和HTTP2 Fields几块内容组成。我们在查看chrome的network里的Headers页签时能看到的内容可以查看到这General、Request和Response几块内容,而Other和HTTP2这里不讨论,剩下就是Entity部分其实也在Request里体现了。这就是一个文件头。
HttpFields是HttpField的集合类。
三、小结
Http协议请求消息由请求行(HttpURI)、首部行(HttpHeader)、实体主体(HttpContent)组成;而响应消息由状态行(HttpStatus)、首部行(HttpHeader)、实体主体(HttpContent)组成。