HttpHeaders优化
This commit is contained in:
@@ -135,15 +135,7 @@ public class HttpHeaders implements RestHeaders, Serializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean contains(String name) {
|
public boolean contains(String name) {
|
||||||
return this.map != null && this.map.containsKey(name);
|
return this.map != null && name != null && get(name) != null;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean containsIgnoreCase(String name) {
|
|
||||||
if (this.map == null || name == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return !this.map.keySet().stream().filter(name::equalsIgnoreCase).findFirst().isEmpty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpHeaders addAll(HttpHeaders header) {
|
public HttpHeaders addAll(HttpHeaders header) {
|
||||||
@@ -178,7 +170,7 @@ public class HttpHeaders implements RestHeaders, Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//服务端接收,无需校验参数合法性
|
//服务端接收,无需校验参数合法性
|
||||||
void addValid(String name, String value) {
|
void addValid(String name, Serializable value) {
|
||||||
if (this.map == null) {
|
if (this.map == null) {
|
||||||
this.map = new LinkedHashMap<>();
|
this.map = new LinkedHashMap<>();
|
||||||
this.map.put(name, value);
|
this.map.put(name, value);
|
||||||
@@ -199,22 +191,7 @@ public class HttpHeaders implements RestHeaders, Serializable {
|
|||||||
|
|
||||||
public HttpHeaders add(String name, String value) {
|
public HttpHeaders add(String name, String value) {
|
||||||
check(name, value);
|
check(name, value);
|
||||||
if (this.map == null) {
|
addValid(name, value);
|
||||||
this.map = new LinkedHashMap<>();
|
|
||||||
this.map.put(name, value);
|
|
||||||
} else {
|
|
||||||
Serializable old = get(name);
|
|
||||||
if (old == null) {
|
|
||||||
this.map.put(name, value);
|
|
||||||
} else if (old instanceof Collection) {
|
|
||||||
((Collection) old).add(value);
|
|
||||||
} else {
|
|
||||||
ArrayList list = new ArrayList();
|
|
||||||
list.add(old);
|
|
||||||
list.add(value);
|
|
||||||
this.map.put(name, list);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,22 +202,7 @@ public class HttpHeaders implements RestHeaders, Serializable {
|
|||||||
for (String val : value) {
|
for (String val : value) {
|
||||||
check(name, val);
|
check(name, val);
|
||||||
}
|
}
|
||||||
if (this.map == null) {
|
addValid(name, new ArrayList(value));
|
||||||
this.map = new LinkedHashMap<>();
|
|
||||||
this.map.put(name, new ArrayList(value));
|
|
||||||
} else {
|
|
||||||
Serializable old = get(name);
|
|
||||||
if (old == null) {
|
|
||||||
this.map.put(name, new ArrayList(value));
|
|
||||||
} else if (old instanceof Collection) {
|
|
||||||
((Collection) old).addAll(value);
|
|
||||||
} else {
|
|
||||||
ArrayList list = new ArrayList();
|
|
||||||
list.add(old);
|
|
||||||
list.addAll(value);
|
|
||||||
this.map.put(name, list);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,19 +260,28 @@ public class HttpHeaders implements RestHeaders, Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//服务端接收,无需校验参数合法性
|
//服务端接收,无需校验参数合法性
|
||||||
void setValid(String name, String value) {
|
void setValid(String name, Serializable value) {
|
||||||
if (this.map == null) {
|
if (this.map == null) {
|
||||||
this.map = new LinkedHashMap<>();
|
this.map = new LinkedHashMap<>();
|
||||||
|
this.map.put(name, value);
|
||||||
|
} else {
|
||||||
|
boolean unfound = true;
|
||||||
|
for (Map.Entry<String, Serializable> en : this.map.entrySet()) {
|
||||||
|
if (en.getKey().equalsIgnoreCase(name)) {
|
||||||
|
this.map.put(en.getKey(), value);
|
||||||
|
unfound = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (unfound) {
|
||||||
|
this.map.put(name, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.map.put(name, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpHeaders set(String name, String value) {
|
public HttpHeaders set(String name, String value) {
|
||||||
check(name, value);
|
check(name, value);
|
||||||
if (this.map == null) {
|
setValid(name, value);
|
||||||
this.map = new LinkedHashMap<>();
|
|
||||||
}
|
|
||||||
this.map.put(name, value);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,10 +292,7 @@ public class HttpHeaders implements RestHeaders, Serializable {
|
|||||||
for (String val : value) {
|
for (String val : value) {
|
||||||
check(name, val);
|
check(name, val);
|
||||||
}
|
}
|
||||||
if (this.map == null) {
|
setValid(name, new ArrayList(value));
|
||||||
this.map = new LinkedHashMap<>();
|
|
||||||
}
|
|
||||||
this.map.put(name, new ArrayList(value));
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -262,11 +262,11 @@ public class HttpSimpleClient extends Client<HttpSimpleConnection, HttpSimpleReq
|
|||||||
array.put(("Host: " + uri.getHost() + "\r\n").getBytes(StandardCharsets.UTF_8));
|
array.put(("Host: " + uri.getHost() + "\r\n").getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
array.put(HttpSimpleRequest.contentLengthBytes(body));
|
array.put(HttpSimpleRequest.contentLengthBytes(body));
|
||||||
if (headers == null || !headers.containsIgnoreCase("User-Agent")) {
|
if (headers == null || !headers.contains("User-Agent")) {
|
||||||
array.put(header_bytes_useragent);
|
array.put(header_bytes_useragent);
|
||||||
}
|
}
|
||||||
array.put(header_bytes_connclose);
|
array.put(header_bytes_connclose);
|
||||||
if (headers == null || !headers.containsIgnoreCase(Rest.REST_HEADER_TRACEID)) {
|
if (headers == null || !headers.contains(Rest.REST_HEADER_TRACEID)) {
|
||||||
array.put((Rest.REST_HEADER_TRACEID + ": " + traceid + "\r\n").getBytes(StandardCharsets.UTF_8));
|
array.put((Rest.REST_HEADER_TRACEID + ": " + traceid + "\r\n").getBytes(StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
if (headers != null) {
|
if (headers != null) {
|
||||||
|
|||||||
@@ -185,16 +185,16 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ
|
|||||||
//写status
|
//写status
|
||||||
array.put(((method == null ? "GET" : method.toUpperCase()) + " " + requestPath + " HTTP/1.1\r\n").getBytes(StandardCharsets.UTF_8));
|
array.put(((method == null ? "GET" : method.toUpperCase()) + " " + requestPath + " HTTP/1.1\r\n").getBytes(StandardCharsets.UTF_8));
|
||||||
//写header
|
//写header
|
||||||
if (traceid != null && !containsHeaderIgnoreCase(Rest.REST_HEADER_TRACEID)) {
|
if (traceid != null && !containsHeader(Rest.REST_HEADER_TRACEID)) {
|
||||||
array.put((Rest.REST_HEADER_TRACEID + ": " + traceid + "\r\n").getBytes(StandardCharsets.UTF_8));
|
array.put((Rest.REST_HEADER_TRACEID + ": " + traceid + "\r\n").getBytes(StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
if (currentUserid != null && !containsHeaderIgnoreCase(Rest.REST_HEADER_CURRUSERID)) {
|
if (currentUserid != null && !containsHeader(Rest.REST_HEADER_CURRUSERID)) {
|
||||||
array.put((Rest.REST_HEADER_CURRUSERID + ": " + currentUserid + "\r\n").getBytes(StandardCharsets.UTF_8));
|
array.put((Rest.REST_HEADER_CURRUSERID + ": " + currentUserid + "\r\n").getBytes(StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
if (!containsHeaderIgnoreCase("User-Agent")) {
|
if (!containsHeader("User-Agent")) {
|
||||||
array.put(header_bytes_useragent);
|
array.put(header_bytes_useragent);
|
||||||
}
|
}
|
||||||
if (!containsHeaderIgnoreCase("Connection")) {
|
if (!containsHeader("Connection")) {
|
||||||
array.put(header_bytes_connalive);
|
array.put(header_bytes_connalive);
|
||||||
}
|
}
|
||||||
array.put(("Content-Type: " + contentType0 + "\r\n").getBytes(StandardCharsets.UTF_8));
|
array.put(("Content-Type: " + contentType0 + "\r\n").getBytes(StandardCharsets.UTF_8));
|
||||||
@@ -210,8 +210,8 @@ public class HttpSimpleRequest extends ClientRequest implements java.io.Serializ
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean containsHeaderIgnoreCase(String name) {
|
protected boolean containsHeader(String name) {
|
||||||
return headers != null && headers.containsIgnoreCase(name);
|
return headers != null && headers.contains(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static byte[] contentLengthBytes(byte[] clientBody) {
|
protected static byte[] contentLengthBytes(byte[] clientBody) {
|
||||||
|
|||||||
@@ -37,7 +37,5 @@ public interface RestHeaders {
|
|||||||
|
|
||||||
public boolean contains(String name);
|
public boolean contains(String name);
|
||||||
|
|
||||||
public boolean containsIgnoreCase(String name);
|
|
||||||
|
|
||||||
public Map<String, Serializable> map();
|
public Map<String, Serializable> map();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user