-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.java
More file actions
133 lines (106 loc) · 3.75 KB
/
Copy pathHttpRequest.java
File metadata and controls
133 lines (106 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package was.httpserver;
import util.MyLogger;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import static java.nio.charset.StandardCharsets.UTF_8;
import static util.MyLogger.log;
public class HttpRequest {
private String method;
private String path;
private Map<String,String> queryParameters = new HashMap<>();
private Map<String,String> headers = new HashMap<>();
public HttpRequest(BufferedReader reader) throws IOException{
parseRequestLine(reader);
parseHeaders(reader);
parseBody(reader);
}
private void parseBody(BufferedReader reader) throws IOException {
if(!headers.containsKey("Content-Length")) return;
int contentLength = Integer.parseInt(headers.get("Content-Length"));
char[] bodyChars = new char[contentLength];
int read = reader.read(bodyChars);
if(read != contentLength) {
throw new IOException("Fail to read entrie body. Expected " + contentLength + " bytes, but read " + read);
}
String body = new String(bodyChars);
log("HTTP Message Body: " + body);
String contentType = headers.get("Content-Type");
if("application/x-www-form-urlencoded".equals(contentType)){
parseQueryParameters(body);
}
}
private void parseRequestLine(BufferedReader reader) throws IOException {
String requestLine = reader.readLine();
if (requestLine == null){
throw new IOException("EOF : No reuest line received");
}
String[] parts = requestLine.split(" ");
if(parts.length != 3){
throw new IOException("Invalid request line " + requestLine);
}
method = parts[0];
String[] pathParts = parts[1].split("\\?");
path = pathParts[0];
if(pathParts.length >1){
parseQueryParameters(pathParts[1]);
}
}
private void parseQueryParameters(String queryString) {
for (String param : queryString.split("&")) {
String[] keyValue = param.split("=");
String key = URLDecoder.decode(keyValue[0], UTF_8);
String value = keyValue.length > 1 ? URLDecoder.decode(keyValue[1], UTF_8) : "";
queryParameters.put(key,value);
}
}
private void parseHeaders(BufferedReader reader) throws IOException {
String line;
while(!(line = reader.readLine()).isEmpty()){
String[] headerParts = line.split(":");
headers.put(headerParts[0].trim(), headerParts[1].trim());
}
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Map<String, String> getQueryParameters() {
return queryParameters;
}
public void setQueryParameters(Map<String, String> queryParameters) {
this.queryParameters = queryParameters;
}
public Map<String, String> getHeaders() {
return headers;
}
public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}
public String getParameter(String name){
return queryParameters.get(name);
}
public String getHeader(String name){
return headers.get(name);
}
@Override
public String toString() {
return "HttpRequest{" +
"method='" + method + '\'' +
", path='" + path + '\'' +
", queryParameters=" + queryParameters +
", headers=" + headers +
'}';
}
}