From dc123932e3eeed4abe3501d8e4c6add756c7a845 Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Tue, 14 Oct 2014 15:52:33 +0200 Subject: [PATCH 01/10] #2 iOS doesn't compile because of a missing framework --- plugin.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin.xml b/plugin.xml index a276786..ce07c4e 100644 --- a/plugin.xml +++ b/plugin.xml @@ -2,7 +2,7 @@ + version="0.1.4"> SSL Pinning @@ -66,6 +66,7 @@ + From d03c6a5a480fa57d5b99b0f0c6b0be04af8aa4a6 Mon Sep 17 00:00:00 2001 From: Martin Bektchiev Date: Tue, 3 Feb 2015 15:08:53 +0200 Subject: [PATCH 02/10] Request Internet permission in AndroidManifest.xml --- plugin.xml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugin.xml b/plugin.xml index ce07c4e..5f9ece0 100644 --- a/plugin.xml +++ b/plugin.xml @@ -78,6 +78,10 @@ + + + + @@ -86,4 +90,4 @@ - \ No newline at end of file + From 20f39b32f15fb5d25572e8a4033584a4edad4600 Mon Sep 17 00:00:00 2001 From: Denis Babineau Date: Wed, 15 Jul 2015 15:21:39 -0300 Subject: [PATCH 03/10] update cordova file plugin dependency to new cordova-plugin-file name/version --- plugin.xml | 2 +- .../com/synconset/CordovaHTTP/CordovaHttpDownload.java | 2 +- www/cordovaHTTP.js | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/plugin.xml b/plugin.xml index ef01aca..91a08b2 100644 --- a/plugin.xml +++ b/plugin.xml @@ -14,7 +14,7 @@ - + diff --git a/src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java b/src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java index e046186..ba700e2 100644 --- a/src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java +++ b/src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java @@ -45,7 +45,7 @@ public class CordovaHttpDownload extends CordovaHttp implements Runnable { 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 { diff --git a/www/cordovaHTTP.js b/www/cordovaHTTP.js index a879e20..0db45f1 100644 --- a/www/cordovaHTTP.js +++ b/www/cordovaHTTP.js @@ -52,11 +52,13 @@ var http = { * */ var win = function(result) { - var entry = new (require('org.apache.cordova.file.FileEntry'))(); + var entry = new (require('cordova-plugin-file.FileEntry'))(); entry.isDirectory = false; entry.isFile = true; entry.name = result.file.name; entry.fullPath = result.file.fullPath; + entry.filesystem = new FileSystem(result.file.filesystemName || (result.file.filesystem == window.PERSISTENT ? 'persistent' : 'temporary')); + entry.nativeURL = result.file.nativeURL; success(entry); }; return exec(win, failure, "CordovaHttpPlugin", "downloadFile", [url, params, headers, filePath]); From 1c1f716cf856925910bf533da356e1dcb3902a66 Mon Sep 17 00:00:00 2001 From: Eddy Verbruggen Date: Mon, 13 Jul 2015 15:46:50 +0200 Subject: [PATCH 04/10] #7 android: acceptAllCerts doesn't call callbacks --- src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java b/src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java index b2cf398..ffe4ad6 100644 --- a/src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java +++ b/src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java @@ -83,6 +83,7 @@ public class CordovaHttpPlugin extends CordovaPlugin { } else if (action.equals("acceptAllCerts")) { boolean accept = args.getBoolean(0); CordovaHttp.acceptAllCerts(accept); + callbackContext.success(); } else if (action.equals("setHeader")) { String header = args.getString(0); String value = args.getString(1); From 43b04a94e2921fb216638c87f03903e384bd7305 Mon Sep 17 00:00:00 2001 From: Denis Babineau Date: Wed, 15 Jul 2015 09:33:44 -0300 Subject: [PATCH 05/10] add acceptAllHosts call to test self-signed certificate in dev environment (still performs pinning but skips host validation) TODO: iOS --- .../com/synconset/CordovaHTTP/CordovaHttp.java | 12 +++++++++--- .../synconset/CordovaHTTP/CordovaHttpPlugin.java | 4 ++++ .../com/synconset/CordovaHTTP/HttpRequest.java | 14 ++++++++++---- www/cordovaHTTP.js | 3 +++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/android/com/synconset/CordovaHTTP/CordovaHttp.java b/src/android/com/synconset/CordovaHTTP/CordovaHttp.java index e90ad6f..c5196c9 100644 --- a/src/android/com/synconset/CordovaHTTP/CordovaHttp.java +++ b/src/android/com/synconset/CordovaHTTP/CordovaHttp.java @@ -35,7 +35,8 @@ public abstract class CordovaHttp { private static AtomicBoolean sslPinning = new AtomicBoolean(false); private static AtomicBoolean acceptAllCerts = new AtomicBoolean(false); - + private static AtomicBoolean acceptAllHosts = new AtomicBoolean(false); + private String urlString; private Map params; private Map headers; @@ -61,7 +62,11 @@ public abstract class CordovaHttp { sslPinning.set(false); } } - + + public static void acceptAllHosts(boolean accept) { + acceptAllHosts.set(accept); + } + protected String getUrlString() { return this.urlString; } @@ -81,10 +86,11 @@ public abstract class CordovaHttp { protected HttpRequest setupSecurity(HttpRequest request) { if (acceptAllCerts.get()) { request.trustAllCerts(); - request.trustAllHosts(); + request.trustAllHosts(true); } if (sslPinning.get()) { request.pinToCerts(); + request.trustAllHosts(acceptAllHosts.get()); } return request; } diff --git a/src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java b/src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java index ffe4ad6..482f2ea 100644 --- a/src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java +++ b/src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java @@ -84,6 +84,10 @@ public class CordovaHttpPlugin extends CordovaPlugin { boolean accept = args.getBoolean(0); CordovaHttp.acceptAllCerts(accept); callbackContext.success(); + } else if (action.equals("acceptAllHosts")) { + boolean accept = args.getBoolean(0); + CordovaHttp.acceptAllHosts(accept); + callbackContext.success(); } else if (action.equals("setHeader")) { String header = args.getString(0); String value = args.getString(1); diff --git a/src/android/com/synconset/CordovaHTTP/HttpRequest.java b/src/android/com/synconset/CordovaHTTP/HttpRequest.java index a6e39a5..42cf568 100644 --- a/src/android/com/synconset/CordovaHTTP/HttpRequest.java +++ b/src/android/com/synconset/CordovaHTTP/HttpRequest.java @@ -3252,11 +3252,17 @@ public class HttpRequest { * * @return this request */ - public HttpRequest trustAllHosts() { + public HttpRequest trustAllHosts(boolean enable) { final HttpURLConnection connection = getConnection(); - if (connection instanceof HttpsURLConnection) - ((HttpsURLConnection) connection) - .setHostnameVerifier(getTrustedVerifier()); + if (connection instanceof HttpsURLConnection) { + if (enable) { + ((HttpsURLConnection) connection) + .setHostnameVerifier(getTrustedVerifier()); + } else { + ((HttpsURLConnection) connection) + .setHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier()); + } + } return this; } diff --git a/www/cordovaHTTP.js b/www/cordovaHTTP.js index 0db45f1..4dc08b3 100644 --- a/www/cordovaHTTP.js +++ b/www/cordovaHTTP.js @@ -19,6 +19,9 @@ var http = { acceptAllCerts: function(allow, success, failure) { return exec(success, failure, "CordovaHttpPlugin", "acceptAllCerts", [allow]); }, + acceptAllHosts: function(allow, success, failure) { + return exec(success, failure, "CordovaHttpPlugin", "acceptAllHosts", [allow]); + }, post: function(url, params, headers, success, failure) { return exec(success, failure, "CordovaHttpPlugin", "post", [url, params, headers]); }, From cabb0db9de5270329b272d3b33fab57f83937622 Mon Sep 17 00:00:00 2001 From: Denis Babineau Date: Tue, 28 Jul 2015 11:15:32 -0300 Subject: [PATCH 06/10] acceptAllHosts can now be called on both platform. --- src/ios/CordovaHttpPlugin.h | 1 + src/ios/CordovaHttpPlugin.m | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/src/ios/CordovaHttpPlugin.h b/src/ios/CordovaHttpPlugin.h index 445bc8a..fd6cd72 100644 --- a/src/ios/CordovaHttpPlugin.h +++ b/src/ios/CordovaHttpPlugin.h @@ -9,6 +9,7 @@ - (void)setHeader:(CDVInvokedUrlCommand*)command; - (void)enableSSLPinning:(CDVInvokedUrlCommand*)command; - (void)acceptAllCerts:(CDVInvokedUrlCommand*)command; +- (void)acceptAllHosts:(CDVInvokedUrlCommand*)command; - (void)post:(CDVInvokedUrlCommand*)command; - (void)get:(CDVInvokedUrlCommand*)command; - (void)uploadFile:(CDVInvokedUrlCommand*)command; diff --git a/src/ios/CordovaHttpPlugin.m b/src/ios/CordovaHttpPlugin.m index 54de660..d938047 100644 --- a/src/ios/CordovaHttpPlugin.m +++ b/src/ios/CordovaHttpPlugin.m @@ -71,6 +71,14 @@ [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } +- (void)acceptAllHosts:(CDVInvokedUrlCommand*)command { + CDVPluginResult* pluginResult = nil; + bool allow = [[command.arguments objectAtIndex:0] boolValue]; + [HttpManager sharedClient].securityPolicy.validatesDomainName = !allow; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + - (void)post:(CDVInvokedUrlCommand*)command { HttpManager *manager = [HttpManager sharedClient]; NSString *url = [command.arguments objectAtIndex:0]; From 141f9960523504ecb32d042b34184904ff9df7d2 Mon Sep 17 00:00:00 2001 From: Denis Babineau Date: Wed, 26 Aug 2015 23:58:50 -0300 Subject: [PATCH 07/10] fix downloadFile method on ios; invalid native path and not using proper instance of CDVFile --- src/ios/CordovaHttpPlugin.m | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ios/CordovaHttpPlugin.m b/src/ios/CordovaHttpPlugin.m index d938047..5630d2b 100644 --- a/src/ios/CordovaHttpPlugin.m +++ b/src/ios/CordovaHttpPlugin.m @@ -177,6 +177,10 @@ [self setRequestHeaders: headers]; + if ([filePath hasPrefix:@"file://"]) { + filePath = [filePath substringFromIndex:7]; + } + CordovaHttpPlugin* __weak weakSelf = self; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { @@ -229,10 +233,10 @@ return; } - CDVFile *file = [[CDVFile alloc] init]; + id filePlugin = [self.commandDelegate getCommandInstance:@"File"]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; - [dictionary setObject:[file getDirectoryEntry:filePath isDirectory:NO] forKey:@"file"]; + [dictionary setObject:[filePlugin getDirectoryEntry:filePath isDirectory:NO] forKey:@"file"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { From 07e0195c91f7160d685873a4eb22e23ddcb26431 Mon Sep 17 00:00:00 2001 From: Denis Babineau Date: Wed, 7 Oct 2015 13:33:07 -0300 Subject: [PATCH 08/10] add HEAD request support and return response headers for all request types both for success and failures. --- .../synconset/CordovaHTTP/CordovaHttp.java | 15 +++++ .../CordovaHTTP/CordovaHttpDownload.java | 1 + .../synconset/CordovaHTTP/CordovaHttpGet.java | 1 + .../CordovaHTTP/CordovaHttpHead.java | 64 +++++++++++++++++++ .../CordovaHTTP/CordovaHttpPlugin.java | 8 +++ .../CordovaHTTP/CordovaHttpPost.java | 1 + .../CordovaHTTP/CordovaHttpUpload.java | 1 + src/ios/CordovaHttpPlugin.m | 55 ++++++++++++++++ www/cordovaHTTP.js | 6 ++ 9 files changed, 152 insertions(+) create mode 100644 src/android/com/synconset/CordovaHTTP/CordovaHttpHead.java diff --git a/src/android/com/synconset/CordovaHTTP/CordovaHttp.java b/src/android/com/synconset/CordovaHTTP/CordovaHttp.java index c5196c9..e6c7626 100644 --- a/src/android/com/synconset/CordovaHTTP/CordovaHttp.java +++ b/src/android/com/synconset/CordovaHTTP/CordovaHttp.java @@ -12,6 +12,8 @@ import java.io.IOException; 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; @@ -109,4 +111,17 @@ public abstract class CordovaHttp { protected void respondWithError(String msg) { this.respondWithError(500, msg); } + + protected void addResponseHeaders(HttpRequest request, JSONObject response) throws JSONException { + Map> headers = request.headers(); + Map parsed_headers = new HashMap(); + for (Map.Entry> entry : headers.entrySet()) { + String key = entry.getKey(); + List value = entry.getValue(); + if ((key != null) && (!value.isEmpty())) { + parsed_headers.put(key, value.get(0)); + } + } + response.put("headers", new JSONObject(parsed_headers)); + } } diff --git a/src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java b/src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java index ba700e2..cd0a9d5 100644 --- a/src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java +++ b/src/android/com/synconset/CordovaHTTP/CordovaHttpDownload.java @@ -40,6 +40,7 @@ public class CordovaHttpDownload extends CordovaHttp implements Runnable { 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); diff --git a/src/android/com/synconset/CordovaHTTP/CordovaHttpGet.java b/src/android/com/synconset/CordovaHTTP/CordovaHttpGet.java index 490597b..4c3ed73 100644 --- a/src/android/com/synconset/CordovaHTTP/CordovaHttpGet.java +++ b/src/android/com/synconset/CordovaHTTP/CordovaHttpGet.java @@ -40,6 +40,7 @@ public class CordovaHttpGet extends CordovaHttp implements Runnable { 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); diff --git a/src/android/com/synconset/CordovaHTTP/CordovaHttpHead.java b/src/android/com/synconset/CordovaHTTP/CordovaHttpHead.java new file mode 100644 index 0000000..9be50b7 --- /dev/null +++ b/src/android/com/synconset/CordovaHTTP/CordovaHttpHead.java @@ -0,0 +1,64 @@ +/** + * A HTTP plugin for Cordova / Phonegap + */ +package com.synconset; + +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.UnknownHostException; +import java.util.Map; + +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.HostnameVerifier; + +import javax.net.ssl.SSLHandshakeException; + +import org.apache.cordova.CallbackContext; +import org.json.JSONException; +import org.json.JSONObject; + +import android.util.Log; + +import com.github.kevinsawicki.http.HttpRequest; +import com.github.kevinsawicki.http.HttpRequest.HttpRequestException; + +public class CordovaHttpHead extends CordovaHttp implements Runnable { + public CordovaHttpHead(String urlString, Map params, Map headers, CallbackContext callbackContext) { + super(urlString, params, headers, callbackContext); + } + + @Override + public void run() { + try { + HttpRequest request = HttpRequest.head(this.getUrlString(), this.getParams(), true); + this.setupSecurity(request); + request.acceptCharset(CHARSET); + request.headers(this.getHeaders()); + int code = request.code(); + JSONObject response = new JSONObject(); + this.addResponseHeaders(request, response); + response.put("status", code); + if (code >= 200 && code < 300) { + // no 'body' to return for HEAD request + this.getCallbackContext().success(response); + } else { + String body = request.body(CHARSET); + response.put("error", body); + this.getCallbackContext().error(response); + } + } catch (JSONException e) { + this.respondWithError("There was an error generating the response"); + } catch (HttpRequestException e) { + if (e.getCause() instanceof UnknownHostException) { + this.respondWithError(0, "The host could not be resolved"); + } else if (e.getCause() instanceof SSLHandshakeException) { + this.respondWithError("SSL handshake failed"); + } else { + this.respondWithError("There was an error with the request"); + } + } + } +} \ No newline at end of file diff --git a/src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java b/src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java index 482f2ea..bdb76c6 100644 --- a/src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java +++ b/src/android/com/synconset/CordovaHTTP/CordovaHttpPlugin.java @@ -58,6 +58,14 @@ public class CordovaHttpPlugin extends CordovaPlugin { HashMap headersMap = this.addToMap(this.globalHeaders, headers); CordovaHttpGet get = new CordovaHttpGet(urlString, paramsMap, headersMap, callbackContext); cordova.getThreadPool().execute(get); + } else if (action.equals("head")) { + String urlString = args.getString(0); + JSONObject params = args.getJSONObject(1); + JSONObject headers = args.getJSONObject(2); + HashMap paramsMap = this.getMapFromJSONObject(params); + HashMap headersMap = this.addToMap(this.globalHeaders, headers); + CordovaHttpHead head = new CordovaHttpHead(urlString, paramsMap, headersMap, callbackContext); + cordova.getThreadPool().execute(head); } else if (action.equals("post")) { String urlString = args.getString(0); JSONObject params = args.getJSONObject(1); diff --git a/src/android/com/synconset/CordovaHTTP/CordovaHttpPost.java b/src/android/com/synconset/CordovaHTTP/CordovaHttpPost.java index 7e31b75..2b8e227 100644 --- a/src/android/com/synconset/CordovaHTTP/CordovaHttpPost.java +++ b/src/android/com/synconset/CordovaHTTP/CordovaHttpPost.java @@ -33,6 +33,7 @@ public class CordovaHttpPost extends CordovaHttp implements Runnable { 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); diff --git a/src/android/com/synconset/CordovaHTTP/CordovaHttpUpload.java b/src/android/com/synconset/CordovaHTTP/CordovaHttpUpload.java index 086e7ac..2e89558 100644 --- a/src/android/com/synconset/CordovaHTTP/CordovaHttpUpload.java +++ b/src/android/com/synconset/CordovaHTTP/CordovaHttpUpload.java @@ -70,6 +70,7 @@ public class CordovaHttpUpload extends CordovaHttp implements Runnable { 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); diff --git a/src/ios/CordovaHttpPlugin.m b/src/ios/CordovaHttpPlugin.m index 5630d2b..97763fe 100644 --- a/src/ios/CordovaHttpPlugin.m +++ b/src/ios/CordovaHttpPlugin.m @@ -91,12 +91,18 @@ [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; + if (operation.response != nil) { + [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + } [dictionary setObject:responseObject forKey:@"data"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; + if (operation.response != nil) { + [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + } [dictionary setObject:[error localizedDescription] forKey:@"error"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; @@ -116,18 +122,55 @@ [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; + if (operation.response != nil) { + [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + } [dictionary setObject:responseObject forKey:@"data"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; + if (operation.response != nil) { + [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + } [dictionary setObject:[error localizedDescription] forKey:@"error"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; }]; } +- (void)head:(CDVInvokedUrlCommand*)command { + HttpManager *manager = [HttpManager sharedClient]; + NSString *url = [command.arguments objectAtIndex:0]; + NSDictionary *parameters = [command.arguments objectAtIndex:1]; + NSDictionary *headers = [command.arguments objectAtIndex:2]; + [self setRequestHeaders: headers]; + + CordovaHttpPlugin* __weak weakSelf = self; + + manager.responseSerializer = [AFHTTPResponseSerializer serializer]; + [manager HEAD:url parameters:parameters success:^(AFHTTPRequestOperation *operation) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; + if (operation.response != nil) { + [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + } + // no 'body' for HEAD request, omitting 'data' + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; + [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; + if (operation.response != nil) { + [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + } + [dictionary setObject:[error localizedDescription] forKey:@"error"]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; + [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }]; +} + - (void)uploadFile:(CDVInvokedUrlCommand*)command { HttpManager *manager = [HttpManager sharedClient]; NSString *url = [command.arguments objectAtIndex:0]; @@ -156,11 +199,17 @@ } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; + if (operation.response != nil) { + [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + } CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; + if (operation.response != nil) { + [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + } [dictionary setObject:[error localizedDescription] forKey:@"error"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; @@ -236,12 +285,18 @@ id filePlugin = [self.commandDelegate getCommandInstance:@"File"]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; + if (operation.response != nil) { + [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + } [dictionary setObject:[filePlugin getDirectoryEntry:filePath isDirectory:NO] forKey:@"file"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:[NSNumber numberWithInt:operation.response.statusCode] forKey:@"status"]; + if (operation.response != nil) { + [dictionary setObject:operation.response.allHeaderFields forKey:@"headers"]; + } [dictionary setObject:[error localizedDescription] forKey:@"error"]; CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:dictionary]; [weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; diff --git a/www/cordovaHTTP.js b/www/cordovaHTTP.js index 4dc08b3..9613f3e 100644 --- a/www/cordovaHTTP.js +++ b/www/cordovaHTTP.js @@ -28,6 +28,9 @@ var http = { get: function(url, params, headers, success, failure) { return exec(success, failure, "CordovaHttpPlugin", "get", [url, params, headers]); }, + head: function(url, params, headers, success, failure) { + return exec(success, failure, "CordovaHttpPlugin", "head", [url, params, headers]); + }, uploadFile: function(url, params, headers, filePath, name, success, failure) { return exec(success, failure, "CordovaHttpPlugin", "uploadFile", [url, params, headers, filePath, name]); }, @@ -122,6 +125,9 @@ if (typeof angular !== "undefined") { get: function(url, params, headers) { return makePromise(http.get, [url, params, headers], true); }, + head: function(url, params, headers) { + return makePromise(http.head, [url, params, headers], true); + }, uploadFile: function(url, params, headers, filePath, name) { return makePromise(http.uploadFile, [url, params, headers, filePath, name], true); }, From c9a9296396bc20be6c4d7843249b3a208bb938b1 Mon Sep 17 00:00:00 2001 From: Denis Babineau Date: Wed, 10 Feb 2016 14:34:05 -0400 Subject: [PATCH 09/10] missing file reference in plugin.xml --- plugin.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin.xml b/plugin.xml index 0be919b..481df5d 100644 --- a/plugin.xml +++ b/plugin.xml @@ -85,6 +85,7 @@ + From dbe9d003b8e1c67f34d97f96cfb5a9b855d51b1e Mon Sep 17 00:00:00 2001 From: Denis Babineau Date: Tue, 16 Feb 2016 10:36:06 -0400 Subject: [PATCH 10/10] remove unused #import to non-existant file (now a private header in CordovaLib) --- src/ios/CordovaHttpPlugin.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ios/CordovaHttpPlugin.h b/src/ios/CordovaHttpPlugin.h index fd6cd72..af5aa1f 100644 --- a/src/ios/CordovaHttpPlugin.h +++ b/src/ios/CordovaHttpPlugin.h @@ -1,7 +1,6 @@ #import #import -#import @interface CordovaHttpPlugin : CDVPlugin