-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathHttpRequestUtils.java
152 lines (126 loc) · 4.15 KB
/
HttpRequestUtils.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
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
150
151
152
package util;
import model.User;
import db.DataBase;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
public class HttpRequestUtils {
public static String headerRequest(InputStream in) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = reader.readLine();
if (line == null) { return null; }
String url = line.split(" ")[1];
System.out.println(line);
//if (!url.endsWith(".html")) {return null;}
/*while (!"".equals(line)) {
if (line == null) { return null; }
System.out.println(line);
line = reader.readLine();
}*/
return url;
}
public static String parseUrl(String url) {
int index = url.indexOf("?");
String requestPath = url.substring(0, index);
return url.substring(index+1);
}
public static void signUpRequest(String url) {
String params = parseUrl(url);
Map<String, String> map = parseQueryString(params);
User user = new User(
map.get("userId"),
map.get("password"),
map.get("name"),
map.get("email")
);
}
/**
* @param queryString은
* URL에서 ? 이후에 전달되는 field1=value1&field2=value2 형식임
* @return
*/
public static Map<String, String> parseQueryString(String queryString) {
return parseValues(queryString, "&");
}
/**
* @param 쿠키 값은 name1=value1; name2=value2 형식임
* @return
*/
public static Map<String, String> parseCookies(String cookies) {
return parseValues(cookies, ";");
}
private static Map<String, String> parseValues(String values, String separator) {
if (Strings.isNullOrEmpty(values)) {
return Maps.newHashMap();
}
String[] tokens = values.split(separator);
return Arrays.stream(tokens).map(t -> getKeyValue(t, "=")).filter(p -> p != null)
.collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));
}
static Pair getKeyValue(String keyValue, String regex) {
if (Strings.isNullOrEmpty(keyValue)) {
return null;
}
String[] tokens = keyValue.split(regex);
if (tokens.length != 2) {
return null;
}
return new Pair(tokens[0], tokens[1]);
}
public static Pair parseHeader(String header) {
return getKeyValue(header, ": ");
}
public static class Pair {
String key;
String value;
Pair(String key, String value) {
this.key = key.trim();
this.value = value.trim();
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
@Override
public String toString() {
return "Pair [key=" + key + ", value=" + value + "]";
}
}
}