Skip to content

Commit 1bcfe8d

Browse files
author
zhourenjian
committed
Adding HTML5 Local Storage support for Simple Store, try window["j2s.html5.store"]=true; to enable it
Fixing bug of Simple RPC/Pipe
1 parent df20531 commit 1bcfe8d

6 files changed

Lines changed: 132 additions & 35 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ public void onLoaded() {
523523
}
524524
doc.write (html);
525525
doc.close ();
526+
// To avoid blank title in title bar
527+
document.title = document.title;
526528
} catch (e) {
527529
window.setTimeout (net.sf.j2s.ajax.SimplePipeRequest.generateLazyIframeWriting (handle, domain, html), 25);
528530
}
@@ -547,7 +549,7 @@ static void pipeScript(SimplePipeRunnable runnable) { // xss
547549
* domain = document.domain;
548550
* } catch (e) {
549551
* }
550-
* ok4IFrameScript = domain != null && domain.length > 0;
552+
* ok4IFrameScript = (domain != null && domain.length > 0) || navigator.userAgent.toLowerCase ().indexOf ("msie") == -1;
551553
*/ {}
552554
if (ok4IFrameScript) {
553555
// in xss mode, iframe is used to avoid blocking other *.js loading

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,9 @@ protected static String adjustRequestURL(String method, String url, String seria
209209
locHost = locHost.substring (0, idx4);
210210
}
211211
if (arguments.length == 2) { // check subdomain
212-
return host.indexOf ("." + locHost) != -1 && locPort == port
213-
&& loc.protocol == protocol && loc.protocol != "file:";
212+
var idx5 = host.indexOf ("." + locHost);
213+
return idx5 != -1 && idx5 == host.length - locHost.length - 1
214+
&& locPort == port && loc.protocol == protocol && loc.protocol != "file:";
214215
}
215216
return (locHost != host || locPort != port
216217
|| loc.protocol != protocol || loc.protocol == "file:");

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class SimpleSerializable implements Cloneable {
5656
}
5757
buffer[1] = clazzName;
5858
buffer[2] = '#';
59-
buffer[3] = "00000000$"
59+
buffer[3] = "00000000$";
6060
var headSize = buffer.join ('').length;
6161
6262
var fields = oClass.declared$Fields;
@@ -621,6 +621,11 @@ private void serializeString(StringBuffer buffer, String s) throws UnsupportedEn
621621
}
622622
}
623623
}
624+
var count = 0;
625+
while (!fieldMap[fieldName] && count++ <= 3) {
626+
fieldName = "$" + fieldName;
627+
}
628+
624629
index += l1;
625630
var c2 = str.charAt (index++);
626631
if (c2 == 'A') {
@@ -813,6 +818,12 @@ public boolean deserialize(final String str, int start) {
813818
}
814819
}
815820
}
821+
while (fieldName.startsWith("$")) {
822+
if (fieldMap.containsKey(fieldName)) {
823+
break;
824+
}
825+
fieldName = fieldName.substring(1);
826+
}
816827
index += l1;
817828
char c2 = str.charAt(index++);
818829
if (c2 == 'A') {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package net.sf.j2s.store;
2+
3+
4+
/**
5+
*
6+
* @author Zhou Renjian (http://zhourenjian.com)
7+
*
8+
* July 10, 2010
9+
*
10+
*/
11+
public class HTML5LocalStorage implements IStore {
12+
13+
/**
14+
* @j2sNative
15+
* return localStorage.getItem (name);
16+
*/
17+
public String getProperty(String name) {
18+
return null;
19+
}
20+
21+
public boolean isReady() {
22+
return true;
23+
}
24+
25+
/**
26+
* @j2sNative
27+
* localStorage.setItem (name, value);
28+
*/
29+
public void setProperty(String name, String value) {
30+
}
31+
32+
}

sources/net.sf.j2s.ajax/store/net/sf/j2s/store/SimpleStore.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,31 @@ public class SimpleStore implements IStore {
1414
private SimpleStore() {
1515
/**
1616
* @j2sNative
17-
* var ua = navigator.userAgent.toLowerCase ();
18-
* var isOldIE = ua.indexOf ("msie 5.5") != -1 || ua.indexOf ("msie 5.0") != -1;
19-
* var cookieURL = window["j2s.xss.cookie.url"];
20-
* var isLocal = false;
21-
* try {
22-
* isLocal = window.location.protocol == "file:"
23-
* || window.location.host.toLowerCase ().indexOf ("localhost") != -1;
24-
* } catch (e) {
25-
* isLocal = true;
26-
* }
27-
* if (!isLocal && cookieURL != null && !isOldIE) {
28-
* this.store = new net.sf.j2s.store.XSSCookieStore(cookieURL);
29-
* } else {
30-
* this.store = new net.sf.j2s.store.CookieStore();
31-
* }
17+
var ua = navigator.userAgent.toLowerCase ();
18+
var isLocalFile = false;
19+
try {
20+
isLocalFile = window.location.protocol == "file:";
21+
} catch (e) {
22+
isLocalFile = true;
23+
}
24+
if (window["j2s.html5.store"] && window["localStorage"] != null && (ua.indexOf ("gecko/") == -1 || !isLocalFile)) {
25+
this.store = new net.sf.j2s.store.HTML5LocalStorage ();
26+
return;
27+
}
28+
var isLocal = false;
29+
try {
30+
isLocal = window.location.protocol == "file:"
31+
|| window.location.host.toLowerCase () == "localhost";
32+
} catch (e) {
33+
isLocal = true;
34+
}
35+
var isOldIE = ua.indexOf ("msie 5.5") != -1 || ua.indexOf ("msie 5.0") != -1;
36+
var cookieURL = window["j2s.xss.cookie.url"];
37+
if (!isLocal && cookieURL != null && !isOldIE) {
38+
this.store = new net.sf.j2s.store.XSSCookieStore(cookieURL);
39+
} else {
40+
this.store = new net.sf.j2s.store.CookieStore();
41+
}
3242
*/ {
3343
File storeFile = new File(System.getProperty("user.home"), ".java2script.store");
3444
this.store = new INIFileStore(storeFile.getAbsolutePath());
@@ -58,12 +68,24 @@ public void execute(Runnable runnable) {
5868
if (store instanceof XSSCookieStore && !store.isReady()) {
5969
/**
6070
* @j2sNative
61-
window.xssCookieReadyCallback = (function (r) {
71+
window.xssCookieReadyCallback = (function (r1, r2) {
6272
return function () {
6373
net.sf.j2s.store.XSSCookieStore.initialized = true;
64-
r.run ();
74+
if (r1 != null) {
75+
try {
76+
r1.run ();
77+
} catch (e) {
78+
}
79+
}
80+
r2.run ();
6581
};
66-
}) (runnable);
82+
}) (window.xssCookieReadyCallback, runnable);
83+
window.setTimeout (function () {
84+
if (!net.sf.j2s.store.XSSCookieStore.initialized
85+
&& window.xssCookieReadyCallback != null) {
86+
window.xssCookieReadyCallback ();
87+
}
88+
}, 10000);
6789
*/ {}
6890
return;
6991
}

sources/net.sf.j2s.ajax/store/net/sf/j2s/store/XSSCookieStore.java

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,48 @@
77
* Mar 28, 2009
88
*
99
* @j2sSuffix
10-
* var ua = navigator.userAgent.toLowerCase ();
11-
* var isOldIE = ua.indexOf ("msie 5.5") != -1 || ua.indexOf ("msie 5.0") != -1;
12-
* var xssCookieURL = window["j2s.xss.cookie.url"];
13-
* var isLocal = false;
14-
* try {
15-
* isLocal = window.location.protocol == "file:"
16-
* || window.location.host.toLowerCase ().indexOf ("localhost") != -1;
17-
* } catch (e) {
18-
* isLocal = true;
19-
* }
20-
* if (!isLocal && xssCookieURL != null && !isOldIE) {
21-
* net.sf.j2s.store.XSSCookieStore.initialize(xssCookieURL);
22-
* }
10+
var ua = navigator.userAgent.toLowerCase ();
11+
var isLocalFile = false;
12+
try {
13+
isLocalFile = window.location.protocol == "file:";
14+
} catch (e) {
15+
isLocalFile = true;
16+
}
17+
if (!window["j2s.html5.store"] || window["localStorage"] == null || (ua.indexOf ("gecko/") != -1 && isLocalFile)) {
18+
var isLocal = false;
19+
try {
20+
isLocal = window.location.protocol == "file:"
21+
|| window.location.host.toLowerCase () == "localhost";
22+
} catch (e) {
23+
isLocal = true;
24+
}
25+
var isOldIE = ua.indexOf ("msie 5.5") != -1 || ua.indexOf ("msie 5.0") != -1;
26+
var xssCookieURL = window["j2s.xss.cookie.url"];
27+
if (xssCookieURL == null) {
28+
var host = null;
29+
try {
30+
host = window.location.host;
31+
} catch (e) {
32+
}
33+
if (host != null && host.indexOf ("www.") == 0) {
34+
host = host.substring (4).toLowerCase ();
35+
}
36+
37+
var knownXSSCookieSites = window["j2s.known.xss.domains"];
38+
if (knownXSSCookieSites != null) {
39+
for (var i = 0; i < knownXSSCookieSites.length; i++) {
40+
if (host == knownXSSCookieSites[i]) {
41+
xssCookieURL = "http://c." + host + "/xss-cookie.html";
42+
window["j2s.xss.cookie.url"] = xssCookieURL;
43+
break;
44+
}
45+
}
46+
}
47+
}
48+
if (!isLocal && xssCookieURL != null && !isOldIE) {
49+
net.sf.j2s.store.XSSCookieStore.initialize(xssCookieURL);
50+
}
51+
}
2352
*/
2453
class XSSCookieStore implements IStore {
2554

0 commit comments

Comments
 (0)