Skip to content

Commit f1ed590

Browse files
author
yangzhixiang
committed
修改加载图片时不打印字节流,增加标准httpstatus篡改以及延时
1 parent 6686654 commit f1ed590

File tree

5 files changed

+239
-168
lines changed

5 files changed

+239
-168
lines changed

jsondata/data.json

+90-142
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,93 @@
11
[
2-
{
3-
"path": "/api/v1/merchants",
4-
"method": "get",
5-
"response": {
6-
"code": "200",
7-
"message": "",
8-
"data": {
9-
"total_number": 1,
10-
"total_page": 1,
11-
"current_page": 1,
12-
"data": [
13-
{
14-
"id": "58f7ec836597d000018dc029",
15-
"name": "murphy",
16-
"sn": "1680002786541",
17-
"core_biz_id": "1538b69d-2554-11e7-b68f-00163e00625b",
18-
"certificate": {},
19-
"business": {
20-
"legal_person_name": "992",
21-
"content": "",
22-
"license_photo": "http://images.wosaimg.com/05/810ec5718cf886c0da38f394205f78a4752cd8.jpg"
23-
},
24-
"owner": {
25-
"name": "",
26-
"mobile": ""
27-
},
28-
"contact": {
29-
"name": "",
30-
"mobile": "",
31-
"email": ""
32-
},
33-
"user": {
34-
"id": "58458b7f4cbef1b9948ffd05",
35-
"name": "王新昌"
36-
},
37-
"area": {
38-
"province_id": "110000",
39-
"province": "上海市",
40-
"city_id": "110100",
41-
"city": "",
42-
"district_id": "110102",
43-
"district": ""
44-
},
45-
"address": {
46-
"logitude": null,
47-
"latitude": null,
48-
"street_address": "松江大学城文汇路188号"
49-
},
50-
"verify_status": 0,
51-
"withdraw_mode": 2,
52-
"industry_id": null,
53-
"ctime": 1492642872000,
54-
"scope_id": null,
55-
"scope_name": null,
56-
"status": 1
57-
}
58-
]
59-
}
60-
}
61-
},
62-
{
63-
"path": "/api/v1/merchant_audits",
64-
"method": "get",
65-
"response": {
66-
"code": "200",
67-
"message": "",
68-
"data": {
69-
"total_number": 21,
70-
"total_page": 2,
71-
"current_page": 1,
72-
"data": [
73-
{
74-
"id": "4767a500-75f0-42fa-8eda-acf3436a0aa5",
75-
"merchant_id": "25faa770-d7df-482a-9abf-b8f956e6189a",
76-
"merchant_sn": "1680002786535",
77-
"merchant_name": "10912341234验证商户11",
78-
"merchant_ctime": 1492590605552,
79-
"status": 3,
80-
"remark": "",
81-
"description": "这次给通过啦",
82-
"merchant_province": "内蒙古自治区",
83-
"merchant_city": "呼和浩特市",
84-
"merchant_district": "回民区",
85-
"merchant_street_address": "回民区1234号",
86-
"merchant_contact_name": "我是回民",
87-
"merchant_contact_cellphone": "10912341234",
88-
"brand_photo": "http://images.wosaimg.com/b8/7d48ca9f1d71a6e1aed7e75b2c3146246ce47f.png",
89-
"indoor_material_photo": "http://images.wosaimg.com/aa/ed8404c7bc75d3ca75aa23e32da3fbb20bb15c.png",
90-
"outdoor_material_photo": "http://images.wosaimg.com/eb/8098a59fe3496c38c27cb5a9cde2e495e2a75d.png",
91-
"other_photos": [
92-
"http://images.wosaimg.com/b8/7d48ca9f1d71a6e1aed7e75b2c3146246ce47f.png",
93-
"http://images.wosaimg.com/aa/ed8404c7bc75d3ca75aa23e32da3fbb20bb15c.png",
94-
"http://images.wosaimg.com/eb/8098a59fe3496c38c27cb5a9cde2e495e2a75d.png"
95-
],
96-
"ctime": 1492590605779,
97-
"mtime": 1492594607390,
98-
"status_change_details": [
99-
{
100-
"operator_type": 1,
101-
"operator": "58ad5790e4b00858c8d74814",
102-
"operator_platform": "CRM",
103-
"status": 1,
104-
"remark": "哈哈1234!!!",
105-
"ctime": 1492594241104
106-
},
107-
{
108-
"operator_type": 2,
109-
"operator": "10911113333",
110-
"operator_platform": "osp",
111-
"status": 4,
112-
"ctime": 1492594501828,
113-
"remark": "驳回啦,不开心"
114-
},
115-
{
116-
"operator_type": 1,
117-
"operator": "58ad5790e4b00858c8d74814",
118-
"operator_platform": "CRM",
119-
"status": 1,
120-
"remark": "哈哈1234!!!",
121-
"ctime": 1492594582860
122-
},
123-
{
124-
"operator_type": 2,
125-
"operator": "10911113333",
126-
"operator_platform": "osp",
127-
"status": 3,
128-
"ctime": 1492594607390,
129-
"remark": "这次给通过啦"
130-
}
131-
]
132-
}
133-
]
134-
}
135-
}
136-
},
137-
{
138-
"path": "/api/v1/keeprecord/store/*",
139-
"method": "post",
140-
"response": {
141-
"code": "10001",
142-
"message": "维护人不能为空"
143-
}
2+
{
3+
"path": "/api/v1/merchants",
4+
"method": "get",
5+
"wait": "3",
6+
"response": "500"
7+
},
8+
{
9+
"path": "/api/v1/merchant_audits",
10+
"method": "get",
11+
"wait":"0",
12+
"response": {
13+
"code": "200",
14+
"message": "",
15+
"data": {
16+
"total_number": 21,
17+
"total_page": 2,
18+
"current_page": 1,
19+
"data": [
20+
{
21+
"id": "4767a500-75f0-42fa-8eda-acf3436a0aa5",
22+
"merchant_id": "25faa770-d7df-482a-9abf-b8f956e6189a",
23+
"merchant_sn": "1680002786535",
24+
"merchant_name": "10912341234验证商户11",
25+
"merchant_ctime": 1492590605552,
26+
"status": 3,
27+
"remark": "",
28+
"description": "这次给通过啦",
29+
"merchant_province": "内蒙古自治区",
30+
"merchant_city": "呼和浩特市",
31+
"merchant_district": "回民区",
32+
"merchant_street_address": "回民区1234号",
33+
"merchant_contact_name": "我是回民",
34+
"merchant_contact_cellphone": "10912341234",
35+
"brand_photo": "http://images.wosaimg.com/b8/7d48ca9f1d71a6e1aed7e75b2c3146246ce47f.png",
36+
"indoor_material_photo": "http://images.wosaimg.com/aa/ed8404c7bc75d3ca75aa23e32da3fbb20bb15c.png",
37+
"outdoor_material_photo": "http://images.wosaimg.com/eb/8098a59fe3496c38c27cb5a9cde2e495e2a75d.png",
38+
"other_photos": [
39+
"http://images.wosaimg.com/b8/7d48ca9f1d71a6e1aed7e75b2c3146246ce47f.png",
40+
"http://images.wosaimg.com/aa/ed8404c7bc75d3ca75aa23e32da3fbb20bb15c.png",
41+
"http://images.wosaimg.com/eb/8098a59fe3496c38c27cb5a9cde2e495e2a75d.png"
42+
],
43+
"ctime": 1492590605779,
44+
"mtime": 1492594607390,
45+
"status_change_details": [
46+
{
47+
"operator_type": 1,
48+
"operator": "58ad5790e4b00858c8d74814",
49+
"operator_platform": "CRM",
50+
"status": 1,
51+
"remark": "哈哈1234!!!",
52+
"ctime": 1492594241104
53+
},
54+
{
55+
"operator_type": 2,
56+
"operator": "10911113333",
57+
"operator_platform": "osp",
58+
"status": 4,
59+
"ctime": 1492594501828,
60+
"remark": "驳回啦,不开心"
61+
},
62+
{
63+
"operator_type": 1,
64+
"operator": "58ad5790e4b00858c8d74814",
65+
"operator_platform": "CRM",
66+
"status": 1,
67+
"remark": "哈哈1234!!!",
68+
"ctime": 1492594582860
69+
},
70+
{
71+
"operator_type": 2,
72+
"operator": "10911113333",
73+
"operator_platform": "osp",
74+
"status": 3,
75+
"ctime": 1492594607390,
76+
"remark": "这次给通过啦"
77+
}
78+
]
79+
}
80+
]
81+
}
14482
}
83+
},
84+
{
85+
"path": "/api/v1/keeprecord/store/*",
86+
"method": "post",
87+
"wait": "0",
88+
"response": {
89+
"code": "10001",
90+
"message": "维护人不能为空"
91+
}
92+
}
14593
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.wosaitest.httpproxy;
2+
3+
/**
4+
* Created by yangzhixiang on 2017/6/27.
5+
*/
6+
public class DataPair {
7+
8+
private int wait;
9+
private Object resp;
10+
11+
public DataPair(int wait, Object resp) {
12+
this.wait = wait;
13+
this.resp = resp;
14+
}
15+
16+
public int getWait() {
17+
return this.wait;
18+
}
19+
20+
public Object getValue() {
21+
return this.resp;
22+
}
23+
}

src/main/java/com/wosaitest/httpproxy/LaunchMain.java

+33-16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.HashMap;
66
import java.util.Map;
77

8+
import io.netty.handler.codec.http.*;
89
import org.littleshoot.proxy.HttpFilters;
910
import org.littleshoot.proxy.HttpFiltersAdapter;
1011
import org.littleshoot.proxy.HttpFiltersSourceAdapter;
@@ -17,10 +18,6 @@
1718
import io.netty.buffer.CompositeByteBuf;
1819
import io.netty.buffer.Unpooled;
1920
import io.netty.channel.ChannelHandlerContext;
20-
import io.netty.handler.codec.http.FullHttpResponse;
21-
import io.netty.handler.codec.http.HttpObject;
22-
import io.netty.handler.codec.http.HttpRequest;
23-
import io.netty.handler.codec.http.HttpResponse;
2421

2522
/**
2623
* Created by yangzhixiang on 2017/6/22.
@@ -32,7 +29,7 @@ public class LaunchMain {
3229
private static final String FIDDLE_TYPE = "FIDDLE_MODE";
3330
private static int type = 0;
3431
private static String data_file = "data.json";
35-
private static Map<KeyPair, String> map = new HashMap<>();
32+
private static Map<KeyPair, DataPair> map = new HashMap<>();
3633

3734
public static void main(String[] args) {
3835
if (args.length == 1) {
@@ -69,11 +66,12 @@ public HttpResponse clientToProxyRequest(HttpObject httpObject) {
6966
return null;
7067
}
7168

69+
7270
@Override
7371
public HttpObject proxyToClientResponse(HttpObject httpObject) {
7472
if (httpObject instanceof FullHttpResponse) {
7573
FullHttpResponse fullHttpResponse = (FullHttpResponse) httpObject;
76-
modifyHttpResponse(originalRequest, fullHttpResponse);
74+
return modifyHttpResponse(originalRequest, fullHttpResponse);
7775
}
7876
return httpObject;
7977
}
@@ -92,28 +90,47 @@ public int getMaximumResponseBufferSizeInBytes() {
9290
}).start();
9391
}
9492

95-
private static void modifyHttpResponse(HttpRequest orgRequest, FullHttpResponse httpResponse) {
93+
private static FullHttpResponse modifyHttpResponse(HttpRequest orgRequest, FullHttpResponse httpResponse) {
9694
KeyPair keyPair = new KeyPair(orgRequest.getUri(), ParserJson.getMethodType(orgRequest.getMethod().toString()));
97-
log.info("Org KeyPair: " + keyPair.toString() + "\n Org Response: "
98-
+ httpResponse.content().toString(Charset.forName("utf-8")));
95+
String acceptRanges = httpResponse.headers().get(HttpHeaderNames.ACCEPT_RANGES);
96+
if (acceptRanges != null && acceptRanges.contains("bytes")) {
97+
log.info("Org KeyPair: " + keyPair.toString() + "\n Org Response: Whoops! maybe the type of response is byte stream, so we don't show it");
98+
} else {
99+
log.info("Org KeyPair: " + keyPair.toString() + "\n Org Response: " + httpResponse.content().toString(Charset.forName("utf-8")));
100+
}
99101
if (type == 1) {
100102
CompositeByteBuf contentBuf = (CompositeByteBuf) httpResponse.content();
101-
for (Map.Entry<KeyPair, String> entry : map.entrySet()) {
103+
for (Map.Entry<KeyPair, DataPair> entry : map.entrySet()) {
102104
KeyPair tmp = entry.getKey();
103105
if (tmp.equals(keyPair)) {
104106
try {
105-
String newResp = entry.getValue();
106-
log.info("Find Match KeyPair: " + tmp.toString() + "\n New Response: " + newResp);
107-
ByteBuf newContent = Unpooled.wrappedBuffer(newResp.getBytes("utf-8"));
108-
int len = newContent.readableBytes();
109-
contentBuf.clear().writeBytes(newContent);
110-
httpResponse.headers().set("content-length", len);
107+
DataPair data = entry.getValue();
108+
int wait = data.getWait();
109+
Object resp = data.getValue();
110+
if (resp instanceof String) {
111+
log.info("Find Match KeyPair: " + tmp.toString() + "\n New Response: " + resp.toString());
112+
ByteBuf newContent = Unpooled.wrappedBuffer(resp.toString().getBytes("utf-8"));
113+
int len = newContent.readableBytes();
114+
contentBuf.clear().writeBytes(newContent);
115+
httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, len);
116+
} else {
117+
log.info("Find Match KeyPair: " + tmp.toString() + "\n New HttpStatusCode: " + ((HttpResponseStatus) resp).code());
118+
httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, ((HttpResponseStatus) resp));
119+
log.info(httpResponse.toString());
120+
}
121+
if (wait != 0) {
122+
log.info("Org KeyPair: " + keyPair.toString() + " response sleep for {} seconds", wait);
123+
Thread.sleep(wait * 1000);
124+
}
111125
} catch (UnsupportedEncodingException e) {
112126
// TODO Auto-generated catch block
113127
log.error(e.getMessage());
128+
} catch (InterruptedException e) {
129+
log.error(e.getMessage());
114130
}
115131
}
116132
}
117133
}
134+
return httpResponse;
118135
}
119136
}

0 commit comments

Comments
 (0)