Skip to content

Commit 44f08a6

Browse files
committed
I got it to send the right pages!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1 parent e8d8fb9 commit 44f08a6

File tree

10 files changed

+162
-20
lines changed

10 files changed

+162
-20
lines changed

src/main/java/dev/hdprojects/HttpServer/config/Configuration.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,27 @@ public class Configuration {
44

55
private int port;
66
private String webroot;
7+
private String style_replace;
8+
private String js_replace;
79
private String[] css;
810
private String[] js;
911

12+
public String getStyle_replace() {
13+
return style_replace;
14+
}
15+
16+
public void setStyle_replace(String style_replace) {
17+
this.style_replace = style_replace;
18+
}
19+
20+
public String getJs_replace() {
21+
return js_replace;
22+
}
23+
24+
public void setJs_replace(String js_replace) {
25+
this.js_replace = js_replace;
26+
}
27+
1028
public String[] getCss() {
1129
return css;
1230
}

src/main/java/dev/hdprojects/HttpServer/core/HttpConnectionWorkerThread.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,63 @@
22

33
import dev.hdprojects.http.GenerateHttpHeader;
44
import dev.hdprojects.http.HttpParser;
5+
import dev.hdprojects.website.HtmlParser;
56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
78

9+
import java.io.File;
810
import java.io.IOException;
911
import java.io.InputStream;
1012
import java.io.OutputStream;
1113
import java.net.Socket;
1214

13-
public class HttpConnectionWorkerThread extends Thread{
15+
public class HttpConnectionWorkerThread extends Thread {
1416

1517
private final static Logger LOGGER = LoggerFactory.getLogger(HttpConnectionWorkerThread.class);
1618

19+
private String webRoot;
1720
private Socket socket;
1821

19-
public HttpConnectionWorkerThread(Socket socket) {
22+
public HttpConnectionWorkerThread(Socket socket, String webRoot) {
23+
this.webRoot = webRoot;
2024
this.socket = socket;
2125
}
2226

2327
@Override
2428
public void run() {
29+
2530
InputStream inputStream = null;
2631
OutputStream outputStream = null;
2732
try {
33+
34+
// Create the streams to write to
2835
inputStream = socket.getInputStream();
2936
outputStream = socket.getOutputStream();
3037

38+
// Create an instance of the HttpParser
39+
LOGGER.info("Starting HTTP Parser ... ");
3140
HttpParser httpParser = new HttpParser(inputStream);
32-
3341
httpParser.parseHttpRequest();
42+
HtmlParser htmlParser = new HtmlParser(new File(webRoot + httpParser.getRequestedPage()), "", "", "", "");
43+
LOGGER.info("Done With HTTP Parser.");
3444

35-
String html = "<html><head> <title>Simple Java HTTP Server</title></head><body><h1>Build this server with java HTTP</h1></body></html>";
45+
// Set HTML variable
46+
String html = htmlParser.toString();
3647

37-
final String CRLF = "\r\n"; // 13, 10 ASCII
48+
/* 13, 10 ASCII */
49+
final String CRLF = "\r\n";
3850

39-
GenerateHttpHeader httpHeader = new GenerateHttpHeader(200, html.length(), "text/html", "hd");
51+
// Get the length of the html
52+
int contentLength = html.getBytes().length;
4053

41-
String response = httpHeader + html + CRLF + CRLF;
54+
// Generate an HTTP Header and response
55+
LOGGER.info("Generating HTTP Header ... ");
56+
GenerateHttpHeader httpHeader = new GenerateHttpHeader(200, contentLength, "text/html", "hd");
57+
String response = httpHeader.toString() + html + CRLF + CRLF;
58+
LOGGER.info("Done Generating HTTP Header.");
4259

60+
// Send the response
61+
LOGGER.info("Sending response ... ");
4362
outputStream.write(response.getBytes());
4463

4564
LOGGER.info("Connection processing finished");

src/main/java/dev/hdprojects/HttpServer/core/ServerListenerThread.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void run() {
3131

3232
LOGGER.info("Connection accepted" + socket.getInetAddress());
3333

34-
HttpConnectionWorkerThread workerThread = new HttpConnectionWorkerThread(socket);
34+
HttpConnectionWorkerThread workerThread = new HttpConnectionWorkerThread(socket, webroot);
3535
workerThread.start();
3636

3737
}

src/main/java/dev/hdprojects/http/GenerateHttpHeader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public GenerateHttpHeader(int responseCode, int contentLength, String contentTyp
3939
"Date: "+ new Date() + CRLF +
4040
"Server: " + CRLF +
4141
"Content-Length: " + this.contentLength + CRLF +
42-
"Content-Type: " + this.contentType + CRLF;
42+
"Content-Type: " + this.contentType + CRLF + CRLF;
4343
}
4444

4545
private String generateResponseString(){

src/main/java/dev/hdprojects/http/HttpParser.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,11 @@ public void parseHttpRequest() throws IOException {
3434

3535
int _byte;
3636

37-
int lastChar;
38-
3937
String tempString = "";
4038

4139
while( (_byte = this.inputStream.read()) >= 0){
4240
tempString += (char) _byte;
4341

44-
lastChar = _byte;
45-
4642
// This if statement checks if there is a newline and resets the string if there is
4743
if (tempString.indexOf("\r\n") > 0) {
4844
tempString = "";

src/main/java/dev/hdprojects/website/CssReplacer.java

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,68 @@
11
package dev.hdprojects.website;
22

3+
import dev.hdprojects.HttpServer.HttpServer;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.io.*;
8+
39
public class HtmlParser {
410

5-
private String file;
6-
private String contents;
11+
private final static Logger LOGGER = LoggerFactory.getLogger(HttpServer.class);
12+
13+
private File file;
14+
private String contents = "";
15+
private String html = "";
16+
17+
public HtmlParser(File file, String css, String cssReplace, String JS, String JSReplace) {
18+
this.file = file;
19+
20+
// Read the file
21+
try {
22+
// Open buffered reader
23+
// FileReader throws a FileNotFoundException
24+
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
25+
26+
int charValue;
27+
28+
// Loop through the characters of
29+
// the file and store them to the contents String
30+
// can throw a IOException
31+
while ((charValue = bufferedReader.read()) != -1) contents += (char) charValue;
32+
} catch (FileNotFoundException exception) {
33+
LOGGER.error("File Not Found: " + exception);
34+
} catch (IOException exception) {
35+
LOGGER.error("Can't Read File: " + exception);
36+
}
37+
LOGGER.info(contents);
38+
39+
// Replace the style
40+
//Replacer styleReplace = new Replacer(cssReplace, css);
41+
42+
// And the js
43+
//Replacer jsReplace = new Replacer(styleReplace.toString(), JS);
44+
45+
html = contents;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return html;
51+
}
52+
53+
public String getHtml() {
54+
return html;
55+
}
56+
57+
public void setHtml(String html) {
58+
this.html = html;
59+
}
760

61+
public File getFile() {
62+
return file;
63+
}
864

65+
public String getContents() {
66+
return contents;
67+
}
968
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package dev.hdprojects.website;
2+
3+
public class Replacer {
4+
5+
private String replaceString;
6+
private String contents;
7+
8+
public Replacer(String replaceString, String contents) {
9+
this.contents = contents;
10+
this.replaceString = replaceString;
11+
}
12+
13+
private String replace() {
14+
return this.replaceString.replaceAll(contents, replaceString);
15+
}
16+
17+
@Override
18+
public String toString() {
19+
return contents;
20+
}
21+
22+
/*
23+
*
24+
* Getters and Setters
25+
*
26+
*/
27+
28+
public String getReplaceString() {
29+
return replaceString;
30+
}
31+
32+
public void setReplaceString(String replaceString) {
33+
this.replaceString = replaceString;
34+
}
35+
36+
public String getContents() {
37+
return this.replace();
38+
}
39+
40+
public void setContents(String contents) {
41+
this.contents = contents;
42+
}
43+
}

src/main/resources/http.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"port": 80,
3-
"webroot": "/tmp",
3+
"webroot": "src/main/resources/webroot",
4+
"style_replace": "/*STYLE_REPLACE*/",
5+
"js_replace": "/*JS_REPLACE*/",
46
"css": [
57
"style.css"
68
],

src/main/resources/webroot/test.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Test</title>
5+
</head>
6+
<body>
7+
<h1>Hope this works!</h1>
8+
</body>
9+
</html>

0 commit comments

Comments
 (0)