Search in sources :

Example 6 with URI

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

the class StatsAPI method handleApiView.

@Override
public ApiResponse handleApiView(String name, JSONObject params) throws ApiException {
    ApiResponse result = null;
    InMemoryStats memStats = extension.getInMemoryStats();
    if (memStats == null) {
        throw new ApiException(ApiException.Type.DOES_NOT_EXIST);
    }
    if (VIEW_STATS.equals(name)) {
        Map<String, String> map = new TreeMap<>();
        for (Entry<String, Long> stat : memStats.getStats(this.getParam(params, PARAM_KEY_PREFIX, "")).entrySet()) {
            map.put(stat.getKey(), stat.getValue().toString());
        }
        result = new ApiResponseSet<String>(name, map);
    } else if (VIEW_ALL_SITES_STATS.equals(name)) {
        result = new ApiResponseList(name);
        for (Entry<String, Map<String, Long>> stats : memStats.getAllSiteStats(this.getParam(params, PARAM_KEY_PREFIX, "")).entrySet()) {
            ((ApiResponseList) result).addItem(new SiteStatsApiResponse(stats.getKey(), stats.getValue()));
        }
    } else if (VIEW_SITE_STATS.equals(name)) {
        String site = params.getString(PARAM_SITE);
        URI siteURI;
        try {
            siteURI = new URI(site, true);
            site = SessionStructure.getHostName(siteURI);
        } catch (Exception e) {
            throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_SITE);
        }
        String scheme = siteURI.getScheme();
        if (scheme == null || (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https"))) {
            throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, PARAM_SITE);
        }
        result = new SiteStatsApiResponse(site, memStats.getSiteStats(site, this.getParam(params, PARAM_KEY_PREFIX, "")));
    } else {
        throw new ApiException(ApiException.Type.BAD_VIEW);
    }
    return result;
}
Also used : TreeMap(java.util.TreeMap) URI(org.apache.commons.httpclient.URI) ApiResponse(org.zaproxy.zap.extension.api.ApiResponse) ApiException(org.zaproxy.zap.extension.api.ApiException) Entry(java.util.Map.Entry) ApiResponseList(org.zaproxy.zap.extension.api.ApiResponseList) ApiException(org.zaproxy.zap.extension.api.ApiException)

Example 7 with URI

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

the class StandardParameterParserUnitTest method ansestorPath.

/**
	 * Gets the path of the URI's ancestor found at the given depth, taking into account any context
	 * specific configuration (e.g. structural parameters). The depth could also be seen as the
	 * number of path elements returned.
	 * <p/>
	 * A few examples (uri, depth):
	 * <ul>
	 * <li>(<i>http://example.org/path/to/element</i>, 0) -> ""</li>
	 * <li>(<i>http://example.org/path/to/element</i>, 1) -> "/path"</li>
	 * <li>(<i>http://example.org/path/to/element</i>, 3) -> "/path/to/element"</li>
	 * <li>(<i>http://example.org/path?page=12&data=123</i>, 2) -> "/path?page=12", if {@code page}
	 * is a structural parameter</li>
	 * <li>(<i>http://example.org/path?page=12&data=123&type=1</i>, 3) -> "/path?page=12&type=1", if
	 * {@code page} and {@code type} are both structural parameter</li>
	 * </ul>
	 * @throws NullPointerException 
	 * 
	 * @throws URIException if an error occurred while accessing the provided uri
	 */
