Skip to content

Commit ee1fa06

Browse files
committed
don't know v2
1 parent 370b796 commit ee1fa06

File tree

7 files changed

+80
-100
lines changed

7 files changed

+80
-100
lines changed

src/jswerve/BaseHTTPServer.java

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
11
package jswerve;
22

3-
import java.io.BufferedReader;
4-
import java.io.PrintWriter;
53
import java.io.IOException;
6-
import java.io.InputStreamReader;
7-
import java.io.OutputStreamWriter;
84
import java.net.ServerSocket;
95
import java.net.Socket;
10-
import java.util.Date;
6+
import java.util.ArrayList;
117

12-
public class BaseHTTPServer {
8+
public class BaseHTTPServer implements Runnable {
139

1410
private int port = 80;
15-
private BufferedReader reader;
16-
private PrintWriter writer;
1711
private ServerSocket ss;
1812
private Socket s;
19-
20-
public void initServerSocket() {
13+
public static boolean isStopped = false;
14+
15+
private void initServerSocket() {
2116
try {
2217
ss = new ServerSocket(port);
2318
} catch(IOException e) {
24-
System.out.println(e.getMessage());
19+
System.out.println(e.getMessage()); // replace with logging utility
2520
}
2621
}
2722

28-
public void acceptConectionsAndThrowToHandlers() throws InterruptedException {
23+
private void acceptConectionsAndThrowToHandlers() {
2924
try {
30-
while (true) {
25+
while (!isStopped) {
3126
s = ss.accept();
3227

3328
(new Thread(new ConnectionHandler(s))).start();
@@ -38,26 +33,27 @@ public void acceptConectionsAndThrowToHandlers() throws InterruptedException {
3833
}
3934
}
4035

41-
public BaseHTTPServer(int port) throws InterruptedException {
36+
public BaseHTTPServer(int port) {
4237
this.port = port;
43-
initServerSocket();
44-
try {
45-
acceptConectionsAndThrowToHandlers();
46-
} catch (Exception e) {
47-
// TODO Auto-generated catch block
48-
e.printStackTrace();
49-
}
5038
}
51-
52-
public BaseHTTPServer() throws InterruptedException {
53-
initServerSocket();
54-
try {
55-
acceptConectionsAndThrowToHandlers();
56-
} catch (Exception e) {
57-
// TODO Auto-generated catch block
58-
e.printStackTrace();
59-
}
39+
40+
public void stop() {
41+
isStopped = true;
6042
}
43+
44+
public void run() {
45+
initServerSocket();
46+
47+
try {
48+
acceptConectionsAndThrowToHandlers();
49+
} catch (Exception e) {
50+
e.printStackTrace(); // replace with logging utility
51+
}
6152

53+
}
54+
55+
public void setRoute(String route, RouteHandler rh) {
56+
57+
}
58+
}
6259

63-
}

src/jswerve/ConnectionHandler.java

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,53 @@
11
package jswerve;
22

3-
import java.io.InputStream;
4-
53
import java.io.IOException;
64
import java.io.BufferedReader;
75
import java.io.InputStreamReader;
86
import java.io.PrintWriter;
97
import java.io.OutputStreamWriter;
10-
import java.io.OutputStream;
118
import java.net.Socket;
12-
import java.util.Date;
139

1410
public class ConnectionHandler implements Runnable {
1511

1612
StringBuilder reqFullContents;
1713
BufferedReader br;
1814
PrintWriter pw;
1915
Socket conn;
16+
private boolean close = false;
2017

21-
public ConnectionHandler(Socket s) throws IOException {
22-
this.conn = s;
23-
24-
br = new BufferedReader(
25-
new InputStreamReader(s.getInputStream()));
26-
27-
pw = new PrintWriter(
28-
new OutputStreamWriter(s.getOutputStream()));
29-
30-
// read the contents of request, assign stringbuilder to entirity of value, and then parse using requestparser,
31-
// then finally compose a response and send it.
32-
33-
Response resp = new Response(STATUS.OK);
34-
35-
resp.setConnectionType("close");
36-
resp.setContentType("text/html");
37-
38-
39-
pw.close();
18+
public ConnectionHandler(Socket s) {
19+
this.conn = s;
4020
}
4121

4222
public void run() {
43-
try {
44-
conn.close();
45-
} catch (IOException e) {
46-
// add to logs
47-
}
48-
}
23+
try {
24+
br = new BufferedReader(
25+
new InputStreamReader(conn.getInputStream()));
26+
27+
String content;
28+
29+
while((content = br.readLine()) != null)
30+
System.out.println(content);
31+
} catch (IOException e) {
32+
e.printStackTrace();
33+
// insert logging utility
34+
}
35+
36+
try {
37+
pw = new PrintWriter(
38+
new OutputStreamWriter(conn.getOutputStream()));
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
43+
Response resp = new Response(STATUS.OK);
44+
45+
resp.setConnectionType("close");
46+
resp.setContentType("text/html");
47+
resp.insertContent("hello world!");
48+
resp.insertContent("<br><div><b>sdf</b></div>");
49+
pw.print(resp);
50+
51+
pw.close();
52+
}
4953
}

src/jswerve/Response.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,45 @@
22

33
public class Response {
44

5-
StringBuilder reqContents;
5+
private StringBuilder respContents;
66
private boolean bodyContentInserted = false;
77

88
public Response(STATUS s) {
9-
reqContents = new StringBuilder(30);
9+
respContents = new StringBuilder(30);
1010

1111
if(s.equals(STATUS.OK)) {
12-
reqContents.append("HTTP/1.1 200");
12+
respContents.append("HTTP/1.1 200");
1313
}
1414

1515
else if(s.equals(STATUS.NOT_FOUND)) {
16-
reqContents.append("HTTP/1.1 404");
16+
respContents.append("HTTP/1.1 404");
1717
}
1818
}
1919

2020
public void setContentType(String contentType) {
21-
if(!reqContents.toString().contains(contentType)) {
22-
reqContents.append("\nContent-type: " + contentType);
21+
if(!respContents.toString().contains(contentType)) {
22+
respContents.append("\nContent-type: " + contentType);
2323
}
2424
}
2525

2626
public void setConnectionType(String connectionType) {
27-
if(!reqContents.toString().contains(connectionType)) {
28-
reqContents.append("\nConnection-type: " + connectionType);
27+
if(!respContents.toString().contains(connectionType)) {
28+
respContents.append("\nConnection-type: " + connectionType);
2929
}
3030
}
3131

3232
public void insertContent(String content) {
3333
if(!bodyContentInserted) {
34-
reqContents.append("\n\n" + content);
34+
respContents.append("\n\n" + content);
3535
bodyContentInserted = true;
3636
}
3737

3838
else {
39-
reqContents.append("\n" + content);
39+
respContents.append("\n" + content);
4040
}
4141
}
4242

4343
public String toString() {
44-
return reqContents.toString();
44+
return respContents.toString();
4545
}
4646
}

src/jswerve/RouteHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package jswerve;
2+
3+
public interface RouteHandler {
4+
5+
}

src/jswerve/STATUS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
public enum STATUS {
44
OK,
5-
NOT_FOUND
5+
NOT_FOUND
66
}

src/jswerve/TestingClient.java

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

src/jswerve/TestingServer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
public class TestingServer {
44

55
public static void main(String[] args) throws InterruptedException {
6-
int port = 8000;
6+
int port = 8080;
77
System.out.println("Starting server at port: " + port);
88

9-
BaseHTTPServer bhs = new BaseHTTPServer(port);
9+
BaseHTTPServer bhs = new BaseHTTPServer(8080);
10+
(new Thread(bhs)).start();
1011
}
1112
}

0 commit comments

Comments
 (0)