Skip to content

Commit

Permalink
v3.1.3 (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
scx567888 authored Oct 2, 2024
1 parent e33b050 commit 3a303ee
Show file tree
Hide file tree
Showing 31 changed files with 128 additions and 68 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<artifactId>scx</artifactId>
<packaging>pom</packaging>
<version>3.1.2</version>
<version>3.1.3</version>

<name>SCX</name>
<url>https://github.com/scx567888/scx</url>
Expand Down
2 changes: 1 addition & 1 deletion scx-ansi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cool.scx</groupId>
<artifactId>scx</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
</parent>

<artifactId>scx-ansi</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion scx-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cool.scx</groupId>
<artifactId>scx</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
</parent>

<artifactId>scx-common</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion scx-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cool.scx</groupId>
<artifactId>scx</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
</parent>

<artifactId>scx-config</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion scx-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cool.scx</groupId>
<artifactId>scx</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
</parent>

<artifactId>scx-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion scx-core/src/main/java/cool/scx/core/ScxVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ScxVersion {
/**
* SCX 版本号
*/
public static final String SCX_VERSION = "3.1.2";
public static final String SCX_VERSION = "3.1.3";

/**
* 在控制台上打印 banner
Expand Down
2 changes: 1 addition & 1 deletion scx-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cool.scx</groupId>
<artifactId>scx</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
</parent>

<artifactId>scx-data-jdbc</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion scx-data-mysql-x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cool.scx</groupId>
<artifactId>scx</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
</parent>

<artifactId>scx-data-mysql-x</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion scx-data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cool.scx</groupId>
<artifactId>scx</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
</parent>

<artifactId>scx-data</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion scx-ext/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cool.scx</groupId>
<artifactId>scx</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
</parent>

<artifactId>scx-ext</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion scx-ffm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cool.scx</groupId>
<artifactId>scx</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
</parent>

<artifactId>scx-ffm</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion scx-http-helidon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cool.scx</groupId>
<artifactId>scx</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
</parent>

<artifactId>scx-http-helidon</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import cool.scx.http.ScxHttpHeaders;
import cool.scx.http.ScxHttpHeadersWritable;
import cool.scx.http.uri.ScxURI;
import io.helidon.common.uri.UriEncoding;
import io.helidon.webclient.websocket.WsClient;
import io.helidon.websocket.WsSession;

import java.net.URI;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -70,7 +72,7 @@ public void connect() {
}
}
var wsClient = wsClientBuilder.build();
wsClient.connect(uri.toString(), this);
wsClient.connect(URI.create(UriEncoding.encodeUri(uri.encode())), this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import cool.scx.http.ScxHttpClient;
import cool.scx.http.ScxHttpClientOptions;
import io.helidon.webclient.api.WebClient;
import io.helidon.webclient.websocket.WsClient;

/**
* HelidonHttpClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import io.helidon.http.Method;
import io.helidon.webclient.api.WebClient;

import java.net.URI;

/**
* HelidonHttpClientRequestBuilder
*/
Expand All @@ -26,7 +24,7 @@ public HelidonHttpClientRequest(WebClient webClient) {
public HelidonHttpClientResponse send(MediaWriter writer) {
var r = webClient.method(Method.create(method.value()));
//这里已经转换为 URL 编码了 无需再转换一遍
r.uri(URI.create(uri.encode()));
r.uri(uri.encode());
writer.beforeWrite(headers, ScxHttpHeaders.of());
for (var h : headers) {
r.header(HeaderNames.create(h.getKey().value()), h.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ public static void main(String[] args) throws IOException, InterruptedException

public static void test1() throws IOException, InterruptedException {
var httpServer = new HelidonHttpServer(new ScxHttpServerOptions().setPort(8990));
httpServer.requestHandler(c -> {
System.out.println(c.uri());
c.response().send("Hi Client !!!");
});
httpServer.webSocketHandler(c -> {
System.out.println(c.uri());
c.onTextMessage(t -> {
System.out.println(t);
});
c.send("Hi Client !!!");
c.send("Hi WS Client !!!");
});
httpServer.start();
}

public static void test2() {
var httpClient = new HelidonHttpClient();
var webSocketBuilder = httpClient.webSocket().uri("http://localhost:8990/test");
var webSocketBuilder = httpClient.webSocket().uri("http://localhost:8990/中:文|路径/ddd?查询=🎈🎈|🎈");
webSocketBuilder.onConnect(webSocket -> {
webSocket.onTextMessage(t -> {
System.out.println(t);
Expand All @@ -38,13 +42,13 @@ public static void test2() {
webSocketBuilder.connect();
}

public static void test3() throws IOException, InterruptedException {
public static void test3() {
var httpClient = new HelidonHttpClient();
var response = httpClient.request()
.uri("http://www.baidu.com")
.uri("http://localhost:8990/中:文|路径/ddd?查询=🎈🎈|🎈")
.send();
var string = response.body().asString();
System.out.println(string.length());
System.out.println(string);
}

}
2 changes: 1 addition & 1 deletion scx-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>cool.scx</groupId>
<artifactId>scx</artifactId>
<version>3.1.2</version>
<version>3.1.3</version>
</parent>

<artifactId>scx-http</artifactId>
Expand Down
12 changes: 7 additions & 5 deletions scx-http/src/main/java/cool/scx/http/ParametersImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import cool.scx.common.util.MultiMap;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

/**
* ParametersImpl
*/
public class ParametersImpl<K, V> implements ParametersWritable<K, V> {

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

@SuppressWarnings("unchecked")
@Override
Expand Down Expand Up @@ -70,4 +67,9 @@ public Map<K, List<V>> toMap() {
return map.toMultiValueMap();
}

@Override
public String toString() {
return map.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class ScxHttpHeadersImpl extends ParametersImpl<ScxHttpHeaderName, String> imple
public String toString() {
return encode();
}

}
10 changes: 8 additions & 2 deletions scx-http/src/main/java/cool/scx/http/uri/ScxURI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import java.net.URI;

import static cool.scx.http.uri.ScxURIHelper.parseURI;

/**
* ScxURI
*/
Expand All @@ -14,7 +16,7 @@ static ScxURIWritable of() {
}

static ScxURIWritable of(String uri) {
return of(URI.create(uri));
return of(parseURI(uri));
}

static ScxURIWritable of(URI u) {
Expand Down Expand Up @@ -49,7 +51,11 @@ default String getQuery(String name) {
* @return a
*/
default String encode() {
return ScxURIHelper.encodeURI(this);
return encode(false);
}

default String encode(boolean uriEncoding) {
return ScxURIHelper.encodeURI(this, uriEncoding);
}

}
67 changes: 50 additions & 17 deletions scx-http/src/main/java/cool/scx/http/uri/ScxURIHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cool.scx.http.Parameters;
import cool.scx.http.ParametersWritable;

import java.net.URI;
import java.net.URLEncoder;
import java.util.ArrayList;

Expand All @@ -28,66 +29,98 @@ public static ParametersWritable<String, String> decodeQuery(String value) {
return query;
}

public static String encodeQuery(Parameters<String, String> query) {
public static String encodeQuery(Parameters<String, String> query, boolean uriEncoding) {
var l = new ArrayList<String>();
for (var v : query) {
var key = v.getKey();
var value = v.getValue();
for (var s : value) {
var kk = URLEncoder.encode(key, UTF_8);
var vv = URLEncoder.encode(s, UTF_8);
l.add(kk + "=" + vv);
if (uriEncoding) {
var kk = URLEncoder.encode(key, UTF_8);
var vv = URLEncoder.encode(s, UTF_8);
l.add(kk + "=" + vv);
} else {
l.add(key + "=" + s);
}
}
}
return String.join("&", l);
}

public static String encodeURI(ScxURI uri) {
public static String encodeURI(ScxURI uri, boolean uriEncoding) {
var scheme = uri.scheme();
var host = uri.host();
var port = uri.port();
var path = uri.path();
var query = uri.query();
var fragment = uri.fragment();

//拼接 scheme
var sb = new StringBuilder();
if (scheme != null) {
sb.append(scheme);
sb.append(':');
}

//拼接 host
if (host != null) {
sb.append("//");

boolean needBrackets = ((host.indexOf(':') >= 0)
&& !host.startsWith("[")
&& !host.endsWith("]"));
if (needBrackets) {
sb.append('[');
}
sb.append(host);
if (needBrackets) {
sb.append(']');
}

//拼接 端口号
if (port != -1) {
sb.append(':');
sb.append(port);
}
}

//拼接 path
if (path != null) {
sb.append(URLEncoder.encode(path, UTF_8).replace("%2F", "/"));
//是否需要进行 uri 编码
if (uriEncoding) {
//我们不编码 "/"
sb.append(URLEncoder.encode(path, UTF_8).replace("%2F", "/"));
} else {
sb.append(path);
}
}

//拼接查询参数
if (query != null && !query.isEmpty()) {
sb.append('?');
sb.append(encodeQuery(query));
sb.append(encodeQuery(query, uriEncoding));
}

if (fragment != null) {
sb.append('#');
sb.append(URLEncoder.encode(fragment, UTF_8));
//是否需要进行 uri 编码
if (uriEncoding) {
sb.append(URLEncoder.encode(fragment, UTF_8));
} else {
sb.append(fragment);
}
}
return sb.toString();
}

public static URI parseURI(String uriStr) {
// 解析 URI
return URI.create(encodeUri(uriStr));
}

public static String encodeUri(String uriStr) {
// 预处理 URI,将特殊字符进行编码
uriStr = URLEncoder.encode(uriStr, UTF_8)
.replace("%3A", ":")
.replace("%2F", "/")
.replace("%3F", "?")
.replace("%3D", "=")
.replace("%26", "&")
.replace("%23", "#");

// 解析 URI
return uriStr;
}

}
Loading

0 comments on commit 3a303ee

Please sign in to comment.