-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathHttpRequest.java
89 lines (69 loc) · 2.09 KB
/
HttpRequest.java
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
package webserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import util.HttpRequestUtils;
import util.IOUtils;
public class HttpRequest {
private static final Logger log = LoggerFactory.getLogger(HttpRequest.class);
private String method;
private String path;
private Map<String, String> headers = new HashMap<String, String>();
private Map<String, String> params = new HashMap<String, String>();
public HttpRequest(InputStream in) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line = br.readLine();
if (line == null) {
return;
}
processRequestLine(line);
while (line.equals("")) {
log.debug("header line: {}", line);
line = br.readLine();
String[] tokens = line.split(":");
headers.put(tokens[0].trim(), tokens[1].trim());
}
if("POST".equals(method)) {
String body = IOUtils.readData(br, Integer.parseInt(headers.get("Content-Length")));
params = HttpRequestUtils.parseQueryString(body);
}
} catch (IOException io) {
log.error(io.getMessage());
}
}
private void processRequestLine(String requestline) {
log.debug("request line: {}", requestline); // GET /doc/text.html HTTP/1.1
String[] tokens = requestline.split(" ");
method = tokens[0]; // GET
if ("POST".equals(method)) {
path = tokens[1];
return;
}
int index = tokens[1].indexOf("?");
if (index == -1) {
path = tokens[1];
} else {
path = tokens[1].substring(0, index); // 잘라내고 싶은 범위 정하기 (0부터 index까지)
params = HttpRequestUtils.parseQueryString(tokens[1].substring(index + 1));// 파라미터 받아오기
}
}
public String getMethod() {
return method;
}
public String getPath() {
return path;
}
public String getHeader(String name) {
return headers.get(name);
}
public String getParameter(String name) {
return params.get(name);
}
}