Search in sources :

Example 1 with ProtocolException

use of org.apache.http.ProtocolException in project webmagic by code4craft.

the class CustomRedirectStrategy method getRedirect.

@Override
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
    URI uri = getLocationURI(request, response, context);
    String method = request.getRequestLine().getMethod();
    if ("post".equalsIgnoreCase(method)) {
        try {
            HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) request;
            httpRequestWrapper.setURI(uri);
            httpRequestWrapper.removeHeaders("Content-Length");
            return httpRequestWrapper;
        } catch (Exception e) {
            logger.error("强转为HttpRequestWrapper出错");
        }
        return new HttpPost(uri);
    } else {
        return new HttpGet(uri);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpGet(org.apache.http.client.methods.HttpGet) HttpRequestWrapper(org.apache.http.client.methods.HttpRequestWrapper) URI(java.net.URI) ProtocolException(org.apache.http.ProtocolException)

Example 2 with ProtocolException

use of org.apache.http.ProtocolException in project robovm by robovm.

the class StrictContentLengthStrategy method determineLength.

public long determineLength(final HttpMessage message) throws HttpException {
    if (message == null) {
        throw new IllegalArgumentException("HTTP message may not be null");
    }
    // Although Transfer-Encoding is specified as a list, in practice
    // it is either missing or has the single value "chunked". So we
    // treat it as a single-valued header here.
    Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
    Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
    if (transferEncodingHeader != null) {
        String s = transferEncodingHeader.getValue();
        if (HTTP.CHUNK_CODING.equalsIgnoreCase(s)) {
            if (message.getProtocolVersion().lessEquals(HttpVersion.HTTP_1_0)) {
                throw new ProtocolException("Chunked transfer encoding not allowed for " + message.getProtocolVersion());
            }
            return CHUNKED;
        } else if (HTTP.IDENTITY_CODING.equalsIgnoreCase(s)) {
            return IDENTITY;
        } else {
            throw new ProtocolException("Unsupported transfer encoding: " + s);
        }
    } else if (contentLengthHeader != null) {
        String s = contentLengthHeader.getValue();
        try {
            long len = Long.parseLong(s);
            return len;
        } catch (NumberFormatException e) {
            throw new ProtocolException("Invalid content length: " + s);
        }
    } else {
        return IDENTITY;
    }
}
Also used : ProtocolException(org.apache.http.ProtocolException) Header(org.apache.http.Header)

Example 3 with ProtocolException

use of org.apache.http.ProtocolException in project robovm by robovm.

the class AbstractMessageParser method parse.

public HttpMessage parse() throws IOException, HttpException {
    HttpMessage message = null;
    try {
        message = parseHead(this.sessionBuffer);
    } catch (ParseException px) {
        throw new ProtocolException(px.getMessage(), px);
    }
    Header[] headers = AbstractMessageParser.parseHeaders(this.sessionBuffer, this.maxHeaderCount, this.maxLineLen, this.lineParser);
    message.setHeaders(headers);
    return message;
}
Also used : ProtocolException(org.apache.http.ProtocolException) Header(org.apache.http.Header) ParseException(org.apache.http.ParseException) HttpMessage(org.apache.http.HttpMessage)

Example 4 with ProtocolException

use of org.apache.http.ProtocolException in project robovm by robovm.

the class HttpService method handleException.

protected void handleException(final HttpException ex, final HttpResponse response) {
    if (ex instanceof MethodNotSupportedException) {
        response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
    } else if (ex instanceof UnsupportedHttpVersionException) {
        response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED);
    } else if (ex instanceof ProtocolException) {
        response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
    } else {
        response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    }
    byte[] msg = EncodingUtils.getAsciiBytes(ex.getMessage());
    ByteArrayEntity entity = new ByteArrayEntity(msg);
    entity.setContentType("text/plain; charset=US-ASCII");
    response.setEntity(entity);
}
Also used : ProtocolException(org.apache.http.ProtocolException) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) MethodNotSupportedException(org.apache.http.MethodNotSupportedException) UnsupportedHttpVersionException(org.apache.http.UnsupportedHttpVersionException)

