Skip to content

Commit

Permalink
Added support for returning headers for the `native(url, success, err…
Browse files Browse the repository at this point in the history
…or, postBody, {returnHeaders: true})` requests
  • Loading branch information
usmanec committed Nov 15, 2024
1 parent 1c2d493 commit ed4cb57
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
8 changes: 7 additions & 1 deletion app/src/main/java/top/rootu/lampa/AndroidJS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ class AndroidJS(private val mainActivity: MainActivity, private val browser: Bro
val url = jSONObject.optString("url")
val data = jSONObject.opt("post_data")
var headers = jSONObject.optJSONObject("headers")
val returnHeaders = jSONObject.optBoolean("returnHeaders", false)
var contentType = jSONObject.optString("contentType")
val timeout = jSONObject.optInt("timeout", 15000)
var requestContent = ""
Expand Down Expand Up @@ -290,13 +291,18 @@ class AndroidJS(private val mainActivity: MainActivity, private val browser: Bro
val json: JSONObject?
val http = Http()
try {
s = if (TextUtils.isEmpty(finalRequestContent)) {
val responseJSON = if (TextUtils.isEmpty(finalRequestContent)) {
// GET
http.Get(url, finalHeaders, timeout)
} else {
// POST
http.Post(url, finalRequestContent, finalHeaders, timeout)
}
s = if (returnHeaders) {
responseJSON.toString()
} else {
responseJSON.optString("body", "")
}
} catch (e: Exception) {
json = JSONObject()
try {
Expand Down
58 changes: 42 additions & 16 deletions app/src/main/java/top/rootu/lampa/net/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import com.btr.proxy.selector.pac.PacScriptSource;
import com.btr.proxy.selector.pac.UrlPacScriptSource;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -49,24 +52,53 @@ private Request.Builder getReqBuilder(String url, JSONObject headers) {
return rb;
}

public String Get(String url, JSONObject headers, int timeout) throws Exception {
private JSONObject getJsonFromResponse(Response response) throws Exception {
if (!response.isSuccessful()) {
this.lastErrorCode = response.code();
throw new Exception("Invalid response from server: " + response.code() + " " + response.message());
}
Headers rh = response.headers();
JSONObject jsonHeaders = new JSONObject();
for (int i = 0, size = rh.size(); i < size; i++) {
String hName = rh.name(i).toLowerCase();
if (hName.equals("set-cookie")) {
if (!jsonHeaders.has(hName)) jsonHeaders.put(hName, new JSONArray());
JSONArray hValues = jsonHeaders.getJSONArray(hName);
hValues.put(hValues.length(), rh.value(i));
} else if (jsonHeaders.has(hName)) {
String hValue = jsonHeaders.optString(hName, "");
// see https://datatracker.ietf.org/doc/html/rfc7230#section-3.2.2
jsonHeaders.put(hName, hValue + ", " + rh.value(i));
} else {
jsonHeaders.put(hName, rh.value(i));
}
}
ResponseBody body = response.body();
assert response.networkResponse() != null;
assert body != null;
JSONObject json = new JSONObject();
try {
json.put("headers", jsonHeaders);
json.put("currentUrl", response.request().url().toString());
json.put("body", body.string());
} catch (JSONException jsonException) {
jsonException.printStackTrace();
}
return json;
}

public JSONObject Get(String url, JSONObject headers, int timeout) throws Exception {
if (!url.toLowerCase().startsWith("http")) {
this.lastErrorCode = 400;
throw new Exception("Bad Request (Invalid protocol; use http or https)");
}
Request.Builder rb = getReqBuilder(url, headers);
OkHttpClient client = HttpHelper.getOkHttpClient(timeout);
Response response = client.newCall(rb.build()).execute();
if (!response.isSuccessful()) {
this.lastErrorCode = response.code();
throw new Exception("Invalid response from server: " + response.code() + " " + response.message());
}
ResponseBody body = response.body();
assert body != null;
return body.string();
return getJsonFromResponse(response);
}

public String Post(String url, String data, JSONObject headers, int timeout) throws Exception {
public JSONObject Post(String url, String data, JSONObject headers, int timeout) throws Exception {
if (!url.toLowerCase().startsWith("http")) {
this.lastErrorCode = 400;
throw new Exception("Bad Request (Invalid protocol; use http or https)");
Expand All @@ -79,13 +111,7 @@ public String Post(String url, String data, JSONObject headers, int timeout) thr
rb.post(requestBody);
OkHttpClient client = HttpHelper.getOkHttpClient(timeout);
Response response = client.newCall(rb.build()).execute();
if (!response.isSuccessful()) {
this.lastErrorCode = response.code();
throw new Exception("Invalid response from server: " + response.code() + " " + response.message());
}
ResponseBody body = response.body();
assert body != null;
return body.string();
return getJsonFromResponse(response);
}

public int getLastErrorCode() {
Expand Down

0 comments on commit ed4cb57

Please sign in to comment.