- 浏览: 204411 次
- 性别:
- 来自: 福建省
文章分类
最新评论
-
c929833623:
...
Mysql JDBC驱动源码分析(Statement,ResultSet的创建)四 -
pythonlord:
顶 很有帮助,感谢楼主,好人一生平安
Mysql JDBC驱动源码分析(加载驱动)一 -
kiaonly:
代码有错误,我戳。
解释器模式(Interpreter)---java与模式(例子) -
wyzxzws:
小鸟学习了!
JAVA编码问题记录 -
xiaotao.2010:
写的不错! 弱弱说一句 建议使用URL二次转码, 这样可以避免 ...
JAVA编码问题记录
一,经过以上文章(JIoEndpoint$Worker#run)执行,就成功把socket交给tomcat中的coyote包执行
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler#process(Socket socket)
public boolean process(Socket socket) { Http11Processor processor = recycledProcessors.poll(); try { if (processor == null) { //创建Http11Processor processor = createProcessor(); } if (processor instanceof ActionHook) { //标记process started为ture意为开始处理 ((ActionHook) processor).action(ActionCode.ACTION_START, null); } if (proto.secure && (proto.sslImplementation != null)) { //支持ssl processor.setSSLSupport (proto.sslImplementation.getSSLSupport(socket)); } else { processor.setSSLSupport(null); } //继续往下处理 processor.process(socket); return false; } catch(java.net.SocketException e) { // SocketExceptions are normal Http11Protocol.log.debug (sm.getString ("http11protocol.proto.socketexception.debug"), e); } catch (java.io.IOException e) { // IOExceptions are normal Http11Protocol.log.debug (sm.getString ("http11protocol.proto.ioexception.debug"), e); } // Future developers: if you discover any other // rare-but-nonfatal exceptions, catch them here, and log as // above. catch (Throwable e) { // any other exception or error is odd. Here we log it // with "ERROR" level, so it will show up even on // less-than-verbose logs. Http11Protocol.log.error (sm.getString("http11protocol.proto.error"), e); } finally { // if(proto.adapter != null) proto.adapter.recycle(); // processor.recycle(); if (processor instanceof ActionHook) {//设置started为false表示处理完毕 ((ActionHook) processor).action(ActionCode.ACTION_STOP, null); } recycledProcessors.offer(processor); } return false; }
在创建Http11Processor的时候,进行了一系列的对象的初始化如下:
ublic Http11Processor(int headerBufferSize, JIoEndpoint endpoint) { this.endpoint = endpoint; //org.apache.coyote.Request request = new Request(); //输入流 inputBuffer = new InternalInputBuffer(request, headerBufferSize); request.setInputBuffer(inputBuffer); //org.apache.coyote.Response response = new Response(); response.setHook(this); //输出流 outputBuffer = new InternalOutputBuffer(response, headerBufferSize); response.setOutputBuffer(outputBuffer); request.setResponse(response); initializeFilters(); // Cause loading of HexUtils int foo = HexUtils.DEC[0]; }
Http11Processor#process(Socket socket)
public void process(Socket socket) throws IOException { RequestInfo rp = request.getRequestProcessor(); rp.setStage(org.apache.coyote.Constants.STAGE_PARSE); // Set the remote address remoteAddr = null; remoteHost = null; localAddr = null; localName = null; remotePort = -1; localPort = -1; // Setting up the I/O this.socket = socket; //对scoket流进行设置处理 inputBuffer.setInputStream(socket.getInputStream()); outputBuffer.setOutputStream(socket.getOutputStream()); // Error flag error = false; keepAlive = true; // 连接的计数器 int keepAliveLeft = maxKeepAliveRequests; //超时设置 int soTimeout = socket.getSoTimeout(); int oldSoTimeout = soTimeout; // 当前JIoEndpoint中的已经使用的Worker与总Worker的比例 int threadRatio = (endpoint.getCurrentThreadsBusy() * 100) / endpoint.getMaxThreads(); // 如果使用中的Worker比例高于75%,只保持连接1次 if (threadRatio > 75) { keepAliveLeft = 1; } if (soTimeout != oldSoTimeout) { try { //设置进超时时限 socket.setSoTimeout(soTimeout); } catch (Throwable t) { log.debug(sm.getString("http11processor.socket.timeout"), t); error = true; } } //是否被保持着连接 boolean keptAlive = false; while (started && !error && keepAlive) { // Parsing the request header try { if (keptAlive) { if (keepAliveTimeout > 0) { socket.setSoTimeout(keepAliveTimeout); } else if (soTimeout > 0) { socket.setSoTimeout(soTimeout); } } //读取请求行 inputBuffer.parseRequestLine(); //设置请求开始时间 request.setStartTime(System.currentTimeMillis()); keptAlive = true; if (!disableUploadTimeout) { socket.setSoTimeout(timeout); } //解析头部文件 inputBuffer.parseHeaders(); } catch (IOException e) { error = true; break; } catch (Throwable t) { if (log.isDebugEnabled()) { log.debug(sm.getString("http11processor.header.parse"), t); } // 400 - Bad Request response.setStatus(400); error = true; } if (!error) { // Setting up filters, and parse some request headers rp.setStage(org.apache.coyote.Constants.STAGE_PREPARE); try { //开始准备请求,就是在里面把需要的信息设置进需要处理的对象inputbuffer outputbuffer,request,response等 prepareRequest(); } catch (Throwable t) { if (log.isDebugEnabled()) { log.debug(sm.getString("http11processor.request.prepare"), t); } // 400 - Internal Server Error response.setStatus(400); error = true; } } if (maxKeepAliveRequests > 0 && --keepAliveLeft == 0) keepAlive = false; // Process the request in the adapter if (!error) { try { rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE); // adapter是 org.apache.catalina.connector.CoyoteAdapter类的对象 //这里面还对coyote.Request/Response转换成 //org.apache.catalina.connector.Request/Response //这一步是请求的下一步执行 adapter.service(request, response); // Handle when the response was committed before a serious // error occurred. Throwing a ServletException should both // set the status to 500 and set the errorException. // If we fail here, then the response is likely already // committed, so we can't try and set headers. if(keepAlive && !error) { // Avoid checking twice. error = response.getErrorException() != null || statusDropsConnection(response.getStatus()); } } catch (InterruptedIOException e) { error = true; } catch (Throwable t) { log.error(sm.getString("http11processor.request.process"), t); // 500 - Internal Server Error response.setStatus(500); error = true; } } // Finish the handling of the request try { rp.setStage(org.apache.coyote.Constants.STAGE_ENDINPUT); // If we know we are closing the connection, don't drain input. // This way uploading a 100GB file doesn't tie up the thread // if the servlet has rejected it. if(error) inputBuffer.setSwallowInput(false); //结束请求 inputBuffer.endRequest(); } catch (IOException e) { error = true; } catch (Throwable t) { log.error(sm.getString("http11processor.request.finish"), t); // 500 - Internal Server Error response.setStatus(500); error = true; } try { rp.setStage(org.apache.coyote.Constants.STAGE_ENDOUTPUT); outputBuffer.endRequest(); } catch (IOException e) { error = true; } catch (Throwable t) { log.error(sm.getString("http11processor.response.finish"), t); error = true; } // If there was an error, make sure the request is counted as // and error, and update the statistics counter if (error) { response.setStatus(500); } request.updateCounters(); rp.setStage(org.apache.coyote.Constants.STAGE_KEEPALIVE); // Don't reset the param - we'll see it as ended. Next request // will reset it // thrA.setParam(null); // Next request inputBuffer.nextRequest(); outputBuffer.nextRequest(); } rp.setStage(org.apache.coyote.Constants.STAGE_ENDED); // Recycle //对流进行回收 inputBuffer.recycle(); outputBuffer.recycle(); this.socket = null; // Recycle ssl info sslSupport = null; }
接下去对这一步进行详解adapter.service(request, response);
org.apache.catalina.connector.CoyoteAdapter#service对org.apache.coyote.Request/org.apache.coyote.Response进行封装成org.apache.catalina.connector.Request/Response
/** * Service method. */ public void service(org.apache.coyote.Request req, org.apache.coyote.Response res) throws Exception { Request request = (Request) req.getNote(ADAPTER_NOTES); Response response = (Response) res.getNote(ADAPTER_NOTES); if (request == null) { 进行封装成connector的reqeust.. // Create objects request = (Request) connector.createRequest(); request.setCoyoteRequest(req); response = (Response) connector.createResponse(); response.setCoyoteResponse(res); // Link objects request.setResponse(response); response.setRequest(request); // Set as notes req.setNote(ADAPTER_NOTES, request); res.setNote(ADAPTER_NOTES, response); // Set query string encoding req.getParameters().setQueryStringEncoding (connector.getURIEncoding()); } if (connector.getXpoweredBy()) { response.addHeader("X-Powered-By", "Servlet/2.5"); } boolean comet = false; try { // Parse and set Catalina and configuration specific // request parameters req.getRequestProcessor().setWorkerThreadName(Thread.currentThread().getName()); //这里面有对sessionid的处理,以后详解 if (postParseRequest(req, request, res, response)) { // Calling the container //运用pipeline以及valve,使request/response在容器间处理,,,重点详解这一步 connector.getContainer().getPipeline().getFirst().invoke(request, response); if (request.isComet()) { if (!response.isClosed() && !response.isError()) { if (request.getAvailable() || (request.getContentLength() > 0 && (!request.isParametersParsed()))) { // Invoke a read event right away if there are available bytes if (event(req, res, SocketStatus.OPEN)) { comet = true; res.action(ActionCode.ACTION_COMET_BEGIN, null); } } else { comet = true; res.action(ActionCode.ACTION_COMET_BEGIN, null); } } else { // Clear the filter chain, as otherwise it will not be reset elsewhere // since this is a Comet request request.setFilterChain(null); } } } if (!comet) { response.finishResponse();//执行响应操作,返回给客户端 req.action(ActionCode.ACTION_POST_REQUEST , null); } } catch (IOException e) { ; } catch (Throwable t) { log.error(sm.getString("coyoteAdapter.service"), t); } finally { req.getRequestProcessor().setWorkerThreadName(null); // Recycle the wrapper request and response if (!comet) { request.recycle(); response.recycle(); } else { // Clear converters so that the minimum amount of memory // is used by this processor request.clearEncoders(); response.clearEncoders(); } } }
发表评论
-
Nginx1.1实现Resin4集群
2011-10-17 17:56 7214一,web服务器小论 以前的公司使用的web服务器是to ... -
Apache实现Tomcat集群
2010-06-08 20:14 1389一,配置介绍 1,linux 2,tomcat6. ... -
Tomcat源码---容器生命周期管理(Lifecycle)一
2010-03-31 11:12 4414一,tomcat中每一个容器 ... -
Tomcat源码---Session的分析一
2010-03-29 11:31 3795一,前面看了大致的tomcat的请求/响应,接下来的文章对to ... -
Tomcat源码---响应处理五
2010-03-25 16:01 1784一,响应工作我们应该从CoyoteAdapter#servic ... -
Tomcat源码---请求处理四(2)
2010-03-25 15:34 1582对以上的StandardWrapperValve#invok ... -
Tomcat源码---请求处理四(1)
2010-03-25 11:08 1617一,现在到了StandardWrapperValve# ... -
Tomcat源码---请求处理三
2010-03-25 10:39 1314一,这一章节主要讲request与response通过管道,阀 ... -
Tomcat源码---请求处理(接收,线程分配)一
2010-03-24 14:34 2787一,在以上文章中tomcat启动已经完毕,接着要做的是消息的请 ... -
Tomcat源码---容器启动六(4)
2010-03-24 11:14 1399现在容器已经启动成功的StanderService#start ... -
Tomcat源码---容器启动六(3)
2010-03-24 10:48 2251一,容器已经启动到部暑文件(webapps),接下去是Stan ... -
Tomcat源码---容器启动六(2)
2010-03-23 16:42 1457super.start()--->org.apach ... -
Tomcat源码---容器启动六(1)
2010-03-22 16:02 1455一,完成了以上的初始化工作,现在进行容器的启动工作由 ... -
Tomcat源码---初始化容器五
2010-03-22 15:03 1775一,上面文章完成了对server.xml载入以及解析,现在主要 ... -
Tomcat源码---载入相应的资源及解析四(2)
2010-03-19 16:47 1468一,根据以上文章所讲的对server.xml的解析作下简单的分 ... -
Tomat源码---载入相应的资源及解析四(1)
2010-03-19 16:22 1366一,进行了以上的类包加载后,现在主要的工作是载入server. ... -
Tomcat源码---启动.初始化(加载类包)分析三
2010-03-19 15:37 2194一,启动 Tomcat是从org.apache.catali ... -
Tomcat的简单了解二
2010-03-19 14:40 1798在查看源代码时,在网上找了一系列的文章,在些作详解: ... -
Tomcat中server.xml的配置分析一
2010-03-19 14:07 1781最近查看了tomcat6的源代码,了解了里面的大概 ...
相关推荐
apache-tomcat-9.0.8 其中有完整的源码 bin目录下有你所想要的所有完整的
3-5Tomcat响应请求源码与nio处理请求源码实现.mp4
从零手写Tomcat【源码】【abl-tomcat-004】【基础完善-处理动态资源请求】 文章地址:https://blog.csdn.net/m0_37969197/article/details/124096403 目录地址:...
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以这样认为,当在一台...
Tomcat启动流程分析 组件的生命周期管理 用Lifecycle管理启动、停止、关闭 Lifecycle接口预览 ...根据请求地址匹配正确的容器进行处理 将响应返回客户端 Container设计 Servlet容器的实现。
从零手写Tomcat【源码】【abl-tomcat-005】【添加多线程,处理请求】 文章地址:https://blog.csdn.net/m0_37969197/article/details/124098639 目录地址:...
在这里,会话被实现为非粘性的(意味着,每个请求都可以转到集群中的任何服务器,这与Apache提供的Tomcat集群设置不同。) 请求会话将立即存储到Redis中(会话属性必须是可序列化的),以供其他服务器使用。 当...
3-8Tomcat请求容器中的处理与启动过程源码实现(1).mp4
3-8Tomcat请求容器中的处理与启动过程源码实现(2).mp4
Tomcat初始化流程分析,Tomcat启动流程分析 Tomcat处理一次请求过程分析 servlet初始化流程
对 NIO 模式,请求的流程描述的很详细。值得去仔细的研究。
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以...最新的源码供研究
1.web.xml内容修改,添加servlet标签 2.添加Servlet接口与实现 3.解析web.xml 4.修改启动时逻辑,扫描全部servlet 5.Request、Response 说明 6.动态处理请求,通过反射创建Servlet 7.浏览器测试
NULL 博文链接:https://liudeh-009.iteye.com/blog/1563948
Tomcat是一款servlet服务器,它能够接受请求,处理请求并给出响应。
requestProcess.pdf-Tomcat请求处理流程序列图(2014.10.28-2014.10.29) 架构/-Tomcat服务器的架构结构和设计(2014.10.29-2014.10.29) server.xml-Tomcat容器配置文件(2014.10.29-) config /-Tomcat配置引用...
资源是关于tomcat深入剖析的源码. 本书深入剖析Tomcat 4和Tomcat 5中的每个组件,并揭示其内部工作原理。通过学习本书,你将可以自行开发Tomcat组件,或者扩展已有的组件。 Tomcat是目前比较流行的Web服务器之一。...
java雷电飞机源码 http服务&ajax编程,请求和响应信息解析,json数据格式 1. 服务器 通俗的讲,能够提供某种服务的机器(计算机)称为服务器 1.2 服务器软件 使计算机能提供某种服务的应用软件,称之为服务器软件. ...
https原理及tomcat配置https方法;SSL加密传输;请求路径https://...;客户端需安装证书...
NULL 博文链接:https://yjhexy.iteye.com/blog/661491