Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public final class ConfigurationAsyncClientBuilder {
httpLogDetailLevel = HttpLogDetailLevel.NONE;
policies = new ArrayList<>();

headers = new HttpHeaders();
headers.set(ECHO_REQUEST_ID_HEADER, "true");
headers.set(CONTENT_TYPE_HEADER, CONTENT_TYPE_HEADER_VALUE);
headers.set(ACCEPT_HEADER, ACCEPT_HEADER_VALUE);
headers = new HttpHeaders()
.put(ECHO_REQUEST_ID_HEADER, "true")
.put(CONTENT_TYPE_HEADER, CONTENT_TYPE_HEADER_VALUE)
.put(ACCEPT_HEADER, ACCEPT_HEADER_VALUE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineN

// All three of these headers are used by ConfigurationClientCredentials to generate the
// Authentication header value. So, we need to ensure that they exist.
headers.set(HOST_HEADER, context.httpRequest().url().getHost());
headers.set(CONTENT_HASH_HEADER, contentHash);
headers.put(HOST_HEADER, context.httpRequest().url().getHost())
.put(CONTENT_HASH_HEADER, contentHash);

if (headers.value(DATE_HEADER) == null) {
String utcNow = OffsetDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.RFC_1123_DATE_TIME);
headers.set(DATE_HEADER, utcNow);
headers.put(DATE_HEADER, utcNow);
}

return next.process();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public AsyncCredentialsPolicy(AsyncServiceClientCredentials credentials) {
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {
return credentials.authorizationHeaderValueAsync(context.httpRequest())
.flatMap(token -> {
context.httpRequest().headers().set("Authorization", token);
context.httpRequest().headers().put("Authorization", token);
return next.process();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ private static Map<String, String> toMap(HttpHeaders headers) {

public static HttpHeaders responseHeaders() {
return new HttpHeaders()
.set("Date", "Fri, 13 Oct 2017 20:33:09 GMT")
.set("Via", "1.1 vegur")
.set("Connection", "keep-alive")
.set("X-Processed-Time", "1.0")
.set("Access-Control-Allow-Credentials", "true")
.set("Content-Type", "application/json");
.put("Date", "Fri, 13 Oct 2017 20:33:09 GMT")
.put("Via", "1.1 vegur")
.put("Connection", "keep-alive")
.put("X-Processed-Time", "1.0")
.put("Access-Control-Allow-Credentials", "true")
.put("Content-Type", "application/json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public Mono<String> bodyAsString(Charset charset) {
* @return The updated response object.
*/
public MockHttpResponse addHeader(String name, String value) {
headers.set(name, value);
headers.put(name, value);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private Mono<HttpResponse> playbackHttpResponse(final HttpRequest request) {
rawHeader = rawHeader.replaceAll(rule.getKey(), rule.getValue());
}
}
headers.set(pair.getKey(), rawHeader);
headers.put(pair.getKey(), rawHeader);
}
}

Expand All @@ -122,7 +122,7 @@ private Mono<HttpResponse> playbackHttpResponse(final HttpRequest request) {
}

bytes = rawBody.getBytes(StandardCharsets.UTF_8);
headers.set("Content-Length", String.valueOf(bytes.length));
headers.put("Content-Length", String.valueOf(bytes.length));
}

HttpResponse response = new MockHttpResponse(request, recordStatusCode, headers, bytes);
Expand Down
37 changes: 20 additions & 17 deletions core/azure-core/src/main/java/com/azure/core/http/HttpHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public HttpHeaders() {
*/
public HttpHeaders(Map<String, String> headers) {
for (final Map.Entry<String, String> header : headers.entrySet()) {
this.set(header.getKey(), header.getValue());
this.put(header.getKey(), header.getValue());
}
}

Expand All @@ -46,7 +46,7 @@ public HttpHeaders(Iterable<HttpHeader> headers) {
this();

for (final HttpHeader header : headers) {
this.set(header.name(), header.value());
this.put(header.name(), header.value());
}
}

Expand All @@ -62,23 +62,27 @@ public int size() {
/**
* Set a header.
*
* if header with same name already exists then the value will be overwritten.
* if value is null and header with provided name already exists then it will be removed.
* If header with same name already exists then the value will be overwritten.
*
* @param name the name
* @param value the value
* @return this HttpHeaders
* @return The updated HttpHeaders object
*/
public HttpHeaders set(String name, String value) {
final String headerKey = name.toLowerCase(Locale.ROOT);
if (value == null) {
headers.remove(headerKey);
} else {
headers.put(headerKey, new HttpHeader(name, value));
}
public HttpHeaders put(String name, String value) {
headers.put(formatKey(name), new HttpHeader(name, value));
return this;
}

/**
* Get the {@link HttpHeader header} for the provided header name. Null will be returned if the header isn't found.
*
* @param name the name of the header to find.
* @return the header if found, null otherwise.
*/
public HttpHeader get(String name) {
return headers.get(formatKey(name));
}

/**
* Get the header value for the provided header name. Null will be returned if the header
* name isn't found.
Expand All @@ -87,7 +91,7 @@ public HttpHeaders set(String name, String value) {
* @return The String value of the header, or null if the header isn't found
*/
public String value(String name) {
final HttpHeader header = getHeader(name);
final HttpHeader header = get(name);
return header == null ? null : header.value();
}

Expand All @@ -99,13 +103,12 @@ public String value(String name) {
* @return the values of the header, or null if the header isn't found
*/
public String[] values(String name) {
final HttpHeader header = getHeader(name);
final HttpHeader header = get(name);
return header == null ? null : header.values();
}

private HttpHeader getHeader(String headerName) {
final String headerKey = headerName.toLowerCase(Locale.ROOT);
return headers.get(headerKey);
private String formatKey(final String key) {
return key.toLowerCase(Locale.ROOT);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public HttpRequest withHeaders(HttpHeaders headers) {
* @return this HttpRequest
*/
public HttpRequest withHeader(String name, String value) {
headers.set(name, value);
headers.put(name, value);
return this;
}

Expand Down Expand Up @@ -147,7 +147,7 @@ public HttpRequest withBody(String content) {
* @return this HttpRequest
*/
public HttpRequest withBody(byte[] content) {
headers.set("Content-Length", String.valueOf(content.length));
headers.put("Content-Length", String.valueOf(content.length));
// Unpooled.wrappedBuffer(body) allocates ByteBuf from unpooled heap
return withBody(Flux.defer(() -> Flux.just(Unpooled.wrappedBuffer(content))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ public Mono<HttpResponse> send(final HttpRequest request) {
Objects.requireNonNull(request.url());
Objects.requireNonNull(request.url().getProtocol());
//
Mono<HttpResponse> response = httpClient
.request(HttpMethod.valueOf(request.httpMethod().toString()))
.uri(request.url().toString())
.send(bodySendDelegate(request))
.responseConnection(responseDelegate(request))
.single();
return response;
return httpClient
.request(HttpMethod.valueOf(request.httpMethod().toString()))
.uri(request.url().toString())
.send(bodySendDelegate(request))
.responseConnection(responseDelegate(request))
.single();
}

/**
Expand All @@ -75,9 +74,11 @@ public Mono<HttpResponse> send(final HttpRequest request) {
* @return a delegate upon invocation sets the request body in reactor-netty outbound object
*/
private static BiFunction<HttpClientRequest, NettyOutbound, Publisher<Void>> bodySendDelegate(final HttpRequest restRequest) {
BiFunction<HttpClientRequest, NettyOutbound, Publisher<Void>> sendDelegate = (reactorNettyRequest, reactorNettyOutbound) -> {
return (reactorNettyRequest, reactorNettyOutbound) -> {
for (HttpHeader header : restRequest.headers()) {
reactorNettyRequest.header(header.name(), header.value());
if (header.value() != null) {
reactorNettyRequest.header(header.name(), header.value());
}
}
if (restRequest.body() != null) {
Flux<ByteBuf> nettyByteBufFlux = restRequest.body().map(Unpooled::wrappedBuffer);
Expand All @@ -86,7 +87,6 @@ private static BiFunction<HttpClientRequest, NettyOutbound, Publisher<Void>> bod
return reactorNettyOutbound;
}
};
return sendDelegate;
}

/**
Expand Down Expand Up @@ -140,7 +140,7 @@ public String headerValue(String name) {
@Override
public HttpHeaders headers() {
HttpHeaders headers = new HttpHeaders();
reactorNettyResponse.responseHeaders().forEach(e -> headers.set(e.getKey(), e.getValue()));
reactorNettyResponse.responseHeaders().forEach(e -> headers.put(e.getKey(), e.getValue()));
return headers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class AddDatePolicy implements HttpPipelinePolicy {
@Override
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {
return Mono.defer(() -> {
context.httpRequest().headers().set("Date", format.format(OffsetDateTime.now()));
context.httpRequest().headers().put("Date", format.format(OffsetDateTime.now()));
return next.process();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public AsyncCredentialsPolicy(AsyncServiceClientCredentials credentials) {
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {
return credentials.authorizationHeaderValueAsync(context.httpRequest())
.flatMap(token -> {
context.httpRequest().headers().set("Authorization", token);
context.httpRequest().headers().put("Authorization", token);
return next.process();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineN

Map<String, List<String>> requestCookies = cookies.get(uri, cookieHeaders);
for (Map.Entry<String, List<String>> entry : requestCookies.entrySet()) {
context.httpRequest().headers().set(entry.getKey(), String.join(",", entry.getValue()));
context.httpRequest().headers().put(entry.getKey(), String.join(",", entry.getValue()));
}

return next.process().map(httpResponse -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public CredentialsPolicy(ServiceClientCredentials credentials) {
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {
try {
String token = credentials.authorizationHeaderValue(context.httpRequest().url().toString());
context.httpRequest().headers().set("Authorization", token);
context.httpRequest().headers().put("Authorization", token);
return next.process();
} catch (IOException e) {
return Mono.error(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class RequestIdPolicy implements HttpPipelinePolicy {
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {
String requestId = context.httpRequest().headers().value(REQUEST_ID_HEADER);
if (requestId == null) {
context.httpRequest().headers().set(REQUEST_ID_HEADER, UUID.randomUUID().toString());
context.httpRequest().headers().put(REQUEST_ID_HEADER, UUID.randomUUID().toString());
}
return next.process();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineN
} else {
header = userAgent + " " + header;
}
context.httpRequest().headers().set("User-Agent", header);
context.httpRequest().headers().put("User-Agent", header);
return next.process();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private HttpRequest createHttpRequest(OperationDescription operationDescription,
private HttpRequest configRequest(HttpRequest request, SwaggerMethodParser methodParser, Object[] args) throws IOException {
final Object bodyContentObject = methodParser.body(args);
if (bodyContentObject == null) {
request.headers().set("Content-Length", "0");
request.headers().put("Content-Length", "0");
} else {
String contentType = methodParser.bodyContentType();
if (contentType == null || contentType.isEmpty()) {
Expand All @@ -234,7 +234,7 @@ private HttpRequest configRequest(HttpRequest request, SwaggerMethodParser metho
}
}

request.headers().set("Content-Type", contentType);
request.headers().put("Content-Type", contentType);

boolean isJson = false;
final String[] contentTypeParts = contentType.split(";");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public class SwaggerMethodParser implements HttpResponseDecodeData {
if (!headerName.isEmpty()) {
final String headerValue = header.substring(colonIndex + 1).trim();
if (!headerValue.isEmpty()) {
this.headers.set(headerName, headerValue);
this.headers.put(headerName, headerValue);
}
}
}
Expand Down Expand Up @@ -328,12 +328,12 @@ public Iterable<HttpHeader> headers(Object[] swaggerMethodArguments) {
for (final Map.Entry<String, ?> headerCollectionEntry : headerCollection.entrySet()) {
final String headerName = headerCollectionPrefix + headerCollectionEntry.getKey();
final String headerValue = serialize(headerCollectionEntry.getValue());
result.set(headerName, headerValue);
result.put(headerName, headerValue);
}
} else {
final String headerName = headerSubstitution.urlParameterName();
final String headerValue = serialize(methodArgument);
result.set(headerName, headerValue);
result.put(headerName, headerValue);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ public class HttpHeadersTests {
public void testSet() {
final HttpHeaders headers = new HttpHeaders();

headers.set("a", "b");
headers.put("a", "b");
assertEquals("b", headers.value("a"));

headers.set("a", "c");
headers.put("a", "c");
assertEquals("c", headers.value("a"));

headers.set("a", null);
headers.put("a", null);
assertNull(headers.value("a"));

headers.set("A", "");
headers.put("A", "");
assertEquals("", headers.value("a"));

headers.set("A", "b");
headers.put("A", "b");
assertEquals("b", headers.value("A"));

headers.set("a", null);
headers.put("a", null);
assertNull(headers.value("a"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ public void constructor() throws MalformedURLException {

@Test
public void testClone() throws IOException {
final HttpHeaders headers = new HttpHeaders();
headers.set("my-header", "my-value");
headers.set("other-header", "other-value");
final HttpHeaders headers = new HttpHeaders().put("my-header", "my-value")
.put("other-header", "other-value");

final HttpRequest request = new HttpRequest(
HttpMethod.PUT,
Expand All @@ -43,7 +42,7 @@ public void testClone() throws IOException {
assertEquals(request.url(), bufferedRequest.url());

assertNotSame(request.headers(), bufferedRequest.headers());
assertEquals(request.headers().toMap().size(), bufferedRequest.headers().toMap().size());
assertEquals(request.headers().size(), bufferedRequest.headers().size());
for (HttpHeader clonedHeader : bufferedRequest.headers()) {
for (HttpHeader originalHeader : request.headers()) {
assertNotSame(clonedHeader, originalHeader);
Expand Down
Loading