-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathWebRequest.java
More file actions
executable file
·199 lines (180 loc) · 5.34 KB
/
Copy pathWebRequest.java
File metadata and controls
executable file
·199 lines (180 loc) · 5.34 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
package javaforce.service;
import java.io.*;
import java.util.*;
import javaforce.*;
/** WebRequest
*
* Created : Aug 23, 2013
*/
public class WebRequest {
//public fields
public String request;
public String[] fields;
public String[] fields0;
public InputStream is;
public String[] cookies; //name=value;...
public static HashMap<String, Session> sessions = new HashMap<String, Session>();
public Session session;
public String serverIP, remoteIP;
public int serverPort, remotePort;
public String method;
public HTTP.Parameters params;
public WebFile[] files;
public boolean secure;
public static class Session {
public String id;
public HashMap<String, Object> props = new HashMap<String, Object>();
public void setAttribute(String key, Object value) {props.put(key, value);}
public Object getAttribute(String key) {return props.get(key);}
}
public Session getSession() {return session;}
public String getQueryString() {
int idx = fields0[1].indexOf("?");
if (idx == -1) return "";
return fields0[1].substring(idx+1);
}
public String getURL() {
int idx = fields0[1].indexOf("?");
if (idx == -1) return fields0[1];
return fields0[1].substring(0, idx);
}
public String getCookie(String name) {
name += "=";
for(int a=0;a<cookies.length;a++) {
if (cookies[a].startsWith(name)) {
return cookies[a].substring(name.length());
}
}
return null;
}
public String getHeader(String name) {
name += ":";
for(int a=0;a<fields.length;a++) {
if (fields[a].startsWith(name)) {
return fields[a].substring(name.length()).trim();
}
}
return null;
}
public int getContentLength() {
String length = getHeader("Content-Length");
if (length == null) return -1;
return Integer.valueOf(length.trim());
}
public String getContentType() {
return getHeader("Content-Type");
}
private String randomID() {
Random r = new Random();
return "" + r.nextLong() + "-" + r.nextLong() + "-" + System.currentTimeMillis();
}
void init(WebResponse res) {
ArrayList<String> list = new ArrayList<String>();
for(int a=1;a<fields.length;a++) {
if (fields[a].startsWith("Cookie: ")) {
String[] sets = fields[a].substring(8).split(";");
for(int b=0;b<sets.length;b++) {
list.add(sets[b].trim());
}
}
}
cookies = list.toArray(JF.StringArrayType);
String id = getCookie("jsession-id");
String orgid = id;
if (id == null) {
id = randomID();
res.addCookie("jsession-id", id);
session = new Session();
session.id = id;
sessions.put(id, session);
} else {
session = sessions.get(id);
if (session == null) {
session = new Session();
session.id = id;
sessions.put(id, session);
}
}
params = new HTTP.Parameters();
String query = getQueryString();
setParameters(query);
String type = getContentType();
if (type != null && type.equals("application/x-www-form-urlencoded")) {
try {
int length = getContentLength();
byte[] data = JF.readAll(is, length);
setParameters(new String(data, "UTF-8"));
} catch (Exception e) {
JFLog.log(e);
}
}
}
private void setParameters(String str) {
String[] ps = str.split("[&]");
for(String p : ps) {
int idx = p.indexOf('=');
if (idx == -1) continue;
String key = p.substring(0, idx);
String value = p.substring(idx + 1);
params.put(key, JF.decodeURL(value));
}
}
public String getHost() {
String host = getHeader("Host");
if (host == null) return "";
int idx = host.indexOf(":");
if (idx == -1) return host;
return host.substring(0, idx);
}
public String getLocalAddr() { return serverIP; }
public int getLocalPort() { return serverPort; }
public String getRemoteAddr() { return remoteIP; }
public int getRemotePort() { return remotePort; }
public String getMethod() { return method; }
public InputStream getInputStream() { return is; }
public String getParameter(String name) {
return params.get(name);
}
public WebFile getPart(String name) {
if (files == null) return null;
for(WebFile file : files) {
if (file.name.equalsIgnoreCase(name)) {
return file;
}
}
return null;
}
/** Reads and returns POST data. */
public byte[] getData() {
String method = getMethod();
if (!method.equals("POST")) return null;
try {
int length = getContentLength();
byte[] data = JF.readAll(is, length);
return data;
} catch (Exception e) {
JFLog.log(e);
return null;
}
}
public boolean isSecure() {
return secure;
}
public HashMap<String, Object> toHashMap() {
HashMap<String, Object> map = new HashMap<>();
map.put("params", params.getHashMap());
map.put("LocalPort", getLocalPort());
map.put("LocalAddr", getLocalAddr());
map.put("isSecure", isSecure());
map.put("RemotePort", getRemotePort());
map.put("RemoteAddr", getRemoteAddr());
map.put("ContentType", getContentType());
map.put("ContentLength", getContentLength());
map.put("InputStream", getInputStream());
map.put("URL", getURL());
map.put("Method", getMethod());
map.put("session", session.props);
map.put("files", files);
return map;
}
};