📄 httpconnection.java
字号:
{ synchronized (this) { if (_exchange != null) { _exchange.getEventListener().onException(e); _exchange.setStatus(HttpExchange.STATUS_EXCEPTED); } } failed = true; throw e; } finally { boolean complete = false; boolean close = failed; // always close the connection on error if (!failed) { // are we complete? if (_generator.isComplete()) { if (!_requestComplete) { _requestComplete = true; _exchange.getEventListener().onRequestComplete(); } // we need to return the HttpConnection to a state that // it can be reused or closed out if (_parser.isComplete()) { _destination.getHttpClient().cancel(_timeout); complete = true; } } } if (complete || failed) { synchronized (this) { if (!close) close = shouldClose(); reset(true); no_progress = 0; flushed = -1; if (_exchange != null) { _exchange = null; if (_pipeline == null) { if (!isReserved()) _destination.returnConnection(this,close); if (close) return; } else { if (close) { if (!isReserved()) _destination.returnConnection(this,close); _destination.send(_pipeline); _pipeline = null; return; } HttpExchange ex = _pipeline; _pipeline = null; send(ex); } } } } } } } /* ------------------------------------------------------------ */ public boolean isIdle() { synchronized (this) { return _exchange == null; } } /* ------------------------------------------------------------ */ public EndPoint getEndPoint() { return _endp; } /* ------------------------------------------------------------ */ private void commitRequest() throws IOException { synchronized (this) { if (_exchange.getStatus() != HttpExchange.STATUS_WAITING_FOR_COMMIT) throw new IllegalStateException(); _exchange.setStatus(HttpExchange.STATUS_SENDING_REQUEST); _generator.setVersion(_exchange._version); String uri = _exchange._uri; if (_destination.isProxied() && uri.startsWith("/")) { // TODO suppress port 80 or 443 uri = (_destination.isSecure()?HttpSchemes.HTTPS:HttpSchemes.HTTP) + "://" + _destination.getAddress().getHost() + ":" + _destination.getAddress().getPort() + uri; Authorization auth = _destination.getProxyAuthentication(); if (auth != null) auth.setCredentials(_exchange); } _generator.setRequest(_exchange._method,uri); if (_exchange._version >= HttpVersions.HTTP_1_1_ORDINAL) { if (!_exchange._requestFields.containsKey(HttpHeaders.HOST_BUFFER)) _exchange._requestFields.add(HttpHeaders.HOST_BUFFER,_destination.getHostHeader()); } if (_exchange._requestContent != null) { _exchange._requestFields.putLongField(HttpHeaders.CONTENT_LENGTH,_exchange._requestContent.length()); _generator.completeHeader(_exchange._requestFields,false); _generator.addContent(_exchange._requestContent,true); } else if (_exchange._requestContentSource != null) { _generator.completeHeader(_exchange._requestFields,false); int available = _exchange._requestContentSource.available(); if (available > 0) { // TODO deal with any known content length // TODO reuse this buffer! byte[] buf = new byte[available]; int length = _exchange._requestContentSource.read(buf); _generator.addContent(new ByteArrayBuffer(buf,0,length),false); } } else { _exchange._requestFields.remove(HttpHeaders.CONTENT_LENGTH); // TODO // : // should // not // be // needed _generator.completeHeader(_exchange._requestFields,true); } _exchange.setStatus(HttpExchange.STATUS_WAITING_FOR_RESPONSE); } } /* ------------------------------------------------------------ */ protected void reset(boolean returnBuffers) throws IOException { _requestComplete = false; _connectionHeader = null; _parser.reset(returnBuffers); _generator.reset(returnBuffers); _http11 = true; } /* ------------------------------------------------------------ */ private boolean shouldClose() { if (_connectionHeader!=null) { if (HttpHeaderValues.CLOSE_BUFFER.equals(_connectionHeader)) return true; if (HttpHeaderValues.KEEP_ALIVE_BUFFER.equals(_connectionHeader)) return false; } return !_http11; } /* ------------------------------------------------------------ */ private class Handler extends HttpParser.EventHandler { @Override public void startRequest(Buffer method, Buffer url, Buffer version) throws IOException { // System.out.println( method.toString() + "///" + url.toString() + // "///" + version.toString() ); // TODO validate this is acceptable, the <!DOCTYPE goop was coming // out here // throw new IllegalStateException(); } @Override public void startResponse(Buffer version, int status, Buffer reason) throws IOException { HttpExchange exchange = _exchange; if (exchange!=null) { _http11 = HttpVersions.HTTP_1_1_BUFFER.equals(version); exchange.getEventListener().onResponseStatus(version,status,reason); exchange.setStatus(HttpExchange.STATUS_PARSING_HEADERS); } } @Override public void parsedHeader(Buffer name, Buffer value) throws IOException { HttpExchange exchange = _exchange; if (exchange!=null) { if (HttpHeaders.CACHE.getOrdinal(name) == HttpHeaders.CONNECTION_ORDINAL) { _connectionHeader = HttpHeaderValues.CACHE.lookup(value); } exchange.getEventListener().onResponseHeader(name,value); } } @Override public void headerComplete() throws IOException { HttpExchange exchange = _exchange; if (exchange!=null) exchange.setStatus(HttpExchange.STATUS_PARSING_CONTENT); } @Override public void content(Buffer ref) throws IOException { HttpExchange exchange = _exchange; if (exchange!=null) exchange.getEventListener().onResponseContent(ref); } @Override public void messageComplete(long contextLength) throws IOException { HttpExchange exchange = _exchange; if (exchange!=null) exchange.setStatus(HttpExchange.STATUS_COMPLETED); } } /* ------------------------------------------------------------ */ public String toString() { return "HttpConnection@" + hashCode() + "//" + _destination.getAddress().getHost() + ":" + _destination.getAddress().getPort(); } /* ------------------------------------------------------------ */ public String toDetailString() { return toString() + " ex=" + _exchange + " " + _timeout.getAge(); } /* ------------------------------------------------------------ */ /** * @return the last */ public long getLast() { return _last; } /* ------------------------------------------------------------ */ /** * @param last * the last to set */ public void setLast(long last) { _last = last; } /* ------------------------------------------------------------ */ public void close() throws IOException { _endp.close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -