Search in sources :

Example 66 with URI

use of org.apache.commons.httpclient.URI in project pinot by linkedin.

the class ServerTableSizeReader method getSizeDetailsFromServers.

public Map<String, List<SegmentSizeInfo>> getSizeDetailsFromServers(BiMap<String, String> serverEndPoints, String table, int timeoutMsec) {
    List<String> serverUrls = new ArrayList<>(serverEndPoints.size());
    BiMap<String, String> endpointsToServers = serverEndPoints.inverse();
    for (String endpoint : endpointsToServers.keySet()) {
        String tableSizeUri = "http://" + endpoint + "/table/" + table + "/size";
        serverUrls.add(tableSizeUri);
    }
    MultiGetRequest mget = new MultiGetRequest(executor, connectionManager);
    LOGGER.info("Reading segment sizes from servers for table: {}, timeoutMsec: {}", table, timeoutMsec);
    CompletionService<GetMethod> completionService = mget.execute(serverUrls, timeoutMsec);
    Map<String, List<SegmentSizeInfo>> serverSegmentSizes = new HashMap<>(serverEndPoints.size());
    for (int i = 0; i < serverUrls.size(); i++) {
        GetMethod getMethod = null;
        try {
            getMethod = completionService.take().get();
            URI uri = getMethod.getURI();
            String instance = endpointsToServers.get(uri.getHost() + ":" + uri.getPort());
            if (getMethod.getStatusCode() >= 300) {
                LOGGER.error("Server: {} returned error: {}", instance, getMethod.getStatusCode());
                continue;
            }
            TableSizeInfo tableSizeInfo = new ObjectMapper().readValue(getMethod.getResponseBodyAsString(), TableSizeInfo.class);
            serverSegmentSizes.put(instance, tableSizeInfo.segments);
        } catch (InterruptedException e) {
            LOGGER.warn("Interrupted exception while reading segment size for table: {}", table, e);
        } catch (ExecutionException e) {
            if (Throwables.getRootCause(e) instanceof SocketTimeoutException) {
                LOGGER.warn("Server request to read table size was timed out for table: {}", table, e);
            } else if (Throwables.getRootCause(e) instanceof ConnectTimeoutException) {
                LOGGER.warn("Server request to read table size timed out waiting for connection. table: {}", table, e);
            } else if (Throwables.getRootCause(e) instanceof ConnectionPoolTimeoutException) {
                LOGGER.warn("Server request to read table size timed out on getting a connection from pool, table: {}", table, e);
            } else {
                LOGGER.warn("Execution exception while reading segment sizes for table: {}", table, e);
            }
        } catch (Exception e) {
            LOGGER.warn("Error while reading segment sizes for table: {}", table);
        } finally {
            if (getMethod != null) {
                getMethod.releaseConnection();
            }
        }
    }
    LOGGER.info("Finished reading segment sizes for table: {}", table);
    return serverSegmentSizes;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URI(org.apache.commons.httpclient.URI) ExecutionException(java.util.concurrent.ExecutionException) ConnectTimeoutException(org.apache.commons.httpclient.ConnectTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectionPoolTimeoutException(org.apache.commons.httpclient.ConnectionPoolTimeoutException) MultiGetRequest(com.linkedin.pinot.common.http.MultiGetRequest) ConnectionPoolTimeoutException(org.apache.commons.httpclient.ConnectionPoolTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) GetMethod(org.apache.commons.httpclient.methods.GetMethod) ArrayList(java.util.ArrayList) List(java.util.List) TableSizeInfo(com.linkedin.pinot.common.restlet.resources.TableSizeInfo) ExecutionException(java.util.concurrent.ExecutionException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ConnectTimeoutException(org.apache.commons.httpclient.ConnectTimeoutException)

Example 67 with URI

use of org.apache.commons.httpclient.URI in project pinpoint by naver.

the class HttpMethodBaseExecuteMethodInterceptor method recordDestination.

private void recordDestination(final Trace trace, final HttpMethod httpMethod, final Object[] args) {
    final SpanEventRecorder recorder = trace.currentSpanEventRecorder();
    try {
        final URI uri = httpMethod.getURI();
        final HttpConnection httpConnection = getHttpConnection(args);
        // if uri have schema or not found HttpConnection argument.
        if (uri.isAbsoluteURI() || httpConnection == null) {
            recorder.recordAttribute(AnnotationKey.HTTP_URL, InterceptorUtils.getHttpUrl(uri.getURI(), param));
            recorder.recordDestinationId(getEndpoint(uri.getHost(), uri.getPort()));
            return;
        }
        if (isDebug) {
            logger.debug("URI is not absolute. {}", uri.getURI());
        }
        // use HttpConnection argument.
        final String host = httpConnection.getHost();
        int port = httpConnection.getPort();
        final StringBuilder httpUrl = new StringBuilder();
        final Protocol protocol = httpConnection.getProtocol();
        if (protocol != null) {
            httpUrl.append(protocol.getScheme()).append("://");
            httpUrl.append(httpConnection.getHost());
            // if port is default port number.
            if (httpConnection.getPort() == protocol.getDefaultPort()) {
                port = -1;
            } else {
                httpUrl.append(":").append(port);
            }
        }
        httpUrl.append(uri.getURI());
        recorder.recordAttribute(AnnotationKey.HTTP_URL, InterceptorUtils.getHttpUrl(httpUrl.toString(), param));
        recorder.recordDestinationId(getEndpoint(host, port));
    } catch (URIException e) {
        logger.error("Fail get URI", e);
        recorder.recordDestinationId("unknown");
    }
}
Also used : URIException(org.apache.commons.httpclient.URIException) HttpConnection(org.apache.commons.httpclient.HttpConnection) SpanEventRecorder(com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder) Protocol(org.apache.commons.httpclient.protocol.Protocol) URI(org.apache.commons.httpclient.URI)

Example 68 with URI

use of org.apache.commons.httpclient.URI in project zaproxy by zaproxy.

the class AbstractDefaultFilePlugin method createURI.

private URI createURI(URI base, String dir, String file) throws URIException {
    if (!dir.startsWith("/")) {
        dir = "/" + dir;
    }
    if (!file.startsWith("/") && !dir.endsWith("/")) {
        file = "/" + file;
    }
    String path = dir + file;
    URI uri = new URI(base, path, true);
    return uri;
}
Also used : URI(org.apache.commons.httpclient.URI)

Example 69 with URI

use of org.apache.commons.httpclient.URI in project zaproxy by zaproxy.

the class AbstractDefaultFilePlugin method scan.

@Override
public void scan() {
    for (int i = 0; i < getListURI().size() && !isStop(); i++) {
        // ZAP: Removed unnecessary cast.
        URI uri = getListURI().get(i);
        HttpMessage msg = getNewMsg();
        try {
            msg.getRequestHeader().setURI(uri);
            msg.getRequestBody().setLength(0);
            sendAndReceive(msg);
            if (isFileExist(msg)) {
                bingo(Alert.RISK_MEDIUM, Alert.CONFIDENCE_LOW, uri.toString(), "", "", "", "", msg);
            }
        } catch (Exception e) {
        }
    }
}
Also used : HttpMessage(org.parosproxy.paros.network.HttpMessage) URI(org.apache.commons.httpclient.URI) URIException(org.apache.commons.httpclient.URIException)

Example 70 with URI

use of org.apache.commons.httpclient.URI in project zaproxy by zaproxy.

the class Analyser method isFileExist.

public boolean isFileExist(HttpMessage msg) {
    if (msg.getResponseHeader().isEmpty()) {
        return false;
    }
    // RFC
    if (msg.getResponseHeader().getStatusCode() == HttpStatusCode.NOT_FOUND) {
        return false;
    }
    // ZAP: catch CloneNotSupportedException as introduced with version 3.1 of HttpClient
    URI uri = null;
    String sUri = null;
    try {
        uri = (URI) msg.getRequestHeader().getURI().clone();
        // strip off last part of path - use folder only
        uri.setQuery(null);
        String path = uri.getPath();
        path = path.replaceAll("/[^/]*$", "");
        uri.setPath(path);
    } catch (Exception e) {
    } finally {
        if (uri != null) {
            sUri = uri.toString();
        }
    }
    // get sample with same relative path position when possible.
    // if not exist, use the host only	
    // ZAP: Removed unnecessary cast.
    SampleResponse sample = mapVisited.get(sUri);
    if (sample == null) {
        try {
            uri.setPath(null);
        } catch (URIException e2) {
        }
        String sHostOnly = uri.toString();
        // ZAP: Removed unnecessary cast.
        sample = mapVisited.get(sHostOnly);
    }
    // check if any analysed result.
    if (sample == null) {
        if (msg.getResponseHeader().getStatusCode() == HttpStatusCode.OK) {
            // no anlaysed result to confirm, assume file exist and return
            return true;
        } else {
            return false;
        }
    }
    // check for redirect response.  If redirect to same location, then file does not exist
    if (HttpStatusCode.isRedirection(msg.getResponseHeader().getStatusCode())) {
        try {
            if (sample.getMessage().getResponseHeader().getStatusCode() == msg.getResponseHeader().getStatusCode()) {
                String location = msg.getResponseHeader().getHeader(HttpHeader.LOCATION);
                if (location != null && location.equals(sample.getMessage().getResponseHeader().getHeader(HttpHeader.LOCATION))) {
                    return false;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return true;
    }
    // Not success code
    if (msg.getResponseHeader().getStatusCode() != HttpStatusCode.OK) {
        return false;
    }
    // remain only OK response here
    // nothing more to determine.  Check for possible not found page pattern.
    Matcher matcher = patternNotFound.matcher(msg.getResponseBody().toString());
    if (matcher.find()) {
        return false;
    }
    // static response
    String body = msg.getResponseBody().toString().replaceAll(p_REMOVE_HEADER, "");
    if (sample.getErrorPageType() == SampleResponse.ERROR_PAGE_STATIC) {
        try {
            if (sample.getMessage().getResponseBody().toString().equals(body)) {
                return false;
            }
        } catch (HttpMalformedHeaderException | DatabaseException e) {
            logger.error("Failed to read the message: " + e.getMessage(), e);
        }
        return true;
    }
    uri = msg.getRequestHeader().getURI();
    try {
        if (sample.getErrorPageType() == SampleResponse.ERROR_PAGE_DYNAMIC_BUT_DETERMINISTIC) {
            body = msg.getResponseBody().toString().replaceAll(getPathRegex(uri), "").replaceAll("\\s[012]\\d:[0-5]\\d:[0-5]\\d\\s", "");
            // ZAP: FindBugs fix - added call to HttpBody.toString() 
            if (sample.getMessage().getResponseBody().toString().equals(body)) {
                return false;
            }
            return true;
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    return true;
}
Also used : URIException(org.apache.commons.httpclient.URIException) Matcher(java.util.regex.Matcher) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) URI(org.apache.commons.httpclient.URI) DatabaseException(org.parosproxy.paros.db.DatabaseException) IOException(java.io.IOException) DatabaseException(org.parosproxy.paros.db.DatabaseException) URIException(org.apache.commons.httpclient.URIException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) HttpException(org.apache.commons.httpclient.HttpException)

Aggregations

URI (org.apache.commons.httpclient.URI)129 Test (org.junit.Test)72 FetchStatus (org.zaproxy.zap.spider.filters.FetchFilter.FetchStatus)33 URIException (org.apache.commons.httpclient.URIException)28 HttpMessage (org.parosproxy.paros.network.HttpMessage)10 ArrayList (java.util.ArrayList)9 HttpRequestHeader (org.parosproxy.paros.network.HttpRequestHeader)8 DatabaseException (org.parosproxy.paros.db.DatabaseException)7 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)6 HandleParametersOption (org.zaproxy.zap.spider.SpiderParam.HandleParametersOption)6 IOException (java.io.IOException)5 Header (org.apache.commons.httpclient.Header)4 InvalidParameterException (java.security.InvalidParameterException)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 JSONException (net.sf.json.JSONException)3 StructuralNode (org.zaproxy.zap.model.StructuralNode)3 File (java.io.File)2 List (java.util.List)2