Polishing

This commit includes a null-safety fix in
HttpComponentsHeadersAdapter.

Closes gh-30267
This commit is contained in:
hongxue.zou 2023-03-29 18:25:49 +08:00 committed by Sébastien Deleuze
parent ca545ac3d4
commit 534d1cd35b
5 changed files with 20 additions and 22 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
@ -53,6 +53,7 @@ class HttpComponentsHeadersAdapter implements MultiValueMap<String, String> {
@Override
@Nullable
public String getFirst(String key) {
Header header = this.message.getFirstHeader(key);
return (header != null ? header.getValue() : null);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -44,7 +44,7 @@ import org.springframework.util.MultiValueMap;
*/
class JettyClientHttpResponse implements ClientHttpResponse {
private static final Pattern SAMESITE_PATTERN = Pattern.compile("(?i).*SameSite=(Strict|Lax|None).*");
private static final Pattern SAME_SITE_PATTERN = Pattern.compile("(?i).*SameSite=(Strict|Lax|None).*");
private final ReactiveResponse reactiveResponse;
@ -90,7 +90,7 @@ class JettyClientHttpResponse implements ClientHttpResponse {
@Nullable
private static String parseSameSite(String headerValue) {
Matcher matcher = SAMESITE_PATTERN.matcher(headerValue);
Matcher matcher = SAME_SITE_PATTERN.matcher(headerValue);
return (matcher.matches() ? matcher.group(1) : null);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -45,6 +45,7 @@ class JettyHeadersAdapter implements MultiValueMap<String, String> {
private final HttpFields headers;
private static final String IMMUTABLE_HEADER_ERROR = "Immutable headers";
JettyHeadersAdapter(HttpFields headers) {
this.headers = headers;
@ -59,7 +60,7 @@ class JettyHeadersAdapter implements MultiValueMap<String, String> {
@Override
public void add(String key, @Nullable String value) {
if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) {
throw new IllegalStateException("Immutable headers");
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
}
mutableHttpFields.add(key, value);
}
@ -77,7 +78,7 @@ class JettyHeadersAdapter implements MultiValueMap<String, String> {
@Override
public void set(String key, @Nullable String value) {
if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) {
throw new IllegalStateException("Immutable headers");
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
}
mutableHttpFields.put(key, value);
}
@ -133,7 +134,7 @@ class JettyHeadersAdapter implements MultiValueMap<String, String> {
@Override
public List<String> put(String key, List<String> value) {
if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) {
throw new IllegalStateException("Immutable headers");
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
}
List<String> oldValues = get(key);
mutableHttpFields.put(key, value);
@ -144,7 +145,7 @@ class JettyHeadersAdapter implements MultiValueMap<String, String> {
@Override
public List<String> remove(Object key) {
if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) {
throw new IllegalStateException("Immutable headers");
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
}
if (key instanceof String name) {
List<String> oldValues = get(key);
@ -162,7 +163,7 @@ class JettyHeadersAdapter implements MultiValueMap<String, String> {
@Override
public void clear() {
if (!(this.headers instanceof HttpFields.Mutable mutableHttpFields)) {
throw new IllegalStateException("Immutable headers");
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
}
mutableHttpFields.clear();
}
@ -236,7 +237,7 @@ class JettyHeadersAdapter implements MultiValueMap<String, String> {
@Override
public List<String> setValue(List<String> value) {
if (!(headers instanceof HttpFields.Mutable mutableHttpFields)) {
throw new IllegalStateException("Immutable headers");
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
}
List<String> previousValues = headers.getValuesList(this.key);
mutableHttpFields.put(this.key, value);
@ -284,7 +285,7 @@ class JettyHeadersAdapter implements MultiValueMap<String, String> {
@Override
public void remove() {
if (!(headers instanceof HttpFields.Mutable mutableHttpFields)) {
throw new IllegalStateException("Immutable headers");
throw new IllegalStateException(IMMUTABLE_HEADER_ERROR);
}
if (this.currentName == null) {
throw new IllegalStateException("No current Header in iterator");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -153,10 +153,8 @@ class ReactorClientHttpResponse implements ClientHttpResponse {
@Nullable
private static String getSameSite(Cookie cookie) {
if (cookie instanceof DefaultCookie defaultCookie) {
if (defaultCookie.sameSite() != null) {
return defaultCookie.sameSite().name();
}
if (cookie instanceof DefaultCookie defaultCookie && defaultCookie.sameSite() != null) {
return defaultCookie.sameSite().name();
}
return null;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -137,10 +137,8 @@ class ReactorNetty2ClientHttpResponse implements ClientHttpResponse {
@Nullable
private static String getSameSite(HttpSetCookie cookie) {
if (cookie instanceof DefaultHttpSetCookie defaultCookie) {
if (defaultCookie.sameSite() != null) {
return defaultCookie.sameSite().name();
}
if (cookie instanceof DefaultHttpSetCookie defaultCookie && defaultCookie.sameSite() != null) {
return defaultCookie.sameSite().name();
}
return null;
}