Skip to content

Commit

Permalink
v3.1.9 (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
scx567888 authored Oct 26, 2024
1 parent e057eb0 commit dbe280b
Show file tree
Hide file tree
Showing 58 changed files with 423 additions and 1,251 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.8</version>
<version>3.1.9</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.8</version>
<version>3.1.9</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.8</version>
<version>3.1.9</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.8</version>
<version>3.1.9</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.8</version>
<version>3.1.9</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.8";
public static final String SCX_VERSION = "3.1.9";

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

<artifactId>scx-http-peach</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,40 @@

import cool.scx.http.*;
import cool.scx.http.uri.ScxURI;
import cool.scx.io.InputStreamDataSupplier;
import cool.scx.io.LinkedDataReader;
import cool.scx.net.ScxTCPServer;
import cool.scx.net.ScxTCPServerOptions;
import cool.scx.net.ScxTCPSocket;
import cool.scx.net.TCPServer;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.function.Consumer;
import java.util.function.Function;

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

public class PeachHttpServer implements ScxHttpServer {

private final Function<ScxTCPServerOptions, ScxTCPServer> tcpServerBuilder;
private final ScxTCPServer tcpServer;
private final ScxHttpServerOptions options;
private Consumer<ScxHttpServerRequest> requestHandler;
private Consumer<ScxServerWebSocket> webSocketHandler;
private Consumer<Throwable> errorHandler;

public PeachHttpServer(ScxHttpServerOptions options, Function<ScxTCPServerOptions, ScxTCPServer> tcpServerBuilder) {
public PeachHttpServer(ScxHttpServerOptions options) {
this.options = options;
this.tcpServerBuilder = tcpServerBuilder;
this.tcpServer = tcpServerBuilder.apply(new ScxTCPServerOptions().port(options.port()));
this.tcpServer = new TCPServer(new ScxTCPServerOptions().port(options.port()));
this.tcpServer.onConnect(this::listen);
}

public PeachHttpServer(ScxHttpServerOptions options) {
this(options, TCPServer::new);
}

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

private void listen(ScxTCPSocket scxTCPSocket) {
var dataReader = new LinkedDataReader(() -> {
try {
byte[] read = scxTCPSocket.read(8192);
return new LinkedDataReader.Node(read);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
var dataReader = new LinkedDataReader(new InputStreamDataSupplier(scxTCPSocket.inputStream()));
while (true) {
//读取 请求行
var requestLineBytes = dataReader.readUntil("\r\n".getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public class PeachHttpServerResponse implements ScxHttpServerResponse {
private final ScxTCPSocket tcpSocket;
private final OutputStream outputStream;
private final ScxHttpHeadersWritable headers;
private final OutputStream out;
private HttpStatusCode status;
private boolean firstSend;

PeachHttpServerResponse(PeachHttpServerRequest request, ScxTCPSocket tcpSocket) {
this.request = request;
this.tcpSocket = tcpSocket;
this.out = tcpSocket.outputStream();
this.headers = ScxHttpHeaders.of();
this.firstSend = true;
this.status = HttpStatusCode.OK;
Expand All @@ -26,19 +28,19 @@ public class PeachHttpServerResponse implements ScxHttpServerResponse {
@Override
public void write(int b) throws IOException {
checkFirstSend();
tcpSocket.write(new byte[]{(byte) b});
out.write(b);
}

@Override
public void write(byte[] b) throws IOException {
checkFirstSend();
tcpSocket.write(b);
out.write(b);
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
checkFirstSend();
tcpSocket.write(b, off, len);
out.write(b, off, len);
}

};
Expand Down Expand Up @@ -102,7 +104,7 @@ public void doFirstSend() {
sb.append("\r\n");

try {
tcpSocket.write(sb.toString().getBytes());
out.write(sb.toString().getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
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.8</version>
<version>3.1.9</version>
</parent>

<artifactId>scx-http</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
import cool.scx.http.ParametersWritable;

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

public class ContentDispositionHelper {

public static ContentDispositionWritable decodedContentDisposition(String contentDispositionStr) {
if (contentDispositionStr == null) {
return null;
}
var split = contentDispositionStr.split(";\\s*");
var split = SEMICOLON_PATTERN.split(contentDispositionStr);
if (split.length == 0) {
return null;
}
var type = split[0];
ParametersWritable<String, String> params = Parameters.of();
for (var i = 1; i < split.length; i = i + 1) {
var s = split[i].split("=", 2);
var s = EQUALS_SIGN_PATTERN.split(split[i], 2);
if (s.length == 2) {
//移除两端的引号
params.add(s[0], removeQuotes(s[1]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
import cool.scx.http.ParametersWritable;
import cool.scx.http.ScxMediaType;

import java.util.regex.Pattern;

public class ContentTypeHelper {

public static final Pattern SEMICOLON_PATTERN = Pattern.compile(";\\s*");
public static final Pattern EQUALS_SIGN_PATTERN = Pattern.compile("=");

public static ContentTypeWritable decodedContentType(String contentTypeStr) {
if (contentTypeStr == null) {
return null;
}
var split = contentTypeStr.split(";\\s*");
var split = SEMICOLON_PATTERN.split(contentTypeStr);
if (split.length == 0) {
return null;
}
var mediaType = ScxMediaType.of(split[0]);
ParametersWritable<String, String> params = Parameters.of();
for (var i = 1; i < split.length; i = i + 1) {
var s = split[i].split("=", 2);
var s = EQUALS_SIGN_PATTERN.split(split[i], 2);
if (s.length == 2) {
params.add(s[0], s[1]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cool.scx.http.content_disposition.ContentDisposition;
import cool.scx.http.content_type.ContentType;
import cool.scx.http.media.MediaReader;
import cool.scx.http.media.path.PathHelper;
import cool.scx.io.IOHelper;

import java.io.InputStream;
import java.nio.file.Path;
Expand All @@ -28,7 +28,7 @@ static MultiPartPartWritable of(String name, byte[] value) {
}

static MultiPartPartWritable of(String name, Path value) {
var fileSize = PathHelper.getFileSize(value);
var fileSize = IOHelper.getFileSize(value);
var contentType = getMediaTypeByFile(value);
var filename = value.getFileName().toString();
return new MultiPartPartImpl().name(name).body(value).size(fileSize).filename(filename).contentType(contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ public Path readContentToPath(Path path) throws IOException {
try {
var i = linkedDataReader.indexOf(boundaryBytes);
// i - 2 因为我们不需要读取内容结尾的 \r\n
linkedDataReader.read(output, i - 2);
linkedDataReader.read((b, off, len) -> {
try {
output.write(b, off, len);
} catch (IOException e) {
throw new RuntimeException(e);
}
}, i - 2);
//跳过 \r\n 方便后续读取
linkedDataReader.skip(2);
} catch (NoMatchFoundException e) {
Expand Down
47 changes: 0 additions & 47 deletions scx-http/src/main/java/cool/scx/http/media/path/PathHelper.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;

import static cool.scx.io.IOHelper.readInToFile;

/**
* 将内容写入到文件
*/
Expand All @@ -29,8 +30,8 @@ public PathReader(Path path, OpenOption... options) {

@Override
public Path read(InputStream inputStream, ScxHttpHeaders headers) {
try (var outputStream = Files.newOutputStream(path, options)) {
inputStream.transferTo(outputStream);
try {
readInToFile(inputStream, path, options);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Loading

0 comments on commit dbe280b

Please sign in to comment.