Fix reception of the puzzle

This commit is contained in:
jeffcheasey88 2023-03-06 12:11:29 +01:00
parent 80bfa7816f
commit 89dc0ede9e
3 changed files with 48 additions and 30 deletions

View file

@ -66,6 +66,25 @@ public class Main {
writer.close(); writer.close();
} }
}); });
router.register(new Response(){
@Override
public Pattern getPattern() {
return Pattern.compile("^(.*)$");
}
@Override
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
HttpUtil.responseHeaders(writer, 200,
"Access-Control-Allow-Origin: *",
"Access-Control-Allow-Methods: *",
"Access-Control-Allow-Headers: *");
}
@Override
public String getType() {
return "OPTIONS";
}
});
initRoutes(router); initRoutes(router);

View file

@ -5,6 +5,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -32,9 +33,6 @@ public class PuzzleResponse implements Response {
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
return; return;
} }
HttpUtil.skipHeaders(reader);
ReceivedResponse received = new ReceivedResponse(matcher, reader); ReceivedResponse received = new ReceivedResponse(matcher, reader);
saveSourceCode(received, databaseRepo.getPlayer(user.getId())); saveSourceCode(received, databaseRepo.getPlayer(user.getId()));
@ -74,6 +72,7 @@ public class PuzzleResponse implements Response {
} }
class ReceivedResponse { class ReceivedResponse {
private int puzzleId; private int puzzleId;
private byte[] response; private byte[] response;
private String fileName; private String fileName;
@ -84,33 +83,11 @@ class ReceivedResponse {
public ReceivedResponse(Matcher matcher, HttpReader reader) throws Exception { public ReceivedResponse(Matcher matcher, HttpReader reader) throws Exception {
this.reader = reader; this.reader = reader;
puzzleId = Integer.parseInt(matcher.group(1)); puzzleId = Integer.parseInt(matcher.group(1));
readResponse();
readFileName(); List<String> multiPartData = HttpUtil.readMultiPartData(reader);
readSourceCode(); this.response = multiPartData.get(0).getBytes();
} this.fileName = multiPartData.get(1);
this.sourceCode = multiPartData.get(2).getBytes();
private void readResponse() throws Exception {
int hSize = reader.readInt();
response = new byte[hSize];
if (hSize != reader.read(response))
response = null;
}
private void readFileName() throws Exception {
byte[] tmpFileName;
int hSize = reader.readInt();
tmpFileName = new byte[hSize];
if (hSize == reader.read(tmpFileName))
fileName = tmpFileName.toString();
else
fileName = null;
}
private void readSourceCode() throws Exception {
int hSize = reader.readInt();
sourceCode = new byte[hSize];
if (hSize != reader.read(sourceCode))
sourceCode = null;
} }
public int getPuzzleId() { public int getPuzzleId() {

View file

@ -1,6 +1,8 @@
package be.jeffcheasey88.peeratcode.webserver; package be.jeffcheasey88.peeratcode.webserver;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -22,6 +24,26 @@ public class HttpUtil {
while(((line = reader.readLine()) != null) && (line.length() > 0)); while(((line = reader.readLine()) != null) && (line.length() > 0));
} }
public static List<String> readMultiPartData(HttpReader reader) throws Exception{
List<String> list = new ArrayList<>();
reader.readLine();
while(reader.ready()){
String line;
while(((line = reader.readLine()) != null) && (line.length() > 0)){
}
String buffer = "";
while(((line = reader.readLine()) != null) && (!line.startsWith("------WebKitFormBoundary"))){
buffer+=line;
}
list.add(buffer);
}
return list;
}
public static void switchToWebSocket(HttpReader reader, HttpWriter writer) throws Exception{ public static void switchToWebSocket(HttpReader reader, HttpWriter writer) throws Exception{
String key = readWebSocketKey(reader); String key = readWebSocketKey(reader);
if(key == null) throw new IllegalArgumentException(); if(key == null) throw new IllegalArgumentException();