Skip to content

Commit fbd5c60

Browse files
committed
Merge pull request #77 from wymsee/update
Update
2 parents 9ed1c45 + fc22004 commit fbd5c60

38 files changed

+1807
-3068
lines changed

.editorconfig

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# EditorConfig helps developers define and maintain consistent
3+
# coding styles between different editors and IDEs
4+
# editorconfig.org
5+
6+
root = true
7+
8+
9+
[*]
10+
11+
# Change these settings to your own preference
12+
indent_style = space
13+
indent_size = 4
14+
15+
# We recommend you to keep these unchanged
16+
end_of_line = lf
17+
charset = utf-8
18+
trim_trailing_whitespace = true
19+
insert_final_newline = true
20+
21+
[*.md]
22+
trim_trailing_whitespace = false

CHANGELOG.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Changelog
2+
3+
## v1.0.0
4+
5+
- Added getBasicAuthHeader function
6+
- Added necessary iOS framework (Thanks to EddyVerbruggen)
7+
- Request internet permission in android (Thanks to mbektchiev)
8+
- Fix acceptAllCerts doesn't call callbacks (Thanks to EddyVerbruggen)
9+
- Add validateDomainName (Thanks to denisbabineau)
10+
- Add HEAD request support (untested) (Thanks to denisbabineau)
11+
12+
### Potentially Breaking Changes
13+
14+
- Update cordova file plugin dependency (Thanks to denisbabineau)
15+
- useBasicAuthHeader and setHeader are now synchronous functions
16+
- updated AFNetworking to 3.0.4 - only iOS 7+ is now supported
17+
- updated http-request to 6.0
18+
19+
## v0.1.4
20+
21+
- Support for certificates in www/certificates folder (Thanks to EddyVerbruggen)
22+
23+
## v0.1.3
24+
25+
- Update AFNetworking to 2.4.1 for iOS bug fix in Xcode 6
26+
27+
## v0.1.2
28+
29+
- Fixed plugin.xml for case sensitive filesystems (Thanks to andrey-tsaplin)
30+
31+
## v0.1.1
32+
33+
- Fixed a bug that prevented building
34+
35+
## v0.1.0
36+
37+
- Initial release
38+
39+
40+
## Contributions not noted above
41+
42+
- Fixed examples (Thanks to devgeeks)
43+
- Reports SSL Handshake errors rather than giving a generic error (Thanks to devgeeks)
44+
- Exporting http as a module (Thanks to pvsaikrishna)
45+
- Added Limitations section to readme (Thanks to cvillerm)
46+
- Fixed examples (Thanks to hideov)

README.md

+29-15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Cordova / Phonegap plugin for communicating with HTTP servers. Supports iOS and
88
- Background threading - all requests are done in a background thread.
99
- SSL Pinning - read more at [LumberBlog](http://blog.lumberlabs.com/2012/04/why-app-developers-should-care-about.html).
1010

11+
## Updates
12+
13+
Please check [CHANGELOG.md](CHANGELOG.md) for details about updating to a new version.
14+
1115
## Installation
1216

1317
The plugin conforms to the Cordova plugin specification, it can be installed
@@ -32,30 +36,31 @@ You can then inject the cordovaHTTP service into your controllers. The function
3236
This plugin registers a `cordovaHTTP` global on window
3337

3438

35-
## Functions
39+
## Sync Functions
40+
41+
### getBasicAuthHeader
42+
This returns an object representing a basic HTTP Authorization header of the form `{'Authorization': 'Basic base64encodedusernameandpassword'}`
3643

37-
All available functions are documented below. Every function takes a success and error callback function as the last 2 arguments.
44+
var header = cordovaHTTP.getBasicAuthHeader("user", "password");
3845

3946
### useBasicAuth
4047
This sets up all future requests to use Basic HTTP authentication with the given username and password.
4148

42-
cordovaHTTP.useBasicAuth("user", "password", function() {
43-
console.log('success!');
44-
}, function() {
45-
console.log('error :(');
46-
});
49+
cordovaHTTP.useBasicAuth("user", "password");
4750

4851
### setHeader
4952
Set a header for all future requests. Takes a header and a value.
5053

51-
cordovaHTTP.setHeader("Header", "Value", function() {
52-
console.log('success!');
53-
}, function() {
54-
console.log('error :(');
55-
});
54+
cordovaHTTP.setHeader("Header", "Value");
55+
56+
57+
## Async Functions
58+
These functions all take success and error callbacks as their last 2 arguments.
5659

5760
### enableSSLPinning
58-
Enable or disable SSL pinning. To use SSL pinning you must include at least one .cer SSL certificate in your app project. You can pin to your server certificate or to one of the issuing CA certificates. For ios include your certificate in the root level of your bundle (just add the .cer file to your project/target at the root level). For android include your certificate in your project's platforms/android/assets folder. In both cases all .cer files found will be loaded automatically. If you only have a .pem certificate see this [stackoverflow answer](http://stackoverflow.com/a/16583429/3182729). You want to convert it to a DER encoded certificate with a .cer extension.
61+
Enable or disable SSL pinning. This defaults to false.
62+
63+
To use SSL pinning you must include at least one .cer SSL certificate in your app project. You can pin to your server certificate or to one of the issuing CA certificates. For ios include your certificate in the root level of your bundle (just add the .cer file to your project/target at the root level). For android include your certificate in your project's platforms/android/assets folder. In both cases all .cer files found will be loaded automatically. If you only have a .pem certificate see this [stackoverflow answer](http://stackoverflow.com/a/16583429/3182729). You want to convert it to a DER encoded certificate with a .cer extension.
5964

6065
As an alternative, you can store your .cer files in the www/certificates folder.
6166

@@ -66,14 +71,23 @@ As an alternative, you can store your .cer files in the www/certificates folder.
6671
});
6772

