How Tomcat Works 读书笔记(第三章)
现在先让我们来看一下这章我们要实现的容器的结构吧。其实我们在看了上面的类图之后我们也很容易来理解它,各个类的职责还是没有怎么变。而且变的更单一,反而比较容易理解。 HttpConnector还是监听在端口8080上,然后每次有一个客户端请求来临之时,都会创建一个HttpProcessor来处理客户端的请求,不过这里我们也可以看到一个问题,虽然HttpConnector是一个单独的线程,但是如果HttpProcessor的process方法还是会阻塞的,真实的Http服务器估计肯定不会这么做了,下一章我们可以看到解决方案。
HttpProcessor用于负责创建Request和Response对象,然后还是会根据请求资源的类型调用合适的Processor来处理,这个就是ServletProcessor和StaticResourceProcessor.
HttpRequestLine很容易理解,它代表Http请求的第一行,有method、uri和protocol,为了效率,它使用了char[]来构造这些串了。
HttpHeader也很容易,就是Http请求头,我们比较常见的就是content-length,user-agent等。
StringManager是tomcat里面的一个辅助类,用来处理国际化信息的。
SocketInputStream继承与InputStream,用来读取请求头和Http头。
HttpRequest是继承与HttServletRequest,我们对这个都比较熟悉,一般我们的参数,cookie,session的信息都是与它有关的。
HttpResponse是继承于HttpServletResponse,用于返回用户的请求。
我感觉SocketInputStream这个类感觉有点复杂,我看了一下,但不是很特别明白,我贴出来,希望大家多多指教:
package ex03.pyrmont.connector.http;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.catalina.util.StringManager;
public class SocketInputStream extends InputStream {
private final static byte CR = '\r';
private final static byte LF = '\n';
private final static byte SP = ' ';
private final static byte HT = '\t';
private final static byte COLON = ':';
private final static byte LC_OFFSET = 'A' - 'a';
protected static StringManager sm = StringManager
.getManager(Constants.Package);
private int count;
protected byte[] buffer;
private int pos;
protected InputStream is;
public SocketInputStream(InputStream is, int bufferLen) {
this.is = is;
buffer = new byte;
}
public void readRequestLine(HttpRequestLine requestLine)
throws IOException {
if (requestLine.methodEnd != 0) {
requestLine.recycle();
}
int chr = 0;
// 这个是用于跳过开始部分的CR和LF,这个我们可以用测试client来进行测试
do {
try {
chr = read();
} catch (IOException e) {
e.printStackTrace();
}
} while (chr == CR || chr == LF);
if (chr == -1) {
throw new EOFException(sm.getString("requestStream.readline.error"));
}
pos--;
int maxRead = requestLine.method.length;
int readStart = pos;
int readCount = 0;
boolean space = true;
while (!space) {
if (readCount >= maxRead) {
if (2 * maxRead <= HttpRequestLine.MAX_METHOD) {
// 创建一个新的缓冲区,把原来读取的拷贝到新的缓冲区里面
char[] newBuf = new char;
System.arraycopy(requestLine.method, 0, buffer, 0, maxRead);
requestLine.method = newBuf;
maxRead = requestLine.method.length;
} else {
throw new EOFException(sm
.getString("requestStream.readline.toolong"));
}
}
// 这个是读到了内部buffer结束的位置,我们应该读一次
// 其实我们这里pos也可以不设置成0,在fill方法里面会对它进行初始化
if (pos >= count) {
int val = read();
if (val == -1) {
throw new IOException(sm
.getString("requestStream.readline.error"));
}
pos = 0;
readStart = 0;
}
// 如果读到空格了,我们设置space为空
if (buffer == SP) {
space = true;
}
// 进行赋值,在读下一个,当前位置也应该跟着变化
requestLine.method = (char) buffer;
readCount++;
pos++;
}
requestLine.methodEnd = readCount - 1;
maxRead = requestLine.uri.length;
readStart = pos;
readCount = 0;
space = false;
boolean eol = false;
while (!space) {
if (readCount >= maxRead) {
if (2 * maxRead <= HttpRequestLine.MAX_URI) {
char[] newBuf = new char;
System.arraycopy(requestLine.uri, 0, newBuf, 0, maxRead);
requestLine.uri = newBuf;
maxRead = requestLine.uri.length;
} else {
throw new EOFException(sm
.getString("requestStream.readline.toolong"));
}
}
if (pos >= count) {
int val = read();
if (val == -1) {
throw new IOException(sm
.getString("requestStream.readline.error"));
}
pos = 0;
readStart = 0;
}
if (buffer == SP) {
space = true;
} else if (buffer == CR || buffer == LF) {
// HTTP 0.9 格式的请求
eol = true;
space = true;
}
requestLine.uri = (char) buffer;
pos++;
readCount++;
}
requestLine.uriEnd = readCount - 1;
while (!eol) {
if (readCount >= maxRead) {
if (2 * maxRead <= HttpRequestLine.MAX_PROTOCOL) {
char[] newBuf = new char;
System.arraycopy(requestLine.protocol, 0, newBuf, 0,
maxRead);
requestLine.protocol = newBuf;
maxRead = requestLine.protocol.length;
} else {
throw new EOFException(sm
.getString("requestStream.readline.toolong"));
}
}
if(pos >= count){
int val = read();
if(val == -1){
throw new IOException(sm
.getString("requestStream.readline.error"));
}
pos = 0;
readStart = 0;
}
if(buffer == CR){
} else if(buffer == LF){
eol = true;
} else {
requestLine.protocol = (char)buffer;
readCount++;
}
pos++;
}
requestLine.protocolEnd = readCount;
}
public List<HttpHeader> readHeaders() {
// TODO Auto-generated method stub
return null;
}
/**
* 返回pos位置的一个字节的数据,如果pos和count相同时,需要填充
* 到最后的时候在fill之后,pos会和count相等,所以会返回-1用来指示 已经都到最后了
*/
@Override
public int read() throws IOException {
if (pos >= count) {
fill();
if (pos >= count) {
return -1;
}
}
return buffer & 0xff;
}
/**
* 这个方法用来一次性的读取buffer.length个字节的数据到缓冲区
*
* @throws IOException
*/
protected void fill() throws IOException {
pos = 0;
count = 0;
int nRead = is.read(buffer, 0, buffer.length);
if (nRead > 0) {
count = nRead;
}
}
}
页:
[1]