`
famoushz
  • 浏览: 2877237 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

tomcat 代码分析 为获取HTTP head body 解析

阅读更多
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 代码
 
  1. /** 
  2.  * Process pipelined HTTP requests using the specified input and output 
  3.  * streams. 
  4.  * 
  5.  * @throws IOException error during an I/O operation 
  6.  */  
  7. public boolean process(long socket)  
  8.     throws IOException {  
  9.     ThreadWithAttributes thrA=  
  10.             (ThreadWithAttributes)Thread.currentThread();  
  11.     RequestInfo rp = request.getRequestProcessor();  
  12.     thrA.setCurrentStage(endpoint, "parsing http request");  
  13.     rp.setStage(org.apache.coyote.Constants.STAGE_PARSE);  
  14.   
  15.     // Set the remote address  
  16.     remoteAddr = null;  
  17.     remoteHost = null;  
  18.     localAddr = null;  
  19.     localName = null;  
  20.     remotePort = -1;  
  21.     localPort = -1;  
  22.   
  23.     // Setting up the socket  
  24.     this.socket = socket;  
  25.     inputBuffer.setSocket(socket);  
  26.     outputBuffer.setSocket(socket);  
  27.   
  28.     // Error flag  
  29.     error = false;  
  30.     keepAlive = true;  
  31.   
  32.     int keepAliveLeft = maxKeepAliveRequests;  
  33.     long soTimeout = endpoint.getSoTimeout();  
  34.       
  35.     int limit = 0;  
  36.     if (endpoint.getFirstReadTimeout() > 0 || endpoint.getFirstReadTimeout() < -1) {  
  37.         limit = endpoint.getMaxThreads() / 2;  
  38.     }  
  39.   
  40.     boolean keptAlive = false;  
  41.     boolean openSocket = false;  
  42.   
  43.     while (started && !error && keepAlive) {  
  44.   
  45.         // Parsing the request header  
  46.         try {  
  47.             if( !disableUploadTimeout && keptAlive && soTimeout > 0 ) {  
  48.                 Socket.timeoutSet(socket, soTimeout * 1000);  
  49.             }  
  50.             if (!inputBuffer.parseRequestLine  
  51.                     (keptAlive && (endpoint.getCurrentThreadsBusy() > limit))) {  
  52.                 // This means that no data is available right now  
  53.                 // (long keepalive), so that the processor should be recycled  
  54.                 // and the method should return true  
  55.                 openSocket = true;  
  56.                 // Add the socket to the poller  
  57.                 endpoint.getPoller().add(socket);  
  58.                 break;  
  59.             }  
  60.             request.setStartTime(System.currentTimeMillis());  
  61.             thrA.setParam(endpoint, request.requestURI());  
  62.             keptAlive = true;  
  63.             if (!disableUploadTimeout) {  
  64.                 Socket.timeoutSet(socket, timeout * 1000);  
  65.             }  
  66.             inputBuffer.parseHeaders();  
  67.         } catch (IOException e) {  
  68.             error = true;  
  69.             break;  
  70.         } catch (Throwable t) {  
  71.             if (log.isDebugEnabled()) {  
  72.                 log.debug(sm.getString("http11processor.header.parse"), t);  
  73.             }  
  74.             // 400 - Bad Request  
  75.             response.setStatus(400);  
  76.             error = true;  
  77.         }  
  78.   
  79.         // Setting up filters, and parse some request headers  
  80.         thrA.setCurrentStage(endpoint, "prepareRequest");  
  81.         rp.setStage(org.apache.coyote.Constants.STAGE_PREPARE);  
  82.         try {  
  83.             prepareRequest();  
  84.         } catch (Throwable t) {  
  85.             if (log.isDebugEnabled()) {  
  86.                 log.debug(sm.getString("http11processor.request.prepare"), t);  
  87.             }  
  88.             // 400 - Internal Server Error  
  89.             response.setStatus(400);  
  90.             error = true;  
  91.         }  
  92.   
  93.         if (maxKeepAliveRequests > 0 && --keepAliveLeft == 0)  
  94.             keepAlive = false;  
  95.   
  96.         // Process the request in the adapter  
  97.         if (!error) {  
  98.             try {  
  99.                 thrA.setCurrentStage(endpoint, "service");  
  100.                 rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);  
  101.                 adapter.service(request, response);  
  102.                 // Handle when the response was committed before a serious  
  103.                 // error occurred.  Throwing a ServletException should both  
  104.                 // set the status to 500 and set the errorException.  
  105.                 // If we fail here, then the response is likely already  
  106.                 // committed, so we can't try and set headers.  
  107.                 if(keepAlive && !error) { // Avoid checking twice.  
  108.                     error = response.getErrorException() != null ||  
  109.                             statusDropsConnection(response.getStatus());  
  110.                 }  
  111.   
  112.             } catch (InterruptedIOException e) {  
  113.                 error = true;  
  114.             } catch (Throwable t) {  
  115.                 log.error(sm.getString("http11processor.request.process"), t);  
  116.                 // 500 - Internal Server Error  
  117.                 response.setStatus(500);  
  118.                 error = true;  
  119.             }  
  120.         }  
  121.   
  122.         // Finish the handling of the request  
  123.         try {  
  124.             thrA.setCurrentStage(endpoint, "endRequestIB");  
  125.             rp.setStage(org.apache.coyote.Constants.STAGE_ENDINPUT);  
  126.             inputBuffer.endRequest();  
  127.         } catch (IOException e) {  
  128.             error = true;  
  129.         } catch (Throwable t) {  
  130.             log.error(sm.getString("http11processor.request.finish"), t);  
  131.             // 500 - Internal Server Error  
  132.             response.setStatus(500);  
  133.             error = true;  
  134.         }  
  135.         try {  
  136.             thrA.setCurrentStage(endpoint, "endRequestOB");  
  137.             rp.setStage(org.apache.coyote.Constants.STAGE_ENDOUTPUT);  
  138.             outputBuffer.endRequest();  
  139.         } catch (IOException e) {  
  140.             error = true;  
  141.         } catch (Throwable t) {  
  142.             log.error(sm.getString("http11processor.response.finish"), t);  
  143.             error = true;  
  144.         }  
  145.   
  146.         // If there was an error, make sure the request is counted as  
  147.         // and error, and update the statistics counter  
  148.         if (error) {  
  149.             response.setStatus(500);  
  150.         }  
  151.         request.updateCounters();  
  152.   
  153.         thrA.setCurrentStage(endpoint, "ended");  
  154.         rp.setStage(org.apache.coyote.Constants.STAGE_KEEPALIVE);  
  155.   
  156.         // Don't reset the param - we'll see it as ended. Next request  
  157.         // will reset it  
  158.         // thrA.setParam(null);  
  159.         // Next request  
  160.         inputBuffer.nextRequest();  
  161.         outputBuffer.nextRequest();  
  162.   
  163.         // Do sendfile as needed: add socket to sendfile and end  
  164.         if (sendfileData != null) {  
  165.             sendfileData.socket = socket;  
  166.             sendfileData.keepAlive = keepAlive;  
  167.             if (!endpoint.getSendfile().add(sendfileData)) {  
  168.                 openSocket = true;  
  169.                 break;  
  170.             }  
  171.         }  
  172.           
  173.     }  
  174.   
  175.     rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);  
  176.   
  177.     // Recycle  
  178.     inputBuffer.recycle();  
  179.     outputBuffer.recycle();  
  180.     this.socket = 0;  
  181.   
  182.     return openSocket;  
  183.       
  184. }  
分享到:
评论
1 楼 毕竟红尘 2008-04-21  
不错啊
最近我也要解析post消息
也想在tomcat里面挖宝

相关推荐

Global site tag (gtag.js) - Google Analytics