/** * Generates <tt>Content-Length</tt> or <tt>Transfer-Encoding: Chunked</tt> * request header, as long as no <tt>Content-Length</tt> request header * already exists. * * @param state current state of http requests * @param conn the connection to use for I/O * * @throws IOException when errors occur reading or writing to/from the * connection * @throws HttpException when a recoverable error occurs */ protected void addContentLengthRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException { LOG.trace("enter EntityEnclosingMethod.addContentLengthRequestHeader(" + "HttpState, HttpConnection)"); if ((getRequestHeader("content-length") == null) && (getRequestHeader("Transfer-Encoding") == null)) { long len = getRequestContentLength(); if (len < 0) { if (getEffectiveVersion().greaterEquals(HttpVersion.HTTP_1_1)) { addRequestHeader("Transfer-Encoding", "chunked"); } else { throw new ProtocolException(getEffectiveVersion() + " does not support chunk encoding"); } } else { addRequestHeader("Content-Length", String.valueOf(len)); } } }
/** * Overrides {@link HttpMethodBase} method to <i>not</i> read a response * body, despite the presence of a <tt>Content-Length</tt> or * <tt>Transfer-Encoding</tt> header. * * @param state the {@link HttpState state} information associated with this method * @param conn the {@link HttpConnection connection} used to execute * this HTTP method * * @throws IOException if an I/O (transport) error occurs. Some transport exceptions * can be recovered from. * @throws HttpException if a protocol exception occurs. Usually protocol exceptions * cannot be recovered from. * * @see #readResponse * @see #processResponseBody * * @since 2.0 */ protected void readResponseBody(HttpState state, HttpConnection conn) throws HttpException, IOException { LOG.trace( "enter HeadMethod.readResponseBody(HttpState, HttpConnection)"); int bodyCheckTimeout = getParams().getIntParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, -1); if (bodyCheckTimeout < 0) { responseBodyConsumed(); } else { if (LOG.isDebugEnabled()) { LOG.debug("Check for non-compliant response body. Timeout in " + bodyCheckTimeout + " ms"); } boolean responseAvailable = false; try { responseAvailable = conn.isResponseAvailable(bodyCheckTimeout); } catch (IOException e) { LOG.debug("An IOException occurred while testing if a response was available," + " we will assume one is not.", e); responseAvailable = false; } if (responseAvailable) { if (getParams().isParameterTrue(HttpMethodParams.REJECT_HEAD_BODY)) { throw new ProtocolException( "Body content may not be sent in response to HTTP HEAD request"); } else { LOG.warn("Body content returned in response to HTTP HEAD"); } super.readResponseBody(state, conn); } } }
public static RequestLine parseLine(final String l) throws HttpException { String method = null; String uri = null; String protocol = null; try { StringTokenizer st = new StringTokenizer(l, " "); method = st.nextToken(); uri = st.nextToken(); protocol = st.nextToken(); } catch (NoSuchElementException e) { throw new ProtocolException("Invalid request line: " + l); } return new RequestLine(method, uri, protocol); }
public void setVersion(String version) { try { setVersion(HttpVersion.parse(version)); } catch (ProtocolException e) { throw new BuildException(e); } }
public RequestLine(final String method, final String uri, final String httpversion) throws ProtocolException { this(method, uri, HttpVersion.parse(httpversion)); }