Skip to content

Commit 0ad7784

Browse files
committed
wtt
1 parent ee1fa06 commit 0ad7784

23 files changed

+277
-219
lines changed

.classpath

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

.project

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

.settings/org.eclipse.jdt.core.prefs

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

BaseHTTPServer.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package main.java.jswerve;
2+
3+
import java.io.IOException;
4+
import java.net.ServerSocket;
5+
import java.net.Socket;
6+
import java.nio.file.Files;
7+
import java.nio.file.*;
8+
import java.util.HashMap;
9+
import java.io.File;
10+
import java.io.BufferedReader;
11+
import java.io.FileReader;
12+
13+
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+
*/
31+
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+
}
39+
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+
}
52+
53+
public BaseHTTPServer(int port) {
54+
this.port = port;
55+
56+
// parse the routes in the routes file
57+
parseRoutes();
58+
}
59+
60+
public void parseRoutes() {
61+
File routesFile = new File(serverPath + "\\" + "routes.txt");
62+
63+
try {
64+
BufferedReader br = new BufferedReader(new FileReader(routesFile));
65+
String line;
66+
String[] paths;
67+
68+
while((line = br.readLine()) != null) {
69+
paths = line.split(" ");
70+
routes.put(paths[0], paths[1]);
71+
}
72+
73+
System.out.println(routes.toString());
74+
75+
} catch (Exception e) {
76+
System.out.println(e.getMessage());
77+
}
78+
}
79+
80+
public void stop() {
81+
isStopped = true;
82+
}
83+
84+
public void setupOrigin(String path) {
85+
serverPath = path;
86+
}
87+
88+
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();
101+
102+
try {
103+
acceptConectionsAndThrowToHandlers();
104+
} catch (Exception e) {
105+
e.printStackTrace(); // replace with logging utility
106+
}
107+
}
108+
}
109+

ConnectionHandler.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main.java.jswerve;
2+
3+
import java.io.InputStreamReader;
4+
import java.io.IOException;
5+
import java.io.BufferedReader;
6+
import java.io.PrintWriter;
7+
import java.net.Socket;
8+
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;
19+
}
20+
21+
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+
}
36+
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) {
51+
52+
}
53+
54+
}
55+
}

RequestParser.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main.java.jswerve;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.util.HashMap;
6+
7+
public class RequestParser {
8+
private String method = "";
9+
private String path = "";
10+
private String httpVersion = "";
11+
private HashMap<String, String> headers;
12+
13+
public RequestParser() {
14+
headers = new HashMap<String, String>();
15+
}
16+
17+
public void parseReq(BufferedReader br) {
18+
19+
try {
20+
if(br.ready()) {
21+
String[] arrOfStrings = br.readLine().split(" "); // should be the METHOD PATH HTTP VERSION
22+
23+
method = arrOfStrings[0];
24+
path = arrOfStrings[1];
25+
httpVersion = arrOfStrings[2];
26+
}
27+
28+
while(br.ready()) {
29+
String str = br.readLine();
30+
String[] header = str.split(":", 2);
31+
32+
if(header.length == 2) {
33+
headers.put(header[0], header[1]);
34+
}
35+
}
36+
} catch (IOException e) {
37+
38+
}
39+
}
40+
41+
public String getMethod() {
42+
return method;
43+
}
44+
45+
public String getPath() {
46+
return path;
47+
}
48+
49+
public String getHttpVersion() {
50+
return httpVersion;
51+
}
52+
}

Response.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main.java.jswerve;
2+
3+
interface Response {
4+
5+
StringBuilder respContents = new StringBuilder();
6+
boolean bodyContentInserted = false;
7+
8+
public void setContentType(String contentType);
9+
public void setConnectionType(String connectionType);
10+
public void insertContent(String content);
11+
}

ResponseWriter.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main.java.jswerve;
2+
3+
import java.io.PrintWriter;
4+
import java.io.OutputStream;
5+
6+
public class ResponseWriter extends PrintWriter implements Response {
7+
private StringBuilder respContents;
8+
9+
public ResponseWriter(OutputStream os) {
10+
super(os);
11+
respContents = new StringBuilder();
12+
13+
// add in default http information
14+
respContents.append("HTTP 1.1/200" + "\n");
15+
}
16+
17+
@Override
18+
public void setContentType(String contentType) {
19+
respContents.append("Content-type: " + contentType + "\n");
20+
}
21+
22+
@Override
23+
public void setConnectionType(String connectionType) {
24+
respContents.append("Connection-type:" + connectionType + "\n");
25+
}
26+
27+
@Override
28+
public void insertContent(String content) {
29+
respContents.append("\n" + content + "\n");
30+
}
31+
32+
public void send() {
33+
print(respContents.toString());
34+
close();
35+
}
36+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package jswerve;
1+
package main.java.jswerve;
22

33
public enum STATUS {
44
OK,

TestingServer.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main.java.jswerve;
2+
3+
public class TestingServer {
4+
5+
public static void main(String[] args) {
6+
7+
int port = args.length > 0 ? Integer.parseInt(args[0]) : 8080;
8+
System.out.println("Starting server at port: " + port);
9+
10+
BaseHTTPServer bhs = new BaseHTTPServer(port);
11+
(new Thread(bhs)).start();
12+
}
13+
}

0 commit comments

Comments
 (0)