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;
}
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));
}
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)));
}
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)));
}
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/")));
}
Aggregations