-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
HttpUtils.java
254 lines (231 loc) · 9.28 KB
/
HttpUtils.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package com.intuit.karate.http;
import com.intuit.karate.FileUtils;
import com.intuit.karate.StringUtils;
import com.intuit.karate.shell.Command;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.security.KeyStore;
import java.security.Security;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author pthomas3
*/
public class HttpUtils {
private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
private HttpUtils() {
// only static methods
}
public static Charset parseContentTypeCharset(String mimeType) {
Map<String, String> map = parseContentTypeParams(mimeType);
if (map == null) {
return null;
}
String cs = map.get("charset");
if (cs == null) {
return null;
}
return Charset.forName(cs);
}
public static Map<String, String> parseContentTypeParams(String mimeType) {
List<String> items = StringUtils.split(mimeType, ';', false);
int count = items.size();
if (count <= 1) {
return null;
}
Map<String, String> map = new LinkedHashMap<>(count - 1);
for (int i = 1; i < count; i++) {
String item = items.get(i);
int pos = item.indexOf('=');
if (pos == -1) {
continue;
}
String key = item.substring(0, pos).trim();
String val = item.substring(pos + 1).trim();
map.put(key, val);
}
return map;
}
public static Map<String, String> parseUriPattern(String pattern, String url) {
int qpos = url.indexOf('?');
if (qpos != -1) {
url = url.substring(0, qpos);
}
List<String> leftList = StringUtils.split(pattern, '/', false);
List<String> rightList = StringUtils.split(url, '/', false);
int leftSize = leftList.size();
int rightSize = rightList.size();
if (rightSize != leftSize) {
return null;
}
Map<String, String> map = new LinkedHashMap<>(leftSize);
for (int i = 0; i < leftSize; i++) {
String left = leftList.get(i);
String right = rightList.get(i);
if (left.equals(right)) {
continue;
}
if (left.startsWith("{") && left.endsWith("}")) {
left = left.substring(1, left.length() - 1);
map.put(left, right);
} else {
return null; // match failed
}
}
return map;
}
public static String normaliseUriPath(String uri) {
uri = uri.indexOf('?') == -1 ? uri : uri.substring(0, uri.indexOf('?'));
if (uri.endsWith("/")) {
uri = uri.substring(0, uri.length() - 1);
}
if (!uri.startsWith("/")) {
uri = "/" + uri;
}
return uri;
}
public static StringUtils.Pair parseUriIntoUrlBaseAndPath(String rawUri) {
int pos = rawUri.indexOf('/');
if (pos == -1) {
return StringUtils.pair(null, "");
}
URI uri;
try {
uri = new URI(rawUri);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (uri.getHost() == null) {
return StringUtils.pair(null, rawUri);
}
String path = uri.getRawPath();
pos = rawUri.lastIndexOf(path); // edge case that path is just "/"
String urlBase = rawUri.substring(0, pos);
return StringUtils.pair(urlBase, rawUri.substring(pos));
}
//==========================================================================
//
public static void flushAndClose(Channel ch) {
if (ch != null && ch.isActive()) {
ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}
public static void createSelfSignedCertificate(File cert, File key) {
try {
SelfSignedCertificate ssc = new SelfSignedCertificate();
FileUtils.copy(ssc.certificate(), cert);
FileUtils.copy(ssc.privateKey(), key);
} catch (Exception e) {
throw new RuntimeException();
}
}
private static final String PROXY_ALIAS = "karate-proxy";
private static final String KEYSTORE_PASSWORD = "karate-secret";
private static final String KEYSTORE_FILENAME = PROXY_ALIAS + ".jks";
public static SSLContext getSslContext(File keyStoreFile) {
keyStoreFile = initKeyStore(keyStoreFile);
String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
if (algorithm == null) {
algorithm = "SunX509";
}
try {
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(keyStoreFile), KEYSTORE_PASSWORD.toCharArray());
TrustManager[] trustManagers = new TrustManager[]{LenientTrustManager.INSTANCE};
KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
kmf.init(ks, KEYSTORE_PASSWORD.toCharArray());
KeyManager[] keyManagers = kmf.getKeyManagers();
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(keyManagers, trustManagers, null);
return ctx;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static File initKeyStore(File keyStoreFile) {
if (keyStoreFile == null) {
keyStoreFile = new File(KEYSTORE_FILENAME);
}
if (keyStoreFile.exists()) {
if (logger.isTraceEnabled()) {
logger.trace("keystore file already exists: {}", keyStoreFile);
}
return keyStoreFile;
}
File parentFile = keyStoreFile.getParentFile();
if (parentFile != null) {
parentFile.mkdirs();
}
Command.exec(false, parentFile, "keytool", "-genkey", "-alias", PROXY_ALIAS, "-keysize",
"4096", "-validity", "36500", "-keyalg", "RSA", "-dname",
"CN=" + PROXY_ALIAS, "-keypass", KEYSTORE_PASSWORD, "-storepass",
KEYSTORE_PASSWORD, "-keystore", keyStoreFile.getName());
Command.exec(false, parentFile, "keytool", "-exportcert", "-alias", PROXY_ALIAS, "-keystore",
keyStoreFile.getName(), "-storepass", KEYSTORE_PASSWORD, "-file", keyStoreFile.getName() + ".der");
return keyStoreFile;
}
public static FullHttpResponse createResponse(int status, String body) {
return createResponse(HttpResponseStatus.valueOf(status), body);
}
public static FullHttpResponse createResponse(HttpResponseStatus status, String body) {
byte[] bytes = FileUtils.toBytes(body);
ByteBuf bodyBuf = Unpooled.copiedBuffer(bytes);
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, bodyBuf);
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, bytes.length);
return response;
}
public static FullHttpResponse transform(FullHttpResponse original, String body) {
FullHttpResponse response = createResponse(original.status(), body);
response.headers().set(original.headers());
return response;
}
private static final HttpResponseStatus CONNECTION_ESTABLISHED = new HttpResponseStatus(200, "Connection established");
public static FullHttpResponse connectionEstablished() {
return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, CONNECTION_ESTABLISHED);
}
public static void fixHeadersForProxy(HttpRequest request) {
String adjustedUri = ProxyContext.removeHostColonPort(request.uri());
request.setUri(adjustedUri);
request.headers().remove(HttpHeaderNames.CONNECTION);
// addViaHeader(request, PROXY_ALIAS);
}
public static void addViaHeader(HttpMessage msg, String alias) {
StringBuilder sb = new StringBuilder();
sb.append(msg.protocolVersion().majorVersion()).append('.');
sb.append(msg.protocolVersion().minorVersion()).append(' ');
sb.append(alias);
List<String> list;
if (msg.headers().contains(HttpHeaderNames.VIA)) {
List<String> existing = msg.headers().getAll(HttpHeaderNames.VIA);
list = new ArrayList<>(existing);
list.add(sb.toString());
} else {
list = Collections.singletonList(sb.toString());
}
msg.headers().set(HttpHeaderNames.VIA, list);
}
}