/** * Returns a new instance of the default {@link FetchOptions} used by this requestor. This * method exists primarily since {@link FetchOptions} provides no copy methods. * * @return new instance of default fetch options used by this requestor. */ public static FetchOptions newDefaultOptions() { return FetchOptions.Builder.withDefaults() .validateCertificate() .doNotFollowRedirects() .disallowTruncate() // seconds .setDeadline((DEFAULT_CONNECT_TIMEOUT_MILLIS + DEFAULT_READ_TIMEOUT_MILLIS)/1000.0); }
FetchOptions.Builder.doNotFollowRedirects().disallowTruncate().validateCertificate(); switch (certificateValidationBehavior) { case VALIDATE: fetchOptions.validateCertificate(); break; case DO_NOT_VALIDATE: fetchOptions.doNotValidateCertificate(); break; default:
private FetchOptions createFetchOptions(HttpRequestImpl request) { FetchOptions fetchOptions = FetchOptions.Builder.withDefaults(); fetchOptions = fetchOptions.setDeadline((double) request.getTimeout() / (double) 1000); return fetchOptions = request.isFollowRedirects() ? fetchOptions.followRedirects() : fetchOptions.doNotFollowRedirects(); }
@Test(expected = IOException.class) public void fetchNonExistentLocalAppThrowsException() throws Exception { URL selfURL = new URL("BOGUS-" + appUrlBase + "/"); FetchOptions fetchOptions = FetchOptions.Builder.withDefaults() .doNotFollowRedirects() .setDeadline(10.0); HTTPRequest httpRequest = new HTTPRequest(selfURL, HTTPMethod.GET, fetchOptions); URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService(); HTTPResponse httpResponse = urlFetchService.fetch(httpRequest); fail("expected exception, got " + httpResponse.getResponseCode()); }
@VisibleForTesting HTTPRequest makeRequest(GcsFilename filename, @Nullable Map<String, String> queryStrings, HTTPMethod method, long timeoutMillis, byte[] payload) { HTTPRequest request = new HTTPRequest(makeUrl(filename, queryStrings), method, FetchOptions.Builder.disallowTruncate() .doNotFollowRedirects() .validateCertificate() .setDeadline(timeoutMillis / 1000.0)); for (HTTPHeader header : headers) { request.addHeader(header); } request.addHeader(USER_AGENT); if (payload != null && payload.length > 0) { request.setHeader(new HTTPHeader(CONTENT_LENGTH, String.valueOf(payload.length))); try { request.setHeader(new HTTPHeader(CONTENT_MD5, BaseEncoding.base64().encode(MessageDigest.getInstance("MD5").digest(payload)))); } catch (NoSuchAlgorithmException e) { log.severe( "Unable to get a MessageDigest instance, no Content-MD5 header sent.\n" + e.toString()); } request.setPayload(payload); } else { request.setHeader(ZERO_CONTENT_LENGTH); } return request; }
@Override public void setTimeout(int connectTimeout, int readTimeout) { request.getFetchOptions().setDeadline( connectTimeout == 0 || readTimeout == 0 ? Double.MAX_VALUE : (connectTimeout + readTimeout) / 1000.0); }
public void sendRequestHeader(HttpRequest request) throws HttpException, IOException { try { HttpHost host = route.getTargetHost(); URI uri = new URI(host.getSchemeName() + "://" + host.getHostName() + ((host.getPort() == -1) ? "" : (":" + host.getPort())) + request.getRequestLine().getUri()); this.request = new HTTPRequest(uri.toURL(), HTTPMethod.valueOf(request.getRequestLine().getMethod()), FetchOptions.Builder.disallowTruncate().doNotFollowRedirects()); } catch (URISyntaxException ex) { throw new IOException("Malformed request URI: " + ex.getMessage(), ex); } catch (IllegalArgumentException ex) { throw new IOException("Unsupported HTTP method: " + ex.getMessage(), ex); } // System.err.println("SEND: " + this.request.getMethod() + " " + this.request.getURL()); for (Header h : request.getAllHeaders()) { // System.err.println("SEND: " + h.getName() + ": " + h.getValue()); this.request.addHeader(new HTTPHeader(h.getName(), h.getValue())); } }
@Test public void testValidateCertificate() throws Exception { FetchOptions options = buildFetchOptions(); options.validateCertificate(); testOptions(options); }
@Test public void testDisallowTruncate() throws Exception { FetchOptions options = buildFetchOptions(); options.disallowTruncate(); testOptions(options); }
@Test public void testDoNotValidateCertificate() throws Exception { FetchOptions options = buildFetchOptions(); options.doNotValidateCertificate(); testOptions(options); }
@Test public void testFollowRedirects() throws Exception { final URL redirect = getUrl("redirect"); FetchOptions options = buildFetchOptions(); options.followRedirects(); testOptions(redirect, options, new ResponseHandler() { public void handle(HTTPResponse response) throws Exception { URL finalURL = response.getFinalUrl(); Assert.assertEquals(getUrl(""), finalURL); } }); }
options.doNotFollowRedirects(); if (utils.relaxHostname() || utils.trustAllCerts()) options.doNotFollowRedirects(); options.setDeadline(10.0);
@Override public void setTimeout(int connectTimeout, int readTimeout) { request.getFetchOptions().setDeadline(connectTimeout == 0 || readTimeout == 0 ? Double.MAX_VALUE : (connectTimeout + readTimeout) / 1000.0); }
public void sendRequestHeader(HttpRequest request) throws HttpException, IOException { try { HttpHost host = route.getTargetHost(); URI uri = new URI(host.getSchemeName() + "://" + host.getHostName() + ((host.getPort() == -1) ? "" : (":" + host.getPort())) + request.getRequestLine().getUri()); this.request = new HTTPRequest(uri.toURL(), HTTPMethod.valueOf(request.getRequestLine().getMethod()), FetchOptions.Builder.disallowTruncate().doNotFollowRedirects()); } catch (URISyntaxException ex) { throw new IOException("Malformed request URI: " + ex.getMessage(), ex); } catch (IllegalArgumentException ex) { throw new IOException("Unsupported HTTP method: " + ex.getMessage(), ex); } // System.err.println("SEND: " + this.request.getMethod() + " " + this.request.getURL()); for (Header h : request.getAllHeaders()) { // System.err.println("SEND: " + h.getName() + ": " + h.getValue()); this.request.addHeader(new HTTPHeader(h.getName(), h.getValue())); } }
.getReadTimeout() / 1000).validateCertificate(); HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST, fetchOptions);
options.doNotFollowRedirects(); if (utils.relaxHostname() || utils.trustAllCerts()) options.doNotFollowRedirects(); options.setDeadline(10.0);
@Override public Response fetchData(String url, Method method) { try { URL aUrl = new URL(url); HTTPMethod httpMethod = HTTPMethod.GET; switch (method) { case POST: httpMethod = HTTPMethod.POST; break; } HTTPRequest httpRequest = new HTTPRequest(aUrl, httpMethod, doNotValidateCertificate().setDeadline(10d)); URLFetchService service = URLFetchServiceFactory .getURLFetchService(); HTTPResponse response = service.fetch(httpRequest); return new Response(new String(response.getContent(), "UTF-8"), response.getResponseCode(), ""); } catch (MalformedURLException e) { return new Response("", 400, "Malformed URL: " + url); } catch (IOException e) { return new Response("", 500, e.getMessage()); } }
@Test public void testDoNotFollowRedirects() throws Exception { final URL redirect = getUrl("redirect"); FetchOptions options = buildFetchOptions(); options.doNotFollowRedirects(); testOptions(redirect, options, new ResponseHandler() { public void handle(HTTPResponse response) throws Exception { Assert.assertEquals(302, response.getResponseCode()); } }); }
.getReadTimeout() / 1000).validateCertificate(); HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST, fetchOptions);
@Test public void testWithDeadline() throws Exception { FetchOptions options = buildFetchOptions(); options.setDeadline(10 * 1000.0); testOptions(options); }