forked from DeNA/PacketProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncodeHTTPBase.java
More file actions
393 lines (299 loc) · 10.2 KB
/
EncodeHTTPBase.java
File metadata and controls
393 lines (299 loc) · 10.2 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Copyright 2019 DeNA Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package packetproxy.encode;
import static packetproxy.util.Logging.errWithStackTrace;
import java.io.InputStream;
import packetproxy.http.Http;
import packetproxy.http2.FramesBase;
import packetproxy.http2.Http2;
import packetproxy.http3.service.Http3;
import packetproxy.model.Packet;
public abstract class EncodeHTTPBase extends Encoder {
public enum HTTPVersion {
HTTP1, HTTP2, HTTP3
}
private HTTPVersion httpVersion;
private FramesBase http2;
private Http3 http3;
private String requestMethod = "";
public EncodeHTTPBase() {
super("http/1.1");
httpVersion = HTTPVersion.HTTP1;
}
public EncodeHTTPBase(String ALPN) throws Exception {
super(ALPN);
if (ALPN == null) {
httpVersion = HTTPVersion.HTTP1;
} else if (ALPN.equals("http/1.0") || ALPN.equals("http/1.1")) {
httpVersion = HTTPVersion.HTTP1;
} else if (ALPN.equals("h2") || ALPN.equals("grpc") || ALPN.equals("grpc-exp")) {
httpVersion = HTTPVersion.HTTP2;
http2 = new Http2();
} else if (ALPN.equals("h3")) {
httpVersion = HTTPVersion.HTTP3;
http3 = new Http3();
} else {
httpVersion = HTTPVersion.HTTP1;
}
}
public EncodeHTTPBase(String ALPN, FramesBase http2CustomFrame) throws Exception {
super(ALPN);
httpVersion = HTTPVersion.HTTP2;
this.http2 = http2CustomFrame;
}
public HTTPVersion getHttpVersion() {
return httpVersion;
}
@Override
public int checkDelimiter(byte[] data) throws Exception {
if (this.httpVersion == HTTPVersion.HTTP1) {
return Http.parseHttpDelimiter(data);
} else if (this.httpVersion == HTTPVersion.HTTP2) {
return http2.checkDelimiter(data);
} else {
return http3.checkDelimiter(data);
}
}
@Override
public int checkResponseDelimiter(byte[] data) throws Exception {
if (this.requestMethod.equals("HEAD")) {
return data.length;
}
return checkDelimiter(data);
}
@Override
public void clientRequestArrived(byte[] frames) throws Exception {
if (this.httpVersion == HTTPVersion.HTTP1) {
super.clientRequestArrived(frames);
} else if (this.httpVersion == HTTPVersion.HTTP2) {
http2.clientRequestArrived(frames);
} else {
http3.clientRequestArrived(frames);
}
}
@Override
public void serverResponseArrived(byte[] frames) throws Exception {
if (this.httpVersion == HTTPVersion.HTTP1) {
super.serverResponseArrived(frames);
} else if (this.httpVersion == HTTPVersion.HTTP2) {
http2.serverResponseArrived(frames);
} else {
http3.serverResponseArrived(frames);
}
}
@Override
public byte[] passThroughClientRequest() throws Exception {
if (this.httpVersion == HTTPVersion.HTTP1) {
return super.passThroughClientRequest();
} else if (this.httpVersion == HTTPVersion.HTTP2) {
return http2.passThroughClientRequest();
} else {
return http3.passThroughClientRequest();
}
}
@Override
public byte[] passThroughServerResponse() throws Exception {
if (this.httpVersion == HTTPVersion.HTTP1) {
return super.passThroughServerResponse();
} else if (this.httpVersion == HTTPVersion.HTTP2) {
return http2.passThroughServerResponse();
} else {
return http3.passThroughServerResponse();
}
}
@Override
public byte[] clientRequestAvailable() throws Exception {
if (this.httpVersion == HTTPVersion.HTTP1) {
return super.clientRequestAvailable();
} else if (this.httpVersion == HTTPVersion.HTTP2) {
return http2.clientRequestAvailable();
} else {
return http3.clientRequestAvailable();
}
}
@Override
public byte[] serverResponseAvailable() throws Exception {
if (this.httpVersion == HTTPVersion.HTTP1) {
return super.serverResponseAvailable();
} else if (this.httpVersion == HTTPVersion.HTTP2) {
return http2.serverResponseAvailable();
} else {
return http3.serverResponseAvailable();
}
}
@Override
public byte[] decodeClientRequest(byte[] input_data) throws Exception {
if (this.httpVersion == HTTPVersion.HTTP2) {
input_data = http2.decodeClientRequest(input_data);
} else if (this.httpVersion == HTTPVersion.HTTP3) {
input_data = http3.decodeClientRequest(input_data);
}
Http http = Http.create(input_data);
Http decodedHttp = http;
if (http.getFirstHeader("X-PacketProxy-Skip-ClientSideEncode").contains("true")) {
encode_mode = 1;
} else {
decodedHttp = decodeClientRequestHttp(http);
}
return decodedHttp.toByteArray();
}
@Override
public byte[] encodeClientRequest(byte[] input_data) throws Exception {
Http http = Http.create(input_data);
this.requestMethod = http.getMethod();
Http encodedHttp = encodeClientRequestHttp(http);
byte[] encodedData = encodedHttp.toByteArray();
if (this.httpVersion == HTTPVersion.HTTP2) {
encodedData = http2.encodeClientRequest(encodedData);
} else if (this.httpVersion == HTTPVersion.HTTP3) {
encodedData = http3.encodeClientRequest(encodedData);
}
return encodedData;
}
@Override
public final byte[] decodeServerResponse(byte[] input_data) throws Exception {
if (this.httpVersion == HTTPVersion.HTTP2) {
input_data = http2.decodeServerResponse(input_data);
} else if (this.httpVersion == HTTPVersion.HTTP3) {
input_data = http3.decodeServerResponse(input_data);
}
Http http;
if (this.requestMethod.equals("HEAD")) {
http = Http.createWithoutTouchingContentLength(input_data);
} else {
http = Http.create(input_data);
}
Http decodedHttp = decodeServerResponseHttp(http);
return decodedHttp.toByteArray();
}
@Override
public byte[] encodeServerResponse(byte[] input_data) throws Exception {
Http http;
if (this.requestMethod.equals("HEAD")) {
http = Http.createWithoutTouchingContentLength(input_data);
} else {
http = Http.create(input_data);
}
Http encodedHttp = http;
if (encode_mode == 0) {
encodedHttp = encodeServerResponseHttp(http);
}
byte[] encodedData = encodedHttp.toByteArray();
if (this.httpVersion == HTTPVersion.HTTP2) {
encodedData = http2.encodeServerResponse(encodedData);
} else if (this.httpVersion == HTTPVersion.HTTP3) {
encodedData = http3.encodeServerResponse(encodedData);
}
return encodedData;
}
@Override
public void putToClientFlowControlledQueue(byte[] frames) throws Exception {
if (this.httpVersion == HTTPVersion.HTTP2) {
http2.putToClientFlowControlledQueue(frames);
} else {
super.putToClientFlowControlledQueue(frames);
}
}
@Override
public void putToServerFlowControlledQueue(byte[] frames) throws Exception {
if (this.httpVersion == HTTPVersion.HTTP2) {
http2.putToServerFlowControlledQueue(frames);
} else {
super.putToServerFlowControlledQueue(frames);
}
}
@Override
public InputStream getClientFlowControlledInputStream() {
if (this.httpVersion == HTTPVersion.HTTP2) {
return http2.getClientFlowControlledInputStream();
} else {
return super.getClientFlowControlledInputStream();
}
}
@Override
public InputStream getServerFlowControlledInputStream() {
if (this.httpVersion == HTTPVersion.HTTP2) {
return http2.getServerFlowControlledInputStream();
} else {
return super.getServerFlowControlledInputStream();
}
}
@Override
public String getContentType(byte[] input_data) throws Exception {
Http http = Http.create(input_data);
return http.getFirstHeader("Content-Type");
}
/**
* レスポンスからContent-Typeを取得し、存在しない場合はリクエストのContent-Typeをフォールバックとして使用する。
* gRPC等のプロトコルでは、レスポンスにContent-Typeが含まれないことがあるため、
* リクエストのContent-Typeを使用することで、History一覧のType列に適切な値を表示する。
*/
@Override
public String getContentType(Packet client_packet, Packet server_packet) throws Exception {
String contentType = getContentType(server_packet.getDecodedData());
if (contentType.isEmpty() && client_packet != null && client_packet.getDecodedData().length > 0) {
contentType = getContentType(client_packet.getDecodedData());
}
return contentType;
}
@Override
public String getSummarizedResponse(Packet packet) {
String summary = "";
if (packet.getDecodedData().length == 0 && packet.getModifiedData().length == 0) {
return "";
}
try {
byte[] data = (packet.getDecodedData().length > 0) ? packet.getDecodedData() : packet.getModifiedData();
Http http = Http.create(data);
String statusCode = http.getStatusCode();
summary = statusCode;
} catch (Exception e) {
errWithStackTrace(e);
summary = "Headlineを生成できません・・・";
}
return summary;
}
@Override
public String getSummarizedRequest(Packet packet) {
String summary = "";
if (packet.getDecodedData().length == 0 && packet.getModifiedData().length == 0) {
return "";
}
try {
byte[] data = (packet.getDecodedData().length > 0) ? packet.getDecodedData() : packet.getModifiedData();
Http http = Http.create(data);
summary = http.getMethod() + " " + http.getURL(packet.getServerPort(), packet.getUseSSL());
} catch (Exception e) {
errWithStackTrace(e);
summary = "Headlineを生成できません・・・";
}
return summary;
}
@Override
public void setGroupId(Packet packet) throws Exception {
if (httpVersion == HTTPVersion.HTTP2) {
http2.setGroupId(packet);
} else if (httpVersion == HTTPVersion.HTTP3) {
http3.setGroupId(packet);
} else {
super.setGroupId(packet);
}
}
protected abstract Http decodeServerResponseHttp(Http inputHttp) throws Exception;
protected abstract Http encodeServerResponseHttp(Http inputHttp) throws Exception;
protected abstract Http decodeClientRequestHttp(Http inputHttp) throws Exception;
protected abstract Http encodeClientRequestHttp(Http inputHttp) throws Exception;
}