Skip to content

Commit

Permalink
Copy HttpHeaders to ensure serializability
Browse files Browse the repository at this point in the history
Not all HttpHeaders implementations are serializable. This commit
ensures that WebClientRequestException and WebClientResponseException
are serializable, by copying any non-serializable HttpHeaders into a
new, serializable, instance.

Closes gh-28321
  • Loading branch information
poutsma committed Jul 28, 2022
1 parent 95a400a commit 97ea8a6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,9 +47,20 @@ public WebClientRequestException(Throwable ex, HttpMethod method, URI uri, HttpH

this.method = method;
this.uri = uri;
this.headers = headers;
this.headers = copy(headers);
}

/**
* Not all {@code HttpHeaders} implementations are serializable, so we
* make a copy to ensure that {@code WebClientResponseException} is.
*/
private static HttpHeaders copy(HttpHeaders headers) {
HttpHeaders result = new HttpHeaders();
result.putAll(headers);
return result;
}


/**
* Return the HTTP request method.
*/
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -96,12 +96,27 @@ public WebClientResponseException(String message, int statusCode, String statusT

this.statusCode = statusCode;
this.statusText = statusText;
this.headers = (headers != null ? headers : HttpHeaders.EMPTY);
this.headers = copy(headers);
this.responseBody = (responseBody != null ? responseBody : new byte[0]);
this.responseCharset = charset;
this.request = request;
}

/**
* Not all {@code HttpHeaders} implementations are serializable, so we
* make a copy to ensure that {@code WebClientResponseException} is.
*/
private static HttpHeaders copy(@Nullable HttpHeaders headers) {
if (headers == null) {
return HttpHeaders.EMPTY;
}
else {
HttpHeaders result = new HttpHeaders();
result.putAll(headers);
return result;
}
}


/**
* Return the HTTP status code value.
Expand Down

0 comments on commit 97ea8a6

Please sign in to comment.