Skip to content

Commit 8fb27a2

Browse files
committed
index.html support
1 parent 8eab5cd commit 8fb27a2

File tree

13 files changed

+69
-10
lines changed

13 files changed

+69
-10
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88

9-
import java.io.File;
109
import java.io.IOException;
1110
import java.io.InputStream;
1211
import java.io.OutputStream;
@@ -39,7 +38,7 @@ public void run() {
3938
LOGGER.info("Starting HTTP Parser ... ");
4039
HttpParser httpParser = new HttpParser(inputStream);
4140
httpParser.parseHttpRequest();
42-
HtmlParser htmlParser = new HtmlParser(new File(webRoot + httpParser.getRequestedPage()), "", "", "", "");
41+
HtmlParser htmlParser = new HtmlParser(webRoot + httpParser.getRequestedPage(), "", "", "", "");
4342
LOGGER.info("Done With HTTP Parser.");
4443

4544
// Set HTML variable

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ public class HtmlParser {
1111
private final static Logger LOGGER = LoggerFactory.getLogger(HttpServer.class);
1212

1313
private File file;
14+
private String filePath;
1415
private String contents = "";
1516
private String html = "";
1617

17-
public HtmlParser(File file, String css, String cssReplace, String JS, String JSReplace) {
18-
this.file = file;
18+
public HtmlParser(String filePath, String css, String cssReplace, String JS, String JSReplace) {
19+
this.file = new File(filePath);
20+
this.filePath = filePath;
1921

2022
// Read the file
2123
try {
@@ -30,9 +32,49 @@ public HtmlParser(File file, String css, String cssReplace, String JS, String JS
3032
// can throw a IOException
3133
while ((charValue = bufferedReader.read()) != -1) contents += (char) charValue;
3234
} catch (FileNotFoundException exception) {
33-
LOGGER.error("File Not Found: " + exception);
35+
if (filePath.charAt(filePath.length()-1) == '/') {
36+
try {
37+
// Open the file
38+
BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(filePath + "index.html")));
39+
40+
int charValue;
41+
42+
// Loop through the characters of
43+
// the file and store them to the contents String
44+
// can throw a IOException
45+
while ((charValue = bufferedReader.read()) != -1) contents += (char) charValue;
46+
} catch (FileNotFoundException exception1) {
47+
// ERROR: 404
48+
LOGGER.error("File Not Found: " + exception);
49+
} catch (IOException exception1) {
50+
// ERROR: 500
51+
LOGGER.error("Error reading file: " + exception);
52+
}
53+
} else if (!((filePath.substring(filePath.length()-5) == ".html") || (filePath.substring(filePath.length()-4) == ".htm"))) {
54+
try {
55+
// Open the file
56+
BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(filePath + "/index.html")));
57+
58+
int charValue;
59+
60+
// Loop through the characters of
61+
// the file and store them to the contents String
62+
// can throw a IOException
63+
while ((charValue = bufferedReader.read()) != -1) contents += (char) charValue;
64+
} catch (FileNotFoundException exception1) {
65+
// ERROR: 404
66+
LOGGER.error("File Not Found: " + exception);
67+
} catch (IOException exception1) {
68+
// ERROR: 500
69+
LOGGER.error("Error reading file: " + exception);
70+
}
71+
} else {
72+
// ERROR: 404
73+
LOGGER.error("File Not Found: " + exception);
74+
}
3475
} catch (IOException exception) {
35-
LOGGER.error("Can't Read File: " + exception);
76+
// ERROR: 500
77+
LOGGER.error("File Not Found: " + exception);
3678
}
3779
LOGGER.info(contents);
3880

src/main/resources/http.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"port": 80,
33
"webroot": "src/main/resources/webroot",
4-
"style_replace": "/*STYLE_REPLACE*/",
5-
"js_replace": "/*JS_REPLACE*/",
4+
"style_replace": "<!--STYLE_REPLACE-->",
5+
"js_replace": "<!--JS_REPLACE-->",
66
"css": [
77
"style.css"
88
],
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>
Binary file not shown.
2.11 KB
Binary file not shown.
Binary file not shown.
3.28 KB
Binary file not shown.
694 Bytes
Binary file not shown.
3.03 KB
Binary file not shown.

0 commit comments

Comments
 (0)