String host() { return originalRequest.url().host(); }
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); if (request.url().host().equals(host)) { request = request.newBuilder() .header("Authorization", credentials) .build(); } return chain.proceed(request); } }
private static Dns buildBootstrapClient(Builder builder) { List<InetAddress> hosts = builder.bootstrapDnsHosts; if (hosts != null) { return new BootstrapDns(builder.url.host(), hosts); } else { return builder.systemDns; } }
@Override public String toString() { StringBuilder result = new StringBuilder() .append("Address{") .append(url.host()).append(":").append(url.port()); if (proxy != null) { result.append(", proxy=").append(proxy); } else { result.append(", proxySelector=").append(proxySelector); } result.append("}"); return result.toString(); } }
/** * Returns true if this cookie should be included on a request to {@code url}. In addition to this * check callers should also confirm that this cookie has not expired. */ public boolean matches(HttpUrl url) { boolean domainMatch = hostOnly ? url.host().equals(domain) : domainMatch(url.host(), domain); if (!domainMatch) return false; if (!pathMatch(url, path)) return false; if (secure && !url.isHttps()) return false; return true; }
private InetAddress getConnectToInetAddress(Proxy proxy, HttpUrl url) throws IOException { return proxy.type() != Proxy.Type.DIRECT ? ((InetSocketAddress) proxy.address()).getAddress() : InetAddress.getByName(url.host()); } }
/** * Returns true if an HTTP request for {@code followUp} can reuse the connection used by this * engine. */ private boolean sameConnection(Response response, HttpUrl followUp) { HttpUrl url = response.request().url(); return url.host().equals(followUp.host()) && url.port() == followUp.port() && url.scheme().equals(followUp.scheme()); } }
Pin(String pattern, String pin) { this.pattern = pattern; this.canonicalHostname = pattern.startsWith(WILDCARD) ? HttpUrl.get("http://" + pattern.substring(WILDCARD.length())).host() : HttpUrl.get("http://" + pattern).host(); if (pin.startsWith("sha1/")) { this.hashAlgorithm = "sha1/"; this.hash = ByteString.decodeBase64(pin.substring("sha1/".length())); } else if (pin.startsWith("sha256/")) { this.hashAlgorithm = "sha256/"; this.hash = ByteString.decodeBase64(pin.substring("sha256/".length())); } else { throw new IllegalArgumentException("pins must start with 'sha256/' or 'sha1/': " + pin); } if (this.hash == null) { throw new IllegalArgumentException("pins must be base64: " + pin); } }
public void crawlPage(HttpUrl url) { // Skip hosts that we've visited many times. AtomicInteger hostnameCount = new AtomicInteger(); AtomicInteger previous = hostnames.putIfAbsent(url.host(), hostnameCount); if (previous != null) hostnameCount = previous; if (hostnameCount.incrementAndGet() > 100) return; // Asynchronously visit URL. pageService.get(url).enqueue(new Callback<Page>() { @Override public void onResponse(Call<Page> call, Response<Page> response) { if (!response.isSuccessful()) { System.out.println(call.request().url() + ": failed: " + response.code()); return; } // Print this page's URL and title. Page page = response.body(); HttpUrl base = response.raw().request().url(); System.out.println(base + ": " + page.title); // Enqueue its links for visiting. for (String link : page.links) { HttpUrl linkUrl = base.resolve(link); if (linkUrl != null && fetchedUrls.add(linkUrl)) { crawlPage(linkUrl); } } } @Override public void onFailure(Call<Page> call, Throwable t) { System.out.println(call.request().url() + ": failed: " + t); } }); }
private Request authenticate(Request request) { String hostName = request.url().host(); String principal = makeServicePrincipal(remoteServiceName, hostName, useCanonicalHostname); byte[] token = generateToken(principal); String credential = format("%s %s", NEGOTIATE, Base64.getEncoder().encodeToString(token)); return request.newBuilder() .header(AUTHORIZATION, credential) .build(); }
/** Returns the next proxy to try. May be PROXY.NO_PROXY but never null. */ private Proxy nextProxy() throws IOException { if (!hasNextProxy()) { throw new SocketException("No route to " + address.url().host() + "; exhausted proxy configurations: " + proxies); } Proxy result = proxies.get(nextProxyIndex++); resetNextInetSocketAddress(result); return result; }
@Override public synchronized void saveCookie(HttpUrl url, Cookie cookie) { List<Cookie> cookies = memoryCookies.get(url.host()); List<Cookie> needRemove = new ArrayList<>(); for (Cookie item : cookies) { if (cookie.name().equals(item.name())) { needRemove.add(item); } } cookies.removeAll(needRemove); cookies.add(cookie); }
public boolean supportsUrl(HttpUrl url) { if (url.port() != route.address().url().port()) { return false; // Port mismatch. } if (!url.host().equals(route.address().url().host())) { // We have a host mismatch. But if the certificate matches, we're still good. return handshake != null && OkHostnameVerifier.INSTANCE.verify( url.host(), (X509Certificate) handshake.peerCertificates().get(0)); } return true; // Success. The URL is supported. }
.name(name) .value(value) .domain(url.host()) .build());
private void startHttp2(int pingIntervalMillis) throws IOException { socket.setSoTimeout(0); // HTTP/2 connection timeouts are set per-stream. http2Connection = new Http2Connection.Builder(true) .socket(socket, route.address().url().host(), source, sink) .listener(this) .pingIntervalMillis(pingIntervalMillis) .build(); http2Connection.start(); }
private void pushPromises(Http2Stream stream, RecordedRequest request, List<PushPromise> promises) throws IOException { for (PushPromise pushPromise : promises) { List<Header> pushedHeaders = new ArrayList<>(); pushedHeaders.add(new Header(Header.TARGET_AUTHORITY, url(pushPromise.path()).host())); pushedHeaders.add(new Header(Header.TARGET_METHOD, pushPromise.method())); pushedHeaders.add(new Header(Header.TARGET_PATH, pushPromise.path())); Headers pushPromiseHeaders = pushPromise.headers(); for (int i = 0, size = pushPromiseHeaders.size(); i < size; i++) { pushedHeaders.add(new Header(pushPromiseHeaders.name(i), pushPromiseHeaders.value(i))); } String requestLine = pushPromise.method() + ' ' + pushPromise.path() + " HTTP/1.1"; List<Integer> chunkSizes = Collections.emptyList(); // No chunked encoding for HTTP/2. requestQueue.add(new RecordedRequest(requestLine, pushPromise.headers(), chunkSizes, 0, new Buffer(), sequenceNumber.getAndIncrement(), socket)); boolean hasBody = pushPromise.response().getBody() != null; Http2Stream pushedStream = stream.getConnection().pushStream(stream.getId(), pushedHeaders, hasBody); writeResponse(pushedStream, request, pushPromise.response()); } } }
@Override public String toString() { return "Connection{" + route.address().url().host() + ":" + route.address().url().port() + ", proxy=" + route.proxy() + " hostAddress=" + route.socketAddress() + " cipherSuite=" + (handshake != null ? handshake.cipherSuite() : "none") + " protocol=" + protocol + '}'; } }
private Address createAddress(HttpUrl url) { SSLSocketFactory sslSocketFactory = null; HostnameVerifier hostnameVerifier = null; CertificatePinner certificatePinner = null; if (url.isHttps()) { sslSocketFactory = client.sslSocketFactory(); hostnameVerifier = client.hostnameVerifier(); certificatePinner = client.certificatePinner(); } return new Address(url.host(), url.port(), client.dns(), client.socketFactory(), sslSocketFactory, hostnameVerifier, certificatePinner, client.proxyAuthenticator(), client.proxy(), client.protocols(), client.connectionSpecs(), client.proxySelector()); }