Search in sources :

Example 1 with NoHttpResponseException

use of org.apache.http.NoHttpResponseException in project android_frameworks_base by ParanoidAndroid.

the class AndroidHttpClientConnection method parseResponseHeader.

/**
     * Parses the response headers and adds them to the
     * given {@code headers} object, and returns the response StatusLine
     * @param headers store parsed header to headers.
     * @throws IOException
     * @return StatusLine
     * @see HttpClientConnection#receiveResponseHeader()
      */
public StatusLine parseResponseHeader(Headers headers) throws IOException, ParseException {
    assertOpen();
    CharArrayBuffer current = new CharArrayBuffer(64);
    if (inbuffer.readLine(current) == -1) {
        throw new NoHttpResponseException("The target server failed to respond");
    }
    // Create the status line from the status string
    StatusLine statusline = BasicLineParser.DEFAULT.parseStatusLine(current, new ParserCursor(0, current.length()));
    if (HttpLog.LOGV)
        HttpLog.v("read: " + statusline);
    int statusCode = statusline.getStatusCode();
    // Parse header body
    CharArrayBuffer previous = null;
    int headerNumber = 0;
    while (true) {
        if (current == null) {
            current = new CharArrayBuffer(64);
        } else {
            // This must be he buffer used to parse the status
            current.clear();
        }
        int l = inbuffer.readLine(current);
        if (l == -1 || current.length() < 1) {
            break;
        }
        // Parse the header name and value
        // Check for folded headers first
        // Detect LWS-char see HTTP/1.0 or HTTP/1.1 Section 2.2
        // discussion on folded headers
        char first = current.charAt(0);
        if ((first == ' ' || first == '\t') && previous != null) {
            // we have continuation folded header
            // so append value
            int start = 0;
            int length = current.length();
            while (start < length) {
                char ch = current.charAt(start);
                if (ch != ' ' && ch != '\t') {
                    break;
                }
                start++;
            }
            if (maxLineLength > 0 && previous.length() + 1 + current.length() - start > maxLineLength) {
                throw new IOException("Maximum line length limit exceeded");
            }
            previous.append(' ');
            previous.append(current, start, current.length() - start);
        } else {
            if (previous != null) {
                headers.parseHeader(previous);
            }
            headerNumber++;
            previous = current;
            current = null;
        }
        if (maxHeaderCount > 0 && headerNumber >= maxHeaderCount) {
            throw new IOException("Maximum header count exceeded");
        }
    }
    if (previous != null) {
        headers.parseHeader(previous);
    }
    if (statusCode >= 200) {
        this.metrics.incrementResponseCount();
    }
    return statusline;
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) StatusLine(org.apache.http.StatusLine) ParserCursor(org.apache.http.message.ParserCursor) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) IOException(java.io.IOException)

Example 2 with NoHttpResponseException

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

the class HttpResponseParser method parseHead.

protected HttpMessage parseHead(final SessionInputBuffer sessionBuffer) throws IOException, HttpException, ParseException {
    this.lineBuf.clear();
    int i = sessionBuffer.readLine(this.lineBuf);
    if (i == -1) {
        throw new NoHttpResponseException("The target server failed to respond");
    }
    //create the status line from the status string
    ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
    StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
    return this.responseFactory.newHttpResponse(statusline, null);
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) ParserCursor(org.apache.http.message.ParserCursor) StatusLine(org.apache.http.StatusLine)

Example 3 with NoHttpResponseException

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

the class HttpResponseParser method parseHead.

protected HttpMessage parseHead(final SessionInputBuffer sessionBuffer) throws IOException, HttpException, ParseException {
    this.lineBuf.clear();
    int i = sessionBuffer.readLine(this.lineBuf);
    if (i == -1) {
        throw new NoHttpResponseException("The target server failed to respond");
    }
    //create the status line from the status string
    ParserCursor cursor = new ParserCursor(0, this.lineBuf.length());
    StatusLine statusline = lineParser.parseStatusLine(this.lineBuf, cursor);
    return this.responseFactory.newHttpResponse(statusline, null);
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) ParserCursor(org.apache.http.message.ParserCursor) StatusLine(org.apache.http.StatusLine)

