Refine search
/** Combines cached headers with a network headers as defined by RFC 7234, 4.3.4. */ private static Headers combine(Headers cachedHeaders, Headers networkHeaders) { Headers.Builder result = new Headers.Builder(); for (int i = 0, size = cachedHeaders.size(); i < size; i++) { String fieldName = cachedHeaders.name(i); String value = cachedHeaders.value(i); if ("Warning".equalsIgnoreCase(fieldName) && value.startsWith("1")) { continue; // Drop 100-level freshness warnings. } if (isContentSpecificHeader(fieldName) || !isEndToEnd(fieldName) || networkHeaders.get(fieldName) == null) { Internal.instance.addLenient(result, fieldName, value); } } for (int i = 0, size = networkHeaders.size(); i < size; i++) { String fieldName = networkHeaders.name(i); if (!isContentSpecificHeader(fieldName) && isEndToEnd(fieldName)) { Internal.instance.addLenient(result, fieldName, networkHeaders.value(i)); } } return result.build(); }
/** * Returns the subset of the headers in {@code requestHeaders} that impact the content of * response's body. */ public static Headers varyHeaders(Headers requestHeaders, Headers responseHeaders) { Set<String> varyFields = varyFields(responseHeaders); if (varyFields.isEmpty()) return new Headers.Builder().build(); Headers.Builder result = new Headers.Builder(); for (int i = 0, size = requestHeaders.size(); i < size; i++) { String fieldName = requestHeaders.name(i); if (varyFields.contains(fieldName)) { result.add(fieldName, requestHeaders.value(i)); } } return result.build(); }
&& HttpHeaders.hasBody(networkResponse)) { GzipSource responseBody = new GzipSource(networkResponse.body().source()); Headers strippedHeaders = networkResponse.headers().newBuilder() .removeAll("Content-Encoding") .removeAll("Content-Length") .build(); responseBuilder.headers(strippedHeaders); String contentType = networkResponse.header("Content-Type");
/** * Set a field with the specified value. If the field is not found, it is added. If the field is * found, the existing values are replaced. */ public Builder set(String name, String value) { checkName(name); checkValue(value, name); removeAll(name); addLenient(name, value); return this; }
/** * Sets the header named {@code name} to {@code value}. If this request already has any headers * with that name, they are all replaced. */ public Builder header(String name, String value) { headers.set(name, value); return this; }
/** * Add a header with the specified name and formatted instant. Does validation of header names * and value. */ @IgnoreJRERequirement public Builder add(String name, Instant value) { if (value == null) throw new NullPointerException("value for name " + name + " == null"); return add(name, new Date(value.toEpochMilli())); }
/** * Adds {@code header} as an HTTP header. For well-formed HTTP {@code header} should contain a * name followed by a colon and a value. */ public MockResponse addHeader(String header) { headers.add(header); return this; }
/** * Adds a new header with the name and value. This may be used to add multiple headers with the * same name. */ public MockResponse addHeader(String name, Object value) { headers.add(name, String.valueOf(value)); return this; }
/** * Sets the header named {@code name} to {@code value}. If this request already has any headers * with that name, they are all replaced. */ public Builder header(String name, String value) { headers.set(name, value); return this; }
public Builder newBuilder() { Builder result = new Builder(); Collections.addAll(result.namesAndValues, namesAndValues); return result; }
/** Add an header line containing a field name, a literal colon, and a value. */ public Builder add(String line) { int index = line.indexOf(":"); if (index == -1) { throw new IllegalArgumentException("Unexpected header: " + line); } return add(line.substring(0, index).trim(), line.substring(index + 1)); }
/** * Adds a header with {@code name} and {@code value}. Prefer this method for multiply-valued * headers like "Cookie". * * <p>Note that for some headers including {@code Content-Length} and {@code Content-Encoding}, * OkHttp may replace {@code value} with a header derived from the request body. */ public Builder addHeader(String name, String value) { headers.add(name, value); return this; }
Request(Builder builder) { this.url = builder.url; this.method = builder.method; this.headers = builder.headers.build(); this.body = builder.body; this.tags = Util.immutableMap(builder.tags); }
@Override public Map<String, List<String>> getRequestProperties() { if (connected) { throw new IllegalStateException( "Cannot access request header fields after connection is set"); } return JavaNetHeaders.toMultimap(requestHeaders.build(), null); }
/** * Set a field with the specified date. If the field is not found, it is added. If the field is * found, the existing values are replaced. */ public Builder set(String name, Date value) { if (value == null) throw new NullPointerException("value for name " + name + " == null"); set(name, HttpDate.format(value)); return this; }
/** Removes all headers named {@code name}. */ public MockResponse removeHeader(String name) { headers.removeAll(name); return this; }
/** * Adds a header with {@code name} and {@code value}. Prefer this method for multiply-valued * headers like "Set-Cookie". */ public Builder addHeader(String name, String value) { headers.add(name, value); return this; }
/** * Add a header with the specified name and formatted date. Does validation of header names and * value. */ public Builder add(String name, Date value) { if (value == null) throw new NullPointerException("value for name " + name + " == null"); add(name, HttpDate.format(value)); return this; }
/** * Removes all HTTP headers including any "Content-Length" and "Transfer-encoding" headers that * were added by default. */ public MockResponse clearHeaders() { headers = new Headers.Builder(); return this; }