设为首页 收藏本站
查看: 538|回复: 0

[经验分享] How Tomcat Works 读书笔记(第三章)

[复制链接]

尚未签到

发表于 2017-2-1 14:18:01 | 显示全部楼层 |阅读模式
现在先让我们来看一下这章我们要实现的容器的结构吧。

DSC0000.jpeg

     其实我们在看了上面的类图之后我们也很容易来理解它,各个类的职责还是没有怎么变。而且变的更单一,反而比较容易理解。 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[bufferLen];
}
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[2 * maxRead];
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[pos] == SP) {
space = true;
}
// 进行赋值,在读下一个,当前位置也应该跟着变化
requestLine.method[readCount] = (char) buffer[pos];
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[2 * maxRead];
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[pos] == SP) {
space = true;
} else if (buffer[pos] == CR || buffer[pos] == LF) {
// HTTP 0.9 格式的请求
eol = true;
space = true;
}
requestLine.uri[readCount] = (char) buffer[pos];
pos++;
readCount++;
}
requestLine.uriEnd = readCount - 1;
while (!eol) {
if (readCount >= maxRead) {
if (2 * maxRead <= HttpRequestLine.MAX_PROTOCOL) {
char[] newBuf = new char[2 * maxRead];
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[pos] == CR){
} else if(buffer[pos] == LF){
eol = true;
} else {
requestLine.protocol[readCount] = (char)buffer[pos];
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[pos++] & 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-336204-1-1.html 上篇帖子: 深入Tomcat 下Jsp乱码处理方法 下篇帖子: Tomcat中Context是什么用的
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表