6873
### acceptAllCerts
69-
Accept all SSL certificates. Or disable accepting all certificates.
74+
Accept all SSL certificates. Or disable accepting all certificates. This defaults to false.
7075

7176
cordovaHTTP.acceptAllCerts(true, function() {
7277
console.log('success!');
7378
}, function() {
7479
console.log('error :(');
7580
});
7681

82+
### validateDomainName
83+
Whether or not to validate the domain name in the certificate. This defaults to true.
84+
85+
cordovaHTTP.validateDomainName(false, function() {
86+
console.log('success!');
87+
}, function() {
88+
console.log('error :(');
89+
});
90+
7791
### post<a name="post"></a>
7892
Execute a POST request. Takes a URL, parameters, and headers.
7993

@@ -167,7 +181,7 @@ This plugin utilizes some awesome open source networking libraries. These are b
167181

168182
We made a few modifications to http-request. They can be found in a separate repo here: https://github.com/wymsee/http-request
169183

170-
## Limitations
184+
## Current Limitations
171185

172186
This plugin isn't equivalent to using XMLHttpRequest or Ajax calls in Javascript.
173187
For instance, the following features are currently not supported:

package.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "cordova-plugin-http",
3+
"version": "1.0.0",
4+
"description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning",
5+
"cordova": {
6+
"id": "cordova-plugin-http",
7+
"platforms": [
8+
"ios",
9+
"android"
10+
]
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/wymsee/cordova-HTTP.git"
15+
},
16+
"keywords": [
17+
"cordova",
18+
"device",
19+
"ecosystem:cordova",
20+
"cordova-ios",
21+
"cordova-android"
22+
],
23+
"engines": [
24+
{
25+
"name": "cordova",
26+
"version": ">=3.0.0"
27+
}
28+
],
29+
"author": "Wymsee",
30+
"license": "MIT",
31+
"bugs": {
32+
"url": "https://github.com/wymsee/cordova-HTTP/issues"
33+
},
34+
"homepage": "https://github.com/wymsee/cordova-HTTP#readme"
35+
}

plugin.xml

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
33
xmlns:android="http://schemas.android.com/apk/res/android"
4-
id="com.synconset.cordovaHTTP"
5-
version="0.1.4">
4+
id="cordova-plugin-http"
5+
version="0.2.0">
66

77
<name>SSL Pinning</name>
88

@@ -14,10 +14,10 @@
1414
<engine name="cordova" version=">=3.0.0" />
1515
</engines>
1616

17-
<dependency id="org.apache.cordova.file" url="https://github.com/apache/cordova-plugin-file" commit="r0.2.5" />
17+
<dependency id="cordova-plugin-file" version=">=2.0.0" />
1818

1919
<js-module src="www/cordovaHTTP.js" name="CordovaHttpPlugin">
20-
<clobbers target="plugins.CordovaHttpPlugin" />
20+
<clobbers target="CordovaHttpPlugin" />
2121
</js-module>
2222

2323
<!-- ios -->
@@ -31,18 +31,9 @@
3131
<header-file src="src/ios/CordovaHttpPlugin.h" />
3232
<source-file src="src/ios/CordovaHttpPlugin.m" />
3333

34-
<header-file src="src/ios/HTTPManager.h" />
35-
<source-file src="src/ios/HTTPManager.m" />
36-
3734
<header-file src="src/ios/TextResponseSerializer.h" />
3835
<source-file src="src/ios/TextResponseSerializer.m" />
3936

40-
<header-file src="src/ios/AFNetworking/AFHTTPRequestOperation.h" />
41-
<source-file src="src/ios/AFNetworking/AFHTTPRequestOperation.m" />
42-
43-
<header-file src="src/ios/AFNetworking/AFHTTPRequestOperationManager.h" />
44-
<source-file src="src/ios/AFNetworking/AFHTTPRequestOperationManager.m" />
45-
4637
<header-file src="src/ios/AFNetworking/AFHTTPSessionManager.h" />
4738
<source-file src="src/ios/AFNetworking/AFHTTPSessionManager.m" />
4839

