Skip to content

Commit 711b3a7

Browse files
author
zhourenjian@gmail.com
committed
Support configure user agent and keep alive headers in Simple RPC
1 parent 6e628d1 commit 711b3a7

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,16 @@ private static void pipeRequest(final SimplePipeRunnable runnable) {
428428
serialize = null;
429429
}
430430
final HttpRequest request = getRequest();
431+
if (!runnable.supportsKeepAlive()) {
432+
request.setRequestHeader("Connection", "close");
433+
}
434+
if (runnable instanceof ISimpleRequestInfo) {
435+
ISimpleRequestInfo reqInfo = (ISimpleRequestInfo) runnable;
436+
String ua = reqInfo.getRemoteUserAgent();
437+
if (ua != null) {
438+
request.setRequestHeader("User-Agent", ua);
439+
}
440+
}
431441
request.open(method, url, true);
432442
request.registerOnReadyStateChange(new XHRCallbackAdapter() {
433443
public void onLoaded() {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ private static void ajaxRequest(final SimpleRPCRunnable runnable) {
141141
}
142142

143143
final HttpRequest request = getRequest();
144+
if (!runnable.supportsKeepAlive()) {
145+
request.setRequestHeader("Connection", "close");
146+
}
147+
if (runnable instanceof ISimpleRequestInfo) {
148+
ISimpleRequestInfo reqInfo = (ISimpleRequestInfo) runnable;
149+
String ua = reqInfo.getRemoteUserAgent();
150+
if (ua != null) {
151+
request.setRequestHeader("User-Agent", ua);
152+
}
153+
}
144154
request.open(method, url, true);
145155
request.registerOnReadyStateChange(new XHRCallbackAdapter() {
146156
public void onLoaded() {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,15 @@ public boolean supportsGZipEncoding() {
7575
return true;
7676
}
7777

78+
/**
79+
* Whether supporting HTTP Keep-Alive feature or not.
80+
*
81+
* This method will be called by server side.
82+
*
83+
* @return supported
84+
*/
85+
public boolean supportsKeepAlive() {
86+
return true;
87+
}
88+
7889
}

sources/net.sf.j2s.ajax/ajaxswt/net/sf/j2s/ajax/SimplePipeSWTRequest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ private static void swtPipeRequest(final SimplePipeRunnable runnable) {
174174
serialize = null;
175175
}
176176
final HttpRequest request = getRequest();
177+
if (!runnable.supportsKeepAlive()) {
178+
request.setRequestHeader("Connection", "close");
179+
}
180+
if (runnable instanceof ISimpleRequestInfo) {
181+
ISimpleRequestInfo reqInfo = (ISimpleRequestInfo) runnable;
182+
String ua = reqInfo.getRemoteUserAgent();
183+
if (ua != null) {
184+
request.setRequestHeader("User-Agent", ua);
185+
}
186+
}
177187
request.open(method, url, true);
178188
request.registerOnReadyStateChange(new XHRCallbackSWTAdapter() {
179189
public void swtOnLoaded() {

sources/net.sf.j2s.ajax/ajaxswt/net/sf/j2s/ajax/SimpleRPCSWTRequest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ private static void swtAJAXRequest(final SimpleRPCRunnable runnable) {
7272
serialize = null;
7373
}
7474
final HttpRequest request = getRequest();
75+
if (!runnable.supportsKeepAlive()) {
76+
request.setRequestHeader("Connection", "close");
77+
}
78+
if (runnable instanceof ISimpleRequestInfo) {
79+
ISimpleRequestInfo reqInfo = (ISimpleRequestInfo) runnable;
80+
String ua = reqInfo.getRemoteUserAgent();
81+
if (ua != null) {
82+
request.setRequestHeader("User-Agent", ua);
83+
}
84+
}
7585
request.open(method, url, true);
7686
request.registerOnReadyStateChange(new XHRCallbackSWTAdapter() {
7787
public void swtOnLoaded() {

0 commit comments

Comments
 (0)