|
2 | 2 |
|
3 | 3 | import dev.hdprojects.http.GenerateHttpHeader; |
4 | 4 | import dev.hdprojects.http.HttpParser; |
| 5 | +import dev.hdprojects.website.HtmlParser; |
5 | 6 | import org.slf4j.Logger; |
6 | 7 | import org.slf4j.LoggerFactory; |
7 | 8 |
|
| 9 | +import java.io.File; |
8 | 10 | import java.io.IOException; |
9 | 11 | import java.io.InputStream; |
10 | 12 | import java.io.OutputStream; |
11 | 13 | import java.net.Socket; |
12 | 14 |
|
13 | | -public class HttpConnectionWorkerThread extends Thread{ |
| 15 | +public class HttpConnectionWorkerThread extends Thread { |
14 | 16 |
|
15 | 17 | private final static Logger LOGGER = LoggerFactory.getLogger(HttpConnectionWorkerThread.class); |
16 | 18 |
|
| 19 | + private String webRoot; |
17 | 20 | private Socket socket; |
18 | 21 |
|
19 | | - public HttpConnectionWorkerThread(Socket socket) { |
| 22 | + public HttpConnectionWorkerThread(Socket socket, String webRoot) { |
| 23 | + this.webRoot = webRoot; |
20 | 24 | this.socket = socket; |
21 | 25 | } |
22 | 26 |
|
23 | 27 | @Override |
24 | 28 | public void run() { |
| 29 | + |
25 | 30 | InputStream inputStream = null; |
26 | 31 | OutputStream outputStream = null; |
27 | 32 | try { |
| 33 | + |
| 34 | + // Create the streams to write to |
28 | 35 | inputStream = socket.getInputStream(); |
29 | 36 | outputStream = socket.getOutputStream(); |
30 | 37 |
|
| 38 | + // Create an instance of the HttpParser |
| 39 | + LOGGER.info("Starting HTTP Parser ... "); |
31 | 40 | HttpParser httpParser = new HttpParser(inputStream); |
32 | | - |
33 | 41 | httpParser.parseHttpRequest(); |
| 42 | + HtmlParser htmlParser = new HtmlParser(new File(webRoot + httpParser.getRequestedPage()), "", "", "", ""); |
| 43 | + LOGGER.info("Done With HTTP Parser."); |
34 | 44 |
|
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(); |
36 | 47 |
|
37 | | - final String CRLF = "\r\n"; // 13, 10 ASCII |
| 48 | + /* 13, 10 ASCII */ |
| 49 | + final String CRLF = "\r\n"; |
38 | 50 |
|
39 | | - GenerateHttpHeader httpHeader = new GenerateHttpHeader(200, html.length(), "text/html", "hd"); |
| 51 | + // Get the length of the html |
| 52 | + int contentLength = html.getBytes().length; |
40 | 53 |
|
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."); |
42 | 59 |
|
| 60 | + // Send the response |
| 61 | + LOGGER.info("Sending response ... "); |
43 | 62 | outputStream.write(response.getBytes()); |
44 | 63 |
|
45 | 64 | LOGGER.info("Connection processing finished"); |
|
0 commit comments