Search in sources :

Example 16 with URIException

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

the class VariantODataIdQuery method parse.

private void parse(URI uri) {
    try {
        resourceParameter = null;
        beforeMultipleIDs = null;
        afterMultipleIDs = null;
        listParams = null;
        String path = uri.getPath();
        if (path != null) {
            // Detection of the resource and resource id (if any)
            String resourceName = "";
            String resourceID;
            // check for single ID (unnamed)
            Matcher matcher = patternResourceIdentifierUnquoted.matcher(path);
            if (matcher.find()) {
                resourceName = matcher.group(1);
                resourceID = matcher.group(2);
                String subString = resourceName + "(" + resourceID + ")";
                int begin = path.indexOf(subString);
                int end = begin + subString.length();
                String beforeSubstring = path.substring(0, begin);
                String afterSubstring = path.substring(end);
                resourceParameter = new ResourceParameter(resourceName, resourceID, beforeSubstring, afterSubstring);
            } else {
                matcher = patternResourceMultipleIdentifier.matcher(path);
                if (matcher.find()) {
                    // We've found a composite identifier. i.e: /Resource(field1=a,field2=3)
                    String multipleIdentifierSection = matcher.group(1);
                    int begin = path.indexOf(multipleIdentifierSection);
                    int end = begin + multipleIdentifierSection.length();
                    beforeMultipleIDs = path.substring(0, begin);
                    afterMultipleIDs = path.substring(end);
                    listParams = new ArrayList<>();
                    matcher = patternResourceMultipleIdentifierDetail.matcher(multipleIdentifierSection);
                    int i = 1;
                    while (matcher.find()) {
                        String paramName = matcher.group(1);
                        String value = matcher.group(2);
                        NameValuePair vp = new NameValuePair(NameValuePair.TYPE_QUERY_STRING, paramName, value, i++);
                        listParams.add(vp);
                    }
                }
            }
        }
    } catch (URIException e) {
        LOG.error(e.getMessage() + uri, e);
    }
}
Also used : URIException(org.apache.commons.httpclient.URIException) Matcher(java.util.regex.Matcher)

Example 17 with URIException

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

the class VariantDdnPath method setParameter.

private String setParameter(HttpMessage msg, NameValuePair originalPair, String name, String value, boolean escaped) {
    URI uri = msg.getRequestHeader().getURI();
    String[] paths = uri.getEscapedPath().split("/");
    if (originalPair.getPosition() < paths.length) {
        String encodedValue = (escaped) ? value : getEscapedValue(value);
        paths[originalPair.getPosition()] = encodedValue;
        String path = String.join("/", paths);
        try {
            uri.setEscapedPath(path);
        } catch (URIException e) {
            // Looks like it wasn't escaped after all
            try {
                uri.setPath(path);
            } catch (URIException e1) {
                LOGGER.debug(e1.getMessage(), e1);
            }
            LOGGER.warn(e.getMessage(), e);
        }
    }
    return value;
}
Also used : URIException(org.apache.commons.httpclient.URIException) URI(org.apache.commons.httpclient.URI)

Example 18 with URIException

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

the class VariantODataFilterQuery method parse.

private void parse(URI uri) {
    try {
        String query = uri.getQuery();
        // Detection of a filter statement if any
        if (query != null) {
            Matcher matcher = patternFilterParameters.matcher(query);
            if (matcher.find()) {
                String filterExpression = "";
                filterExpression = matcher.group(1);
                int begin = query.indexOf(filterExpression);
                int end = begin + filterExpression.length();
                beforeFilterExpression = query.substring(0, begin);
                afterFilterExpression = query.substring(end);
                // Now scan the expression in order to identify all parameters
                mapParameters = new HashMap<>();
                Matcher matcherParameters = patternParameters.matcher(filterExpression);
                while (matcherParameters.find()) {
                    String nameOpAndValue = matcherParameters.group(0);
                    String paramName = matcherParameters.group(1);
                    String operator = matcherParameters.group(2);
                    String paramValue = matcherParameters.group(3);
                    begin = filterExpression.indexOf(nameOpAndValue);
                    end = begin + nameOpAndValue.length();
                    String before = filterExpression.substring(0, begin);
                    String after = filterExpression.substring(end);
                    OperationParameter opParam = new OperationParameter(paramName, operator, paramValue, before, after);
                    mapParameters.put(opParam.getParameterName(), opParam);
                }
            } else {
                beforeFilterExpression = null;
                afterFilterExpression = null;
                mapParameters = Collections.emptyMap();
            }
        } else {
            beforeFilterExpression = null;
            afterFilterExpression = null;
            mapParameters = Collections.emptyMap();
        }
    } catch (URIException e) {
        LOG.error(e.getMessage() + uri, e);
    }
}
Also used : URIException(org.apache.commons.httpclient.URIException) Matcher(java.util.regex.Matcher)

