-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.java
More file actions
149 lines (123 loc) · 4.3 KB
/
Copy pathHttpRequest.java
File metadata and controls
149 lines (123 loc) · 4.3 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package httpserver;
import java.io.IOException;
import java.io.BufferedReader;
import java.lang.RuntimeException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Set;
public class HttpRequest {
// Possible HTTP request methods.
static enum Method {GET, HEAD}; // POST,PUT, ...
protected Method requestMethod;
protected String path;
protected HashMap<String, String> headers;
private String lastHeader;
/**
* Request factory
*
* Creates a factory by reading the socket input stream.
*/
public static HttpRequest fromReader(BufferedReader socketReader)
throws IOException, HttpError, NoRequestException
{
// The first line is the request line.
HttpRequest request = new HttpRequest(socketReader.readLine());
// Next lines are the headers.
boolean processing_headers = true;
String headerLine;
while (processing_headers) {
headerLine = socketReader.readLine();
if (headerLine == null || headerLine.equals("")) {
// The headers are terminated by a blank line.
processing_headers = false;
} else {
request.parseHeaderLine(headerLine);
}
}
return request;
}
public HttpRequest() {
requestMethod = Method.GET;
path = "/";
headers = new HashMap<String, String>();
lastHeader = null;
}
public HttpRequest(String requestLine)
throws HttpError, NoRequestException
{
parseRequestLine(requestLine);
headers = new HashMap<String, String>();
lastHeader = null;
}
public String toString() {
return String.format("%1s %2s HTTP/1.1", requestMethod, path);
}
private void parseRequestLine(String requestLine)
throws HttpError, NoRequestException
{
// Catch empty input streams.
if (requestLine == null) {
throw new NoRequestException();
}
String[] rlParts = requestLine.split(" ");
// Exactly 3 parts are expected.
if (rlParts.length != 3) {
throw new HttpError(HttpStatus.BadRequest(), "Malformed request line.");
}
// Check and select method enum.
switch (rlParts[0]) {
case "GET":
requestMethod = Method.GET;
break;
case "HEAD":
requestMethod = Method.HEAD;
break;
default:
throw new HttpError(HttpStatus.NotImplemented(), rlParts[0]);
}
// Parse the target to get the path.
try {
URI uri = new URI(rlParts[1]);
path = uri.getPath();
} catch (URISyntaxException urise) {
throw new HttpError(HttpStatus.BadRequest(), "Malformed target in request line.");
}
// Check Protocol.
if (!rlParts[2].equals("HTTP/1.1")) {
throw new HttpError(HttpStatus.HttpVersionNotSupported(), rlParts[2]);
}
}
private void parseHeaderLine(String headerLine) throws HttpError {
String key, value;
if (headerLine.startsWith(" ") || headerLine.startsWith("\t")) {
// Header continuation.
// Multi-line headers are deprecated and must be converted to
// single-line before further processing (RFC 7230 3.2.4)
if (lastHeader == null) {
// No header to continue.
throw new HttpError(HttpStatus.BadRequest(), "Malformed header: " + headerLine);
}
key = lastHeader;
value = headers.get(key) + " " + headerLine.trim();
} else {
String[] keyvalue = headerLine.split(": *", 2);
if (keyvalue.length != 2) {
throw new HttpError(HttpStatus.BadRequest(), "Malformed header: " + headerLine);
}
key = keyvalue[0].toLowerCase();
value = keyvalue[1].trim();
}
headers.put(key, value);
lastHeader = key;
}
public Method getMethod() {
return requestMethod;
}
public String getHeader(String headerName) {
return headers.get(headerName.toLowerCase());
}
public Set<String> getHeaderNames() {
return headers.keySet();
}
}