Skip to content

Commit 73e76f6

Browse files
author
Sefa Ilkimen
committed
feature silkimen#11: implement support for "browser" platform
1 parent 3fcac64 commit 73e76f6

File tree

7 files changed

+210
-3
lines changed

7 files changed

+210
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 1.11.0
44

55
- Feature #77: allow overriding global settings for each single request
6+
- Feature #11: add support for "browser" platform
67

78
## 1.10.2
89

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.10.2",
44
"description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning",
55
"scripts": {
6+
"buildbrowser": "./scripts/build-test-app.sh --browser",
67
"testandroid": "./scripts/build-test-app.sh --android --emulator && ./scripts/test-app.sh --android --emulator",
78
"testios": "./scripts/build-test-app.sh --ios --emulator && ./scripts/test-app.sh --ios --emulator",
89
"testapp": "npm run testandroid && npm run testios",

plugin.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,15 @@
7070
<source-file src="src/android/com/synconset/cordovahttp/CordovaHttpUpload.java" target-dir="src/com/synconset/cordovahttp"/>
7171
<framework src="com.squareup.okhttp3:okhttp-urlconnection:3.10.0"/>
7272
</platform>
73+
<platform name="browser">
74+
<config-file target="config.xml" parent="/*">
75+
<feature name="CordovaHttpPlugin">
76+
<param name="browser-package" value="CordovaHttpPlugin" />
77+
</feature>
78+
</config-file>
79+
80+
<js-module src="src/browser/cordova-http-plugin.js" name="http-proxy">
81+
<runs />
82+
</js-module>
83+
</platform>
7384
</plugin>

scripts/build-test-app.sh

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
#!/usr/bin/env bash
22
set -e
33

4-
PLATFORM=$([[ "${@#--android}" = "$@" ]] && echo "ios" || echo "android")
5-
TARGET=$([[ "${@#--device}" = "$@" ]] && echo "emulator" || echo "device")
64
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/..
75
CDV=$ROOT/node_modules/.bin/cordova
86

7+
PLATFORM=ios
8+
TARGET=emulator
9+
10+
while :; do
11+
case $1 in
12+
--android)
13+
PLATFORM=android
14+
;;
15+
--browser)
16+
PLATFORM=browser
17+
;;
18+
--ios)
19+
PLATFORM=ios
20+
;;
21+
--device)
22+
TARGET=device
23+
;;
24+
--emulator)
25+
TARGET=emulator
26+
;;
27+
-?*)
28+
printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
29+
;;
30+
*)
31+
break
32+
esac
33+
34+
shift
35+
done
36+
937
rm -rf $ROOT/temp
1038
mkdir $ROOT/temp
1139
cp -r $ROOT/test/app-template/ $ROOT/temp/