Example 5 with ProtocolException

use of org.apache.http.ProtocolException in project XobotOS by xamarin.

the class DefaultRedirectHandler method getLocationURI.

public URI getLocationURI(final HttpResponse response, final HttpContext context) throws ProtocolException {
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }
    //get the location header to find out where to redirect to
    Header locationHeader = response.getFirstHeader("location");
    if (locationHeader == null) {
        // got a redirect response, but no location header
        throw new ProtocolException("Received redirect response " + response.getStatusLine() + " but no location header");
    }
    String location = locationHeader.getValue();
    if (this.log.isDebugEnabled()) {
        this.log.debug("Redirect requested to location '" + location + "'");
    }
    URI uri;
    try {
        uri = new URI(location);
    } catch (URISyntaxException ex) {
        throw new ProtocolException("Invalid redirect URI: " + location, ex);
    }
    HttpParams params = response.getParams();
    // Location       = "Location" ":" absoluteURI
    if (!uri.isAbsolute()) {
        if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
            throw new ProtocolException("Relative redirect location '" + uri + "' not allowed");
        }
        // Adjust location URI
        HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        if (target == null) {
            throw new IllegalStateException("Target host not available " + "in the HTTP context");
        }
        HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        try {
            URI requestURI = new URI(request.getRequestLine().getUri());
            URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
            uri = URIUtils.resolve(absoluteRequestURI, uri);
        } catch (URISyntaxException ex) {
            throw new ProtocolException(ex.getMessage(), ex);
        }
    }
    if (params.isParameterFalse(ClientPNames.ALLOW_CIRCULAR_REDIRECTS)) {
        RedirectLocations redirectLocations = (RedirectLocations) context.getAttribute(REDIRECT_LOCATIONS);
        if (redirectLocations == null) {
            redirectLocations = new RedirectLocations();
            context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
        }
        URI redirectURI;
        if (uri.getFragment() != null) {
            try {
                HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
                redirectURI = URIUtils.rewriteURI(uri, target, true);
            } catch (URISyntaxException ex) {
                throw new ProtocolException(ex.getMessage(), ex);
            }
        } else {
            redirectURI = uri;
        }
        if (redirectLocations.contains(redirectURI)) {
            throw new CircularRedirectException("Circular redirect to '" + redirectURI + "'");
        } else {
            redirectLocations.add(redirectURI);
        }
    }
    return uri;
}
Also used : HttpRequest(org.apache.http.HttpRequest) ProtocolException(org.apache.http.ProtocolException) CircularRedirectException(org.apache.http.client.CircularRedirectException) HttpParams(org.apache.http.params.HttpParams) Header(org.apache.http.Header) HttpHost(org.apache.http.HttpHost) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

ProtocolException (org.apache.http.ProtocolException)58 Header (org.apache.http.Header)21 ArrayList (java.util.ArrayList)20 HttpResponse (org.apache.http.HttpResponse)18 URI (java.net.URI)17 HttpRequest (org.apache.http.HttpRequest)17 HttpContext (org.apache.http.protocol.HttpContext)17 HttpPost (org.apache.http.client.methods.HttpPost)16 HttpHost (org.apache.http.HttpHost)14 NameValuePair (org.apache.http.NameValuePair)14 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)14 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)14 TestHttpClient (io.undertow.testutils.TestHttpClient)13 DefaultRedirectStrategy (org.apache.http.impl.client.DefaultRedirectStrategy)13 Test (org.junit.Test)12 URISyntaxException (java.net.URISyntaxException)11 ParseException (org.apache.http.ParseException)9 ProtocolVersion (org.apache.http.ProtocolVersion)9 HttpGet (org.apache.http.client.methods.HttpGet)9 IOException (java.io.IOException)8