Skip to content

Commit

Permalink
Scx Http Peach 支持 WebSocket (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
scx567888 authored Oct 30, 2024
1 parent 8491606 commit f7e01e4
Show file tree
Hide file tree
Showing 40 changed files with 651 additions and 79 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.9</version>
<version>3.1.10</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.9</version>
<version>3.1.10</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.9</version>
<version>3.1.10</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.9</version>
<version>3.1.10</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.9</version>
<version>3.1.10</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.9";
public static final String SCX_VERSION = "3.1.10";

/**
* 在控制台上打印 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.9</version>
<version>3.1.10</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.9</version>
<version>3.1.10</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.9</version>
<version>3.1.10</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.9</version>
<version>3.1.10</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.9</version>
<version>3.1.10</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.9</version>
<version>3.1.10</version>
</parent>

<artifactId>scx-http-helidon</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion scx-http-peach/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.9</version>
<version>3.1.10</version>
</parent>

<artifactId>scx-http-peach</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import cool.scx.net.ScxTCPServerOptions;
import cool.scx.net.ScxTCPSocket;
import cool.scx.net.TCPServer;
import cool.scx.net.tls.TLS;

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

import static cool.scx.http.HttpFieldName.CONNECTION;
import static cool.scx.http.HttpFieldName.SERVER;
import static cool.scx.http.HttpFieldName.*;
import static cool.scx.http.HttpMethod.GET;
import static java.nio.charset.StandardCharsets.UTF_8;

public class PeachHttpServer implements ScxHttpServer {
Expand All @@ -26,15 +27,16 @@ public class PeachHttpServer implements ScxHttpServer {

public PeachHttpServer(ScxHttpServerOptions options) {
this.options = options;
this.tcpServer = new TCPServer(new ScxTCPServerOptions().port(options.port()));
this.tcpServer.onConnect(this::listen);
this.tcpServer = new TCPServer(new ScxTCPServerOptions().port(options.port()).tls((TLS) options.tls()));
this.tcpServer.onConnect(this::handle);
}

public PeachHttpServer() {
this(new ScxHttpServerOptions());
}

private void listen(ScxTCPSocket scxTCPSocket) {
private void handle(ScxTCPSocket scxTCPSocket) {
//先假定 这是一个 http 1.1 连接
var dataReader = new LinkedDataReader(new InputStreamDataSupplier(scxTCPSocket.inputStream()));
while (true) {
//读取 请求行
Expand All @@ -44,24 +46,37 @@ private void listen(ScxTCPSocket scxTCPSocket) {
if (split.length != 3) {
throw new RuntimeException("Invalid request line: " + requestLine);
}
var method = split[0];
var path = split[1];
var version = split[2];
var method0 = split[0];
var path0 = split[1];
var version0 = split[2];

var request = new PeachHttpServerRequest();

request.method = ScxHttpMethod.of(method);
request.uri = ScxURI.of(URLDecoder.decode(path, UTF_8));
request.version = HttpVersion.of(version);
var method = ScxHttpMethod.of(method0);
var path = URLDecoder.decode(path0, UTF_8);
var version = HttpVersion.of(version0);

var headerBytes = dataReader.readUntil("\r\n\r\n".getBytes());

var headerStr = new String(headerBytes);

var headers = ScxHttpHeaders.of(headerStr);

request.headers = headers;
var connection = headers.get(CONNECTION);
var upgrade = headers.get(UPGRADE);

var isWebSocketHandshake = method == GET && "Upgrade".equals(connection) && "websocket".equals(upgrade);

PeachHttpServerRequest request;

if (isWebSocketHandshake) {
request = new PeachServerWebSocketHandshakeRequest(dataReader, scxTCPSocket.outputStream());
} else {
request = new PeachHttpServerRequest();
}

request.method = method;
request.uri = ScxURI.of(path);
request.version = version;
request.headers = headers;

ScxHttpBody body = null;

Expand All @@ -87,6 +102,16 @@ private void listen(ScxTCPSocket scxTCPSocket) {
requestHandler.accept(request);
}

//尝试启动 websocket 监听 todo 这里应该重新设计
if (request instanceof PeachServerWebSocketHandshakeRequest w) {
var ws = w.webSocket;
if (ws != null) {
ws.start();
}
// websocket 独占整个连接 退出循环
break;
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
}

@Override
public void flush() throws IOException {
out.flush();
}

@Override
public void close() throws IOException {
checkFirstSend();
}

};
}

Expand Down
Loading

0 comments on commit f7e01e4

Please sign in to comment.