Skip to content
This repository was archived by the owner on Apr 18, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.fzdwx</groupId>
<artifactId>lambda</artifactId>
<version>0.10.5</version>
<version>0.10.6</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand All @@ -18,7 +18,7 @@

<junit.version>5.8.2</junit.version>
<lombok.version>1.18.22</lombok.version>
<hutool.version>5.8.0</hutool.version>
<hutool.version>5.8.2</hutool.version>
<jline.version>3.21.0</jline.version>
<spring-core.version>5.3.20</spring-core.version>
</properties>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/github/fzdwx/lambada/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ public static <T> T defVal(T t, final T defaultValue) {
}
return t;
}

public static void gt0(final int chunkSize, final String message) {
if (chunkSize <= 0) {
Exceptions.illegalArgument(message);
}
}
}
32 changes: 32 additions & 0 deletions src/main/java/io/github/fzdwx/lambada/Io.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -198,4 +200,34 @@ static String read(String str, int fromIndexInclude, int toIndexExclude) {

return str.substring(fromIndexInclude, toIndexExclude);
}

/**
* new randomAccessFile. when File not found, then return null.
*
* @param filePath filePath
* @return {@link RandomAccessFile }
*/
@Nullable
static RandomAccessFile newRaf(String filePath) {
try {
return new RandomAccessFile(filePath, "r");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PATH_TRAVERSAL_IN: This API (java/io/RandomAccessFile.(Ljava/lang/String;Ljava/lang/String;)V) reads a file whose location might be specified by user input

(at-me in a reply with help or ignore)


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

} catch (Exception e) {
return null;
}
}

static File newFile(String filePath) {
return new File(filePath);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PATH_TRAVERSAL_IN: This API (java/io/File.(Ljava/lang/String;)V) reads a file whose location might be specified by user input

(at-me in a reply with help or ignore)


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

}

