📄 httpconnection.java
字号:
/* ------------------------------------------------------------ */ public boolean isResponseCommitted() { return _generator.isCommitted(); } /* ------------------------------------------------------------ */ public void handle() throws IOException { // Loop while more in buffer boolean more_in_buffer = true; // assume true until proven otherwise int no_progress = 0; while (more_in_buffer) { try { synchronized (this) { if (_handling) throw new IllegalStateException(); // TODO delete this // check _handling = true; } setCurrentConnection(this); long io = 0; Continuation continuation = _request.getContinuation(); if (continuation != null && continuation.isPending()) { Log.debug("resume continuation {}",continuation); if (_request.getMethod() == null) throw new IllegalStateException(); handleRequest(); } else { // If we are not ended then parse available if (!_parser.isComplete()) io = _parser.parseAvailable(); // Do we have more generating to do? // Loop here because some writes may take multiple steps and // we need to flush them all before potentially blocking in // the // next loop. while (_generator.isCommitted() && !_generator.isComplete()) { long written = _generator.flush(); io += written; if (written <= 0) break; if (_endp.isBufferingOutput()) _endp.flush(); } // Flush buffers if (_endp.isBufferingOutput()) { _endp.flush(); if (!_endp.isBufferingOutput()) no_progress = 0; } if (io > 0) no_progress = 0; else if (no_progress++ >= 2) return; } } catch (HttpException e) { if (Log.isDebugEnabled()) { Log.debug("uri=" + _uri); Log.debug("fields=" + _requestFields); Log.debug(e); } _generator.sendError(e.getStatus(),e.getReason(),null,true); _parser.reset(true); _endp.close(); throw e; } finally { setCurrentConnection(null); more_in_buffer = _parser.isMoreInBuffer() || _endp.isBufferingInput(); synchronized (this) { _handling = false; if (_destroy) { destroy(); return; } } if (_parser.isComplete() && _generator.isComplete() && !_endp.isBufferingOutput()) { if (!_generator.isPersistent()) { _parser.reset(true); more_in_buffer = false; } reset(!more_in_buffer); no_progress = 0; } Continuation continuation = _request.getContinuation(); if (continuation != null && continuation.isPending()) { break; } else if (_generator.isCommitted() && !_generator.isComplete() && _endp instanceof SelectChannelEndPoint) // TODO // remove // SelectChannel // dependency ((SelectChannelEndPoint)_endp).setWritable(false); } } } /* ------------------------------------------------------------ */ public void reset(boolean returnBuffers) { _parser.reset(returnBuffers); // TODO maybe only release when low on // resources _requestFields.clear(); _request.recycle(); _generator.reset(returnBuffers); // TODO maybe only release when low on // resources _responseFields.clear(); _response.recycle(); _uri.clear(); } /* ------------------------------------------------------------ */ protected void handleRequest() throws IOException { if (_server.isRunning()) { boolean retrying = false; boolean error = false; String threadName = null; try { String info = URIUtil.canonicalPath(_uri.getDecodedPath()); if (info == null) throw new HttpException(400); _request.setPathInfo(info); if (_out != null) _out.reopen(); if (Log.isDebugEnabled()) { threadName = Thread.currentThread().getName(); Thread.currentThread().setName(threadName + " - " + _uri); } _connector.customize(_endp,_request); _server.handle(this); } catch (RetryRequest r) { if (Log.isDebugEnabled()) Log.ignore(r); retrying = true; } catch (EofException e) { Log.ignore(e); error = true; } catch (HttpException e) { Log.debug(e); _request.setHandled(true); _response.sendError(e.getStatus(),e.getReason()); error = true; } catch (Exception e) { Log.warn(e); _request.setHandled(true); _generator.sendError(500,null,null,true); error = true; } catch (Error e) { Log.warn(e); _request.setHandled(true); _generator.sendError(500,null,null,true); error = true; } finally { if (threadName != null) Thread.currentThread().setName(threadName); if (!retrying) { if (_request.getContinuation() != null) { Log.debug("continuation still pending {}"); _request.getContinuation().reset(); } if (_endp.isOpen()) { if (_generator.isPersistent()) _connector.persist(_endp); if (error) _endp.close(); else { if (!_response.isCommitted() && !_request.isHandled()) _response.sendError(HttpServletResponse.SC_NOT_FOUND); _response.complete(); } } else { _response.complete(); // TODO ???????????? } } } } } /* ------------------------------------------------------------ */ public void commitResponse(boolean last) throws IOException { if (!_generator.isCommitted()) { _generator.setResponse(_response.getStatus(),_response.getReason()); _generator.completeHeader(_responseFields,last); } if (last) _generator.complete(); } /* ------------------------------------------------------------ */ public void completeResponse() throws IOException { if (!_generator.isCommitted()) { _generator.setResponse(_response.getStatus(),_response.getReason()); _generator.completeHeader(_responseFields,HttpGenerator.LAST); } _generator.complete(); } /* ------------------------------------------------------------ */ public void flushResponse() throws IOException { try { commitResponse(HttpGenerator.MORE); _generator.flush(); } catch (IOException e) { throw (e instanceof EofException)?e:new EofException(e); } } /* ------------------------------------------------------------ */ public Generator getGenerator() { return _generator; } /* ------------------------------------------------------------ */ public boolean isIncluding() { return _include > 0; } /* ------------------------------------------------------------ */ public void include() { _include++; } /* ------------------------------------------------------------ */ public void included() { _include--; if (_out != null) _out.reopen(); } /* ------------------------------------------------------------ */ public boolean isIdle() { return _generator.isIdle() && (_parser.isIdle() || _delayedHandling); } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ private class RequestHandler extends HttpParser.EventHandler { private String _charset; /* * * @see * org.mortbay.jetty.HttpParser.EventHandler#startRequest(org.mortbay * .io.Buffer, org.mortbay.io.Buffer, org.mortbay.io.Buffer) */ public void startRequest(Buffer method, Buffer uri, Buffer version) throws IOException { _host = false; _expect = UNKNOWN; _delayedHandling = false; _charset = null; if (_request.getTimeStamp() == 0) _request.setTimeStamp(System.currentTimeMillis()); _request.setMethod(method.toString()); try { _uri.parse(uri.array(),uri.getIndex(),uri.length()); _request.setUri(_uri); if (version == null) { _request.setProtocol(HttpVersions.HTTP_0_9); _version = HttpVersions.HTTP_0_9_ORDINAL; } else { version = HttpVersions.CACHE.get(version); _version = HttpVersions.CACHE.getOrdinal(version); if (_version <= 0) _version = HttpVersions.HTTP_1_0_ORDINAL; _request.setProtocol(version.toString()); } _head = method == HttpMethods.HEAD_BUFFER; // depends on method // being decached. } catch (Exception e) { throw new HttpException(HttpStatus.ORDINAL_400_Bad_Request,null,e); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -