Skip to content

Commit 7a5fa99

Browse files
committed
POST data is now accessible in the request parameter, in the route method.
1 parent 8c6b12e commit 7a5fa99

File tree

4 files changed

+117
-60
lines changed

4 files changed

+117
-60
lines changed

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99

1010
class ConnectionHandler implements Runnable {
1111

12-
BufferedReader reader;
13-
ResponseWriter responseWriter;
14-
BufferedReader requestReader;
15-
Path originalServerPath;
16-
Socket connection;
17-
String contentType;
18-
NotFoundPageHandler notFoundPageHandler;
19-
HashMap<String, WebRouteHandler> routesToHandlers;
20-
12+
private boolean keepAlive = true;
13+
private BufferedReader requestReader;
14+
private ResponseWriter responseWriter;
15+
private Path originalServerPath;
16+
private Path publicAssetPath;
17+
private Socket connection;
18+
private NotFoundPageHandler notFoundPageHandler;
19+
private HashMap<String, WebRouteHandler> routesToHandlers;
2120

2221
public ConnectionHandler(ConnectionHandlerConfiguration configuration) {
2322
this.connection = configuration.getConnection();
@@ -48,9 +47,15 @@ public ConnectionHandler(ConnectionHandlerConfiguration configuration) {
4847
}
4948

5049
public void run() {
50+
while(keepAlive) {
5151
RequestParser requestParser = new RequestParser(requestReader);
5252
requestParser.parseRequest();
5353

54+
// Check if it's a persistent/session connection
55+
if(!( requestParser.getHeaders().get("Connection").equals("keep-alive") )) {
56+
keepAlive = false;
57+
}
58+
5459
WebRouteHandler webRouteHandler = routesToHandlers.get(requestParser.getPath() + requestParser.getMethod());
5560

5661
// If the page is not officially registered as a path, in other words - a 404 page is now necessary
@@ -60,4 +65,7 @@ public void run() {
6065
webRouteHandler.handler(requestParser, responseWriter);
6166
}
6267
}
68+
69+
System.out.print("\n\n");
70+
}
6371
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.devsegal.jserve;
22

3-
import java.io.BufferedReader;
4-
53
public interface FilterData<T> {
6-
public boolean badData(T data, BufferedReader reader);
4+
public boolean badData(T data);
75
}

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

Lines changed: 92 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,124 @@
33
import java.io.BufferedReader;
44
import java.io.IOException;
55
import java.util.HashMap;
6+
import java.util.function.Consumer;
7+
import java.util.function.Function;
8+
import java.util.function.Predicate;
69

710
public class RequestParser {
811
private String method = "";
912
private String path = "";
1013
private String httpVersion = "";
1114
private BufferedReader reader;
15+
private HashMap<String, String> postData;
1216
private HashMap<String, String> headers;
13-
14-
17+
1518
public RequestParser(BufferedReader reader) {
1619
this.reader = reader;
20+
postData = new HashMap<String, String>();
1721
headers = new HashMap<>();
1822
}
1923

20-
public void getHeaders(FilterData<String> filterData) {
21-
try {
22-
while(reader.ready()) {
23-
String line = reader.readLine();
24-
25-
// if the filter has found a reason to stop continuing
26-
if(filterData.badData(line, reader)) {
27-
break;
28-
}
29-
30-
String[] header = line.split(":", 2);
31-
32-
System.out.println(line);
33-
34-
if(header.length == 2) {
35-
headers.put(header[0], header[1]);
36-
}
37-
}
38-
} catch(IOException e) {
39-
e.printStackTrace();
24+
public void parseRequest() {
25+
parseFirstLine();
26+
27+
if(method.equals("GET")) {
28+
parseContentAfterFirstLineForGetRequest();
29+
} else if(method.equals("POST")) {
30+
parseContentAfterFirstLineForPostRequest();
4031
}
4132
}
4233

43-
public void parseRequest() {
34+
public void parseFirstLine() {
4435
try {
36+
4537
boolean waitingForInformation = true;
4638

4739
while(waitingForInformation) {
4840
if(reader.ready()) {
4941
waitingForInformation = false;
5042

51-
String[] firstLine = reader.readLine().split(" "); // should be the METHOD PATH HTTP VERSION
43+
String[] firstLine = reader.readLine().split(" ");
5244

5345
method = firstLine[0];
5446
path = firstLine[1];
5547
httpVersion = firstLine[2];
5648
}
5749
}
5850

59-
if(method.equals("GET")) {
60-
getHeaders((data, reader) -> {
61-
return true;
51+
} catch(IOException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
56+
public void parseContentAfterFirstLineForGetRequest() {
57+
readContent(line -> false);
58+
}
59+
60+
public void parseContentAfterFirstLineForPostRequest() {
61+
readContent(line -> parseRequestLineForSignsOfPostData(line));
62+
}
63+
64+
public void readContent(FilterData<String> filterData) {
65+
try {
66+
while(reader.ready()) {
67+
String line = reader.readLine();
68+
69+
// If the filter found a reason to stop continuing reading data from the request
70+
if(filterData.badData(line)) {
71+
break;
72+
}
73+
74+
splitLine(line, ":", (splittedLine) -> splittedLine.length == 2, (splittedLine) -> {
75+
headers.put(splittedLine[0], splittedLine[1]);
76+
77+
System.out.println(splittedLine[0] + ":" + splittedLine[1]);
6278
});
63-
} else if(method.equals("POST")) {
64-
getHeaders((data, reader) -> {
65-
if(data.length() == 0) {
66-
try {
67-
while(reader.ready())
68-
System.out.println("post data: " + reader.readLine());
69-
} catch(IOException e) {
70-
e.printStackTrace();
71-
}
72-
return true;
73-
} else {
74-
return false;
79+
80+
}
81+
} catch(IOException e) {
82+
e.printStackTrace();
83+
}
84+
}
85+
86+
public void splitLine(String line, String splitter, Predicate<String[]> predicate, Consumer<String[]> action) {
87+
String[] splitLine = line.split(splitter);
88+
89+
if(predicate.test(splitLine)) {
90+
action.accept(splitLine);
91+
}
92+
}
93+
94+
public boolean parseRequestLineForSignsOfPostData(String line) {
95+
if(line.length() == 0) {
96+
97+
try {
98+
StringBuilder tempPostData = new StringBuilder();
99+
100+
while(reader.ready()) {
101+
tempPostData.append((char)reader.read());
102+
}
103+
104+
System.out.println(tempPostData);
105+
106+
splitLine(tempPostData.toString(), "&", (splittedLine) -> true, (splittedLine) -> {
107+
108+
for(String parameterAndValue : splittedLine) {
109+
String[] parameterSplitted = parameterAndValue.split("=");
110+
postData.put(parameterSplitted[0], parameterSplitted[1]);
111+
System.out.println(parameterSplitted[0] + "=" + parameterSplitted[1]);
75112
}
113+
76114
});
115+
116+
return true; // Post data will be the next line
117+
118+
} catch(IOException e) {
119+
e.printStackTrace();
77120
}
78-
} catch (IOException e) {
79-
e.printStackTrace();
80121
}
122+
123+
return false; // If post data has not been recognized
81124
}
82125

83126
public String getMethod() {
@@ -91,4 +134,12 @@ public String getPath() {
91134
public String getHttpVersion() {
92135
return httpVersion;
93136
}
137+
138+
public HashMap<String, String> getPostData() {
139+
return postData;
140+
}
141+
142+
public HashMap<String, String> getHeaders() {
143+
return headers;
144+
}
94145
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.devsegal.jserve;
22

3-
import java.util.EnumSet;
4-
53
public class ResponseHeaders {
64
private STATUS responseStatus;
75
private int responseCode;
86

97
private String contentType;
108
private String connectionType;
119

10+
private static final String CRLF = "\r\n";
11+
1212
public ResponseHeaders(STATUS responseStatus, int responseCode, String contentType, String connectionType) {
1313
this.responseStatus = responseStatus;
1414
this.responseCode = responseCode;
@@ -43,16 +43,16 @@ public String getConnectionType() {
4343
public static String convertToString(ResponseHeaders responseHeaders) {
4444
StringBuilder responseString = new StringBuilder();
4545

46-
String firstLineResponsePrefix = "HTTP 1.1/";
46+
String firstLineResponsePrefix = "HTTP/1.1 ";
4747
responseString.append(firstLineResponsePrefix + responseHeaders.getResponseCode() + " ");
48-
responseString.append(ResponseHeaders.convertResponseStatusToString(responseHeaders) + "\n");
48+
responseString.append(ResponseHeaders.convertResponseStatusToString(responseHeaders) + CRLF);
4949

50-
responseString.append("Connection-type:" + responseHeaders.getConnectionType() + "\n");
50+
responseString.append("Connection-type:" + responseHeaders.getConnectionType() + CRLF);
5151

52-
responseString.append("Content-type:" + responseHeaders.getContentType() + "\n");
52+
responseString.append("Content-type:" + responseHeaders.getContentType() + CRLF);
5353

5454
// before adding the content, add one line in between headers and body
55-
responseString.append("\n");
55+
responseString.append(CRLF);
5656

5757
return responseString.toString();
5858
}

0 commit comments

Comments
 (0)