Skip to content

Commit 987473a

Browse files
committed
Merge branch 'restructure'
2 parents cbe6850 + d77a6a3 commit 987473a

File tree

6 files changed

+131
-129
lines changed

6 files changed

+131
-129
lines changed

lib/src/main/java/com/devsegal/jserve/BaseHTTPServer.java

Lines changed: 45 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,49 @@
33
import java.io.IOException;
44
import java.net.ServerSocket;
55
import java.net.Socket;
6-
import java.nio.file.Files;
7-
import java.nio.file.*;
86
import java.util.HashMap;
9-
import java.io.File;
10-
import java.io.BufferedReader;
11-
import java.io.FileReader;
127

138
public class BaseHTTPServer implements Runnable {
14-
15-
private int port = 80;
16-
private ServerSocket ss;
17-
private Socket s;
18-
public static boolean isStopped = false;
19-
private String serverPath = "C:\\Users\\gitpu\\programming\\jswerve\\server"; // for other users it will be empty
20-
private HashMap<String, String> routes;
21-
22-
// structure of a server folder
23-
/*
24-
1. Routes file
25-
--- includes all the information on routes (from -> where)
26-
2. Data files, etc
27-
--- for most of this, I believe it will be inherit
28-
3. Connections, mass communication
29-
--- this is more of a inherit/just-in-time system based on more complex infrastructure
30-
*/
319

32-
private void initServerSocket() {
33-
try {
34-
ss = new ServerSocket(port);
35-
} catch(IOException e) {
36-
System.out.println(e.getMessage()); // replace with logging utility
37-
}
38-
}
10+
private int port = 80;
11+
private boolean isStopped = false;
3912

40-
private void acceptConectionsAndThrowToHandlers() {
41-
try {
42-
while (!isStopped) {
43-
s = ss.accept();
44-
45-
(new Thread(new ConnectionHandler(s))).start();
46-
}
47-
48-
} catch(IOException e) {
49-
System.out.println(e.getMessage());
50-
}
51-
}
13+
private String serverPath = "";
5214

15+
private ServerSocket server;
16+
private HashMap<String, WebRouteHandler> routesToHandlers;
17+
5318
public BaseHTTPServer(int port) {
5419
this.port = port;
20+
routesToHandlers = new HashMap<>();
21+
}
22+
23+
public void setupOrigin(String path) {
24+
serverPath = path;
25+
}
5526

56-
// parse the routes in the routes file
57-
parseRoutes();
27+
public void route(String path, String method, WebRouteHandler handler) {
28+
routesToHandlers.put(path + method, handler);
29+
}
30+
31+
private void initializeServerSocket() {
32+
try {
33+
server = new ServerSocket(port);
34+
} catch(IOException e) {
35+
System.out.println(e.getMessage());
36+
}
5837
}
5938

39+
/*
6040
public void parseRoutes() {
61-
File routesFile = new File(serverPath + "\\" + "routes.txt");
62-
41+
File routesFile = new File(serverPath + "routes.txt");
42+
6343
try {
64-
BufferedReader br = new BufferedReader(new FileReader(routesFile));
44+
BufferedReader reader = new BufferedReader(new FileReader(routesFile));
6545
String line;
6646
String[] paths;
6747
68-
while((line = br.readLine()) != null) {
48+
while((line = reader.readLine()) != null) {
6949
paths = line.split(" ");
7050
routes.put(paths[0], paths[1]);
7151
}
@@ -74,36 +54,34 @@ public void parseRoutes() {
7454
7555
} catch (Exception e) {
7656
System.out.println(e.getMessage());
77-
}
78-
}
57+
}
58+
} */
7959

80-
public void stop() {
81-
isStopped = true;
82-
}
83-
84-
public void setupOrigin(String path) {
85-
serverPath = path;
60+
private void acceptConectionsAndThrowToHandlers() {
61+
try {
62+
while (!isStopped) {
63+
Socket connection = server.accept();
64+
65+
(new Thread(new ConnectionHandler(connection, routesToHandlers, serverPath))).start();
66+
}
67+
68+
} catch(IOException e) {
69+
System.out.println(e.getMessage());
70+
}
8671
}
8772

8873
public void run() {
89-
// before starting the server, we have to get all the information regarding:
90-
/*
91-
1. Routes - routes file
92-
2. Data (files, strings, etc) includes any html, js, css files etc.... (all srcs will originate from here I believe)
93-
3. Connections (just-in-time approach) bulk messaging, communication handling, etc
94-
*/
95-
96-
// for this version, we'll take in a file and parse it with all the routes, etc...
97-
// In later versions, perhaps we'll provide an option in doing this programmatically...
98-
99-
100-
initServerSocket();
74+
initializeServerSocket();
10175

10276
try {
10377
acceptConectionsAndThrowToHandlers();
10478
} catch (Exception e) {
10579
e.printStackTrace(); // replace with logging utility
10680
}
10781
}
82+
83+
public void stop() {
84+
isStopped = true;
85+
}
10886
}
10987

lib/src/main/java/com/devsegal/jserve/ConnectionHandler.java

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,54 @@
33
import java.io.InputStreamReader;
44
import java.io.IOException;
55
import java.io.BufferedReader;
6-
import java.io.PrintWriter;
76
import java.net.Socket;
7+
import java.nio.file.Files;
88
import java.util.HashMap;
9-
10-
public class ConnectionHandler implements Runnable {
11-
12-
StringBuilder reqFullContents;
13-
BufferedReader br;
14-
PrintWriter pw;
15-
Socket conn;
16-
17-
public ConnectionHandler(Socket s) {
18-
this.conn = s;
9+
import java.nio.file.Path;
10+
import java.util.function.Consumer;
11+
12+
class ConnectionHandler implements Runnable {
13+
14+
StringBuilder requestContents;
15+
BufferedReader reader;
16+
ResponseWriter responseWriter;
17+
BufferedReader requestReader;
18+
Socket connection;
19+
HashMap<String, WebRouteHandler> routes;
20+
String serverPath;
21+
String contentType;
22+
String responsePath;
23+
24+
25+
public ConnectionHandler(Socket connection, HashMap<String, WebRouteHandler> routes, String serverPath) {
26+
this.connection = connection;
27+
this.routes = routes;
28+
this.serverPath = serverPath;
29+
this.requestContents = new StringBuilder();
30+
31+
try {
32+
responseWriter = new ResponseWriter(connection.getOutputStream());
33+
} catch(IOException e) {
34+
e.printStackTrace();
35+
}
36+
37+
try {
38+
requestReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
39+
} catch(IOException e) {
40+
e.printStackTrace();
41+
}
1942
}
2043

2144
public void run() {
22-
try {
23-
reqFullContents = new StringBuilder();
24-
BufferedReader br = new BufferedReader(
25-
new InputStreamReader(conn.getInputStream()));
26-
27-
// parse request
28-
RequestParser rp = new RequestParser();
29-
rp.parseReq(br);
30-
31-
32-
33-
} catch (IOException e) {
34-
35-
}
45+
RequestParser requestParser = new RequestParser();
46+
requestParser.parseRequest(requestReader);
3647

37-
try {
38-
pw = new PrintWriter(
39-
conn.getOutputStream());
40-
} catch (IOException e) {
41-
e.printStackTrace();
42-
}
43-
44-
try {
45-
ResponseWriter rw = new ResponseWriter(conn.getOutputStream());
46-
rw.setConnectionType("close");
47-
rw.setContentType("text/html");
48-
49-
rw.send();
50-
} catch (IOException e) {
48+
WebRouteHandler webRouteHandler = routes.get(requestParser.getPath() + requestParser.getMethod());
5149

50+
if(webRouteHandler == null) {
51+
responseWriter.send(); // response to invalid requests
52+
} else {
53+
webRouteHandler.handler(requestParser, responseWriter);
5254
}
53-
54-
}
55+
}
5556
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.devsegal.jserve;
2+
3+
public class Request {
4+
private String method;
5+
private String path;
6+
7+
public Request(String method, String path) {
8+
this.method = method;
9+
this.path = path;
10+
}
11+
}

lib/src/main/java/com/devsegal/jserve/RequestParser.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,31 @@ public class RequestParser {
1010
private String httpVersion = "";
1111
private HashMap<String, String> headers;
1212

13+
1314
public RequestParser() {
14-
headers = new HashMap<String, String>();
15+
headers = new HashMap<>();
1516
}
1617

17-
public void parseReq(BufferedReader br) {
18-
18+
public void parseRequest(BufferedReader reader) {
1919
try {
20-
if(br.ready()) {
21-
String[] arrOfStrings = br.readLine().split(" "); // should be the METHOD PATH HTTP VERSION
20+
if(reader.ready()) {
21+
String[] firstLine = reader.readLine().split(" "); // should be the METHOD PATH HTTP VERSION
2222

23-
method = arrOfStrings[0];
24-
path = arrOfStrings[1];
25-
httpVersion = arrOfStrings[2];
23+
method = firstLine[0];
24+
path = firstLine[1];
25+
httpVersion = firstLine[2];
2626
}
2727

28-
while(br.ready()) {
29-
String str = br.readLine();
30-
String[] header = str.split(":", 2);
28+
while(reader.ready()) {
29+
String line = reader.readLine();
30+
String[] header = line.split(":", 2);
3131

3232
if(header.length == 2) {
3333
headers.put(header[0], header[1]);
3434
}
3535
}
3636
} catch (IOException e) {
37-
37+
e.printStackTrace();
3838
}
3939
}
4040

lib/src/main/java/com/devsegal/jserve/ResponseWriter.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,42 @@
22

33
import java.io.PrintWriter;
44
import java.io.OutputStream;
5+
import java.util.List;
56

67
public class ResponseWriter extends PrintWriter implements Response {
7-
private StringBuilder respContents;
8+
private StringBuilder responseContents;
89

9-
public ResponseWriter(OutputStream os) {
10-
super(os);
11-
respContents = new StringBuilder();
10+
public ResponseWriter(OutputStream output) {
11+
super(output);
12+
responseContents = new StringBuilder();
1213

1314
// add in default http information
14-
respContents.append("HTTP 1.1/200" + "\n");
15+
responseContents.append("HTTP 1.1/200" + "\n");
1516
}
1617

1718
@Override
1819
public void setContentType(String contentType) {
19-
respContents.append("Content-type: " + contentType + "\n");
20+
responseContents.append("Content-type: " + contentType + "\n");
2021
}
2122

2223
@Override
2324
public void setConnectionType(String connectionType) {
24-
respContents.append("Connection-type:" + connectionType + "\n");
25+
responseContents.append("Connection-type:" + connectionType + "\n");
2526
}
2627

2728
@Override
2829
public void insertContent(String content) {
29-
respContents.append("\n" + content + "\n");
30+
responseContents.append("\n" + content + "\n");
31+
}
32+
33+
public void insertContent(List<String> content) {
34+
for(String contentMember : content) {
35+
responseContents.append(contentMember);
36+
}
3037
}
3138

3239
public void send() {
33-
print(respContents.toString());
40+
print(responseContents);
3441
close();
3542
}
3643
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.devsegal.jserve;
2+
3+
public interface WebRouteHandler {
4+
public void handler(RequestParser requestParser, ResponseWriter responseWriter);
5+
}

0 commit comments

Comments
 (0)