private void setKeepAliveForHeader(final Header header, final HttpRequestPacket requestPacket) { final MimeHeaders headers = requestPacket.getHeaders(); // Assign Connection: ... if needed if (!headers.contains(header)) { if (requestPacket.getProcessingState().isKeepAlive()) { headers.addValue(header).setBytes(KEEP_ALIVE_VALUE.getByteArray()); } else if (Protocol.HTTP_1_1.equals(requestPacket.getProtocol())) { headers.addValue(header).setBytes(CLOSE_VALUE.getByteArray()); } // switch (requestPacket.getProtocol()) { // case HTTP_0_9: // case HTTP_1_0: // if (requestPacket.getProcessingState().isKeepAlive()) { // headers.addValue(header).setBytes(KEEP_ALIVE_VALUE.getByteArray()); // } // break; // case HTTP_1_1: // if (!requestPacket.getProcessingState().isKeepAlive()) { // headers.addValue(header).setBytes(CLOSE_VALUE.getByteArray()); // } // break; // } } }
/** * Get HTTP protocol version. * @return {@link Protocol}. */ public Protocol getProtocol() { if (parsedProtocol != null) { return parsedProtocol; } parsedProtocol = Protocol.valueOf(protocolC); return parsedProtocol; }
@Override public int getProtocolMajorVersion() { return response.getProtocol().getMajorVersion(); }
private static boolean checkKeepAlive(final HttpResponsePacket response) { final int statusCode = response.getStatus(); final boolean isExpectContent = response.isExpectContent(); boolean keepAlive = !statusDropsConnection(statusCode) || (!isExpectContent || !response.isChunked() || response.getContentLength() == -1); // double-check the transfer encoding here if (keepAlive) { // Check the Connection header final DataChunk cVal = response.getHeaders().getValue(Header.Connection); if (response.getProtocol().compareTo(Protocol.HTTP_1_1) < 0) { // HTTP 1.0 response // "Connection: keep-alive" should be specified explicitly keepAlive = cVal != null && cVal.equalsIgnoreCase(KEEPALIVE_BYTES); } else { // HTTP 1.1+ // keep-alive by default, if there's no "Connection: close" keepAlive = cVal == null || !cVal.equalsIgnoreCase(CLOSE_BYTES); } } return keepAlive; }
/** * {@inheritDoc} */ @Override public String toString() { final StringBuilder sb = new StringBuilder(256); sb.append("HttpRequestPacket (\n method=").append(getMethod()) .append("\n url=").append(getRequestURI()) .append("\n query=").append(getQueryString()) .append("\n protocol=").append(getProtocol().getProtocolString()) .append("\n content-length=").append(getContentLength()) .append("\n headers=["); final MimeHeaders headersLocal = getHeaders(); for (final String name : headersLocal.names()) { for (String value : headersLocal.values(name)) { sb.append("\n ").append(name).append('=').append(value); } } sb.append("]\n)"); return sb.toString(); }
/** * {@inheritDoc} */ @Override public String toString() { final StringBuilder sb = new StringBuilder(256); sb.append("HttpResponsePacket (\n status=").append(getStatus()) .append("\n reason=").append(getReasonPhrase()) .append("\n protocol=").append(getProtocol().getProtocolString()) .append("\n content-length=").append(getContentLength()) .append("\n committed=").append(isCommitted()) .append("\n headers=["); final MimeHeaders headersLocal = getHeaders(); for (final String name : headersLocal.names()) { for (String value : headersLocal.values(name)) { sb.append("\n ").append(name).append('=').append(value); } } sb.append("]\n)"); return sb.toString(); }
public static void writeTraceMessage(final Request request, final Response response) throws IOException { response.setStatus(HttpStatus.OK_200); response.setContentType("message/http"); final Writer writer = response.getWriter(); writer.append(request.getMethod().toString()).append(' ') .append(request.getRequest().getRequestURIRef().getOriginalRequestURIBC().toString()) .append(' ').append(request.getProtocol().getProtocolString()) .append("\r\n"); for (String headerName : request.getHeaderNames()) { for (String headerValue : request.getHeaders(headerName)) { writer.append(headerName).append(": ").append(headerValue).append("\r\n"); } } }
/** * Get the HTTP message protocol version. The result format is "HTTP/1.x". * * @return the HTTP message protocol version. The result format is "HTTP/1.x". */ public String getProtocolString() { if (parsedProtocol == null) { return getProtocolDC().toString(); } return parsedProtocol.getProtocolString(); }
public static Protocol valueOf(final DataChunk protocolC) { if (protocolC.getLength() == 0) { return Protocol.HTTP_0_9; } else if (protocolC.equals(Protocol.HTTP_1_1.getProtocolBytes())) { return Protocol.HTTP_1_1; } else if (protocolC.equals(Protocol.HTTP_1_0.getProtocolBytes())) { return Protocol.HTTP_1_0; } else if (protocolC.equals(Protocol.HTTP_2_0.getProtocolBytes())) { return Protocol.HTTP_2_0; } else if (protocolC.equals(Protocol.HTTP_0_9.getProtocolBytes())) { return Protocol.HTTP_0_9; } throw new IllegalStateException("Unknown protocol " + protocolC.toString()); }
public static Protocol valueOf(final byte[] protocolBytes, final int offset, final int len) { if (len == 0) { return Protocol.HTTP_0_9; } else if (equals(HTTP_1_1, protocolBytes, offset, len)) { return Protocol.HTTP_1_1; } else if (equals(HTTP_1_0, protocolBytes, offset, len)) { return Protocol.HTTP_1_0; } else if (equals(HTTP_2_0, protocolBytes, offset, len)) { return Protocol.HTTP_2_0; } else if (equals(HTTP_0_9, protocolBytes, offset, len)) { return Protocol.HTTP_0_9; } throw new IllegalStateException("Unknown protocol " + new String(protocolBytes, offset, len, Charsets.ASCII_CHARSET)); }
/** * Set <tt>true</tt>, if this {@link HttpPacket} content will be transferred * in chunking mode, or <tt>false</tt> if case of fixed-length message. * * NOTE: If the protocol version of this header is 1.0 or older, chunking * will be disabled regardless of the value passed. * * @param isChunked <tt>true</tt>, if this {@link HttpPacket} content * will be transferred in chunking mode, or <tt>false</tt> if case * of fixed-length message. */ public void setChunked(boolean isChunked) { if (getProtocol().compareTo(Protocol.HTTP_1_1) >= 0) { // HTTP/1.1 and later this.isChunked = isChunked; if (isChunked) { headers.removeHeader(Header.ContentLength); } } else { this.isChunked = false; } }
/** * {@inheritDoc} */ @Override public String getProtocol() { if (request == null) { throw new IllegalStateException("Null request object"); } return request.getProtocol().getProtocolString(); }
@Override public int getProtocolMinorVersion() { return response.getProtocol().getMinorVersion(); }
private static boolean equals(final Protocol protocol, final Buffer protocolBuffer, final int offset, final int len) { final byte[] knownProtocolBytes = protocol.getProtocolBytes(); return BufferChunk.equals(knownProtocolBytes, 0, knownProtocolBytes.length, protocolBuffer, offset, len); }
private static boolean checkKeepAlive(final HttpResponsePacket response) { final int statusCode = response.getStatus(); final boolean isExpectContent = response.isExpectContent(); boolean keepAlive = !statusDropsConnection(statusCode) || (!isExpectContent || !response.isChunked() || response.getContentLength() == -1); // double-check the transfer encoding here if (keepAlive) { // Check the Connection header final DataChunk cVal = response.getHeaders().getValue(Header.Connection); if (response.getProtocol().compareTo(Protocol.HTTP_1_1) < 0) { // HTTP 1.0 response // "Connection: keep-alive" should be specified explicitly keepAlive = cVal != null && cVal.equalsIgnoreCase(KEEPALIVE_BYTES); } else { // HTTP 1.1+ // keep-alive by default, if there's no "Connection: close" keepAlive = cVal == null || !cVal.equalsIgnoreCase(CLOSE_BYTES); } } return keepAlive; }
/** * {@inheritDoc} */ @Override public String toString() { final StringBuilder sb = new StringBuilder(256); sb.append("HttpRequestPacket (\n method=").append(getMethod()) .append("\n url=").append(getRequestURI()) .append("\n query=").append(getQueryString()) .append("\n protocol=").append(getProtocol().getProtocolString()) .append("\n content-length=").append(getContentLength()) .append("\n headers=["); final MimeHeaders headersLocal = getHeaders(); for (final String name : headersLocal.names()) { for (String value : headersLocal.values(name)) { sb.append("\n ").append(name).append('=').append(value); } } sb.append("]\n)"); return sb.toString(); }
/** * {@inheritDoc} */ @Override public String toString() { final StringBuilder sb = new StringBuilder(256); sb.append("HttpResponsePacket (\n status=").append(getStatus()) .append("\n reason=").append(getReasonPhrase()) .append("\n protocol=").append(getProtocol().getProtocolString()) .append("\n content-length=").append(getContentLength()) .append("\n committed=").append(isCommitted()) .append("\n headers=["); final MimeHeaders headersLocal = getHeaders(); for (final String name : headersLocal.names()) { for (String value : headersLocal.values(name)) { sb.append("\n ").append(name).append('=').append(value); } } sb.append("]\n)"); return sb.toString(); }
public static void writeTraceMessage(final Request request, final Response response) throws IOException { response.setStatus(HttpStatus.OK_200); response.setContentType("message/http"); final Writer writer = response.getWriter(); writer.append(request.getMethod().toString()).append(' ') .append(request.getRequest().getRequestURIRef().getOriginalRequestURIBC().toString()) .append(' ').append(request.getProtocol().getProtocolString()) .append("\r\n"); for (String headerName : request.getHeaderNames()) { for (String headerValue : request.getHeaders(headerName)) { writer.append(headerName).append(": ").append(headerValue).append("\r\n"); } } }
/** * Get the HTTP message protocol version. The result format is "HTTP/1.x". * * @return the HTTP message protocol version. The result format is "HTTP/1.x". */ public String getProtocolString() { if (parsedProtocol == null) { return getProtocolDC().toString(); } return parsedProtocol.getProtocolString(); }
public static Protocol valueOf(final DataChunk protocolC) { if (protocolC.getLength() == 0) { return Protocol.HTTP_0_9; } else if (protocolC.equals(Protocol.HTTP_1_1.getProtocolBytes())) { return Protocol.HTTP_1_1; } else if (protocolC.equals(Protocol.HTTP_1_0.getProtocolBytes())) { return Protocol.HTTP_1_0; } else if (protocolC.equals(Protocol.HTTP_2_0.getProtocolBytes())) { return Protocol.HTTP_2_0; } else if (protocolC.equals(Protocol.HTTP_0_9.getProtocolBytes())) { return Protocol.HTTP_0_9; } throw new IllegalStateException("Unknown protocol " + protocolC.toString()); }