Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added current request url to the response #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions src/android/com/synconset/CordovaHTTP/CordovaHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
import android.util.Log;

import com.github.kevinsawicki.http.HttpRequest;

public abstract class CordovaHttp {
protected static final String TAG = "CordovaHTTP";
protected static final String CHARSET = "UTF-8";

private static AtomicBoolean sslPinning = new AtomicBoolean(false);
private static AtomicBoolean acceptAllCerts = new AtomicBoolean(false);
private static AtomicBoolean validateDomainName = new AtomicBoolean(true);
Expand All @@ -43,21 +43,21 @@ public abstract class CordovaHttp {
private Map<?, ?> params;
private Map<String, String> headers;
private CallbackContext callbackContext;

public CordovaHttp(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext) {
this.urlString = urlString;
this.params = params;
this.headers = headers;
this.callbackContext = callbackContext;
}

public static void enableSSLPinning(boolean enable) {
sslPinning.set(enable);
if (enable) {
acceptAllCerts.set(false);
}
}

public static void acceptAllCerts(boolean accept) {
acceptAllCerts.set(accept);
if (accept) {
Expand All @@ -72,19 +72,19 @@ public static void validateDomainName(boolean accept) {
protected String getUrlString() {
return this.urlString;
}

protected Map<?, ?> getParams() {
return this.params;
}

protected Map<String, String> getHeaders() {
return this.headers;
}

protected CallbackContext getCallbackContext() {
return this.callbackContext;
}

protected HttpRequest setupSecurity(HttpRequest request) {
if (acceptAllCerts.get()) {
request.trustAllCerts();
Expand All @@ -97,7 +97,7 @@ protected HttpRequest setupSecurity(HttpRequest request) {
}
return request;
}

protected void respondWithError(int status, String msg) {
try {
JSONObject response = new JSONObject();
Expand All @@ -108,7 +108,7 @@ protected void respondWithError(int status, String msg) {
this.callbackContext.error(msg);
}
}

protected void respondWithError(String msg) {
this.respondWithError(500, msg);
}
Expand All @@ -125,4 +125,8 @@ protected void addResponseHeaders(HttpRequest request, JSONObject response) thro
}
response.put("headers", new JSONObject(parsed_headers));
}
}

protected void addCurrentRequestUrl(HttpRequest request, JSONObject response) throws JSONException {
response.put("currentRequestUrl", request.url().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

public class CordovaHttpDownload extends CordovaHttp implements Runnable {
private String filePath;

public CordovaHttpDownload(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext, String filePath) {
super(urlString, params, headers, callbackContext);
this.filePath = filePath;
}

@Override
public void run() {
try {
Expand All @@ -41,6 +41,7 @@ public void run() {

JSONObject response = new JSONObject();
this.addResponseHeaders(request, response);
this.addCurrentRequestUrl(request, response);
response.put("status", code);
if (code >= 200 && code < 300) {
URI uri = new URI(filePath);
Expand Down
7 changes: 4 additions & 3 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

import com.github.kevinsawicki.http.HttpRequest;
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;

public class CordovaHttpGet extends CordovaHttp implements Runnable {
public CordovaHttpGet(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext) {
super(urlString, params, headers, callbackContext);
}

@Override
public void run() {
try {
Expand All @@ -41,6 +41,7 @@ public void run() {
String body = request.body(CHARSET);
JSONObject response = new JSONObject();
this.addResponseHeaders(request, response);
this.addCurrentRequestUrl(request, response);
response.put("status", code);
if (code >= 200 && code < 300) {
response.put("data", body);
Expand All @@ -61,4 +62,4 @@ public void run() {
}
}
}
}
}
7 changes: 4 additions & 3 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpHead.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

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<String, String> headers, CallbackContext callbackContext) {
super(urlString, params, headers, callbackContext);
}

@Override
public void run() {
try {
Expand All @@ -40,6 +40,7 @@ public void run() {
int code = request.code();
JSONObject response = new JSONObject();
this.addResponseHeaders(request, response);
this.addCurrentRequestUrl(request, response);
response.put("status", code);
if (code >= 200 && code < 300) {
// no 'body' to return for HEAD request
Expand All @@ -61,4 +62,4 @@ public void run() {
}
}
}
}
}
5 changes: 3 additions & 2 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

import com.github.kevinsawicki.http.HttpRequest;
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;

public class CordovaHttpPost extends CordovaHttp implements Runnable {
public CordovaHttpPost(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext) {
super(urlString, params, headers, callbackContext);
}

@Override
public void run() {
try {
Expand All @@ -34,6 +34,7 @@ public void run() {
String body = request.body(CHARSET);
JSONObject response = new JSONObject();
this.addResponseHeaders(request, response);
this.addCurrentRequestUrl(request, response);
response.put("status", code);
if (code >= 200 && code < 300) {
response.put("data", body);
Expand Down
13 changes: 7 additions & 6 deletions src/android/com/synconset/CordovaHTTP/CordovaHttpUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@

import com.github.kevinsawicki.http.HttpRequest;
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;

public class CordovaHttpUpload extends CordovaHttp implements Runnable {
private String filePath;
private String name;

public CordovaHttpUpload(String urlString, Map<?, ?> params, Map<String, String> headers, CallbackContext callbackContext, String filePath, String name) {
super(urlString, params, headers, callbackContext);
this.filePath = filePath;
this.name = name;
}

@Override
public void run() {
try {
Expand All @@ -49,7 +49,7 @@ public void run() {
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeType = mimeTypeMap.getMimeTypeFromExtension(ext);
request.part(this.name, filename, mimeType, new File(uri));

Set<?> set = (Set<?>)this.getParams().entrySet();
Iterator<?> i = set.iterator();
while (i.hasNext()) {
Expand All @@ -65,12 +65,13 @@ public void run() {
return;
}
}

int code = request.code();
String body = request.body(CHARSET);

JSONObject response = new JSONObject();
this.addResponseHeaders(request, response);
this.addCurrentRequestUrl(request, response);
response.put("status", code);
if (code >= 200 && code < 300) {
response.put("data", body);
Expand Down
38 changes: 20 additions & 18 deletions src/ios/CordovaHttpPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ - (void)setResults:(NSMutableDictionary*)dictionary withTask:(NSURLSessionTask*)
NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
[dictionary setObject:[NSNumber numberWithInt:response.statusCode] forKey:@"status"];
[dictionary setObject:response.allHeaderFields forKey:@"headers"];
NSURLRequest *currentRequest = (NSURLRequest *)task.currentRequest;
[dictionary setObject:currentRequest.URL.absoluteString forKey:@"currentRequestUrl"];
}
}

Expand All @@ -41,27 +43,27 @@ - (void)enableSSLPinning:(CDVInvokedUrlCommand*)command {
} else {
securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
}

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)acceptAllCerts:(CDVInvokedUrlCommand*)command {
CDVPluginResult* pluginResult = nil;
bool allow = [[command.arguments objectAtIndex:0] boolValue];

securityPolicy.allowInvalidCertificates = allow;

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)validateDomainName:(CDVInvokedUrlCommand*)command {
CDVPluginResult* pluginResult = nil;
bool validate = [[command.arguments objectAtIndex:0] boolValue];

securityPolicy.validatesDomainName = validate;

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
Expand All @@ -73,7 +75,7 @@ - (void)post:(CDVInvokedUrlCommand*)command {
NSDictionary *parameters = [command.arguments objectAtIndex:1];
NSDictionary *headers = [command.arguments objectAtIndex:2];
[self setRequestHeaders: headers forManager: manager];

CordovaHttpPlugin* __weak weakSelf = self;
manager.responseSerializer = [TextResponseSerializer serializer];
[manager POST:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
Expand All @@ -99,9 +101,9 @@ - (void)get:(CDVInvokedUrlCommand*)command {
NSDictionary *parameters = [command.arguments objectAtIndex:1];
NSDictionary *headers = [command.arguments objectAtIndex:2];
[self setRequestHeaders: headers forManager: manager];

CordovaHttpPlugin* __weak weakSelf = self;

manager.responseSerializer = [TextResponseSerializer serializer];
[manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
Expand All @@ -126,9 +128,9 @@ - (void)head:(CDVInvokedUrlCommand*)command {
NSDictionary *parameters = [command.arguments objectAtIndex:1];
NSDictionary *headers = [command.arguments objectAtIndex:2];
[self setRequestHeaders: headers forManager: manager];

CordovaHttpPlugin* __weak weakSelf = self;

manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager HEAD:url parameters:parameters success:^(NSURLSessionTask *task) {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
Expand All @@ -154,11 +156,11 @@ - (void)uploadFile:(CDVInvokedUrlCommand*)command {
NSDictionary *headers = [command.arguments objectAtIndex:2];
NSString *filePath = [command.arguments objectAtIndex: 3];
NSString *name = [command.arguments objectAtIndex: 4];

NSURL *fileURL = [NSURL URLWithString: filePath];

[self setRequestHeaders: headers forManager: manager];

CordovaHttpPlugin* __weak weakSelf = self;
manager.responseSerializer = [TextResponseSerializer serializer];
[manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
Expand Down Expand Up @@ -195,13 +197,13 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command {
NSDictionary *parameters = [command.arguments objectAtIndex:1];
NSDictionary *headers = [command.arguments objectAtIndex:2];
NSString *filePath = [command.arguments objectAtIndex: 3];

[self setRequestHeaders: headers forManager: manager];

if ([filePath hasPrefix:@"file://"]) {
filePath = [filePath substringFromIndex:7];
}

CordovaHttpPlugin* __weak weakSelf = self;
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:url parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
Expand Down Expand Up @@ -229,7 +231,7 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command {
*/
// Download response is okay; begin streaming output to file
NSString* parentPath = [filePath stringByDeletingLastPathComponent];

// create parent directories if needed
NSError *error;
if ([[NSFileManager defaultManager] createDirectoryAtPath:parentPath withIntermediateDirectories:YES attributes:nil error:&error] == NO) {
Expand All @@ -253,7 +255,7 @@ - (void)downloadFile:(CDVInvokedUrlCommand*)command {
[weakSelf.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}

id filePlugin = [self.commandDelegate getCommandInstance:@"File"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[self setResults: dictionary withTask: task];
Expand Down