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