/** 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)); }
/** * 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())); }
/** 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)); }
path = value; } else if (protocol == Protocol.HTTP_2 || protocol == Protocol.H2_PRIOR_KNOWLEDGE) { httpHeaders.add(name, value); } else { throw new IllegalStateException();
/** * 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; }
/** * 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; }
result.add("Accept-Encoding", "gzip");
private Headers parseHeaders(String[] headers) { Headers.Builder builder = new Headers.Builder(); for (String header : headers) { int colon = header.indexOf(':'); if (colon == -1 || colon == 0 || colon == header.length() - 1) { throw methodError(method, "@Headers value must be in the form \"Name: Value\". Found: \"%s\"", header); } String headerName = header.substring(0, colon); String headerValue = header.substring(colon + 1).trim(); if ("Content-Type".equalsIgnoreCase(headerName)) { try { contentType = MediaType.get(headerValue); } catch (IllegalArgumentException e) { throw methodError(method, e, "Malformed content type: %s", headerValue); } } else { builder.add(headerName, headerValue); } } return builder.build(); }
@Override public void addRequestProperty(String field, String value) { if (connected) { throw new IllegalStateException("Cannot add request property after connection is made"); } if (field == null) { throw new NullPointerException("field == null"); } if (value == null) { // Silently ignore null header values for backwards compatibility with older // android versions as well as with other URLConnection implementations. // // Some implementations send a malformed HTTP header when faced with // such requests, we respect the spec and ignore the header. Platform.get().log(WARN, "Ignoring header " + field + " because its value was null.", null); return; } requestHeaders.add(field, value); }
/** * Sets the response body to {@code body}, chunked every {@code maxChunkSize} bytes. */ public MockResponse setChunkedBody(Buffer body, int maxChunkSize) { removeHeader("Content-Length"); headers.add(CHUNKED_BODY_HEADER); Buffer bytesOut = new Buffer(); while (!body.exhausted()) { long chunkSize = Math.min(body.size(), maxChunkSize); bytesOut.writeHexadecimalUnsignedLong(chunkSize); bytesOut.writeUtf8("\r\n"); bytesOut.write(body, chunkSize); bytesOut.writeUtf8("\r\n"); } bytesOut.writeUtf8("0\r\n"); // Last chunk. Trailers follow! this.body = bytesOut; return this; }
/** * 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(); }
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Headers newHeaders = request.headers() .newBuilder() .add("Date", new Date()) .build(); Request newRequest = request.newBuilder() .headers(newHeaders) .build(); return chain.proceed(newRequest); } }
private Headers getHeaders() throws IOException { if (responseHeaders == null) { Response response = getResponse(true); Headers headers = response.headers(); responseHeaders = headers.newBuilder() .add(SELECTED_PROTOCOL, response.protocol().toString()) .add(RESPONSE_SOURCE, responseSourceHeader(response)) .build(); } return responseHeaders; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }