HttpForm.java
6.51 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
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.xfa.protocol;
import com.adobe.xfa.protocol.Protocol;
import com.adobe.xfa.protocol.ProtocolUtils;
import com.adobe.xfa.ut.ExFull;
import com.adobe.xfa.ut.MsgFormat;
import com.adobe.xfa.ut.ResId;
import com.adobe.xfa.ut.Resolver;
import com.adobe.xfa.ut.StringHolder;
import com.adobe.xfa.ut.StringUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HttpForm {
public static final int ChunkSize = 4096;
public static final int MixedSize = 32;
private PostEncodingType meEncoding = PostEncodingType.UNKNOWN_ENCODING;
private String msContentType;
private String msCharSet;
private Map<String, String> msHeaderMap;
private ByteArrayOutputStream mPairs;
private ByteArrayOutputStream mChunk;
private List<Protocol.MultiPartDesc> moMulti;
private byte[] mRespData;
private int mnRespCode;
private String msRespType;
public void setEncodingType(PostEncodingType ePostEncodingType) {
this.meEncoding = ePostEncodingType;
this.moMulti = null;
this.msCharSet = null;
this.mChunk = null;
this.msContentType = null;
if (this.msHeaderMap != null) {
this.msHeaderMap.clear();
}
if (this.mPairs != null) {
this.mPairs.reset();
}
this.msRespType = null;
this.mnRespCode = 0;
}
public void addNameValuePair(String name, String value) {
assert (this.meEncoding != PostEncodingType.MULTIPART_ENCODING);
if (StringUtils.isEmpty(this.msContentType)) {
this.msContentType = "text/plain";
}
byte[] encodedName = null;
byte[] encodedValue = null;
try {
encodedName = ProtocolUtils.urlEncode(name).getBytes("US-ASCII");
encodedValue = ProtocolUtils.urlEncode(value).getBytes("US-ASCII");
}
catch (UnsupportedEncodingException ignored) {
// empty catch block
}
try {
if (this.mPairs == null) {
this.mPairs = new ByteArrayOutputStream();
} else {
this.mPairs.write(38);
}
this.mPairs.write(encodedName);
this.mPairs.write(61);
this.mPairs.write(encodedValue);
}
catch (IOException ignored) {
// empty catch block
}
}
public void addEncodedData(byte[] encodedData, String sContentType, String sCharSet) {
assert (this.meEncoding != PostEncodingType.MULTIPART_ENCODING);
if (StringUtils.isEmpty(this.msContentType) && !StringUtils.isEmpty(sContentType)) {
this.msContentType = sContentType;
}
if (StringUtils.isEmpty(this.msCharSet) && !StringUtils.isEmpty(sCharSet)) {
this.msCharSet = sCharSet;
}
if (encodedData != null) {
if (this.mChunk == null) {
this.mChunk = new ByteArrayOutputStream();
}
try {
this.mChunk.write(encodedData);
}
catch (IOException ignored) {
// empty catch block
}
}
}
public void addHeaderData(String sKey, String sValue) {
if (this.msHeaderMap == null) {
this.msHeaderMap = new HashMap<String, String>();
}
this.msHeaderMap.put(sKey, sValue);
}
public void addMultipartData(Protocol.SectionDataOption eOption, byte[] value) {
Protocol.MultiPartDesc oDesc = new Protocol.MultiPartDesc(eOption, value);
if (this.moMulti == null) {
this.moMulti = new ArrayList<Protocol.MultiPartDesc>();
}
this.moMulti.add(oDesc);
}
public void post(String sUrl) {
assert (this.meEncoding != PostEncodingType.UNKNOWN_ENCODING);
StringHolder sScheme = new StringHolder();
StringHolder sUser = new StringHolder();
StringHolder sPwd = new StringHolder();
StringHolder sHost = new StringHolder();
StringHolder sPort = new StringHolder();
StringHolder sPath = new StringHolder();
Resolver.crackUrl(sUrl, sScheme, sUser, sPwd, sHost, sPort, sPath);
Protocol oProtocol = Resolver.getProtocol(sScheme.value);
if (oProtocol == null) {
throw new ExFull(new MsgFormat(ResId.STREAM_PROTOCOL_NOT_AVAIL));
}
Protocol.PostRsvp oRsvp = null;
if (this.meEncoding == PostEncodingType.URL_ENCODING) {
Protocol.SimplePostData oData = new Protocol.SimplePostData(this.mPairs.toByteArray());
this.mPairs = null;
oRsvp = oProtocol.post(oData, sUrl);
} else if (this.meEncoding == PostEncodingType.USER_ENCODING && this.mChunk != null && this.mChunk.size() > 0) {
Protocol.SimplePostData oData = new Protocol.SimplePostData(this.mChunk.toByteArray());
StringBuilder sHead = new StringBuilder();
if (StringUtils.isEmpty(this.msContentType)) {
sHead.append("application/octet-stream");
} else {
sHead.append(this.msContentType);
if (!StringUtils.isEmpty(this.msCharSet)) {
sHead.append("; charset=").append(this.msCharSet);
}
}
oData.headerMap.put("Content-Type", sHead.toString());
if (this.msHeaderMap != null && this.msHeaderMap.size() > 0) {
oData.headerMap.putAll(this.msHeaderMap);
}
oRsvp = oProtocol.post(oData, sUrl);
this.mChunk = null;
} else if (this.meEncoding == PostEncodingType.MULTIPART_ENCODING && this.moMulti != null) {
oRsvp = oProtocol.post(this.moMulti, sUrl);
}
if (oRsvp != null) {
this.mnRespCode = oRsvp.nCode;
this.msRespType = oRsvp.sType;
this.mRespData = oRsvp.data;
if (oRsvp.exception != null) {
throw oRsvp.exception;
}
}
}
public byte[] getResponse() {
return this.mRespData;
}
public int getResponseCode() {
return this.mnRespCode;
}
public String getResponseType() {
return this.msRespType;
}
public static enum PostEncodingType {
UNKNOWN_ENCODING,
URL_ENCODING,
USER_ENCODING,
MULTIPART_ENCODING,
LAST_ENCODING;
private PostEncodingType() {
}
}
}