-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.java
More file actions
75 lines (59 loc) · 1.72 KB
/
HttpRequest.java
File metadata and controls
75 lines (59 loc) · 1.72 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
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HttpRequest {
public class InvalidHttpRequest extends Exception {
private static final long serialVersionUID = 1L;
}
private static Pattern regex = Pattern.compile(
"^([A-Z]+) ([a-zA-Z0-9/\\-_.~!#$%&'()*+,/:;=?@\\[\\]]+) HTTP/(\\d\\.\\d)\\n((?:[A-Za-z0-9\\-]+: .+\\n)*)$");
private Map<String, String> headers;
private String method;
private String path;
private String version;
public HttpRequest(String request) throws InvalidHttpRequest {
headers = new HashMap<String, String>();
Matcher matcher = regex.matcher(request);
if(! matcher.matches()) {
throw new InvalidHttpRequest();
}
method = matcher.group(1);
path = stripPath(matcher.group(2));
version = matcher.group(3);
for(String line: matcher.group(4).split("\n")) {
String[] parts = line.split(": ", 2);
if(parts.length != 2)
continue;
headers.put(parts[0], parts[1]);
}
if(version.equals("1.1") && !headers.containsKey("Host"))
throw new InvalidHttpRequest();
}
public String getHeader(String type) {
return headers.get(type);
}
public String getMethod() {
return method;
}
public String getPath() {
return path;
}
public String getVersion() {
return version;
}
private String stripPath(String path) {
if(path.indexOf('?') != -1)
path = path.substring(0, path.indexOf('?'));
if(path.indexOf('#') != -1)
path = path.substring(0, path.indexOf('#'));
try {
path = URLDecoder.decode(path, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return path;
}
}