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

[经验分享] tomcat 代码分析 为获取HTTP head body 解析

[复制链接]

尚未签到

发表于 2017-2-7 06:30:44 | 显示全部楼层 |阅读模式
Http11Processor class是获得一个Http请求后就由这个class来进行处理,处理函数当然就是 process,然后将获得的inputstream 传递给 InternalInputBuffer class ,这个class 中存放了 所
信息等,InternalInputBuffer 将各个head信息分解后会 传递给 Request 对象,之后我们程序中的想要获得某个页面的信息就可以从 request对象中获得了。

J2ee 只是规定了 HttpServletRequest 所需要获得的一些配置参数,本来还想从j2ee5 sdk中获得 http head body的实现,搞了半天全是interface ,真正的实现是 放在 各个不同容器当中的,所以resin和tomcat或者其他web container 都可能不同。好了,
接下去把其中的宝藏挖出来把。

完成WVBAII项目的 server,汗就是为了不每次http请求都response,费那么经,要我写个 server。

有必要提一下实现InputBuffer 接口的class:
InternalAprInputBuffer 其方法
parseRequestLine()
parseHeader()
...
对HTTP Head 真正的进行了解析。


java 代码
 

  • /** 
  •  * Process pipelined HTTP requests using the specified input and output 
  •  * streams. 
  •  * 
  •  * @throws IOException error during an I/O operation 
  •  */  
  • public boolean process(long socket)  
  •     throws IOException {  
  •     ThreadWithAttributes thrA=  
  •             (ThreadWithAttributes)Thread.currentThread();  
  •     RequestInfo rp = request.getRequestProcessor();  
  •     thrA.setCurrentStage(endpoint, "parsing http request");  
  •     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 socket  
  •     this.socket = socket;  
  •     inputBuffer.setSocket(socket);  
  •     outputBuffer.setSocket(socket);  
  •   
  •     // Error flag  
  •     error = false;  
  •     keepAlive = true;  
  •   
  •     int keepAliveLeft = maxKeepAliveRequests;  
  •     long soTimeout = endpoint.getSoTimeout();  
  •       
  •     int limit = 0;  
  •     if (endpoint.getFirstReadTimeout() > 0 || endpoint.getFirstReadTimeout() < -1) {  
  •         limit = endpoint.getMaxThreads() / 2;  
  •     }  
  •   
  •     boolean keptAlive = false;  
  •     boolean openSocket = false;  
  •   
  •     while (started && !error && keepAlive) {  
  •   
  •         // Parsing the request header  
  •         try {  
  •             if( !disableUploadTimeout && keptAlive && soTimeout > 0 ) {  
  •                 Socket.timeoutSet(socket, soTimeout * 1000);  
  •             }  
  •             if (!inputBuffer.parseRequestLine  
  •                     (keptAlive && (endpoint.getCurrentThreadsBusy() > limit))) {  
  •                 // This means that no data is available right now  
  •                 // (long keepalive), so that the processor should be recycled  
  •                 // and the method should return true  
  •                 openSocket = true;  
  •                 // Add the socket to the poller  
  •                 endpoint.getPoller().add(socket);  
  •                 break;  
  •             }  
  •             request.setStartTime(System.currentTimeMillis());  
  •             thrA.setParam(endpoint, request.requestURI());  
  •             keptAlive = true;  
  •             if (!disableUploadTimeout) {  
  •                 Socket.timeoutSet(socket, timeout * 1000);  
  •             }  
  •             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;  
  •         }  
  •   
  •         // Setting up filters, and parse some request headers  
  •         thrA.setCurrentStage(endpoint, "prepareRequest");  
  •         rp.setStage(org.apache.coyote.Constants.STAGE_PREPARE);  
  •         try {  
  •             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 {  
  •                 thrA.setCurrentStage(endpoint, "service");  
  •                 rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);  
  •                 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 {  
  •             thrA.setCurrentStage(endpoint, "endRequestIB");  
  •             rp.setStage(org.apache.coyote.Constants.STAGE_ENDINPUT);  
  •             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 {  
  •             thrA.setCurrentStage(endpoint, "endRequestOB");  
  •             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();  
  •   
  •         thrA.setCurrentStage(endpoint, "ended");  
  •         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();  
  •   
  •         // Do sendfile as needed: add socket to sendfile and end  
  •         if (sendfileData != null) {  
  •             sendfileData.socket = socket;  
  •             sendfileData.keepAlive = keepAlive;  
  •             if (!endpoint.getSendfile().add(sendfileData)) {  
  •                 openSocket = true;  
  •                 break;  
  •             }  
  •         }  
  •           
  •     }  
  •   
  •     rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);  
  •   
  •     // Recycle  
  •     inputBuffer.recycle();  
  •     outputBuffer.recycle();  
  •     this.socket = 0;  
  •   
  •     return openSocket;  
  •       
  • }  

运维网声明 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-338457-1-1.html 上篇帖子: ubuntu9.10 eclipse 配置tomcat出现的问题"It is missing expected file or folder lib/jaspe 下篇帖子: run tomcat 时报org.objectweb.asm.ClassVisitor.visit异常
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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