[ISSUE-#4854] Modify Header to support Keys Ignore Case (#4870)

* Modify header key Ignore Case, Replace LinkedHashMap with TreeMap(String.case_insensitive_order)

* Delete redundant import

* add junit test case
This commit is contained in:
mai.jh 2021-02-04 19:13:56 +08:00 committed by GitHub
parent c455a900d9
commit 6379ba25bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 11 deletions

View File

@ -24,6 +24,8 @@ import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;
/**
* JDk http client response implement.
@ -49,7 +51,9 @@ public class JdkHttpClientResponse implements HttpClientResponse {
if (this.responseHeader == null) {
this.responseHeader = Header.newInstance();
}
this.responseHeader.setOriginalResponseHeader(conn.getHeaderFields());
for (Map.Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {
this.responseHeader.addOriginalResponseHeader(entry.getKey(), entry.getValue());
}
return this.responseHeader;
}

View File

@ -23,9 +23,9 @@ import com.alibaba.nacos.common.utils.StringUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
* Http header.
@ -41,8 +41,8 @@ public class Header {
private final Map<String, List<String>> originalResponseHeader;
private Header() {
header = new LinkedHashMap<String, String>();
originalResponseHeader = new LinkedHashMap<String, List<String>>();
header = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
originalResponseHeader = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
addParam(HttpHeaderConsts.CONTENT_TYPE, MediaType.APPLICATION_JSON);
addParam(HttpHeaderConsts.ACCEPT_CHARSET, "UTF-8");
addParam(HttpHeaderConsts.ACCEPT_ENCODING, "gzip");
@ -142,14 +142,13 @@ public class Header {
*
* <p>Currently only corresponds to the response header of JDK.
*
* @param headers original response header
* @param key original response header key
* @param values original response header values
*/
public void setOriginalResponseHeader(Map<String, List<String>> headers) {
if (MapUtils.isNotEmpty(headers)) {
this.originalResponseHeader.putAll(headers);
for (Map.Entry<String, List<String>> entry : this.originalResponseHeader.entrySet()) {
addParam(entry.getKey(), entry.getValue().get(0));
}
public void addOriginalResponseHeader(String key, List<String> values) {
if (StringUtils.isNotEmpty(key)) {
this.originalResponseHeader.put(key, values);
addParam(key, values.get(0));
}
}

View File

@ -0,0 +1,31 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.common.http.param;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class HeaderTest {
@Test
public void testHeaderKyeIgnoreCase() {
Header header = Header.newInstance();
header.addParam("Content-Encoding", "gzip");
assertEquals("gzip", header.getValue("content-encoding"));
}
}