@Test public void headersAndPostParams() throws Exception { MockHttpStack mockHttpStack = new MockHttpStack(); final NetworkResponse fakeResponse = new NetworkResponse(200, "foobar".getBytes(), new Headers .Builder().add("requestheader", "foo").build(), 0); mockHttpStack.setResponseToReturn(fakeResponse); HttpNetwork httpNetwork = new HttpNetwork(mockHttpStack); Request<String> request = new Request<String>(Request.Method.GET, "http: @Override protected void deliverResponse(String response) { } }; request.setNetworkRequest(new FormEncodingBuilder() .add("requestpost", "foo") .builder().setHeader("requestheader", "foo").build()); httpNetwork.performRequest(request); assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader")); assertEquals("requestpost=foo", new String(mockHttpStack.getLastPostBody())); }
@Override public NetworkResponse performRequest(Request<?> request) throws JusError { long requestStart = System.nanoTime(); boolean serverAuthRequested = false; boolean proxyAuthRequested = false; while (true) { if (request.isCanceled()) { return null; } if (request.getNoConnectionPolicy() != null && request.getConnectivityManager() != null) { if (request.getConnectivityManager().getActiveNetwork() == null || !request.getConnectivityManager().getActiveNetwork().isConnected()) { JusError error = request.getNoConnectionPolicy().throwOnNoConnection(request); if (error != null) { throw error; } } } NetworkResponse httpResponse = null; try { Headers.Builder headers = new Headers.Builder(); addCacheHeaders(headers, request.getCacheEntry()); addServerAuthHeaders(request.getServerAuthenticator(), headers); addProxyAuthHeaders(request.getProxyAuthenticator(), headers); Headers extraHeaders = headers.build(); request.addMarker(Request.EVENT_NETWORK_STACK_SEND, request.getNetworkRequest(), extraHeaders); httpResponse = httpStack.performRequest(request, extraHeaders, pool); if (httpResponse == null) { throw new NetworkError("No Response"); } if (request.getRedirectPolicy() != null) { Request newR = request.getRedirectPolicy().verifyRedirect(request, httpResponse); Headers rHeaders = new Headers.Builder().build(); while (newR != null) { request.addMarker(Request.EVENT_NETWORK_STACK_REDIRECT_SEND, newR); httpResponse = httpStack.performRequest(newR, rHeaders, pool); request.addMarker(Request.EVENT_NETWORK_STACK_REDIRECT_COMPLETE, httpResponse); newR = request.getRedirectPolicy().verifyRedirect(request, httpResponse); } } request.addMarker(Request.EVENT_NETWORK_STACK_COMPLETE, httpResponse); if (request.getRequestQueue() != null) { httpResponse = request.getRequestQueue().transformResponse(request, httpResponse); request.addMarker(Request.EVENT_NETWORK_TRANSFORM_COMPLETE, httpResponse); } if (httpResponse != null && httpResponse.headers != null) { String contentLen = httpResponse.headers.get(HTTP.CONTENT_LEN); if (contentLen != null) { int cLen = Integer.parseInt(contentLen); if (cLen > httpResponse.data.length && request.getMethod() != Request.Method.HEAD) { throw new NetworkError(httpResponse, "Response Body not completely " + "received"); } } } if (httpResponse.statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) { request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR, httpResponse); if (request.getServerAuthenticator() != null && !serverAuthRequested) { request.getServerAuthenticator().clearAuthValue(); try { request.getServerAuthenticator().getAuthValue(); } catch (AuthError authError) { throw authError; } request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR_RESEND, httpResponse); serverAuthRequested = true; continue; } else { throw new AuthError(httpResponse); } } else if (httpResponse.statusCode == HttpURLConnection.HTTP_PROXY_AUTH) { request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_PROXY_ERROR, httpResponse); if (request.getProxyAuthenticator() != null && !proxyAuthRequested) { request.getProxyAuthenticator().clearAuthValue(); try { request.getProxyAuthenticator().getAuthValue(); } catch (AuthError authError) { throw authError; } request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR_RESEND, httpResponse); proxyAuthRequested = true; continue; } else { throw new AuthError(httpResponse); } } long requestLifetime = System.nanoTime() - requestStart; logSlowRequests(requestLifetime, request, httpResponse.data, httpResponse .statusCode); if (httpResponse.statusCode == HttpURLConnection.HTTP_NOT_MODIFIED) { Entry entry = request.getCacheEntry(); if (entry != null) { final Map<String, List<String>> responseHeaders = httpResponse.headers.toMultimap(); final Map<String, List<String>> cacheHeaders = entry.responseHeaders.toMultimap(); Headers.Builder hBuilder = new Headers.Builder(); hBuilder.addMMap(responseHeaders); if (cacheHeaders != null) { for (Map.Entry<String, List<String>> hentry : cacheHeaders.entrySet()) { if (hentry.getKey() != null && !responseHeaders.containsKey(hentry.getKey())) { hBuilder.add(hentry.getKey(), hentry.getValue()); } } } httpResponse = new NetworkResponse( httpResponse.statusCode, entry.data, hBuilder.build(), System.nanoTime() - requestStart ); } else { httpResponse = new NetworkResponse( httpResponse.statusCode, null, httpResponse.headers, System.nanoTime() - requestStart ); } } else if (httpResponse.statusCode < 200 || httpResponse.statusCode > 299) { throw new IOException(); } return httpResponse; } catch (SocketTimeoutException e) { attemptRetryOnException("socket", request, new TimeoutError("socket Timeout", e)); } catch (MalformedURLException e) { throw new RuntimeException("Bad URL " + request.getUrlString(), e); } catch (IOException e) { NetworkResponse networkResponse; if (httpResponse != null) { networkResponse = httpResponse; } else { throw new NetworkError(e); } if (networkResponse != null) { if (networkResponse.statusCode == HttpURLConnection .HTTP_CLIENT_TIMEOUT) { attemptRetryOnException("http-client", request, new RequestError (networkResponse, "HTTP_CLIENT_TIMEOUT")); } else if (networkResponse.statusCode > 399 && networkResponse.statusCode < 500) { throw new RequestError(networkResponse); } else if (networkResponse.statusCode > 499) { attemptRetryOnException("server", request, new ServerError(networkResponse)); } else { throw new JusError(networkResponse, e); } } else { attemptRetryOnException("network", request, new NetworkError()); } } catch (AuthError authError) { throw authError; } } }
HttpNetwork implements Network { @Override public NetworkResponse performRequest(Request<?> request) throws JusError { long requestStart = System.nanoTime(); boolean serverAuthRequested = false; boolean proxyAuthRequested = false; while (true) { if (request.isCanceled()) { return null; } if (request.getNoConnectionPolicy() != null && request.getConnectivityManager() != null) { if (request.getConnectivityManager().getActiveNetwork() == null || !request.getConnectivityManager().getActiveNetwork().isConnected()) { JusError error = request.getNoConnectionPolicy().throwOnNoConnection(request); if (error != null) { throw error; } } } NetworkResponse httpResponse = null; try { Headers.Builder headers = new Headers.Builder(); addCacheHeaders(headers, request.getCacheEntry()); addServerAuthHeaders(request.getServerAuthenticator(), headers); addProxyAuthHeaders(request.getProxyAuthenticator(), headers); Headers extraHeaders = headers.build(); request.addMarker(Request.EVENT_NETWORK_STACK_SEND, request.getNetworkRequest(), extraHeaders); httpResponse = httpStack.performRequest(request, extraHeaders, pool); if (httpResponse == null) { throw new NetworkError("No Response"); } if (request.getRedirectPolicy() != null) { Request newR = request.getRedirectPolicy().verifyRedirect(request, httpResponse); Headers rHeaders = new Headers.Builder().build(); while (newR != null) { request.addMarker(Request.EVENT_NETWORK_STACK_REDIRECT_SEND, newR); httpResponse = httpStack.performRequest(newR, rHeaders, pool); request.addMarker(Request.EVENT_NETWORK_STACK_REDIRECT_COMPLETE, httpResponse); newR = request.getRedirectPolicy().verifyRedirect(request, httpResponse); } } request.addMarker(Request.EVENT_NETWORK_STACK_COMPLETE, httpResponse); if (request.getRequestQueue() != null) { httpResponse = request.getRequestQueue().transformResponse(request, httpResponse); request.addMarker(Request.EVENT_NETWORK_TRANSFORM_COMPLETE, httpResponse); } if (httpResponse != null && httpResponse.headers != null) { String contentLen = httpResponse.headers.get(HTTP.CONTENT_LEN); if (contentLen != null) { int cLen = Integer.parseInt(contentLen); if (cLen > httpResponse.data.length && request.getMethod() != Request.Method.HEAD) { throw new NetworkError(httpResponse, "Response Body not completely " + "received"); } } } if (httpResponse.statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) { request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR, httpResponse); if (request.getServerAuthenticator() != null && !serverAuthRequested) { request.getServerAuthenticator().clearAuthValue(); try { request.getServerAuthenticator().getAuthValue(); } catch (AuthError authError) { throw authError; } request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR_RESEND, httpResponse); serverAuthRequested = true; continue; } else { throw new AuthError(httpResponse); } } else if (httpResponse.statusCode == HttpURLConnection.HTTP_PROXY_AUTH) { request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_PROXY_ERROR, httpResponse); if (request.getProxyAuthenticator() != null && !proxyAuthRequested) { request.getProxyAuthenticator().clearAuthValue(); try { request.getProxyAuthenticator().getAuthValue(); } catch (AuthError authError) { throw authError; } request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR_RESEND, httpResponse); proxyAuthRequested = true; continue; } else { throw new AuthError(httpResponse); } } long requestLifetime = System.nanoTime() - requestStart; logSlowRequests(requestLifetime, request, httpResponse.data, httpResponse .statusCode); if (httpResponse.statusCode == HttpURLConnection.HTTP_NOT_MODIFIED) { Entry entry = request.getCacheEntry(); if (entry != null) { final Map<String, List<String>> responseHeaders = httpResponse.headers.toMultimap(); final Map<String, List<String>> cacheHeaders = entry.responseHeaders.toMultimap(); Headers.Builder hBuilder = new Headers.Builder(); hBuilder.addMMap(responseHeaders); if (cacheHeaders != null) { for (Map.Entry<String, List<String>> hentry : cacheHeaders.entrySet()) { if (hentry.getKey() != null && !responseHeaders.containsKey(hentry.getKey())) { hBuilder.add(hentry.getKey(), hentry.getValue()); } } } httpResponse = new NetworkResponse( httpResponse.statusCode, entry.data, hBuilder.build(), System.nanoTime() - requestStart ); } else { httpResponse = new NetworkResponse( httpResponse.statusCode, null, httpResponse.headers, System.nanoTime() - requestStart ); } } else if (httpResponse.statusCode < 200 || httpResponse.statusCode > 299) { throw new IOException(); } return httpResponse; } catch (SocketTimeoutException e) { attemptRetryOnException("socket", request, new TimeoutError("socket Timeout", e)); } catch (MalformedURLException e) { throw new RuntimeException("Bad URL " + request.getUrlString(), e); } catch (IOException e) { NetworkResponse networkResponse; if (httpResponse != null) { networkResponse = httpResponse; } else { throw new NetworkError(e); } if (networkResponse != null) { if (networkResponse.statusCode == HttpURLConnection .HTTP_CLIENT_TIMEOUT) { attemptRetryOnException("http-client", request, new RequestError (networkResponse, "HTTP_CLIENT_TIMEOUT")); } else if (networkResponse.statusCode > 399 && networkResponse.statusCode < 500) { throw new RequestError(networkResponse); } else if (networkResponse.statusCode > 499) { attemptRetryOnException("server", request, new ServerError(networkResponse)); } else { throw new JusError(networkResponse, e); } } else { attemptRetryOnException("network", request, new NetworkError()); } } catch (AuthError authError) { throw authError; } } } }
HttpNetwork implements Network { @Override public NetworkResponse performRequest(Request<?> request) throws JusError { long requestStart = System.nanoTime(); boolean serverAuthRequested = false; boolean proxyAuthRequested = false; while (true) { if (request.isCanceled()) { return null; } if (request.getNoConnectionPolicy() != null && request.getConnectivityManager() != null) { if (request.getConnectivityManager().getActiveNetwork() == null || !request.getConnectivityManager().getActiveNetwork().isConnected()) { JusError error = request.getNoConnectionPolicy().throwOnNoConnection(request); if (error != null) { throw error; } } } NetworkResponse httpResponse = null; try { Headers.Builder headers = new Headers.Builder(); addCacheHeaders(headers, request.getCacheEntry()); addServerAuthHeaders(request.getServerAuthenticator(), headers); addProxyAuthHeaders(request.getProxyAuthenticator(), headers); Headers extraHeaders = headers.build(); request.addMarker(Request.EVENT_NETWORK_STACK_SEND, request.getNetworkRequest(), extraHeaders); httpResponse = httpStack.performRequest(request, extraHeaders, pool); if (httpResponse == null) { throw new NetworkError("No Response"); } if (request.getRedirectPolicy() != null) { Request newR = request.getRedirectPolicy().verifyRedirect(request, httpResponse); Headers rHeaders = new Headers.Builder().build(); while (newR != null) { request.addMarker(Request.EVENT_NETWORK_STACK_REDIRECT_SEND, newR); httpResponse = httpStack.performRequest(newR, rHeaders, pool); request.addMarker(Request.EVENT_NETWORK_STACK_REDIRECT_COMPLETE, httpResponse); newR = request.getRedirectPolicy().verifyRedirect(request, httpResponse); } } request.addMarker(Request.EVENT_NETWORK_STACK_COMPLETE, httpResponse); if (request.getRequestQueue() != null) { httpResponse = request.getRequestQueue().transformResponse(request, httpResponse); request.addMarker(Request.EVENT_NETWORK_TRANSFORM_COMPLETE, httpResponse); } if (httpResponse != null && httpResponse.headers != null) { String contentLen = httpResponse.headers.get(HTTP.CONTENT_LEN); if (contentLen != null) { int cLen = Integer.parseInt(contentLen); if (cLen > httpResponse.data.length && request.getMethod() != Request.Method.HEAD) { throw new NetworkError(httpResponse, "Response Body not completely " + "received"); } } } if (httpResponse.statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) { request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR, httpResponse); if (request.getServerAuthenticator() != null && !serverAuthRequested) { request.getServerAuthenticator().clearAuthValue(); try { request.getServerAuthenticator().getAuthValue(); } catch (AuthError authError) { throw authError; } request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR_RESEND, httpResponse); serverAuthRequested = true; continue; } else { throw new AuthError(httpResponse); } } else if (httpResponse.statusCode == HttpURLConnection.HTTP_PROXY_AUTH) { request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_PROXY_ERROR, httpResponse); if (request.getProxyAuthenticator() != null && !proxyAuthRequested) { request.getProxyAuthenticator().clearAuthValue(); try { request.getProxyAuthenticator().getAuthValue(); } catch (AuthError authError) { throw authError; } request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR_RESEND, httpResponse); proxyAuthRequested = true; continue; } else { throw new AuthError(httpResponse); } } long requestLifetime = System.nanoTime() - requestStart; logSlowRequests(requestLifetime, request, httpResponse.data, httpResponse .statusCode); if (httpResponse.statusCode == HttpURLConnection.HTTP_NOT_MODIFIED) { Entry entry = request.getCacheEntry(); if (entry != null) { final Map<String, List<String>> responseHeaders = httpResponse.headers.toMultimap(); final Map<String, List<String>> cacheHeaders = entry.responseHeaders.toMultimap(); Headers.Builder hBuilder = new Headers.Builder(); hBuilder.addMMap(responseHeaders); if (cacheHeaders != null) { for (Map.Entry<String, List<String>> hentry : cacheHeaders.entrySet()) { if (hentry.getKey() != null && !responseHeaders.containsKey(hentry.getKey())) { hBuilder.add(hentry.getKey(), hentry.getValue()); } } } httpResponse = new NetworkResponse( httpResponse.statusCode, entry.data, hBuilder.build(), System.nanoTime() - requestStart ); } else { httpResponse = new NetworkResponse( httpResponse.statusCode, null, httpResponse.headers, System.nanoTime() - requestStart ); } } else if (httpResponse.statusCode < 200 || httpResponse.statusCode > 299) { throw new IOException(); } return httpResponse; } catch (SocketTimeoutException e) { attemptRetryOnException("socket", request, new TimeoutError("socket Timeout", e)); } catch (MalformedURLException e) { throw new RuntimeException("Bad URL " + request.getUrlString(), e); } catch (IOException e) { NetworkResponse networkResponse; if (httpResponse != null) { networkResponse = httpResponse; } else { throw new NetworkError(e); } if (networkResponse != null) { if (networkResponse.statusCode == HttpURLConnection .HTTP_CLIENT_TIMEOUT) { attemptRetryOnException("http-client", request, new RequestError (networkResponse, "HTTP_CLIENT_TIMEOUT")); } else if (networkResponse.statusCode > 399 && networkResponse.statusCode < 500) { throw new RequestError(networkResponse); } else if (networkResponse.statusCode > 499) { attemptRetryOnException("server", request, new ServerError(networkResponse)); } else { throw new JusError(networkResponse, e); } } else { attemptRetryOnException("network", request, new NetworkError()); } } catch (AuthError authError) { throw authError; } } } HttpNetwork(HttpStack httpStack);  HttpNetwork(HttpStack httpStack, ByteArrayPool pool); }
HttpNetwork implements Network { @Override public NetworkResponse performRequest(Request<?> request) throws JusError { long requestStart = System.nanoTime(); boolean serverAuthRequested = false; boolean proxyAuthRequested = false; while (true) { if (request.isCanceled()) { return null; } if (request.getNoConnectionPolicy() != null && request.getConnectivityManager() != null) { if (request.getConnectivityManager().getActiveNetwork() == null || !request.getConnectivityManager().getActiveNetwork().isConnected()) { JusError error = request.getNoConnectionPolicy().throwOnNoConnection(request); if (error != null) { throw error; } } } NetworkResponse httpResponse = null; try { Headers.Builder headers = new Headers.Builder(); addCacheHeaders(headers, request.getCacheEntry()); addServerAuthHeaders(request.getServerAuthenticator(), headers); addProxyAuthHeaders(request.getProxyAuthenticator(), headers); Headers extraHeaders = headers.build(); request.addMarker(Request.EVENT_NETWORK_STACK_SEND, request.getNetworkRequest(), extraHeaders); httpResponse = httpStack.performRequest(request, extraHeaders, pool); if (httpResponse == null) { throw new NetworkError("No Response"); } if (request.getRedirectPolicy() != null) { Request newR = request.getRedirectPolicy().verifyRedirect(request, httpResponse); Headers rHeaders = new Headers.Builder().build(); while (newR != null) { request.addMarker(Request.EVENT_NETWORK_STACK_REDIRECT_SEND, newR); httpResponse = httpStack.performRequest(newR, rHeaders, pool); request.addMarker(Request.EVENT_NETWORK_STACK_REDIRECT_COMPLETE, httpResponse); newR = request.getRedirectPolicy().verifyRedirect(request, httpResponse); } } request.addMarker(Request.EVENT_NETWORK_STACK_COMPLETE, httpResponse); if (request.getRequestQueue() != null) { httpResponse = request.getRequestQueue().transformResponse(request, httpResponse); request.addMarker(Request.EVENT_NETWORK_TRANSFORM_COMPLETE, httpResponse); } if (httpResponse != null && httpResponse.headers != null) { String contentLen = httpResponse.headers.get(HTTP.CONTENT_LEN); if (contentLen != null) { int cLen = Integer.parseInt(contentLen); if (cLen > httpResponse.data.length && request.getMethod() != Request.Method.HEAD) { throw new NetworkError(httpResponse, "Response Body not completely " + "received"); } } } if (httpResponse.statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) { request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR, httpResponse); if (request.getServerAuthenticator() != null && !serverAuthRequested) { request.getServerAuthenticator().clearAuthValue(); try { request.getServerAuthenticator().getAuthValue(); } catch (AuthError authError) { throw authError; } request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR_RESEND, httpResponse); serverAuthRequested = true; continue; } else { throw new AuthError(httpResponse); } } else if (httpResponse.statusCode == HttpURLConnection.HTTP_PROXY_AUTH) { request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_PROXY_ERROR, httpResponse); if (request.getProxyAuthenticator() != null && !proxyAuthRequested) { request.getProxyAuthenticator().clearAuthValue(); try { request.getProxyAuthenticator().getAuthValue(); } catch (AuthError authError) { throw authError; } request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR_RESEND, httpResponse); proxyAuthRequested = true; continue; } else { throw new AuthError(httpResponse); } } long requestLifetime = System.nanoTime() - requestStart; logSlowRequests(requestLifetime, request, httpResponse.data, httpResponse .statusCode); if (httpResponse.statusCode == HttpURLConnection.HTTP_NOT_MODIFIED) { Entry entry = request.getCacheEntry(); if (entry != null) { final Map<String, List<String>> responseHeaders = httpResponse.headers.toMultimap(); final Map<String, List<String>> cacheHeaders = entry.responseHeaders.toMultimap(); Headers.Builder hBuilder = new Headers.Builder(); hBuilder.addMMap(responseHeaders); if (cacheHeaders != null) { for (Map.Entry<String, List<String>> hentry : cacheHeaders.entrySet()) { if (hentry.getKey() != null && !responseHeaders.containsKey(hentry.getKey())) { hBuilder.add(hentry.getKey(), hentry.getValue()); } } } httpResponse = new NetworkResponse( httpResponse.statusCode, entry.data, hBuilder.build(), System.nanoTime() - requestStart ); } else { httpResponse = new NetworkResponse( httpResponse.statusCode, null, httpResponse.headers, System.nanoTime() - requestStart ); } } else if (httpResponse.statusCode < 200 || httpResponse.statusCode > 299) { throw new IOException(); } return httpResponse; } catch (SocketTimeoutException e) { attemptRetryOnException("socket", request, new TimeoutError("socket Timeout", e)); } catch (MalformedURLException e) { throw new RuntimeException("Bad URL " + request.getUrlString(), e); } catch (IOException e) { NetworkResponse networkResponse; if (httpResponse != null) { networkResponse = httpResponse; } else { throw new NetworkError(e); } if (networkResponse != null) { if (networkResponse.statusCode == HttpURLConnection .HTTP_CLIENT_TIMEOUT) { attemptRetryOnException("http-client", request, new RequestError (networkResponse, "HTTP_CLIENT_TIMEOUT")); } else if (networkResponse.statusCode > 399 && networkResponse.statusCode < 500) { throw new RequestError(networkResponse); } else if (networkResponse.statusCode > 499) { attemptRetryOnException("server", request, new ServerError(networkResponse)); } else { throw new JusError(networkResponse, e); } } else { attemptRetryOnException("network", request, new NetworkError()); } } catch (AuthError authError) { throw authError; } } } HttpNetwork(HttpStack httpStack);  HttpNetwork(HttpStack httpStack, ByteArrayPool pool); @Override NetworkResponse performRequest(Request<?> request); }
HttpNetwork implements Network { @Override public NetworkResponse performRequest(Request<?> request) throws JusError { long requestStart = System.nanoTime(); boolean serverAuthRequested = false; boolean proxyAuthRequested = false; while (true) { if (request.isCanceled()) { return null; } if (request.getNoConnectionPolicy() != null && request.getConnectivityManager() != null) { if (request.getConnectivityManager().getActiveNetwork() == null || !request.getConnectivityManager().getActiveNetwork().isConnected()) { JusError error = request.getNoConnectionPolicy().throwOnNoConnection(request); if (error != null) { throw error; } } } NetworkResponse httpResponse = null; try { Headers.Builder headers = new Headers.Builder(); addCacheHeaders(headers, request.getCacheEntry()); addServerAuthHeaders(request.getServerAuthenticator(), headers); addProxyAuthHeaders(request.getProxyAuthenticator(), headers); Headers extraHeaders = headers.build(); request.addMarker(Request.EVENT_NETWORK_STACK_SEND, request.getNetworkRequest(), extraHeaders); httpResponse = httpStack.performRequest(request, extraHeaders, pool); if (httpResponse == null) { throw new NetworkError("No Response"); } if (request.getRedirectPolicy() != null) { Request newR = request.getRedirectPolicy().verifyRedirect(request, httpResponse); Headers rHeaders = new Headers.Builder().build(); while (newR != null) { request.addMarker(Request.EVENT_NETWORK_STACK_REDIRECT_SEND, newR); httpResponse = httpStack.performRequest(newR, rHeaders, pool); request.addMarker(Request.EVENT_NETWORK_STACK_REDIRECT_COMPLETE, httpResponse); newR = request.getRedirectPolicy().verifyRedirect(request, httpResponse); } } request.addMarker(Request.EVENT_NETWORK_STACK_COMPLETE, httpResponse); if (request.getRequestQueue() != null) { httpResponse = request.getRequestQueue().transformResponse(request, httpResponse); request.addMarker(Request.EVENT_NETWORK_TRANSFORM_COMPLETE, httpResponse); } if (httpResponse != null && httpResponse.headers != null) { String contentLen = httpResponse.headers.get(HTTP.CONTENT_LEN); if (contentLen != null) { int cLen = Integer.parseInt(contentLen); if (cLen > httpResponse.data.length && request.getMethod() != Request.Method.HEAD) { throw new NetworkError(httpResponse, "Response Body not completely " + "received"); } } } if (httpResponse.statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) { request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR, httpResponse); if (request.getServerAuthenticator() != null && !serverAuthRequested) { request.getServerAuthenticator().clearAuthValue(); try { request.getServerAuthenticator().getAuthValue(); } catch (AuthError authError) { throw authError; } request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR_RESEND, httpResponse); serverAuthRequested = true; continue; } else { throw new AuthError(httpResponse); } } else if (httpResponse.statusCode == HttpURLConnection.HTTP_PROXY_AUTH) { request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_PROXY_ERROR, httpResponse); if (request.getProxyAuthenticator() != null && !proxyAuthRequested) { request.getProxyAuthenticator().clearAuthValue(); try { request.getProxyAuthenticator().getAuthValue(); } catch (AuthError authError) { throw authError; } request.addMarker(Request.EVENT_NETWORK_STACK_AUTH_ERROR_RESEND, httpResponse); proxyAuthRequested = true; continue; } else { throw new AuthError(httpResponse); } } long requestLifetime = System.nanoTime() - requestStart; logSlowRequests(requestLifetime, request, httpResponse.data, httpResponse .statusCode); if (httpResponse.statusCode == HttpURLConnection.HTTP_NOT_MODIFIED) { Entry entry = request.getCacheEntry(); if (entry != null) { final Map<String, List<String>> responseHeaders = httpResponse.headers.toMultimap(); final Map<String, List<String>> cacheHeaders = entry.responseHeaders.toMultimap(); Headers.Builder hBuilder = new Headers.Builder(); hBuilder.addMMap(responseHeaders); if (cacheHeaders != null) { for (Map.Entry<String, List<String>> hentry : cacheHeaders.entrySet()) { if (hentry.getKey() != null && !responseHeaders.containsKey(hentry.getKey())) { hBuilder.add(hentry.getKey(), hentry.getValue()); } } } httpResponse = new NetworkResponse( httpResponse.statusCode, entry.data, hBuilder.build(), System.nanoTime() - requestStart ); } else { httpResponse = new NetworkResponse( httpResponse.statusCode, null, httpResponse.headers, System.nanoTime() - requestStart ); } } else if (httpResponse.statusCode < 200 || httpResponse.statusCode > 299) { throw new IOException(); } return httpResponse; } catch (SocketTimeoutException e) { attemptRetryOnException("socket", request, new TimeoutError("socket Timeout", e)); } catch (MalformedURLException e) { throw new RuntimeException("Bad URL " + request.getUrlString(), e); } catch (IOException e) { NetworkResponse networkResponse; if (httpResponse != null) { networkResponse = httpResponse; } else { throw new NetworkError(e); } if (networkResponse != null) { if (networkResponse.statusCode == HttpURLConnection .HTTP_CLIENT_TIMEOUT) { attemptRetryOnException("http-client", request, new RequestError (networkResponse, "HTTP_CLIENT_TIMEOUT")); } else if (networkResponse.statusCode > 399 && networkResponse.statusCode < 500) { throw new RequestError(networkResponse); } else if (networkResponse.statusCode > 499) { attemptRetryOnException("server", request, new ServerError(networkResponse)); } else { throw new JusError(networkResponse, e); } } else { attemptRetryOnException("network", request, new NetworkError()); } } catch (AuthError authError) { throw authError; } } } HttpNetwork(HttpStack httpStack);  HttpNetwork(HttpStack httpStack, ByteArrayPool pool); @Override NetworkResponse performRequest(Request<?> request); static final int DEFAULT_POOL_SIZE; }