Skip to content

Commit 2b551a1

Browse files
author
zhourenjian
committed
Bug-fix for dealing https protocol and others
1 parent b0514cf commit 2b551a1

File tree

2 files changed

+56
-27
lines changed

2 files changed

+56
-27
lines changed

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleRPCHttpServlet.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -196,31 +196,35 @@ protected boolean validateRunnable(String clazzName) {
196196
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
197197
throws ServletException, IOException {
198198
String request = null;
199+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
200+
byte[] buf = new byte[1024];
201+
int read = 0;
199202
InputStream res = req.getInputStream();
200-
try {
201-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
202-
byte[] buf = new byte[1024];
203-
int read = 0;
204-
while ((read = res.read(buf)) != -1) {
205-
baos.write(buf, 0, read);
206-
if (baos.size() > maxPostLimit()) {
207-
/*
208-
* Some malicious request may try to allocate huge size of memory!
209-
* DoS attack? Limit the data size of HTTP request!
210-
*/
211-
res.close();
212-
resp.sendError(HttpServletResponse.SC_FORBIDDEN,
213-
"Data size reaches the limit of Java2Script Simple RPC!");
214-
return;
215-
}
203+
while (true) {
204+
try {
205+
read = res.read(buf);
206+
} catch (IOException e) {
207+
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
208+
res.close();
209+
return;
210+
}
211+
if (read == -1) {
212+
break;
213+
}
214+
baos.write(buf, 0, read);
215+
if (baos.size() > maxPostLimit()) {
216+
/*
217+
* Some malicious request may try to allocate huge size of memory!
218+
* DoS attack? Limit the data size of HTTP request!
219+
*/
220+
resp.sendError(HttpServletResponse.SC_FORBIDDEN,
221+
"Data size reaches the limit of Java2Script Simple RPC!");
222+
res.close();
223+
return;
216224
}
217-
res.close();
218-
request = baos.toString();
219-
} catch (IOException e) {
220-
e.printStackTrace();
221-
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
222-
return;
223225
}
226+
request = baos.toString();
227+
res.close();
224228

225229
SimpleRPCRunnable runnable = getRunnableByRequest(request);
226230
if (runnable == null) {

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleRPCRequest.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,38 @@ protected static String adjustRequestURL(String method, String url, String seria
139139
if (url != null && (url.indexOf ("http://") == 0
140140
|| url.indexOf ("https://") == 0)) {
141141
var host = null;
142-
var idx = url.indexOf ('/', 9);
143-
if (idx != -1) {
144-
host = url.substring (url.indexOf ("//") + 2, idx);
142+
var idx1 = url.indexOf ("//") + 2;
143+
var idx2 = url.indexOf ('/', 9);
144+
if (idx2 != -1) {
145+
host = url.substring (idx1, idx2);
145146
} else {
146-
host = url.substring (url.indexOf ("//") + 2);
147+
host = url.substring (idx1);
147148
}
148-
return (window.location.host != host || window.location.protocol == "file:");
149+
var protocol = null; // http: or https:
150+
var idx0 = url.indexOf ("://");
151+
if (idx0 != -1) {
152+
protocol = url.substring (0, idx0 + 1);
153+
} else {
154+
protocol = window.location.protocol;
155+
}
156+
var port = null;
157+
var idx3 = host.indexOf (':'); // there is port number
158+
if (idx3 != -1) {
159+
port = parseInt (host.substring (idx3 + 1));
160+
host = host.substring (0, idx3);
161+
} else {
162+
if ("http:" == protocol) {
163+
port = 80;
164+
} else if ("https:" == protocol) {
165+
port = 443;
166+
} else {
167+
port = window.location.port;
168+
}
169+
}
170+
var loc = window.location;
171+
return (loc.host != host || loc.protocol != protocol
172+
|| loc.port != port
173+
|| loc.protocol == "file:");
149174
}
150175
return false; // ftp ...
151176
*/

0 commit comments

Comments
 (0)