Serve from different filenames, add flexibility to handle status messages, pages, and content
This commit is contained in:
parent
80ebd864d9
commit
6cd3a226a9
@ -64,7 +64,7 @@ public class sigPlace {
|
|||||||
}
|
}
|
||||||
System.out.println("Site has been built into the "+OUTDIR+" directory.");
|
System.out.println("Site has been built into the "+OUTDIR+" directory.");
|
||||||
System.out.println("\nStarting web server...");
|
System.out.println("\nStarting web server...");
|
||||||
sigServer server = new sigServer();
|
new sigServer();
|
||||||
}
|
}
|
||||||
private static Set<Path> GetFilesInDir(String directory) {
|
private static Set<Path> GetFilesInDir(String directory) {
|
||||||
Path dir = Paths.get(directory);
|
Path dir = Paths.get(directory);
|
||||||
|
@ -2,13 +2,11 @@ import java.io.BufferedReader;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.file.Files;
|
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.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class sigServer {
|
public class sigServer {
|
||||||
@ -26,13 +24,19 @@ public class sigServer {
|
|||||||
String[] splitter = line.split(Pattern.quote(" "));
|
String[] splitter = line.split(Pattern.quote(" "));
|
||||||
if (splitter.length==3) {
|
if (splitter.length==3) {
|
||||||
//This is valid.
|
//This is valid.
|
||||||
|
if (splitter[0].equals("GET")) { //This is a GET request.
|
||||||
if (splitter[2].equals("HTTP/1.1")||splitter[2].equals("HTTP/2.0")) {
|
if (splitter[2].equals("HTTP/1.1")||splitter[2].equals("HTTP/2.0")) {
|
||||||
String requestloc = splitter[1];
|
String requestloc = splitter[1];
|
||||||
if (requestloc.equals("/")) {
|
if (requestloc.equals("/")) {
|
||||||
//Send default directory.
|
//Send default directory.
|
||||||
CreateRequest(client,"testfile.html");
|
CreateRequest(client,"200","OK","testfile.html");
|
||||||
|
} else {
|
||||||
|
CreateRequest(client,"200","OK",Paths.get("/", requestloc.replace("/","")).toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
CreateRequest(client,"501","Not Implemented","testfile.html");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
while (!(line=in.readLine()).isBlank()) {
|
while (!(line=in.readLine()).isBlank()) {
|
||||||
//System.out.println(line);
|
//System.out.println(line);
|
||||||
@ -43,14 +47,31 @@ public class sigServer {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void CreateRequest(Socket client, String string) {
|
|
||||||
|
private void CreateRawRequest(OutputStream stream, String statusCode, String statusMsg, String contentType, byte[] content) {
|
||||||
|
try {
|
||||||
|
stream.write(("HTTP/1.1 "+statusCode+" "+statusMsg+"\r\n").getBytes());
|
||||||
|
stream.write(("ContentType: "+contentType+"\r\n").getBytes());
|
||||||
|
stream.write("\r\n".getBytes());
|
||||||
|
stream.write(content);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CreateRequest(Socket client, String statusCode, String statusMsg, String string) {
|
||||||
Path file = Paths.get(sigPlace.OUTDIR,string);
|
Path file = Paths.get(sigPlace.OUTDIR,string);
|
||||||
try {
|
try {
|
||||||
OutputStream clientOutput = client.getOutputStream();
|
OutputStream clientOutput = client.getOutputStream();
|
||||||
clientOutput.write("HTTP/1.1 200 OK\r\n".getBytes());
|
if (statusCode.equals("200")) {
|
||||||
clientOutput.write(("ContentType: text/html\r\n").getBytes());
|
if (Files.exists(file)) {
|
||||||
clientOutput.write("\r\n".getBytes());
|
CreateRawRequest(clientOutput,statusCode,statusMsg,Files.probeContentType(file),Files.readAllBytes(file));
|
||||||
clientOutput.write(Files.readAllBytes(file));
|
} else {
|
||||||
|
CreateRawRequest(clientOutput,statusCode,statusMsg,"text/html","We're sorry, your webpage is in another castle!".getBytes());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
CreateRawRequest(clientOutput,statusCode,statusMsg,"text/html","We're sorry, your webpage exploded!".getBytes());
|
||||||
|
}
|
||||||
clientOutput.write("\r\n\r\n".getBytes());
|
clientOutput.write("\r\n\r\n".getBytes());
|
||||||
clientOutput.flush();
|
clientOutput.flush();
|
||||||
client.close();
|
client.close();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user