protected void captureHeaders(HttpHeaders headers) { for (Map.Entry<String, String> header : headers.entries()) { harEntry.getRequest().getHeaders().add(new HarNameValuePair(header.getKey(), header.getValue())); } }
protected void captureRedirectUrl(HttpResponse httpResponse) { String locationHeaderValue = HttpHeaders.getHeader(httpResponse, HttpHeaders.Names.LOCATION); if (locationHeaderValue != null) { harEntry.getResponse().setRedirectURL(locationHeaderValue); } }
@Override public void onBindViewHolder(MyViewHolder holder, int position) { HarEntry harEntry = harEntryList.get(position); holder.rootView.setOnClickListener(new ClickListner(harEntry)); holder.tv.setText(harEntry.getRequest().getUrl()); if(harEntry.getResponse().getStatus()>400){ holder.iconView.setImageDrawable(getResources().getDrawable(R.drawable.ic_error_black_24dp)); }else if(harEntry.getResponse().getStatus()>300){ holder.iconView.setImageDrawable(getResources().getDrawable(R.drawable.ic_directions_black_24dp)); }else if(harEntry.getResponse().getContent().getMimeType().contains("image")) { holder.iconView.setImageDrawable(getResources().getDrawable(R.drawable.ic_photo_black_24dp)); }else{ holder.iconView.setImageDrawable(getResources().getDrawable(R.drawable.ic_description_black_24dp)); } holder.detailTextView.setText("Status:" + harEntry.getResponse().getStatus() + " Size:" + harEntry.getResponse().getBodySize() + "Bytes Time:" + harEntry.getTime() + "ms"); }
@Override public void proxyToServerResolutionSucceeded(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress) { long dnsResolutionFinishedNanos = System.nanoTime(); if (dnsResolutionStartedNanos > 0L) { harEntry.getTimings().setDns(dnsResolutionFinishedNanos - dnsResolutionStartedNanos, TimeUnit.NANOSECONDS); } else { harEntry.getTimings().setDns(0L, TimeUnit.NANOSECONDS); } // the address *should* always be resolved at this point InetAddress resolvedAddress = resolvedRemoteAddress.getAddress(); if (resolvedAddress != null) { addressResolved = true; harEntry.setServerIPAddress(resolvedAddress.getHostAddress()); } }
@Override public void proxyToServerResolutionFailed(String hostAndPort) { HarResponse response = HarCaptureUtil.createHarResponseForFailure(); harEntry.setResponse(response); response.setError(HarCaptureUtil.getResolutionFailedErrorMessage(hostAndPort)); // record the amount of time we attempted to resolve the hostname in the HarTimings object if (dnsResolutionStartedNanos > 0L) { harEntry.getTimings().setDns(System.nanoTime() - dnsResolutionStartedNanos, TimeUnit.NANOSECONDS); } }
harEntry.setStartedDateTime(new Date()); har.getLog().addEntry(harEntry); harEntry.setRequest(request); harEntry.setResponse(defaultHarResponse); harEntry.getRequest().setBodySize(requestBodySize.get());
/** * Creates a {@link HarEntry} for a failed CONNECT request. Initializes and populates the entry, including the * {@link HarRequest}, {@link HarResponse}, and {@link HarTimings}. (Note: only successful timing information is * populated in the timings object; the calling method must populate the timing information for the final, failed * step. For example, if DNS resolution failed, this method will populate the network 'blocked' time, but not the DNS * time.) Populates the specified errorMessage in the {@link HarResponse}'s error field. * * @param errorMessage error message to place in the har response * @return a new HAR entry */ private HarEntry createHarEntryForFailedCONNECT(String errorMessage) { HarEntry harEntry = new HarEntry(currentPageRef); harEntry.setStartedDateTime(requestStartTime); HarRequest request = createRequestForFailedConnect(originalRequest); harEntry.setRequest(request); HarResponse response = HarCaptureUtil.createHarResponseForFailure(); harEntry.setResponse(response); response.setError(errorMessage); populateTimingsForFailedCONNECT(harEntry); populateServerIpAddress(harEntry); return harEntry; }
System.out.println("request code: " + entry.getRequest().getMethod()); //Output request type System.out.println("request url: " + entry.getRequest().getUrl()); //Output request Url System.out.println(" start time: " + entry.getStartedDateTime().getTime()); // Output start time System.out.println(" time: " + entry.getTime()); // Output start time System.out.println("response code: " + entry.getResponse().getStatus()); //Output response code responseSize=entry.getResponse().getHeadersSize()+entry.getResponse().getBodySize(); long entryLoadTime = entry.getStartedDateTime().getTime() + entry.getTime();
protected void captureQueryParameters(HttpRequest httpRequest) { // capture query parameters. it is safe to assume the query string is UTF-8, since it "should" be in US-ASCII (a subset of UTF-8), // but sometimes does include UTF-8 characters. QueryStringDecoder queryStringDecoder = new QueryStringDecoder(httpRequest.getUri(), StandardCharsets.UTF_8); try { for (Map.Entry<String, List<String>> entry : queryStringDecoder.parameters().entrySet()) { for (String value : entry.getValue()) { harEntry.getRequest().getQueryString().add(new HarNameValuePair(entry.getKey(), value)); } } } catch (IllegalArgumentException e) { // QueryStringDecoder will throw an IllegalArgumentException if it cannot interpret a query string. rather than cause the entire request to // fail by propagating the exception, simply skip the query parameter capture. harEntry.setComment("Unable to decode query parameters on URI: " + httpRequest.getUri()); log.info("Unable to decode query parameters on URI: " + httpRequest.getUri(), e); } }
System.out.println("request code: " + entry.getRequest().getMethod()); //Output request type System.out.println(" start time: " + entry.getStartedDateTime().getTime()); // Output start time System.out.println(" time: " + entry.getTime()); // Output start time long entryLoadTime = entry.getStartedDateTime().getTime() + entry.getTime();
public void checkAssetsStatuses(String domain) { har = getHar(); for (HarEntry entry : har.getLog().getEntries()) { if (entry.getRequest().getUrl().contains(domain)) { Log.log( "RESPONSE STATUS: " + entry.getResponse().getStatus(), entry.getRequest().getUrl(), entry.getResponse().getStatus() < 400 ); } } }
protected void captureQueryParameters(HttpRequest httpRequest) { // capture query parameters. it is safe to assume the query string is UTF-8, since it "should" be in US-ASCII (a subset of UTF-8), // but sometimes does include UTF-8 characters. Log.e("InnerHandle", "captureQueryParameters " + harEntry.getId()); QueryStringDecoder queryStringDecoder = null; queryStringDecoder = new QueryStringDecoder(httpRequest.getUri(), Charset.forName("UTF-8")); try { for (Map.Entry<String, List<String>> entry : queryStringDecoder.parameters().entrySet()) { for (String value : entry.getValue()) { harEntry.getRequest().getQueryString().add(new HarNameValuePair(entry.getKey(), value)); } } } catch (IllegalArgumentException e) { // QueryStringDecoder will throw an IllegalArgumentException if it cannot interpret a query string. rather than cause the entire request to // fail by propagating the exception, simply skip the query parameter capture. harEntry.setComment("Unable to decode query parameters on URI: " + httpRequest.getUri()); log.info("Unable to decode query parameters on URI: " + httpRequest.getUri(), e); } }
@Override public InetSocketAddress proxyToServerResolutionStarted(String resolvingServerHostAndPort) { dnsResolutionStartedNanos = System.nanoTime(); // resolution started means the connection is no longer queued, so populate 'blocked' time if (connectionQueuedNanos > 0L) { harEntry.getTimings().setBlocked(dnsResolutionStartedNanos - connectionQueuedNanos, TimeUnit.NANOSECONDS); } else { harEntry.getTimings().setBlocked(0L, TimeUnit.NANOSECONDS); } return null; }
Log.e("Capture", "clientToProxyRequest " + harEntry.getId()); if (requestCaptureFilter != null) { requestCaptureFilter.clientToProxyRequest(httpObject); harEntry.setStartedDateTime(new Date()); har.getLog().addEntry(harEntry); harEntry.setRequest(request); harEntry.setResponse(defaultHarResponse);
@Override public void proxyToServerResolutionFailed(String hostAndPort) { Log.e("Capture", "proxyToServerResolutionFailed " + harEntry.getId()); HarResponse response = HarCaptureUtil.createHarResponseForFailure(); harEntry.setResponse(response); response.setError(HarCaptureUtil.getResolutionFailedErrorMessage(hostAndPort)); // record the amount of time we attempted to resolve the hostname in the HarTimings object if (dnsResolutionStartedNanos > 0L) { harEntry.getTimings().setDns(System.nanoTime() - dnsResolutionStartedNanos, TimeUnit.NANOSECONDS); } }
@Override public void proxyToServerResolutionSucceeded(String serverHostAndPort, InetSocketAddress resolvedRemoteAddress) { Log.e("Capture", "proxyToServerResolutionSucceeded " + harEntry.getId()); long dnsResolutionFinishedNanos = System.nanoTime(); if (dnsResolutionStartedNanos > 0L) { harEntry.getTimings().setDns(dnsResolutionFinishedNanos - dnsResolutionStartedNanos, TimeUnit.NANOSECONDS); } else { harEntry.getTimings().setDns(0L, TimeUnit.NANOSECONDS); } // the address *should* always be resolved at this point InetAddress resolvedAddress = resolvedRemoteAddress.getAddress(); if (resolvedAddress != null) { addressResolved = true; harEntry.setServerIPAddress(resolvedAddress.getHostAddress()); } }
private void populateServerIpAddress(HarEntry harEntry) { // populate the server IP address if it was resolved as part of this request. otherwise, populate the IP address from the cache. if (resolvedAddress != null) { harEntry.setServerIPAddress(resolvedAddress.getHostAddress()); } else { String serverHost = HttpUtil.getHostFromRequest(modifiedHttpRequest); if (serverHost != null && !serverHost.isEmpty()) { String resolvedAddress = ResolvedHostnameCacheFilter.getPreviouslyResolvedAddressForHost(serverHost); if (resolvedAddress != null) { harEntry.setServerIPAddress(resolvedAddress); } else { // the resolvedAddress may be null if the ResolvedHostnameCacheFilter has expired the entry (which is unlikely), // or in the far more common case that the proxy is using a chained proxy to connect to connect to the // remote host. since the chained proxy handles IP address resolution, the IP address in the HAR must be blank. log.trace("Unable to find cached IP address for host: {}. IP address in HAR entry will be blank.", serverHost); } } else { log.warn("Unable to identify host from request uri: {}", modifiedHttpRequest.getUri()); } } }
/** * Retrieves the time for this HarEntry in milliseconds. To retrieve the time in another time unit, use {@link #getTime(java.util.concurrent.TimeUnit)}. * Rather than storing the time directly, calculate the time from the HarTimings as required in the HAR spec. * From <a href="https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/HAR/Overview.html">https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/HAR/Overview.html</a>, * section <code>4.2.16 timings</code>: <pre> Following must be true in case there are no -1 values (entry is an object in log.entries) : entry.time == entry.timings.blocked + entry.timings.dns + entry.timings.connect + entry.timings.send + entry.timings.wait + entry.timings.receive; </pre> * @return time for this HAR entry, in milliseconds */ public long getTime() { return getTime(TimeUnit.MILLISECONDS); }
@Override public InetSocketAddress proxyToServerResolutionStarted(String resolvingServerHostAndPort) { Log.e("Capture", "proxyToServerResolutionStarted " + harEntry.getId()); dnsResolutionStartedNanos = System.nanoTime(); // resolution started means the connection is no longer queued, so populate 'blocked' time if (connectionQueuedNanos > 0L) { harEntry.getTimings().setBlocked(dnsResolutionStartedNanos - connectionQueuedNanos, TimeUnit.NANOSECONDS); } else { harEntry.getTimings().setBlocked(0L, TimeUnit.NANOSECONDS); } return null; }
protected void captureResponse(HttpResponse httpResponse) { HarResponse response = new HarResponse(httpResponse.getStatus().code(), httpResponse.getStatus().reasonPhrase(), httpResponse.getProtocolVersion().text()); harEntry.setResponse(response); captureResponseHeaderSize(httpResponse); captureResponseMimeType(httpResponse); if (dataToCapture.contains(CaptureType.RESPONSE_COOKIES)) { captureResponseCookies(httpResponse); } if (dataToCapture.contains(CaptureType.RESPONSE_HEADERS)) { captureResponseHeaders(httpResponse); } if (BrowserMobHttpUtil.isRedirect(httpResponse)) { captureRedirectUrl(httpResponse); } }