Where people come together to learn, code, and play. Custom-built HTTP server, site generator, and website from scratch using no external libraries. Goal is to be as minimalistic and fun as possible.
http://projectdivar.com
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
2.0 KiB
40 lines
2.0 KiB
try {
|
|
socket = new ServerSocket(8080);
|
|
System.out.println("Listening on port 8080.");
|
|
while (true) {
|
|
try (Socket client = socket.accept()) {
|
|
System.out.println("New client connection detected: "+client.toString());
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
|
String line;
|
|
line=in.readLine(); //Read the first line, this should be our request.
|
|
String[] splitter = line.split(Pattern.quote(" "));
|
|
if (splitter.length==3) {
|
|
//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")) {
|
|
String requestloc = splitter[1];
|
|
if (requestloc.equals("/")) {
|
|
//Send default directory.
|
|
CreateRequest(client,"200","OK","testfile.html");
|
|
} else {
|
|
CreateRequest(client,"200","OK",requestloc.replace("/",""));
|
|
}
|
|
}
|
|
} else {
|
|
CreateRequest(client,"501","Not Implemented","testfile.html");
|
|
}
|
|
}
|
|
while (!(line=in.readLine()).isBlank()) {
|
|
//System.out.println(line);
|
|
}
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
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());
|
|
|