@@ -54,9 +45,6 @@
5445
<header-file src="src/ios/AFNetworking/AFSecurityPolicy.h" />
5546
<source-file src="src/ios/AFNetworking/AFSecurityPolicy.m" />
5647

57-
<header-file src="src/ios/AFNetworking/AFURLConnectionOperation.h" />
58-
<source-file src="src/ios/AFNetworking/AFURLConnectionOperation.m" />
59-
6048
<header-file src="src/ios/AFNetworking/AFURLRequestSerialization.h" />
6149
<source-file src="src/ios/AFNetworking/AFURLRequestSerialization.m" />
6250

@@ -78,12 +66,17 @@
7866
</feature>
7967
</config-file>
8068

69+
<config-file target="AndroidManifest.xml" parent="/manifest">
70+
<uses-permission android:name="android.permission.INTERNET" />
71+
</config-file>
72+
8173
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttp.java" target-dir="src/com/synconset" />
8274
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpGet.java" target-dir="src/com/synconset" />
8375
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpPost.java" target-dir="src/com/synconset" />
76+
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpHead.java" target-dir="src/com/synconset" />
8477
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpUpload.java" target-dir="src/com/synconset" />
8578
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java" target-dir="src/com/synconset" />
8679
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java" target-dir="src/com/synconset" />
8780
<source-file src="src/android/com/synconset/CordovaHTTP/HttpRequest.java" target-dir="src/com/github/kevinsawicki/http" />
8881
</platform>
89-
</plugin>
82+
</plugin>

src/android/com/synconset/CordovaHTTP/CordovaHttp.java

+24-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.io.InputStream;
1313
import java.io.InputStreamReader;
1414
import java.io.BufferedReader;
15+
import java.util.HashMap;
16+
import java.util.List;
1517
import java.util.Map;
1618
import java.util.concurrent.atomic.AtomicBoolean;
1719

@@ -35,7 +37,8 @@ public abstract class CordovaHttp {
3537

3638
private static AtomicBoolean sslPinning = new AtomicBoolean(false);
3739
private static AtomicBoolean acceptAllCerts = new AtomicBoolean(false);
38-
40+
private static AtomicBoolean validateDomainName = new AtomicBoolean(true);
41+
3942
private String urlString;
4043
private Map<?, ?> params;
4144
private Map<String, String> headers;
@@ -61,7 +64,11 @@ public static void acceptAllCerts(boolean accept) {
6164
sslPinning.set(false);
6265
}
6366
}
64-
67+
68+
public static void validateDomainName(boolean accept) {
69+
validateDomainName.set(accept);
70+
}
71+
6572
protected String getUrlString() {
6673
return this.urlString;
6774
}
@@ -81,6 +88,8 @@ protected CallbackContext getCallbackContext() {
8188
protected HttpRequest setupSecurity(HttpRequest request) {
8289
if (acceptAllCerts.get()) {
8390
request.trustAllCerts();
91+
}
92+
if (!validateDomainName.get()) {
8493
request.trustAllHosts();
8594
}
8695
if (sslPinning.get()) {
@@ -103,4 +112,17 @@ protected void respondWithError(int status, String msg) {
103112
protected void respondWithError(String msg) {
104113
this.respondWithError(500, msg);
105114
}
115+
116+
protected void addResponseHeaders(HttpRequest request, JSONObject response) throws JSONException {
117+
Map<String, List<String>> headers = request.headers();
118+
Map<String, String> parsed_headers = new HashMap<String, String>();
119+
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
120+
String key = entry.getKey();
121+
List<String> value = entry.getValue();
122+
if ((key != null) && (!value.isEmpty())) {
123+
parsed_headers.put(key, value.get(0));
124+
}
125+
}
126+
response.put("headers", new JSONObject(parsed_headers));
127+
}
106128
}

src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ public void run() {
4040
int code = request.code();
4141

4242
JSONObject response = new JSONObject();
43+
this.addResponseHeaders(request, response);
4344
response.put("status", code);
4445
if (code >= 200 && code < 300) {
4546
URI uri = new URI(filePath);
4647
File file = new File(uri);
4748
request.receive(file);
48-
JSONObject fileEntry = FileUtils.getEntry(file);
49+
JSONObject fileEntry = FileUtils.getFilePlugin().getEntryForFile(file);
4950
response.put("file", fileEntry);
5051
this.getCallbackContext().success(response);
5152
} else {

src/android/com/synconset/CordovaHTTP/CordovaHttpGet.java

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public void run() {
4040
int code = request.code();
4141
String body = request.body(CHARSET);
4242
JSONObject response = new JSONObject();
43+
this.addResponseHeaders(request, response);
4344
response.put("status", code);
4445
if (code >= 200 && code < 300) {
4546
response.put("data", body);

0 commit comments

Comments
 (0)