@Override public okhttp3.Response intercept(Chain chain) throws IOException { Request request = chain.request(); String host = this.host; if (host != null) { HttpUrl newUrl = request.url().newBuilder() .host(host) .build(); request = request.newBuilder() .url(newUrl) .build(); } return chain.proceed(request); } }
/** * Returns the URL that would be retrieved by following {@code link} from this URL, or null if * the resulting URL is not well-formed. */ public @Nullable HttpUrl resolve(String link) { Builder builder = newBuilder(link); return builder != null ? builder.build() : null; }
private Request buildRequest(String hostname, int type) { Request.Builder requestBuilder = new Request.Builder().header("Accept", DNS_MESSAGE.toString()); ByteString query = DnsRecordCodec.encodeQuery(hostname, type); if (post) { requestBuilder = requestBuilder.url(url).post(RequestBody.create(DNS_MESSAGE, query)); } else { String encoded = query.base64Url().replace("=", ""); HttpUrl requestUrl = url.newBuilder().addQueryParameter("dns", encoded).build(); requestBuilder = requestBuilder.url(requestUrl); } return requestBuilder.build(); }
void delete(UUID uuid) throws IOException { HttpUrl url = RuneLiteAPI.getApiRoot().newBuilder() .addPathSegment("session") .addQueryParameter("session", uuid.toString()) .build(); Request request = new Request.Builder() .delete() .url(url) .build(); RuneLiteAPI.CLIENT.newCall(request).execute().close(); } }
@Test public void respectsHttpClientUri() throws Exception { final Response<Pojo> response = service.postForm("Cony", 26).get(); assertThat(response.raw().request().url()).isEqualTo( new HttpUrl.Builder().scheme("http") .host("127.0.0.1") .port(server.httpPort()) .addPathSegment("postForm") .build()); }
@Test public void credentials_are_propagated_to_POST_redirects() throws Exception { HttpUrl url = server.url("/redirect").newBuilder().username("theLogin").password("thePassword").build(); Webhook webhook = new Webhook(WEBHOOK_UUID, PROJECT_UUID, CE_TASK_UUID, RandomStringUtils.randomAlphanumeric(40),"my-webhook", url.toString()); // /redirect redirects to /target server.enqueue(new MockResponse().setResponseCode(307).setHeader("Location", server.url("target"))); server.enqueue(new MockResponse().setResponseCode(200)); WebhookDelivery delivery = newSender().call(webhook, PAYLOAD); assertThat(delivery.getHttpStatus().get()).isEqualTo(200); RecordedRequest redirectedRequest = takeAndVerifyPostRequest("/redirect"); assertThat(redirectedRequest.getHeader("Authorization")).isEqualTo(Credentials.basic(url.username(), url.password())); RecordedRequest targetRequest = takeAndVerifyPostRequest("/target"); assertThat(targetRequest.getHeader("Authorization")).isEqualTo(Credentials.basic(url.username(), url.password())); }
/** * Returns the URL that would be retrieved by following {@code link} from this URL, or null if * the resulting URL is not well-formed. */ public @Nullable HttpUrl resolve(String link) { Builder builder = newBuilder(link); return builder != null ? builder.build() : null; }
/** * Returns a new {@code HttpUrl} representing {@code url}. * * @throws IllegalArgumentException If {@code url} is not a well-formed HTTP or HTTPS URL. */ public static HttpUrl get(String url) { return new Builder().parse(null, url).build(); }
/** * Returns a string with containing this URL with its username, password, query, and fragment * stripped, and its path replaced with {@code /...}. For example, redacting {@code * http://username:password@example.com/path} returns {@code http://example.com/...}. */ public String redact() { return newBuilder("/...") .username("") .password("") .build() .toString(); }
@Test public void send_basic_authentication_header_if_url_contains_credentials() throws Exception { HttpUrl url = server.url("/ping").newBuilder().username("theLogin").password("thePassword").build(); Webhook webhook = new Webhook(WEBHOOK_UUID, PROJECT_UUID, CE_TASK_UUID, RandomStringUtils.randomAlphanumeric(40),"my-webhook", url.toString()); server.enqueue(new MockResponse().setBody("pong")); WebhookDelivery delivery = newSender().call(webhook, PAYLOAD); assertThat(delivery.getWebhook().getUrl()) .isEqualTo(url.toString()) .contains("://theLogin:thePassword@"); RecordedRequest recordedRequest = takeAndVerifyPostRequest("/ping"); assertThat(recordedRequest.getHeader("Authorization")).isEqualTo(Credentials.basic(url.username(), url.password())); }
HttpUrl.Builder urlBuilder = this.urlBuilder; if (urlBuilder != null) { url = urlBuilder.build(); } else {
private Request.Builder prepareOkRequestBuilder(WsRequest getRequest, HttpUrl.Builder urlBuilder) { Request.Builder okHttpRequestBuilder = new Request.Builder() .url(urlBuilder.build()) .header("Accept", getRequest.getMediaType()) .header("Accept-Charset", "UTF-8"); if (systemPassCode != null) { okHttpRequestBuilder.header("X-Sonar-Passcode", systemPassCode); } getRequest.getHeaders().getNames().forEach(name -> okHttpRequestBuilder.header(name, getRequest.getHeaders().getValue(name).get())); return okHttpRequestBuilder; }
HttpUrl link = response.request().url().resolve(href); if (link == null) continue; // URL is either invalid or its scheme isn't http/https. queue.add(link.newBuilder().fragment(null).build());
public Address(String uriHost, int uriPort, Dns dns, SocketFactory socketFactory, @Nullable SSLSocketFactory sslSocketFactory, @Nullable HostnameVerifier hostnameVerifier, @Nullable CertificatePinner certificatePinner, Authenticator proxyAuthenticator, @Nullable Proxy proxy, List<Protocol> protocols, List<ConnectionSpec> connectionSpecs, ProxySelector proxySelector) { this.url = new HttpUrl.Builder() .scheme(sslSocketFactory != null ? "https" : "http") .host(uriHost) .port(uriPort) .build(); if (dns == null) throw new NullPointerException("dns == null"); this.dns = dns; if (socketFactory == null) throw new NullPointerException("socketFactory == null"); this.socketFactory = socketFactory; if (proxyAuthenticator == null) { throw new NullPointerException("proxyAuthenticator == null"); } this.proxyAuthenticator = proxyAuthenticator; if (protocols == null) throw new NullPointerException("protocols == null"); this.protocols = Util.immutableList(protocols); if (connectionSpecs == null) throw new NullPointerException("connectionSpecs == null"); this.connectionSpecs = Util.immutableList(connectionSpecs); if (proxySelector == null) throw new NullPointerException("proxySelector == null"); this.proxySelector = proxySelector; this.proxy = proxy; this.sslSocketFactory = sslSocketFactory; this.hostnameVerifier = hostnameVerifier; this.certificatePinner = certificatePinner; }
/** See https://api.slack.com/methods/oauth.access. */ public OAuthSession exchangeCode(String code, HttpUrl redirectUrl) throws IOException { HttpUrl url = baseUrl.newBuilder("oauth.access") .addQueryParameter("client_id", clientId) .addQueryParameter("client_secret", clientSecret) .addQueryParameter("code", code) .addQueryParameter("redirect_uri", redirectUrl.toString()) .build(); Request request = new Request.Builder() .url(url) .build(); Call call = httpClient.newCall(request); try (Response response = call.execute()) { JsonAdapter<OAuthSession> jsonAdapter = moshi.adapter(OAuthSession.class); return jsonAdapter.fromJson(response.body().source()); } }
/** See https://api.slack.com/methods/rtm.start. */ public RtmStartResponse rtmStart(String accessToken) throws IOException { HttpUrl url = baseUrl.newBuilder("rtm.start") .addQueryParameter("token", accessToken) .build(); Request request = new Request.Builder() .url(url) .build(); Call call = httpClient.newCall(request); try (Response response = call.execute()) { JsonAdapter<RtmStartResponse> jsonAdapter = moshi.adapter(RtmStartResponse.class); return jsonAdapter.fromJson(response.body().source()); } }
/** * Returns a URL for connecting to this server. * * @param path the request path, such as "/". */ public HttpUrl url(String path) { return new HttpUrl.Builder() .scheme(sslSocketFactory != null ? "https" : "http") .host(getHostName()) .port(getPort()) .build() .resolve(path); }
/** See https://api.slack.com/docs/oauth. */ public HttpUrl authorizeUrl(String scopes, HttpUrl redirectUrl, ByteString state, String team) { HttpUrl.Builder builder = baseUrl.newBuilder("/oauth/authorize") .addQueryParameter("client_id", clientId) .addQueryParameter("scope", scopes) .addQueryParameter("redirect_uri", redirectUrl.toString()) .addQueryParameter("state", state.base64()); if (team != null) { builder.addQueryParameter("team", team); } return builder.build(); }
/** * Returns a string with containing this URL with its username, password, query, and fragment * stripped, and its path replaced with {@code /...}. For example, redacting {@code * http://username:password@example.com/path} returns {@code http://example.com/...}. */ public String redact() { return newBuilder("/...") .username("") .password("") .build() .toString(); }
/** * Returns a new {@code HttpUrl} representing {@code url}. * * @throws IllegalArgumentException If {@code url} is not a well-formed HTTP or HTTPS URL. */ public static HttpUrl get(String url) { return new Builder().parse(null, url).build(); }