Example 4 with NoHttpResponseException

use of org.apache.http.NoHttpResponseException in project crawler4j by yasserg.

the class RobotstxtServer method fetchDirectives.

private HostDirectives fetchDirectives(URL url) {
    WebURL robotsTxtUrl = new WebURL();
    String host = getHost(url);
    String port = ((url.getPort() == url.getDefaultPort()) || (url.getPort() == -1)) ? "" : (":" + url.getPort());
    String proto = url.getProtocol();
    robotsTxtUrl.setURL(proto + "://" + host + port + "/robots.txt");
    HostDirectives directives = null;
    PageFetchResult fetchResult = null;
    try {
        for (int redir = 0; redir < 3; ++redir) {
            fetchResult = pageFetcher.fetchPage(robotsTxtUrl);
            int status = fetchResult.getStatusCode();
            // Follow redirects up to 3 levels
            if ((status == HttpStatus.SC_MULTIPLE_CHOICES || status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_SEE_OTHER || status == HttpStatus.SC_TEMPORARY_REDIRECT || status == 308) && // SC_PERMANENT_REDIRECT RFC7538
            fetchResult.getMovedToUrl() != null) {
                robotsTxtUrl.setURL(fetchResult.getMovedToUrl());
                fetchResult.discardContentIfNotConsumed();
            } else {
                // Done on all other occasions
                break;
            }
        }
        if (fetchResult.getStatusCode() == HttpStatus.SC_OK) {
            Page page = new Page(robotsTxtUrl);
            // Most recent answer on robots.txt max size is
            // https://goo.gl/OqpKbP
            fetchResult.fetchContent(page, 10_000 * 1024);
            if (Util.hasPlainTextContent(page.getContentType())) {
                String content;
                if (page.getContentCharset() == null) {
                    content = new String(page.getContentData());
                } else {
                    content = new String(page.getContentData(), page.getContentCharset());
                }
                directives = RobotstxtParser.parse(content, config);
            } else if (page.getContentType().contains("html")) {
                // TODO This one should be upgraded to remove all
                // html tags
                String content = new String(page.getContentData());
                directives = RobotstxtParser.parse(content, config);
            } else {
                logger.warn("Can't read this robots.txt: {}  as it is not written in plain text, " + "contentType: {}", robotsTxtUrl.getURL(), page.getContentType());
            }
        } else {
            logger.debug("Can't read this robots.txt: {}  as it's status code is {}", robotsTxtUrl.getURL(), fetchResult.getStatusCode());
        }
    } catch (SocketException | UnknownHostException | SocketTimeoutException | NoHttpResponseException se) {
        // No logging here, as it just means that robots.txt doesn't exist on this server
        // which is perfectly ok
        logger.trace("robots.txt probably does not exist.", se);
    } catch (PageBiggerThanMaxSizeException pbtms) {
        logger.error("Error occurred while fetching (robots) url: {}, {}", robotsTxtUrl.getURL(), pbtms.getMessage());
    } catch (Exception e) {
        logger.error("Error occurred while fetching (robots) url: " + robotsTxtUrl.getURL(), e);
    } finally {
        if (fetchResult != null) {
            fetchResult.discardContentIfNotConsumed();
        }
    }
    if (directives == null) {
        // We still need to have this object to keep track of the time we fetched it
        directives = new HostDirectives(config);
    }
    synchronized (host2directivesCache) {
        if (host2directivesCache.size() == config.getCacheSize()) {
            String minHost = null;
            long minAccessTime = Long.MAX_VALUE;
            for (Map.Entry<String, HostDirectives> entry : host2directivesCache.entrySet()) {
                long entryAccessTime = entry.getValue().getLastAccessTime();
                if (entryAccessTime < minAccessTime) {
                    minAccessTime = entryAccessTime;
                    minHost = entry.getKey();
                }
            }
            host2directivesCache.remove(minHost);
        }
        host2directivesCache.put(host, directives);
    }
    return directives;
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) WebURL(edu.uci.ics.crawler4j.url.WebURL) Page(edu.uci.ics.crawler4j.crawler.Page) MalformedURLException(java.net.MalformedURLException) NoHttpResponseException(org.apache.http.NoHttpResponseException) UnknownHostException(java.net.UnknownHostException) PageBiggerThanMaxSizeException(edu.uci.ics.crawler4j.crawler.exceptions.PageBiggerThanMaxSizeException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) PageBiggerThanMaxSizeException(edu.uci.ics.crawler4j.crawler.exceptions.PageBiggerThanMaxSizeException) PageFetchResult(edu.uci.ics.crawler4j.fetcher.PageFetchResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with NoHttpResponseException

use of org.apache.http.NoHttpResponseException in project bw-calendar-engine by Bedework.

the class IscheduleClient method send.

/**
 * @param iout
 * @param hi
 * @param resp Response
 * @throws CalFacadeException
 */
public void send(final IscheduleOut iout, final HostInfo hi, final Response resp) throws CalFacadeException {
    try {
        /* We may have to rediscover and retry. */
        for (int failures = 0; failures < 10; failures++) {
            // PrivateKey key = pkeys.getKey(url, "isched");
            // if (key != null) {
            // iout.sign(hi, key);
            // }
            resp.setHostInfo(hi);
            final URI uri = new URI(hi.getIScheduleUrl());
            final HttpRequestBase req = findMethod(iout.getMethod(), uri);
            if (req == null) {
                throw new CalFacadeException("No method " + iout.getMethod());
            }
            if (!Util.isEmpty(iout.getHeaders())) {
                for (final Header hdr : iout.getHeaders()) {
                    req.addHeader(hdr);
                }
            }
            /* Send the ischedule request. If we get a redirect from the other end
         * we need to do the discovery thing again.
         */
            setContent(req, iout.getContentBytes(), iout.getContentType());
            try (CloseableHttpResponse hresp = cio.execute(req)) {
                final int rcode = getStatus(hresp);
                if (rcode != HttpServletResponse.SC_OK) {
                    error("Got response " + resp.getResponseCode() + ", host " + hi.getHostname() + " and url " + hi.getIScheduleUrl());
                    // hi.setIScheduleUrl(null);
                    discover(hi);
                    continue;
                }
                resp.setHttpResponse(hresp);
                parseResponse(hi, resp);
                return;
            }
        }
    } catch (final NoHttpResponseException nhre) {
        resp.setNoResponse(true);
    } catch (final Throwable t) {
        resp.setException(t);
        throw new CalFacadeException(t);
    }
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) Header(org.apache.http.Header) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) URI(java.net.URI) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Aggregations

NoHttpResponseException (org.apache.http.NoHttpResponseException)40 IOException (java.io.IOException)15 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)13 SocketException (java.net.SocketException)12 SocketTimeoutException (java.net.SocketTimeoutException)12 StatusLine (org.apache.http.StatusLine)12 UnknownHostException (java.net.UnknownHostException)10 ParserCursor (org.apache.http.message.ParserCursor)9 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)8 HttpRequest (org.apache.http.HttpRequest)7 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)7 ConnectException (java.net.ConnectException)6 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)6 InterruptedIOException (java.io.InterruptedIOException)5 SSLException (javax.net.ssl.SSLException)5 ClientProtocolException (org.apache.http.client.ClientProtocolException)5 HashMap (java.util.HashMap)4 HttpEntity (org.apache.http.HttpEntity)4 RequestConfig (org.apache.http.client.config.RequestConfig)4 HttpPost (org.apache.http.client.methods.HttpPost)4