mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-01-13 06:53:19 -05:00
scala-fix changes
Signed-off-by: hrj <harshad.rj@gmail.com>
This commit is contained in:
parent
55288d3346
commit
32169dbe80
@ -22,8 +22,7 @@ public class FontFunCaptcha implements ChallengeProvider {
|
||||
return Map.of(
|
||||
"supportedLevels", List.of("medium"),
|
||||
"supportedMedia", List.of("image/png"),
|
||||
"supportedInputType", List.of("text")
|
||||
);
|
||||
"supportedInputType", List.of("text"));
|
||||
}
|
||||
|
||||
public void configure(String config) {
|
||||
|
@ -59,8 +59,7 @@ public class GifCaptcha implements ChallengeProvider {
|
||||
return Map.of(
|
||||
"supportedLevels", List.of("hard"),
|
||||
"supportedMedia", List.of("image/gif"),
|
||||
"supportedInputType", List.of("text")
|
||||
);
|
||||
"supportedInputType", List.of("text"));
|
||||
}
|
||||
|
||||
public Challenge returnChallenge() {
|
||||
|
@ -31,8 +31,7 @@ public class ShadowTextCaptcha implements ChallengeProvider {
|
||||
return Map.of(
|
||||
"supportedLevels", List.of("easy"),
|
||||
"supportedMedia", List.of("image/png"),
|
||||
"supportedInputType", List.of("text")
|
||||
);
|
||||
"supportedInputType", List.of("text"));
|
||||
}
|
||||
|
||||
public boolean checkAnswer(String secret, String answer) {
|
||||
|
@ -24,7 +24,9 @@ public final class Server {
|
||||
|
||||
public static interface Response {
|
||||
public int getCode();
|
||||
|
||||
public byte[] getBytes();
|
||||
|
||||
public Map<String, List<String>> getResponseHeaders();
|
||||
}
|
||||
|
||||
@ -39,14 +41,21 @@ public final class Server {
|
||||
this.responseHeaders = null;
|
||||
}
|
||||
|
||||
public ByteResponse(final int code, final byte[] bytes, final Map<String, List<String>> responseHeaders) {
|
||||
public ByteResponse(
|
||||
final int code, final byte[] bytes, final Map<String, List<String>> responseHeaders) {
|
||||
this.code = code;
|
||||
this.bytes = bytes;
|
||||
this.responseHeaders = responseHeaders;
|
||||
}
|
||||
|
||||
public int getCode() { return this.code; }
|
||||
public byte[] getBytes() { return this.bytes; }
|
||||
public int getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return this.bytes;
|
||||
}
|
||||
|
||||
public Map<String, List<String>> getResponseHeaders() {
|
||||
return this.responseHeaders;
|
||||
}
|
||||
@ -57,26 +66,31 @@ public final class Server {
|
||||
super(code, msg.getBytes());
|
||||
}
|
||||
|
||||
public StringResponse(final int code, final String msg, final Map<String, List<String>> responseHeaders) {
|
||||
public StringResponse(
|
||||
final int code, final String msg, final Map<String, List<String>> responseHeaders) {
|
||||
super(code, msg.getBytes(), responseHeaders);
|
||||
}
|
||||
}
|
||||
|
||||
public final class Request {
|
||||
final HttpExchange exchange;
|
||||
|
||||
Request(final HttpExchange exchange) {
|
||||
this.exchange = exchange;
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
return exchange.getRequestMethod();
|
||||
}
|
||||
|
||||
public Map<String, List<String>> getQueryParams() {
|
||||
final var query = exchange.getRequestURI().getQuery();
|
||||
final var params = parseParams(query);
|
||||
return params;
|
||||
}
|
||||
|
||||
public byte[] getBody() {
|
||||
try(final var bodyIS = exchange.getRequestBody()) {
|
||||
try (final var bodyIS = exchange.getRequestBody()) {
|
||||
final var bytes = bodyIS.readAllBytes();
|
||||
bodyIS.close();
|
||||
return bytes;
|
||||
@ -84,6 +98,7 @@ public final class Server {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String getBodyString() {
|
||||
return new String(getBody());
|
||||
}
|
||||
@ -98,11 +113,13 @@ public final class Server {
|
||||
public final String path;
|
||||
public final Processor processor;
|
||||
public final String[] methods;
|
||||
|
||||
public Handler(final String path, final Processor processor) {
|
||||
this.path = path;
|
||||
this.processor = processor;
|
||||
this.methods = new String[] {};
|
||||
}
|
||||
|
||||
public Handler(final String path, final String methods, final Processor processor) {
|
||||
this.path = path;
|
||||
this.processor = processor;
|
||||
@ -110,16 +127,23 @@ public final class Server {
|
||||
}
|
||||
}
|
||||
|
||||
public Server(final InetSocketAddress addr, final int backlog, final List<Handler> handlers, final Executor executor) throws IOException {
|
||||
public Server(
|
||||
final InetSocketAddress addr,
|
||||
final int backlog,
|
||||
final List<Handler> handlers,
|
||||
final Executor executor)
|
||||
throws IOException {
|
||||
this.server = HttpServer.create(addr, backlog);
|
||||
this.server.setExecutor(executor);
|
||||
for (final var handler: handlers) {
|
||||
for (final var handler : handlers) {
|
||||
// System.out.println("Registering handler for " + handler.path);
|
||||
this.server.createContext(handler.path, new HttpHandler() {
|
||||
this.server.createContext(
|
||||
handler.path,
|
||||
new HttpHandler() {
|
||||
public void handle(final HttpExchange exchange) {
|
||||
final var method = exchange.getRequestMethod();
|
||||
final Response errorResponse = checkMethods(handler.methods, method);
|
||||
try(final var os = exchange.getResponseBody()) {
|
||||
try (final var os = exchange.getResponseBody()) {
|
||||
Response response;
|
||||
if (errorResponse != null) {
|
||||
response = errorResponse;
|
||||
@ -152,7 +176,7 @@ public final class Server {
|
||||
public static Response checkMethods(final String[] methods, final String method) {
|
||||
if (methods.length > 0) {
|
||||
var found = false;
|
||||
for (var m: methods) {
|
||||
for (var m : methods) {
|
||||
if (m.equals(method)) {
|
||||
found = true;
|
||||
break;
|
||||
@ -178,17 +202,21 @@ public final class Server {
|
||||
}
|
||||
|
||||
// Adapted from https://stackoverflow.com/a/37368660
|
||||
private final static Pattern ampersandPattern = Pattern.compile("&");
|
||||
private final static Pattern equalPattern = Pattern.compile("=");
|
||||
private final static Map<String, List<String>> emptyMap = Map.of();
|
||||
private static final Pattern ampersandPattern = Pattern.compile("&");
|
||||
private static final Pattern equalPattern = Pattern.compile("=");
|
||||
private static final Map<String, List<String>> emptyMap = Map.of();
|
||||
|
||||
private static Map<String, List<String>> parseParams(final String query) {
|
||||
if (query == null) {
|
||||
return emptyMap;
|
||||
}
|
||||
final var params = ampersandPattern
|
||||
final var params =
|
||||
ampersandPattern
|
||||
.splitAsStream(query)
|
||||
.map(s -> Arrays.copyOf(equalPattern.split(s, 2), 2))
|
||||
.collect(Collectors.groupingBy(s -> decode(s[0]), Collectors.mapping(s -> decode(s[1]), Collectors.toList())));
|
||||
.collect(
|
||||
Collectors.groupingBy(
|
||||
s -> decode(s[0]), Collectors.mapping(s -> decode(s[1]), Collectors.toList())));
|
||||
return params;
|
||||
}
|
||||
|
||||
@ -208,42 +236,52 @@ public final class Server {
|
||||
mAddress = new InetSocketAddress(port);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ServerBuilder backlog(final int backlog) {
|
||||
this.backlog = backlog;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ServerBuilder address(final InetSocketAddress addr) {
|
||||
mAddress = addr;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ServerBuilder handle(final Handler handler) {
|
||||
handlers.add(handler);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ServerBuilder GET(final String path, final Processor processor) {
|
||||
handlers.add(new Handler(path, "GET", request -> processor.process(request)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public ServerBuilder POST(final String path, final Processor processor) {
|
||||
handlers.add(new Handler(path, "POST", request -> processor.process(request)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public ServerBuilder PUT(final String path, final Processor processor) {
|
||||
handlers.add(new Handler(path, "PUT", request -> processor.process(request)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public ServerBuilder DELETE(final String path, final Processor processor) {
|
||||
handlers.add(new Handler(path, "DELETE", request -> processor.process(request)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public ServerBuilder HEAD(final String path, final Processor processor) {
|
||||
handlers.add(new Handler(path, "HEAD", request -> processor.process(request)));
|
||||
return this;
|
||||
}
|
||||
|
||||
public ServerBuilder executor(final Executor executor) {
|
||||
this.executor = executor;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Server build() throws IOException {
|
||||
return new Server(mAddress, backlog, handlers, executor);
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import org.limium.picoserve
|
||||
import org.limium.picoserve.Server.ByteResponse
|
||||
|
||||
class Server(port: Int) {
|
||||
val server = picoserve.Server.builder()
|
||||
val server: picoserve.Server = picoserve.Server.builder()
|
||||
.port(8888)
|
||||
.POST("/v1/captcha", (request) => {
|
||||
val json = parse(request.getBodyString())
|
||||
|
Loading…
Reference in New Issue
Block a user