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.9 KiB
40 lines
2.9 KiB
try {
|
|
socket = new ServerSocket(sigPlace.PORT);
|
|
System.out.println("Listening on port "+sigPlace.PORT+".");
|
|
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 requestLine,line;
|
|
ZonedDateTime modifiedDate = null;
|
|
requestLine=in.readLine(); //Read the first line, this should be our request.
|
|
if (requestLine!=null) {
|
|
while ((line=in.readLine())!=null) {
|
|
System.out.println(line);
|
|
if (line.contains("If-Modified-Since: ")) {
|
|
String modifiedSince=line.replace("If-Modified-Since: ","");
|
|
modifiedDate = ZonedDateTime.parse(modifiedSince,DateTimeFormatter.RFC_1123_DATE_TIME);
|
|
//System.out.println("Found a modified date of: "+modifiedDate);
|
|
}
|
|
}
|
|
String[] splitter = requestLine.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[] requestSplit = splitter[1].split(Pattern.quote("?"));
|
|
String requestloc = requestSplit[0];
|
|
HashMap<String,String> requestParams = new HashMap<>();
|
|
if (requestSplit.length>1) {
|
|
String[] params = requestSplit[1].split(Pattern.quote("&"));
|
|
for (String s : params) {
|
|
String key = s.substring(0,s.indexOf('='));
|
|
String value = s.substring(s.indexOf('=')+1);
|
|
requestParams.put(key,value);
|
|
}
|
|
System.out.println(" ==Params for this request are: "+requestParams);
|
|
}
|
|
if (requestloc.equals("/")) {
|
|
//Send default directory.
|
|
if (modifiedDate==null||modifiedDate.isBefore(GetLastModifiedDate(sigPlace.OUTDIR,"testfile.html"))) {
|
|
System.out.println(GetLastModifiedDate(sigPlace.OUTDIR,"testfile.html")+"//"+modifiedDate);
|
|
|