Skip to content

Commit 29826cd

Browse files
committed
Add speedy CSS and JS
1 parent 00214b7 commit 29826cd

24 files changed

+157
-79
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static void main(String[] args){
2828

2929
ServerListenerThread serverListenerThread = null;
3030
try {
31-
serverListenerThread = new ServerListenerThread(conf.getPort(), conf.getWebroot());
31+
serverListenerThread = new ServerListenerThread(conf);
3232
serverListenerThread.start();
3333
} catch (IOException e) {
3434
e.printStackTrace();

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@ public class Configuration {
66
private String webroot;
77
private String style_replace;
88
private String js_replace;
9-
private String[] css;
10-
private String[] js;
9+
private String page_404;
10+
private String css;
11+
private String js;
12+
13+
public String getPage_404() {
14+
return page_404;
15+
}
16+
17+
public void setPage_404(String page_404) {
18+
this.page_404 = page_404;
19+
}
1120

1221
public String getStyle_replace() {
1322
return style_replace;
@@ -25,19 +34,19 @@ public void setJs_replace(String js_replace) {
2534
this.js_replace = js_replace;
2635
}
2736

28-
public String[] getCss() {
37+
public String getCss() {
2938
return css;
3039
}
3140

32-
public void setCss(String[] css) {
41+
public void setCss(String css) {
3342
this.css = css;
3443
}
3544

36-
public String[] getJs() {
45+
public String getJs() {
3746
return js;
3847
}
3948

40-
public void setJs(String[] js) {
49+
public void setJs(String js) {
4150
this.js = js;
4251
}
4352

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.hdprojects.HttpServer.core;
22

3+
import dev.hdprojects.HttpServer.config.Configuration;
34
import dev.hdprojects.http.GenerateHttpHeader;
45
import dev.hdprojects.http.HttpParser;
56
import dev.hdprojects.website.HtmlParser;
@@ -17,10 +18,13 @@ public class HttpConnectionWorkerThread extends Thread {
1718

1819
private String webRoot;
1920
private Socket socket;
21+
private Configuration conf;
2022

21-
public HttpConnectionWorkerThread(Socket socket, String webRoot) {
23+
public HttpConnectionWorkerThread(Socket socket, String webRoot, Configuration conf) {
2224
this.webRoot = webRoot;
2325
this.socket = socket;
26+
27+
this.conf = conf;
2428
}
2529

2630
@Override
@@ -38,7 +42,7 @@ public void run() {
3842
LOGGER.info("Starting HTTP Parser ... ");
3943
HttpParser httpParser = new HttpParser(inputStream);
4044
httpParser.parseHttpRequest();
41-
HtmlParser htmlParser = new HtmlParser(webRoot + httpParser.getRequestedPage(), "", "", "", "");
45+
HtmlParser htmlParser = new HtmlParser(webRoot + httpParser.getRequestedPage(), conf);
4246
LOGGER.info("Done With HTTP Parser.");
4347

4448
// Set HTML variable
@@ -52,7 +56,8 @@ public void run() {
5256

5357
// Generate an HTTP Header and response
5458
LOGGER.info("Generating HTTP Header ... ");
55-
GenerateHttpHeader httpHeader = new GenerateHttpHeader(200, contentLength, "text/html", "hd");
59+
LOGGER.info("error: " + htmlParser.getError());
60+
GenerateHttpHeader httpHeader = new GenerateHttpHeader(htmlParser.getError(), contentLength, "text/html", "hd");
5661
String response = httpHeader.toString() + html + CRLF + CRLF;
5762
LOGGER.info("Done Generating HTTP Header.");
5863

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.hdprojects.HttpServer.core;
22

3+
import dev.hdprojects.HttpServer.config.Configuration;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56

@@ -14,10 +15,12 @@ public class ServerListenerThread extends Thread {
1415
private int port;
1516
private String webroot;
1617
private ServerSocket serverSocket;
18+
private Configuration configuration;
1719

18-
public ServerListenerThread(int port, String webroot) throws IOException {
19-
this.port = port;
20-
this.webroot = webroot;
20+
public ServerListenerThread(Configuration configuration) throws IOException {
21+
this.port = configuration.getPort();
22+
this.webroot = configuration.getWebroot();
23+
this.configuration = configuration;
2124

2225
this.serverSocket = new ServerSocket(this.port);
2326
}
@@ -31,7 +34,7 @@ public void run() {
3134

3235
LOGGER.info("Connection accepted" + socket.getInetAddress());
3336

34-
HttpConnectionWorkerThread workerThread = new HttpConnectionWorkerThread(socket, webroot);
37+
HttpConnectionWorkerThread workerThread = new HttpConnectionWorkerThread(socket, webroot, configuration);
3538
workerThread.start();
3639

3740
}

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

Lines changed: 97 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
package dev.hdprojects.website;
22

3-
import dev.hdprojects.HttpServer.HttpServer;
3+
import dev.hdprojects.HttpServer.config.Configuration;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66

77
import java.io.*;
88

99
public class HtmlParser {
1010

11-
private final static Logger LOGGER = LoggerFactory.getLogger(HttpServer.class);
11+
private final static Logger LOGGER = LoggerFactory.getLogger(HtmlParser.class);
1212

1313
private File file;
1414
private String filePath;
15+
private String css;
16+
private String JS;
17+
private int error = 200;
1518
private String contents = "";
1619
private String html = "";
20+
private Configuration configuration;
1721

18-
public HtmlParser(String filePath, String css, String cssReplace, String JS, String JSReplace) {
22+
public HtmlParser(String filePath, Configuration conf) {
1923
this.file = new File(filePath);
2024
this.filePath = filePath;
25+
this.configuration = conf;
2126

2227
// Read the file
2328
try {
@@ -62,36 +67,118 @@ public HtmlParser(String filePath, String css, String cssReplace, String JS, Str
6267
// can throw a IOException
6368
while ((charValue = bufferedReader.read()) != -1) contents += (char) charValue;
6469
} catch (FileNotFoundException exception1) {
65-
// ERROR: 404
70+
contents = error(404);
6671
LOGGER.error("File Not Found: " + exception);
6772
} catch (IOException exception1) {
68-
// ERROR: 500
73+
contents = error(500);
6974
LOGGER.error("Error reading file: " + exception);
7075
}
7176
} else {
72-
// ERROR: 404
77+
contents = error(404);
7378
LOGGER.error("File Not Found: " + exception);
7479
}
7580
} catch (IOException exception) {
76-
// ERROR: 500
81+
contents = error(500);
7782
LOGGER.error("File Not Found: " + exception);
7883
}
79-
LOGGER.info(contents);
84+
85+
try {
86+
// Open the file
87+
BufferedReader tempBufferedReader = new BufferedReader(new FileReader(new File(configuration.getWebroot() + "/" + configuration.getJs())));
88+
89+
int charValue;
90+
91+
JS = "";
92+
93+
// Loop through the characters of
94+
// the file and store them to the contents String
95+
// can throw a IOException
96+
while ((charValue = tempBufferedReader.read()) != -1) JS += (char) charValue;
97+
98+
// Set the html tags
99+
JS = "<script>" + JS + "</script>";
100+
} catch (Exception exception) {
101+
JS = "/* Unknown Error */";
102+
JS = "<script>" + JS + "</script>";
103+
LOGGER.info("Error opening JS");
104+
}
105+
106+
try {
107+
// Open the file
108+
BufferedReader tempBufferedReader = new BufferedReader(new FileReader(new File(configuration.getWebroot() + "/" + configuration.getCss())));
109+
110+
int charValue;
111+
112+
css = "";
113+
114+
// Loop through the characters of
115+
// the file and store them to the contents String
116+
// can throw a IOException
117+
while ((charValue = tempBufferedReader.read()) != -1) css += (char) charValue;
118+
119+
// Set the html tags
120+
css = "<style>" + css + "</style>";
121+
} catch (Exception exception) {
122+
css = "/* Unknown Error */";
123+
css = "<style>" + css + "</style>";
124+
LOGGER.info("Error opening css");
125+
}
80126

81127
// Replace the style
82-
//Replacer styleReplace = new Replacer(cssReplace, css);
128+
contents = contents.replaceAll(configuration.getStyle_replace(), css);
83129

84130
// And the js
85-
//Replacer jsReplace = new Replacer(styleReplace.toString(), JS);
131+
contents = contents.replaceAll(configuration.getJs_replace(), JS);
86132

87133
html = contents;
88134
}
89135

136+
private String error(int error) {
137+
LOGGER.info("Error: " + 404);
138+
switch (error) {
139+
case 404:
140+
error = 404;
141+
File file = new File(configuration.getWebroot() + "/" + error + ".html");
142+
143+
BufferedReader bufferedReader = null;
144+
try {
145+
bufferedReader = new BufferedReader(new FileReader(file));
146+
} catch (FileNotFoundException exception) {
147+
LOGGER.error(exception.getMessage());
148+
return "Error: 500 INTERNAL SERVER ERROR";
149+
}
150+
151+
int charValue;
152+
153+
while (true) {
154+
try {
155+
if (!((charValue = bufferedReader.read()) != -1)) break;
156+
} catch (IOException exception) {
157+
LOGGER.error(exception.getMessage());
158+
return "Error: 500 INTERNAL SERVER ERROR";
159+
}
160+
contents += (char) charValue;
161+
}
162+
return contents;
163+
default:
164+
error = 500;
165+
return "Error: 500 INTERNAL SERVER ERROR";
166+
}
167+
}
168+
90169
@Override
91170
public String toString() {
92171
return html;
93172
}
94173

174+
public int getError() {
175+
return error;
176+
}
177+
178+
public void setError(int error) {
179+
this.error = error;
180+
}
181+
95182
public String getHtml() {
96183
return html;
97184
}

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/main/resources/http.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
"webroot": "src/main/resources/webroot",
44
"style_replace": "<!--STYLE_REPLACE-->",
55
"js_replace": "<!--JS_REPLACE-->",
6-
"css": [
7-
"style.css"
8-
],
9-
"js": [
10-
"script.js"
11-
]
6+
"css": "style.css",
7+
"js": "script.js",
8+
"page_404": "404.html"
129
}

src/main/resources/webroot/404.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test
175 KB
Binary file not shown.

src/main/resources/webroot/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4+
<!--STYLE_REPLACE-->
45
<title>Test</title>
56
</head>
67
<body>
8+
<!--JS_REPLACE-->
79
<h1>Hope this works!</h1>
810
</body>
911
</html>

src/main/resources/webroot/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/*TEST*/

src/main/resources/webroot/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
font-weight: bolder;
3+
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

target/classes/http.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
"webroot": "src/main/resources/webroot",
44
"style_replace": "<!--STYLE_REPLACE-->",
55
"js_replace": "<!--JS_REPLACE-->",
6-
"css": [
7-
"style.css"
8-
],
9-
"js": [
10-
"script.js"
11-
]
6+
"css": "style.css",
7+
"js": "script.js",
8+
"page_404": "404.html"
129
}

target/classes/webroot/404.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

target/classes/webroot/favicon.ico

175 KB
Binary file not shown.

target/classes/webroot/index.html

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

0 commit comments

Comments
 (0)