src/browser/cordova-http-plugin.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
var pluginId = module.id.slice(0, module.id.lastIndexOf('.'));
2+
3+
var cordovaProxy = require('cordova/exec/proxy');
4+
var helpers = require(pluginId + '.helpers');
5+
6+
function serializeJsonData(data) {
7+
try {
8+
return JSON.stringify(data);
9+
} catch (err) {
10+
return null;
11+
}
12+
}
13+
14+
function serializePrimitive(key, value) {
15+
if (value === null || value === undefined) {
16+
return encodeURIComponent(key) + '=';
17+
}
18+
19+
return encodeURIComponent(key) + '=' + encodeURIComponent(value);
20+
}
21+
22+
function serializeArray(key, values) {
23+
return values.map(function(value) {
24+
return encodeURIComponent(key) + '[]=' + encodeURIComponent(value);
25+
}).join('&');
26+
}
27+
28+
function serializeParams(params) {
29+
if (params === null) return '';
30+
31+
return Object.keys(params).map(function(key) {
32+
if (helpers.getTypeOf(params[key]) === 'Array') {
33+
return serializeArray(key, params[key]);
34+
}
35+
36+
return serializePrimitive(key, params[key]);
37+
}).join('&');
38+
}
39+
40+
function createXhrSuccessObject(xhr) {
41+
return {
42+
url: xhr.responseURL,
43+
status: xhr.status,
44+
data: helpers.getTypeOf(xhr.responseText) === 'String' ? xhr.responseText : xhr.response,
45+
headers: xhr.getAllResponseHeaders()
46+
};
47+
}
48+
49+
function createXhrFailureObject(xhr) {
50+
var obj = {};
51+
52+
obj.headers = xhr.getAllResponseHeaders();
53+
obj.error = helpers.getTypeOf(xhr.responseText) === 'String' ? xhr.responseText : xhr.response;
54+
obj.error = obj.error || 'advanced-http: please check browser console for error messages';
55+
56+
if (xhr.responseURL) obj.url = xhr.responseURL;
57+
if (xhr.status) obj.status = xhr.status;
58+
59+
return obj;
60+
}
61+
62+
function setHeaders(xhr, headers) {
63+
Object.keys(headers).forEach(function(key) {
64+
if (key === 'Cookie') return;
65+
66+
xhr.setRequestHeader(key, headers[key]);
67+
});
68+
}
69+
70+
function sendRequest(method, withData, opts, success, failure) {
71+
var data = withData ? opts[1] : null;
72+
var params = withData ? null : serializeParams(opts[1]);
73+
var serializer = withData ? opts[2] : null;
74+
var headers = withData ? opts[3] : opts[2];
75+
var timeout = withData ? opts[4] : opts[3];
76+
var url = params ? opts[0] + '?' + params : opts[0];
77+
78+
var processedData = null;
79+
var xhr = new XMLHttpRequest();
80+
81+
xhr.open(method, url);
82+
83+
if (headers.Cookie && headers.Cookie.length > 0) {
84+
return failure('advanced-http: custom cookies not supported on browser platform');
85+
}
86+
87+
switch (serializer) {
88+
case 'json':
89+
processedData = serializeJsonData(data);
90+
91+
if (processedData === null) {
92+
return failure('advanced-http: failed serializing data');
93+
}
94+
95+
break;
96+
97+
case 'utf8':
98+
xhr.setRequestHeader('Content-type', 'application/json; charset=utf8');
99+
processedData = data.text;
100+
break;
101+
102+
case 'urlencoded':
103+
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
104+
processedData = serializeParams(data);
105+
break;
106+
}
107+
108+
xhr.timeout = timeout * 1000;
109+
setHeaders(xhr, headers);
110+
111+
xhr.onerror = xhr.ontimeout = function () {
112+
return failure(createXhrFailureObject(xhr));
113+
};
114+
115+
xhr.onload = function () {
116+
if (xhr.readyState !== xhr.DONE) return;
117+
118+
if (xhr.status < 200 || xhr.status > 299) {
119+
return failure(createXhrFailureObject(xhr));
120+
}
121+
122+
return success(createXhrSuccessObject(xhr));
123+
};
124+
125+
xhr.send(processedData);
126+
}
127+
128+
var browserInterface = {
129+
post: function (success, failure, opts) {
130+
return sendRequest('post', true, opts, success, failure);
131+
},
132+
get: function (success, failure, opts) {
133+
return sendRequest('get', false, opts, success, failure);
134+
},
135+
put: function (success, failure, opts) {
136+
return sendRequest('put', true, opts, success, failure);
137+
},
138+
patch: function (success, failure, opts) {
139+
return sendRequest('patch', true, opts, success, failure);
140+
},
141+
delete: function (success, failure, opts) {
142+
return sendRequest('delete', false, opts, success, failure);
143+
},
144+
head: function (success, failure, opts) {
145+
return sendRequest('head', false, opts, success, failure);
146+
},
147+
uploadFile: function (success, failure, opts) {
148+
return failure('advanced-http: function "uploadFile" not supported on browser platform');
149+
},
150+
downloadFile: function (success, failure, opts) {
151+
return failure('advanced-http: function "downloadFile" not supported on browser platform');
152+
},
153+
enableSSLPinning: function (success, failure, opts) {
154+
return failure('advanced-http: function "enableSSLPinning" not supported on browser platform');
155+
},
156+
acceptAllCerts: function (success, failure, opts) {
157+
return failure('advanced-http: function "acceptAllCerts" not supported on browser platform');
158+
},
159+
disableRedirect: function (success, failure, opts) {
160+
return failure('advanced-http: function "disableRedirect" not supported on browser platform');
161+
}
162+
};
163+
164+
module.exports = browserInterface;
165+
cordovaProxy.add('CordovaHttpPlugin', browserInterface);

test/app-template/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<allow-intent href="itms-apps:*" />
2424
</platform>
2525
<engine name="android" spec="6.2.3" />
26+
<engine name="browser" spec="5.0.0" />
2627
<engine name="ios" spec="4.4.0" />
2728
<plugin name="cordova-plugin-file" spec="4.3.3" />
2829
</widget>

test/app-template/www/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
4+
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com https://self-signed.badssl.com http://httpbin.org http://www.columbia.edu 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
55
<meta name="format-detection" content="telephone=no">
66
<meta name="msapplication-tap-highlight" content="no">
77
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">

0 commit comments

Comments
 (0)