Skip to content

Commit

Permalink
Merge pull request #77 from wymsee/update
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
allie-wake-up committed Feb 24, 2016
2 parents 9ed1c45 + fc22004 commit fbd5c60
Show file tree
Hide file tree
Showing 38 changed files with 1,807 additions and 3,068 deletions.
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true


[*]

# Change these settings to your own preference
indent_style = space
indent_size = 4

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
46 changes: 46 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Changelog

## v1.0.0

- Added getBasicAuthHeader function
- Added necessary iOS framework (Thanks to EddyVerbruggen)
- Request internet permission in android (Thanks to mbektchiev)
- Fix acceptAllCerts doesn't call callbacks (Thanks to EddyVerbruggen)
- Add validateDomainName (Thanks to denisbabineau)
- Add HEAD request support (untested) (Thanks to denisbabineau)

### Potentially Breaking Changes

- Update cordova file plugin dependency (Thanks to denisbabineau)
- useBasicAuthHeader and setHeader are now synchronous functions
- updated AFNetworking to 3.0.4 - only iOS 7+ is now supported
- updated http-request to 6.0

## v0.1.4

- Support for certificates in www/certificates folder (Thanks to EddyVerbruggen)

## v0.1.3

- Update AFNetworking to 2.4.1 for iOS bug fix in Xcode 6

## v0.1.2

- Fixed plugin.xml for case sensitive filesystems (Thanks to andrey-tsaplin)

## v0.1.1

- Fixed a bug that prevented building

## v0.1.0

- Initial release


## Contributions not noted above

- Fixed examples (Thanks to devgeeks)
- Reports SSL Handshake errors rather than giving a generic error (Thanks to devgeeks)
- Exporting http as a module (Thanks to pvsaikrishna)
- Added Limitations section to readme (Thanks to cvillerm)
- Fixed examples (Thanks to hideov)
44 changes: 29 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Cordova / Phonegap plugin for communicating with HTTP servers. Supports iOS and
- Background threading - all requests are done in a background thread.
- SSL Pinning - read more at [LumberBlog](http://blog.lumberlabs.com/2012/04/why-app-developers-should-care-about.html).

## Updates

Please check [CHANGELOG.md](CHANGELOG.md) for details about updating to a new version.

## Installation

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


## Functions
## Sync Functions

### getBasicAuthHeader
This returns an object representing a basic HTTP Authorization header of the form `{'Authorization': 'Basic base64encodedusernameandpassword'}`

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

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

cordovaHTTP.useBasicAuth("user", "password", function() {
console.log('success!');
}, function() {
console.log('error :(');
});
cordovaHTTP.useBasicAuth("user", "password");

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

cordovaHTTP.setHeader("Header", "Value", function() {
console.log('success!');
}, function() {
console.log('error :(');
});
cordovaHTTP.setHeader("Header", "Value");


## Async Functions
These functions all take success and error callbacks as their last 2 arguments.

### enableSSLPinning
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.
Enable or disable SSL pinning. This defaults to false.

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.

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

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

### acceptAllCerts
Accept all SSL certificates. Or disable accepting all certificates.
Accept all SSL certificates. Or disable accepting all certificates. This defaults to false.

cordovaHTTP.acceptAllCerts(true, function() {
console.log('success!');
}, function() {
console.log('error :(');
});

### validateDomainName
Whether or not to validate the domain name in the certificate. This defaults to true.

cordovaHTTP.validateDomainName(false, function() {
console.log('success!');
}, function() {
console.log('error :(');
});

### post<a name="post"></a>
Execute a POST request. Takes a URL, parameters, and headers.

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

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

## Limitations
## Current Limitations

This plugin isn't equivalent to using XMLHttpRequest or Ajax calls in Javascript.
For instance, the following features are currently not supported:
Expand Down
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "cordova-plugin-http",
"version": "1.0.0",
"description": "Cordova / Phonegap plugin for communicating with HTTP servers using SSL pinning",
"cordova": {
"id": "cordova-plugin-http",
"platforms": [
"ios",
"android"
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/wymsee/cordova-HTTP.git"
},
"keywords": [
"cordova",
"device",
"ecosystem:cordova",
"cordova-ios",
"cordova-android"
],
"engines": [
{
"name": "cordova",
"version": ">=3.0.0"
}
],
"author": "Wymsee",
"license": "MIT",
"bugs": {
"url": "https://github.com/wymsee/cordova-HTTP/issues"
},
"homepage": "https://github.com/wymsee/cordova-HTTP#readme"
}
27 changes: 10 additions & 17 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.synconset.cordovaHTTP"
version="0.1.4">
id="cordova-plugin-http"
version="0.2.0">

<name>SSL Pinning</name>

Expand All @@ -14,10 +14,10 @@
<engine name="cordova" version=">=3.0.0" />
</engines>

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

<js-module src="www/cordovaHTTP.js" name="CordovaHttpPlugin">
<clobbers target="plugins.CordovaHttpPlugin" />
<clobbers target="CordovaHttpPlugin" />
</js-module>

<!-- ios -->
Expand All @@ -31,18 +31,9 @@
<header-file src="src/ios/CordovaHttpPlugin.h" />
<source-file src="src/ios/CordovaHttpPlugin.m" />

<header-file src="src/ios/HTTPManager.h" />
<source-file src="src/ios/HTTPManager.m" />

<header-file src="src/ios/TextResponseSerializer.h" />
<source-file src="src/ios/TextResponseSerializer.m" />

<header-file src="src/ios/AFNetworking/AFHTTPRequestOperation.h" />
<source-file src="src/ios/AFNetworking/AFHTTPRequestOperation.m" />

<header-file src="src/ios/AFNetworking/AFHTTPRequestOperationManager.h" />
<source-file src="src/ios/AFNetworking/AFHTTPRequestOperationManager.m" />

<header-file src="src/ios/AFNetworking/AFHTTPSessionManager.h" />
<source-file src="src/ios/AFNetworking/AFHTTPSessionManager.m" />

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

<header-file src="src/ios/AFNetworking/AFURLConnectionOperation.h" />
<source-file src="src/ios/AFNetworking/AFURLConnectionOperation.m" />

<header-file src="src/ios/AFNetworking/AFURLRequestSerialization.h" />
<source-file src="src/ios/AFNetworking/AFURLRequestSerialization.m" />

Expand All @@ -78,12 +66,17 @@
</feature>
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.INTERNET" />
</config-file>

<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttp.java" target-dir="src/com/synconset" />
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpGet.java" target-dir="src/com/synconset" />
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpPost.java" target-dir="src/com/synconset" />
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpHead.java" target-dir="src/com/synconset" />
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpUpload.java" target-dir="src/com/synconset" />
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java" target-dir="src/com/synconset" />
<source-file src="src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java" target-dir="src/com/synconset" />
<source-file src="src/android/com/synconset/CordovaHTTP/HttpRequest.java" target-dir="src/com/github/kevinsawicki/http" />
</platform>
</plugin>
</plugin>
26 changes: 24 additions & 2 deletions src/android/com/synconset/CordovaHTTP/CordovaHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

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

private static AtomicBoolean sslPinning = new AtomicBoolean(false);
private static AtomicBoolean acceptAllCerts = new AtomicBoolean(false);

private static AtomicBoolean validateDomainName = new AtomicBoolean(true);

private String urlString;
private Map<?, ?> params;
private Map<String, String> headers;
Expand All @@ -61,7 +64,11 @@ public static void acceptAllCerts(boolean accept) {
sslPinning.set(false);
}
}


