Skip to content

Commit 78f29bc

Browse files
author
zhourenjian@gmail.com
committed
Support direct bytes parsing in Simple RPC/Pipe
Lazy HTTP response text/bytes on being invoked Use char type instead of String for constants
1 parent b9aff1f commit 78f29bc

File tree

7 files changed

+377
-161
lines changed

7 files changed

+377
-161
lines changed

sources/net.sf.j2s.ajax/ajaxcore/net/sf/j2s/ajax/HttpRequest.java

Lines changed: 76 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,13 @@ public static interface IXHRReceiving {
136136
}
137137

138138
public static String DEFAULT_USER_AGENT = "Java2Script/2.0.2";
139-
139+
140140
protected int status;
141141
protected String statusText;
142142
protected int readyState;
143143

144+
protected String responseType;
145+
protected ByteArrayOutputStream responseBAOS;
144146
protected String responseText;
145147
protected byte[] responseBytes;
146148
protected Document responseXML;
@@ -178,6 +180,59 @@ public int getReadyState() {
178180
* or an error happens.
179181
*/
180182
public String getResponseText() {
183+
if (responseText != null) {
184+
return responseText;
185+
}
186+
ByteArrayOutputStream baos = responseBAOS;
187+
String type = responseType;
188+
if (type != null) {
189+
String charset = null;
190+
String lowerType = type.toLowerCase();
191+
int idx = lowerType.indexOf("charset=");
192+
if (idx != -1) {
193+
charset = type.substring(idx + 8);
194+
} else {
195+
idx = lowerType.indexOf("/xml"); // more xml Content-Type?
196+
if (idx != -1) {
197+
String tmp = baos.toString();
198+
Matcher matcher = Pattern.compile(
199+
"<\\?.*encoding\\s*=\\s*[\'\"]([^'\"]*)[\'\"].*\\?>",
200+
Pattern.MULTILINE).matcher(tmp);
201+
if (matcher.find()) {
202+
charset = matcher.group(1);
203+
} else {
204+
// default charset of xml is UTF-8?
205+
responseText = tmp;
206+
}
207+
} else {
208+
idx = lowerType.indexOf("html");
209+
if (idx != -1) {
210+
String tmp = baos.toString();
211+
Matcher matcher = Pattern.compile(
212+
"<meta.*content\\s*=\\s*[\'\"][^'\"]*charset\\s*=\\s*([^'\"]*)\\s*[\'\"].*>",
213+
Pattern.MULTILINE | Pattern.CASE_INSENSITIVE).matcher(tmp);
214+
if (matcher.find()) {
215+
charset = matcher.group(1);
216+
} else {
217+
responseText = tmp;
218+
}
219+
}
220+
}
221+
}
222+
if (charset != null) {
223+
try {
224+
responseText = baos.toString(charset);
225+
} catch (UnsupportedEncodingException e) {
226+
}
227+
}
228+
}
229+
if (responseText == null) {
230+
try {
231+
responseText = baos.toString("iso-8859-1");
232+
} catch (UnsupportedEncodingException e) {
233+
responseText = baos.toString();
234+
}
235+
}
181236
return responseText;
182237
}
183238
/**
@@ -186,6 +241,9 @@ public String getResponseText() {
186241
* or an error happens.
187242
*/
188243
public byte[] getResponseBytes() {
244+
if (responseBytes == null && responseBAOS != null) {
245+
responseBytes = responseBAOS.toByteArray();
246+
}
189247
return responseBytes;
190248
}
191249
/**
@@ -197,15 +255,16 @@ public Document getResponseXML() {
197255
if (responseXML != null) {
198256
return responseXML;
199257
}
200-
String type = connection.getHeaderField("Content-Type");
258+
String type = responseType;
201259
if (type != null && (type.indexOf("/xml") != -1 || type.indexOf("+xml") != -1)) {
202-
if (responseText != null && responseText.length() != 0) {
260+
String responseContent = getResponseText();
261+
if (responseContent != null && responseContent.length() != 0) {
203262
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
204263
dbf.setNamespaceAware(true);
205264
dbf.setAttribute("http://xml.org/sax/features/namespaces", Boolean.TRUE);
206265
try {
207266
DocumentBuilder db = dbf.newDocumentBuilder();
208-
ByteArrayInputStream biStream = new ByteArrayInputStream(responseText.getBytes("utf-8"));
267+
ByteArrayInputStream biStream = new ByteArrayInputStream(responseContent.getBytes("utf-8"));
209268
responseXML = db.parse(biStream);
210269
} catch (Exception e) {
211270
e.printStackTrace();
@@ -361,11 +420,13 @@ public void open(String method, String url, boolean async, String user, String p
361420
this.url = url;
362421
this.user = user;
363422
this.password = password;
423+
responseType = null;
424+
responseBAOS = null;
364425
responseText = null;
365426
responseBytes = null;
366427
responseXML = null;
367428
readyState = 1;
368-
status = 200; // default OK
429+
status = 0; // default OK
369430
statusText = null;
370431
toAbort = false;
371432
if (onreadystatechange != null) {
@@ -504,8 +565,15 @@ private void request() {
504565

505566
receiving = initializeReceivingMonitor();
506567

507-
ByteArrayOutputStream baos = new ByteArrayOutputStream(10240);
508-
byte[] buffer = new byte[10240];
568+
int bufferSize = connection.getContentLength();
569+
if (bufferSize <= 0) {
570+
bufferSize = 10240;
571+
} else if (bufferSize > 512 * 1024) {
572+
bufferSize = 512 * 1024; // buffer increases by 512k
573+
}
574+
ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize);
575+
responseBAOS = baos;
576+
byte[] buffer = new byte[Math.min(bufferSize, 10240)];
509577
int read;
510578
while (!toAbort && (read = is.read(buffer)) != -1) {
511579
if (checkAbort()) return; // stop receiving anything
@@ -527,56 +595,7 @@ private void request() {
527595
is.close();
528596
activeIS = null;
529597
responseText = null;
530-
responseBytes = baos.toByteArray();
531-
String type = connection.getHeaderField("Content-Type");
532-
if (type != null) {
533-
String charset = null;
534-
String lowerType = type.toLowerCase();
535-
int idx = lowerType.indexOf("charset=");
536-
if (idx != -1) {
537-
charset = type.substring(idx + 8);
538-
} else {
539-
idx = lowerType.indexOf("/xml"); // more xml Content-Type?
540-
if (idx != -1) {
541-
String tmp = baos.toString();
542-
Matcher matcher = Pattern.compile(
543-
"<\\?.*encoding\\s*=\\s*[\'\"]([^'\"]*)[\'\"].*\\?>",
544-
Pattern.MULTILINE).matcher(tmp);
545-
if (matcher.find()) {
546-
charset = matcher.group(1);
547-
} else {
548-
// default charset of xml is UTF-8?
549-
responseText = tmp;
550-
}
551-
} else {
552-
idx = lowerType.indexOf("html");
553-
if (idx != -1) {
554-
String tmp = baos.toString();
555-
Matcher matcher = Pattern.compile(
556-
"<meta.*content\\s*=\\s*[\'\"][^'\"]*charset\\s*=\\s*([^'\"]*)\\s*[\'\"].*>",
557-
Pattern.MULTILINE | Pattern.CASE_INSENSITIVE).matcher(tmp);
558-
if (matcher.find()) {
559-
charset = matcher.group(1);
560-
} else {
561-
responseText = tmp;
562-
}
563-
}
564-
}
565-
}
566-
if (charset != null) {
567-
try {
568-
responseText = baos.toString(charset);
569-
} catch (UnsupportedEncodingException e) {
570-
}
571-
}
572-
}
573-
if (responseText == null) {
574-
try {
575-
responseText = baos.toString("iso-8859-1");
576-
} catch (UnsupportedEncodingException e) {
577-
responseText = baos.toString();
578-
}
579-
}
598+
responseType = connection.getHeaderField("Content-Type");
580599
readyState = 4;
581600
if (onreadystatechange != null) {
582601
onreadystatechange.onLoaded();

sources/net.sf.j2s.ajax/ajaxcore/net/sf/j2s/ajax/SimpleThreadHelper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,12 @@ public static void initializePool() {
4141
if (poolInitialized) {
4242
return;
4343
}
44-
poolInitialized = true;
45-
4644
poolExecutor = new ThreadPoolExecutor(SimpleThreadConfig.simpleCoreThreads,
4745
SimpleThreadConfig.simpleMaxThreads <= 0 ? Integer.MAX_VALUE : SimpleThreadConfig.simpleMaxThreads,
4846
SimpleThreadConfig.simpleThreadIdleSeconds, TimeUnit.SECONDS,
4947
new SynchronousQueue<Runnable>(),
5048
new NamedThreadFactory("Simple Worker"));
49+
poolInitialized = true;
5150
}
5251

5352
public static void runTask(Runnable r, String name) {

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeHttpServlet.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,17 @@ public void init() throws ServletException {
107107
@Override
108108
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
109109
throws ServletException, IOException {
110-
String key = req.getParameter(SimplePipeRequest.FORM_PIPE_KEY);
110+
String key = req.getParameter(String.valueOf(SimplePipeRequest.FORM_PIPE_KEY));
111111
if (key == null) {
112112
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
113113
return;
114114
}
115115
char type = SimplePipeRequest.PIPE_TYPE_CONTINUUM;
116-
String typeStr = req.getParameter(SimplePipeRequest.FORM_PIPE_TYPE);
116+
String typeStr = req.getParameter(String.valueOf(SimplePipeRequest.FORM_PIPE_TYPE));
117117
if (typeStr != null && typeStr.length() > 0) {
118118
type = typeStr.charAt(0);
119119
}
120-
String domain = req.getParameter(SimplePipeRequest.FORM_PIPE_DOMAIN);
120+
String domain = req.getParameter(String.valueOf(SimplePipeRequest.FORM_PIPE_DOMAIN));
121121
doPipe(resp, key, type, domain);
122122
}
123123

@@ -345,6 +345,24 @@ protected void doPipe(final HttpServletResponse resp, String key, char type, Str
345345
}
346346
}
347347

348+
protected static String output(char type, String key, char evt) {
349+
StringBuilder builder = new StringBuilder();
350+
if (SimplePipeRequest.PIPE_TYPE_SCRIPT == type) {
351+
// iframe, so $ is a safe method identifier
352+
builder.append("<script type=\"text/javascript\">$ (\"");
353+
} else if (SimplePipeRequest.PIPE_TYPE_XSS == type) {
354+
builder.append("$p1p3p$ (\""); // $p1p3p$
355+
}
356+
builder.append(key);
357+
builder.append(evt);
358+
if (SimplePipeRequest.PIPE_TYPE_SCRIPT == type) { // iframe
359+
builder.append("\");</script>\r\n");
360+
} else if (SimplePipeRequest.PIPE_TYPE_XSS == type) {
361+
builder.append("\");\r\n");
362+
}
363+
return builder.toString();
364+
}
365+
348366
protected static String output(char type, String key, String str) {
349367
StringBuilder builder = new StringBuilder();
350368
if (SimplePipeRequest.PIPE_TYPE_SCRIPT == type) {

0 commit comments

Comments
 (0)