Skip to content

Commit

Permalink
代码优化 (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
scx567888 authored Oct 24, 2024
1 parent 5ffa022 commit 333ecc8
Show file tree
Hide file tree
Showing 19 changed files with 150 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void listen(ScxTCPSocket scxTCPSocket) {

var request = new PeachHttpServerRequest();

request.method = HttpMethod.of(method);
request.method = ScxHttpMethod.of(method);
request.uri = ScxURI.of(URLDecoder.decode(path, UTF_8));
request.version = HttpVersion.of(version);

Expand Down
6 changes: 3 additions & 3 deletions scx-http/src/main/java/cool/scx/http/FileFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ private static Map<String, FileFormat> initMAP() {
return map;
}

public static FileFormat ofExtension(String ext) {
public static FileFormat findByExtension(String ext) {
return MAP.get(ext);
}

public static FileFormat ofFileName(String filename) {
public static FileFormat findByFileName(String filename) {
int li = filename.lastIndexOf('.');
if (li != -1 && li != filename.length() - 1) {
var ext = filename.substring(li + 1);
return ofExtension(ext);
return findByExtension(ext);
}
return null;
}
Expand Down
17 changes: 15 additions & 2 deletions scx-http/src/main/java/cool/scx/http/HttpFieldName.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,28 @@ private static Map<String, HttpFieldName> initMap() {
return map;
}

/**
* @param v v
* @return 未找到 抛出异常
*/
public static HttpFieldName of(String v) {
var upperCase = v.toLowerCase();
var httpFieldName = MAP.get(upperCase);
var lowerCase = v.toLowerCase();
var httpFieldName = MAP.get(lowerCase);
if (httpFieldName == null) {
throw new IllegalArgumentException(v);
}
return httpFieldName;
}

/**
* @param v v
* @return 未找到返回 null
*/
public static HttpFieldName find(String v) {
var lowerCase = v.toLowerCase();
return MAP.get(lowerCase);
}

@Override
public String value() {
return value;
Expand Down
4 changes: 2 additions & 2 deletions scx-http/src/main/java/cool/scx/http/HttpHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public static String getDownloadContentDisposition(String downloadName) {
}

public static MediaType getMediaTypeByExtension(String ext) {
var fileFormat = FileFormat.ofExtension(ext);
var fileFormat = FileFormat.findByExtension(ext);
return fileFormat != null ? fileFormat.mediaType() : null;
}

public static MediaType getMediaTypeByFileName(String filename) {
var fileFormat = FileFormat.ofFileName(filename);
var fileFormat = FileFormat.findByFileName(filename);
return fileFormat != null ? fileFormat.mediaType() : null;
}

Expand Down
27 changes: 26 additions & 1 deletion scx-http/src/main/java/cool/scx/http/HttpMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ public enum HttpMethod implements ScxHttpMethod {
this.value = value;
}

/**
* @param v v
* @return 未找到抛出异常
*/
public static HttpMethod of(String v) {
//数量较少时 switch 性能要高于 Map
return switch (v) {
case "CONNECT" -> CONNECT;
case "DELETE" -> DELETE;
Expand All @@ -75,9 +80,29 @@ public static HttpMethod of(String v) {
};
}

/**
* @param v v
* @return 未找到返回 null
*/
public static HttpMethod find(String v) {
//数量较少时 switch 性能要高于 Map
return switch (v) {
case "CONNECT" -> CONNECT;
case "DELETE" -> DELETE;
case "GET" -> GET;
case "HEAD" -> HEAD;
case "OPTIONS" -> OPTIONS;
case "PATCH" -> PATCH;
case "POST" -> POST;
case "PUT" -> PUT;
case "TRACE" -> TRACE;
default -> null;
};
}

@Override
public String value() {
return this.value;
return value;
}

}
15 changes: 15 additions & 0 deletions scx-http/src/main/java/cool/scx/http/HttpStatusCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ private static HttpStatusCode[] initMap() {
return m;
}

/**
* @param code c
* @return 未找到时 抛出异常
*/
public static HttpStatusCode of(int code) {
if (code < 0 || code > 505) {
throw new IllegalArgumentException("Invalid HTTP status code: " + code);
Expand All @@ -273,6 +277,17 @@ public static HttpStatusCode of(int code) {
return c;
}

/**
* @param code c
* @return 未找到时 返回 null
*/
public static HttpStatusCode find(int code) {
if (code < 0 || code > 505) {
return null;
}
return MAP[code];
}

public int code() {
return code;
}
Expand Down
17 changes: 17 additions & 0 deletions scx-http/src/main/java/cool/scx/http/HttpVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public enum HttpVersion {
this.value = value;
}

/**
* @param version v
* @return 未找到时 抛出异常
*/
public static HttpVersion of(String version) {
var upperCase = version.toUpperCase();
return switch (upperCase) {
Expand All @@ -24,6 +28,19 @@ public static HttpVersion of(String version) {
};
}

/**
* @param version v
* @return 未找到时返回 null
*/
public static HttpVersion find(String version) {
var upperCase = version.toUpperCase();
return switch (upperCase) {
case "HTTP/1.1" -> HTTP_1_1;
case "HTTP/2.0" -> HTTP_2;
default -> null;
};
}

public String value() {
return this.value;
}
Expand Down
12 changes: 12 additions & 0 deletions scx-http/src/main/java/cool/scx/http/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ private static HashMap<String, MediaType> initMap() {
return map;
}

/**
* @param str s
* @return 未找到时抛出异常
*/
public static MediaType of(String str) {
var mediaType = MAP.get(str.toLowerCase());
if (mediaType == null) {
Expand All @@ -79,6 +83,14 @@ public static MediaType of(String str) {
return mediaType;
}

/**
* @param str s
* @return 未找到时返回 null
*/
public static MediaType find(String str) {
return MAP.get(str.toLowerCase());
}

@Override
public String type() {
return type;
Expand Down
2 changes: 1 addition & 1 deletion scx-http/src/main/java/cool/scx/http/ParametersImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public class ParametersImpl<K, V> implements ParametersWritable<K, V> {

private final MultiMap<K, V> map = new MultiMap<>(LinkedHashMap::new, ArrayList::new);
private final MultiMap<K, V> map = new MultiMap<>(LinkedHashMap::new, () -> new ArrayList<>(1));

@SuppressWarnings("unchecked")
@Override
Expand Down
9 changes: 3 additions & 6 deletions scx-http/src/main/java/cool/scx/http/ScxHttpHeaderName.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@
public sealed interface ScxHttpHeaderName permits HttpFieldName, ScxHttpHeaderNameImpl {

static ScxHttpHeaderName of(String name) {
try {
// 优先使用 HttpFieldName
return HttpFieldName.of(name);
} catch (IllegalArgumentException e) {
return new ScxHttpHeaderNameImpl(name.toLowerCase());
}
// 优先使用 HttpFieldName
var n = HttpFieldName.find(name);
return n != null ? n : new ScxHttpHeaderNameImpl(name.toLowerCase());
}

String value();
Expand Down
8 changes: 3 additions & 5 deletions scx-http/src/main/java/cool/scx/http/ScxHttpMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
public sealed interface ScxHttpMethod permits HttpMethod, ScxHttpMethodImpl {

static ScxHttpMethod of(String v) {
try {
return HttpMethod.of(v);
} catch (IllegalArgumentException e) {
return new ScxHttpMethodImpl(v);
}
// 优先使用 HttpMethod
var m = HttpMethod.find(v);
return m != null ? m : new ScxHttpMethodImpl(v);
}

String value();
Expand Down
16 changes: 8 additions & 8 deletions scx-http/src/main/java/cool/scx/http/ScxMediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
public interface ScxMediaType {

static ScxMediaType of(String str) {
try {
return MediaType.of(str);
} catch (IllegalArgumentException e) {
var s = str.split("/");
if (s.length == 2) {
return new ScxMediaTypeImpl(s[0], s[1]);
}
throw new IllegalArgumentException("Invalid media type: " + str);
var m = MediaType.find(str);
if (m != null) {
return m;
}
var s = str.split("/");
if (s.length == 2) {
return new ScxMediaTypeImpl(s[0], s[1]);
}
throw new IllegalArgumentException("Invalid media type: " + str);
}

String type();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import cool.scx.http.Parameters;
import cool.scx.http.ParametersWritable;

import java.util.ArrayList;

import static cool.scx.common.util.StringUtils.removeQuotes;
import static cool.scx.http.content_type.ContentTypeHelper.encodeParams;

public class ContentDispositionHelper {

Expand All @@ -29,28 +28,14 @@ public static ContentDispositionWritable decodedContentDisposition(String conten
return new ContentDispositionImpl().type(type).params(params);
}

public static String encodeContentDisposition(ContentDisposition contentType) {
var sb = new StringBuilder();
var type = contentType.type();
var params = contentType.params();
sb.append(type);
if (contentType.params() != null && !contentType.params().isEmpty()) {
sb.append("; ");
sb.append(encodeParams(params));
public static String encodeContentDisposition(ContentDisposition contentDisposition) {
var type = contentDisposition.type();
var params = contentDisposition.params();
var sb = new StringBuilder(type);
if (params != null) {
encodeParams(sb, params);
}
return sb.toString();
}

public static String encodeParams(Parameters<String, String> params) {
var l = new ArrayList<String>();
for (var v : params) {
var key = v.getKey();
var value = v.getValue();
for (var s : value) {
l.add(key + "=" + s);
}
}
return String.join(";", l);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import cool.scx.http.ParametersWritable;
import cool.scx.http.ScxMediaType;

import java.util.ArrayList;

public class ContentTypeHelper {

public static ContentTypeWritable decodedContentType(String contentTypeStr) {
Expand All @@ -28,27 +26,23 @@ public static ContentTypeWritable decodedContentType(String contentTypeStr) {
}

public static String encodeContentType(ContentTypeImpl contentType) {
var sb = new StringBuilder();
var mediaType = contentType.mediaType();
var params = contentType.params();
sb.append(mediaType.value());
if (contentType.params() != null && !contentType.params().isEmpty()) {
sb.append("; ");
sb.append(encodeParams(params));
var sb = new StringBuilder(mediaType.value());
if (params != null) {
encodeParams(sb, params);
}
return sb.toString();
}

public static String encodeParams(Parameters<String, String> params) {
var l = new ArrayList<String>();
public static void encodeParams(StringBuilder result, Parameters<String, String> params) {
for (var v : params) {
var key = v.getKey();
var value = v.getValue();
for (var s : value) {
l.add(key + "=" + s);
var values = v.getValue();
for (var value : values) {
result.append("; ").append(key).append("=").append(value);
}
}
return String.join(";", l);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static Cookie parseSetCookie(String setCookieHeader) {
} else if (key.equalsIgnoreCase("Max-Age")) {
cookie.maxAge(Long.parseLong(attrValue));
} else if (key.equalsIgnoreCase("SameSite")) {
cookie.sameSite(CookieSameSite.of(attrValue));
cookie.sameSite(CookieSameSite.find(attrValue));
} else if (key.equalsIgnoreCase("Secure")) {
cookie.secure(true);
} else if (key.equalsIgnoreCase("HttpOnly")) {
Expand Down
Loading

0 comments on commit 333ecc8

Please sign in to comment.