Example 19 with URIException

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

the class HttpMessage method hashCode.

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + getRequestHeader().getMethod().toLowerCase(Locale.ROOT).hashCode();
    URI uri = getRequestHeader().getURI();
    if (uri != null) {
        result = prime * result + uri.getPort();
        try {
            result = prime * result + (uri.getRawHost() == null ? 0 : uri.getHost().toLowerCase(Locale.ROOT).hashCode());
        } catch (URIException e) {
            log.error("Failed to obtain the host for hashCode calculation: " + uri.toString(), e);
        }
        result = prime * result + ((uri.getRawPathQuery() == null) ? 0 : uri.getEscapedPathQuery().toLowerCase(Locale.ROOT).hashCode());
    }
    if (getRequestHeader().getMethod().equalsIgnoreCase(HttpRequestHeader.POST)) {
        result = prime * result + getRequestBody().hashCode();
    }
    return result;
}
Also used : URIException(org.apache.commons.httpclient.URIException) URI(org.apache.commons.httpclient.URI)

Example 20 with URIException

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

the class HttpMessage method equalType.

/**
 * Compares this {@code HttpMessage} against another. Messages are equal type if the host, port,
 * path and parameter names are equal. Even though the query values may differ. For POST this
 * assumes x-www-form-urlencoded, for other types (such as JSON) this means that parameter names
 * and values (the full request body) could be included.
 *
 * @param msg the message against which this {@code HttpMessage} is being compared.
 * @return {@code true} if the messages are considered equal, {@code false} otherwise
 */
public boolean equalType(HttpMessage msg) {
    boolean result = false;
    // compare method
    if (!this.getRequestHeader().getMethod().equalsIgnoreCase(msg.getRequestHeader().getMethod())) {
        return false;
    }
    // compare host, port and URI
    URI uri1 = this.getRequestHeader().getURI();
    URI uri2 = msg.getRequestHeader().getURI();
    try {
        if (uri1.getHost() == null || uri2.getHost() == null || !uri1.getHost().equalsIgnoreCase(uri2.getHost())) {
            return false;
        }
        if (uri1.getPort() != uri2.getPort()) {
            return false;
        }
        String path1 = uri1.getPath();
        String path2 = uri2.getPath();
        if (path1 == null && path2 == null) {
            return true;
        }
        if (path1 != null && path2 != null && !path1.equalsIgnoreCase(path2)) {
            return false;
        } else {
            if (path1 == null || path2 == null) {
                return false;
            }
        }
        if (!queryEquals(msg)) {
            return false;
        }
        result = true;
    } catch (URIException e) {
        // ZAP: log error
        log.error(e.getMessage(), e);
    }
    return result;
}
Also used : URIException(org.apache.commons.httpclient.URIException) URI(org.apache.commons.httpclient.URI)

Aggregations

URIException (org.apache.commons.httpclient.URIException)52 URI (org.apache.commons.httpclient.URI)31 IOException (java.io.IOException)9 HttpMethod (org.apache.commons.httpclient.HttpMethod)8 Header (org.apache.commons.httpclient.Header)7 HttpClient (org.apache.commons.httpclient.HttpClient)6 ArrayList (java.util.ArrayList)5 Matcher (java.util.regex.Matcher)5 EntityEnclosingMethod (org.apache.commons.httpclient.methods.EntityEnclosingMethod)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 DatabaseException (org.parosproxy.paros.db.DatabaseException)5 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)4 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 InputStreamReader (java.io.InputStreamReader)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 HttpException (org.apache.commons.httpclient.HttpException)3 HttpMessage (org.parosproxy.paros.network.HttpMessage)3 InvalidParameterException (java.security.InvalidParameterException)2 HashMap (java.util.HashMap)2