@Test
public void ansestorPath() throws Exception {
    // standard urls
    assertEquals("", spp.getAncestorPath(new URI("http://example.org/path/to/element", true), 0));
    assertEquals("/path", spp.getAncestorPath(new URI("http://example.org/path/to/element", true), 1));
    assertEquals("/path/to", spp.getAncestorPath(new URI("http://example.org/path/to/element", true), 2));
    assertEquals("/path/to/element", spp.getAncestorPath(new URI("http://example.org/path/to/element", true), 3));
    assertEquals("/path", spp.getAncestorPath(new URI("http://example.org/path?page=12&data=123", true), 3));
    assertEquals("/path", spp.getAncestorPath(new URI("http://example.org/path?page=12&data=123&type=1", true), 3));
    // With structural params
    List<String> structuralParameters = new ArrayList<String>();
    structuralParameters.add("page");
    structuralParameters.add("type");
    spp.setStructuralParameters(structuralParameters);
    assertEquals("/path?page=12", spp.getAncestorPath(new URI("http://example.org/path?page=12&data=123", true), 3));
    assertEquals("/path?page=12&type=1", spp.getAncestorPath(new URI("http://example.org/path?page=12&data=123&type=1", true), 3));
    // with data driven nodes
    Context context = new Context(session, 0);
    Pattern p = Pattern.compile("http://example.org/(path/to/)(.+?)(/.*)");
    StructuralNodeModifier ddn = new StructuralNodeModifier(StructuralNodeModifier.Type.DataDrivenNode, p, "DDN");
    context.addDataDrivenNodes(ddn);
    spp.setContext(context);
    assertEquals("/path/to/(.+?)", spp.getAncestorPath(new URI("http://example.org/path/to/ddn/aa", true), 3));
    assertEquals("/path/to/(.+?)/aa", spp.getAncestorPath(new URI("http://example.org/path/to/ddn/aa", true), 4));
}
Also used : Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) URI(org.apache.commons.httpclient.URI) Test(org.junit.Test)

Example 8 with URI

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

the class HttpPrefixFetchFilterUnitTest method shouldFilterUriWithDifferentSchemeAsOutOfScope.

@Test
public void shouldFilterUriWithDifferentSchemeAsOutOfScope() throws Exception {
    // Given
    URI prefixUri = new URI("http://example.org/", true);
    HttpPrefixFetchFilter fetchFilter = new HttpPrefixFetchFilter(prefixUri);
    URI uri = new URI("https://example.org/", true);
    // When
    FetchStatus filterStatus = fetchFilter.checkFilter(uri);
    // Then
    assertThat(filterStatus, is(equalTo(FetchStatus.OUT_OF_SCOPE)));
}
Also used : URI(org.apache.commons.httpclient.URI) FetchStatus(org.zaproxy.zap.spider.filters.FetchFilter.FetchStatus) Test(org.junit.Test)

Example 9 with URI

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

the class HttpPrefixFetchFilterUnitTest method shouldFilterUriWithMalformedHostAsOutOfScope.

@Test
public void shouldFilterUriWithMalformedHostAsOutOfScope() throws Exception {
    // Given
    URI prefixUri = new URI("http://example.org/", true);
    HttpPrefixFetchFilter fetchFilter = new HttpPrefixFetchFilter(prefixUri);
    URI uri = new URI("http://a%0/", true);
    // When
    FetchStatus filterStatus = fetchFilter.checkFilter(uri);
    // Then
    assertThat(filterStatus, is(equalTo(FetchStatus.OUT_OF_SCOPE)));
}
Also used : URI(org.apache.commons.httpclient.URI) FetchStatus(org.zaproxy.zap.spider.filters.FetchFilter.FetchStatus) Test(org.junit.Test)

Example 10 with URI

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

the class HttpPrefixFetchFilterUnitTest method shouldKeepDefaultHttpsPortInHttpPrefix.

@Test
public void shouldKeepDefaultHttpsPortInHttpPrefix() throws Exception {
    // Given
    URI prefixUri = new URI("http://example.org:443/", true);
    HttpPrefixFetchFilter fetchFilter = new HttpPrefixFetchFilter(prefixUri);
    // When
    String normalisedPrefix = fetchFilter.getNormalisedPrefix();
    // Then
    assertThat(normalisedPrefix, is(equalTo("http://example.org:443/")));
}
Also used : URI(org.apache.commons.httpclient.URI) Test(org.junit.Test)

Aggregations

URI (org.apache.commons.httpclient.URI)135 Test (org.junit.Test)72 FetchStatus (org.zaproxy.zap.spider.filters.FetchFilter.FetchStatus)33 URIException (org.apache.commons.httpclient.URIException)32 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 IOException (java.io.IOException)6 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)6 HandleParametersOption (org.zaproxy.zap.spider.SpiderParam.HandleParametersOption)6 Header (org.apache.commons.httpclient.Header)5 InvalidParameterException (java.security.InvalidParameterException)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 Cookie (org.apache.commons.httpclient.Cookie)3 EntityEnclosingMethod (org.apache.commons.httpclient.methods.EntityEnclosingMethod)3 StructuralNode (org.zaproxy.zap.model.StructuralNode)3 File (java.io.File)2 HashMap (java.util.HashMap)2