mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-07-26 00:00:14 +08:00
add HEAD request support and return response headers for all request types both for success and failures.
This commit is contained in:
@@ -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<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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<String, String> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,6 +58,14 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
HashMap<String, String> 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<String, String> 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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user