public static void validateDomainName(boolean accept) {
validateDomainName.set(accept);
}

protected String getUrlString() {
return this.urlString;
}
Expand All @@ -81,6 +88,8 @@ protected CallbackContext getCallbackContext() {
protected HttpRequest setupSecurity(HttpRequest request) {
if (acceptAllCerts.get()) {
request.trustAllCerts();
}
if (!validateDomainName.get()) {
request.trustAllHosts();
}
if (sslPinning.get()) {
Expand All @@ -103,4 +112,17 @@ protected void respondWithError(int status, String msg) {
protected void respondWithError(String msg) {
this.respondWithError(500, msg);
}

protected void addResponseHeaders(HttpRequest request, JSONObject response) throws JSONException {
Map<String, List<String>> headers = request.headers();
Map<String, String> parsed_headers = new HashMap<String, String>();
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String key = entry.getKey();
List<String> value = entry.getValue();
if ((key != null) && (!value.isEmpty())) {
parsed_headers.put(key, value.get(0));
}
}
response.put("headers", new JSONObject(parsed_headers));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ public void run() {
int code = request.code();

JSONObject response = new JSONObject();
this.addResponseHeaders(request, response);
response.put("status", code);
if (code >= 200 && code < 300) {
URI uri = new URI(filePath);
File file = new File(uri);
request.receive(file);
JSONObject fileEntry = FileUtils.getEntry(file);
JSONObject fileEntry = FileUtils.getFilePlugin().getEntryForFile(file);
response.put("file", fileEntry);
this.getCallbackContext().success(response);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void run() {
int code = request.code();
String body = request.body(CHARSET);
JSONObject response = new JSONObject();
this.addResponseHeaders(request, response);
response.put("status", code);
if (code >= 200 && code < 300) {
response.put("data", body);
Expand Down
Loading

0 comments on commit fbd5c60

Please sign in to comment.