/**
* file to randomAccessFile. when File not found, then return null.
*/
static RandomAccessFile toRaf(File file) {
try {
return new RandomAccessFile(file, "r");
} catch (Exception e) {
return null;
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/io/github/fzdwx/lambada/anno/NotNull.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.fzdwx.lambada.anno;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* mark value maybe null.(same as @NonNull)
*
* @author <a href="mailto:likelovec@gmail.com">fzdwx</a>
* @apiNote just mark, no effect.
* @date 2022/5/16 14:13
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({
ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD,

})
public @interface NotNull { }
21 changes: 21 additions & 0 deletions src/main/java/io/github/fzdwx/lambada/http/ContentParseParse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.fzdwx.lambada.http;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.github.fzdwx.lambada.Lang;
import io.github.fzdwx.lambada.Seq;

import java.util.Map;

class ContentParseParse {

static Map<String, String> m;

static {
final String s = FileUtil.readString("mime.json", Lang.CHARSET);
final JSONArray array = JSONUtil.parseObj(s).getJSONArray("mime-mapping");
m = Seq.toMap(array.stream(), o1 -> ((JSONObject) o1).getStr("extension"), o2 -> ((JSONObject) o2).getStr("mime-type"));
}
}
26 changes: 6 additions & 20 deletions src/main/java/io/github/fzdwx/lambada/http/ContentType.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package io.github.fzdwx.lambada.http;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import io.github.fzdwx.lambada.Lang;
import io.github.fzdwx.lambada.Seq;
import io.github.fzdwx.lambada.anno.Nullable;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

/**
* http content type
*
* @author <a href="mailto:likelovec@gmail.com">韦朕</a>
* @author <a href="mailto:likelovec@gmail.com">fzdwx</a>
* @date 2022/05/07 18:02:55
*/
public enum ContentType {
Expand Down Expand Up @@ -245,6 +238,10 @@ public String addEncode(Charset charset) {
return value + "; charset=" + charset.name();
}

public static String addUtf8(String contentType) {
return contentType + charset.UTF_8;
}

/**
* 如果 MIME 类型未知,则返回null
*/
Expand All @@ -267,18 +264,7 @@ public static String parseFromFileName(String file) {
@Nullable
public static String parse(String ext) {
if (ext == null || ext.isEmpty()) return null;
return Parse.m.get(ext);
}

static class Parse {

static Map<String, String> m;

static {
final String s = FileUtil.readString("mime.json", Lang.CHARSET);
final List<JSONArray> list = JSONUtil.toBean(s, List.class);
m = Seq.toMap(list, o1 -> o1.getStr(0), o2 -> o2.getStr(1));
}
return ContentParseParse.m.get(ext);
}

}
11 changes: 11 additions & 0 deletions src/main/java/io/github/fzdwx/lambada/http/HttpMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,15 @@ public boolean matches(String method) {
return name().equals(method.toUpperCase(Locale.ROOT));
}

/**
* Determine whether this {@link HttpMethod} matches the given method value.
*
* @param method the HTTP method as a String
* @return {@code true} if it matches, {@code false} otherwise
*/
public boolean matches(HttpMethod method) {
if (method == null) return false;
return this == method;
}

}
17 changes: 13 additions & 4 deletions src/main/java/io/github/fzdwx/lambada/http/HttpPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ public interface HttpPath {
static String format(String path) {
if (path == null || path.equals(StringPool.SLASH)) {
path = Lang.EMPTY_STR;
} else if (path.isEmpty() || path.startsWith(StringPool.SLASH)) {
} else {
}

while (path.startsWith(StringPool.DOUBLE_SLASH)) {
path = path.substring(1);
}

if (!path.startsWith(StringPool.SLASH)) {
path = StringPool.SLASH + path;
}
if (path.endsWith(StringPool.SLASH)) {
return path.substring(0, path.length() - 1);

if(path.length() > 1) {
while (path.endsWith(StringPool.SLASH)) {
path = path.substring(0, path.length() - 1);
}
}

return path;
}
}
4 changes: 2 additions & 2 deletions src/main/java/io/github/fzdwx/lambada/http/Route.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.fzdwx.lambada.http;

import io.github.fzdwx.lambada.lang.NvMap;
import io.github.fzdwx.lambada.lang.KvMap;

public interface Route<Handler> {

Expand All @@ -11,5 +11,5 @@ public interface Route<Handler> {
/**
* extract path parameters.
*/
NvMap extract(final String path);
KvMap extract(final String path);
}
16 changes: 8 additions & 8 deletions src/main/java/io/github/fzdwx/lambada/http/RouterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io.github.fzdwx.lambada.Lang;
import io.github.fzdwx.lambada.anno.NonNull;
import io.github.fzdwx.lambada.anno.Nullable;
import io.github.fzdwx.lambada.lang.NvMap;
import io.github.fzdwx.lambada.lang.KvMap;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -204,11 +204,11 @@ public Handler handler() {
}

@Override
public NvMap extract(final String path) {
final NvMap nvMap = NvMap.create();
public KvMap extract(final String path) {
final KvMap kvMap = KvMap.create();

if (path == null) {
return nvMap;
return kvMap;
}

final String[] searchParts = toParts(path);
Expand All @@ -217,19 +217,19 @@ public NvMap extract(final String path) {
for (int i = 0; i < parts.length; i++) {
final String part = parts[i];
if (part.charAt(0) == ':') {
nvMap.put(part.substring(1), searchParts[i]);
kvMap.put(part.substring(1), searchParts[i]);
}

if (part.charAt(0) == '*' && part.length() > 1) {
nvMap.put(part.substring(1), ArrayUtil.join(ArrayUtil.sub(searchParts, i, searchParts.length), "/"));
kvMap.put(part.substring(1), ArrayUtil.join(ArrayUtil.sub(searchParts, i, searchParts.length), "/"));
break;
}

if (springType(part)) {
nvMap.put(part.substring(1, part.length() - 1), searchParts[i]);
kvMap.put(part.substring(1, part.length() - 1), searchParts[i]);
}
}
return nvMap;
return kvMap;
}

private boolean isEmpty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,19 @@
import java.util.Map;

/**
* 可排序,不区分大小写(Name value map)
* <p>
* 用于:参数解析,Header,Param 处理
*
* @see LinkedCaseInsensitiveMap
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MissingSummary: A summary fragment is required; consider using the value of the @see block as a summary fragment instead.

Suggested change
* @see LinkedCaseInsensitiveMap
*See {@link LinkedCaseInsensitiveMap}.

(at-me in a reply with help or ignore)


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

*/
public class NvMap extends LinkedCaseInsensitiveMap<String> {
public class KvMap extends LinkedCaseInsensitiveMap<String> {

public static NvMap create() {
return new NvMap();
public static KvMap create() {
return new KvMap();
}

public NvMap() {
public KvMap() {
super();
}

public NvMap(Map<?, ?> map) {
public KvMap(Map<?, ?> map) {
super();

if (map != null) {
Expand All @@ -36,17 +32,17 @@ public NvMap(Map<?, ?> map) {
}
}

public NvMap set(String key, String val) {
public KvMap set(String key, String val) {
put(key, val);
return this;
}

public static NvMap from(String[] args) {
public static KvMap from(String[] args) {
return from(Arrays.asList(args));
}

public static NvMap from(List<String> args) {
NvMap d = new NvMap();
public static KvMap from(List<String> args) {
KvMap d = new KvMap();

if (args != null) {
for (String arg : args) {
Expand All @@ -63,7 +59,7 @@ public static NvMap from(List<String> args) {
return d;
}

public NvMap add(final String key, final Iterable<String> value) {
public KvMap add(final String key, final Iterable<String> value) {
for (final String s : value) {
this.put(key, s);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/github/fzdwx/lambada/lang/ResourceLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.fzdwx.lambada.lang;

/**
* @author <a href="mailto:likelovec@gmail.com">fzdwx</a>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MissingSummary: A summary line is required on public/protected Javadocs.

(at-me in a reply with help or ignore)


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

* @date 2022/5/27 18:08
*/
public class ResourceLoader {


}
16 changes: 16 additions & 0 deletions src/main/java/io/github/fzdwx/lambada/lang/StringPool.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.fzdwx.lambada.lang;

import java.io.File;

/**
* str pool.
*
Expand All @@ -12,6 +14,10 @@ public interface StringPool {

String SLASH = "/";

String CONCAT = "-";

String DOUBLE_SLASH = "//";

char SLASH_CHAR = '/';

String WINDOWS_FOLDER_SEPARATOR = "\\";
Expand All @@ -21,4 +27,14 @@ public interface StringPool {
String CURRENT_PATH = ".";

char EXTENSION_SEPARATOR = '.';

String FILE_SEPARATOR = File.separator;

char FILE_SEPARATOR_CHAR = File.separatorChar;

String FILE_PATH_SEPARATOR = File.pathSeparator;

char FILE_PATH_SEPARATOR_CHAR = File.pathSeparatorChar;


}
Loading