Skip to content

Commit

Permalink
[Feature] Change fetch headers to use list of pairs (array of arrays).
Browse files Browse the repository at this point in the history
Because headers may be repeatable.
  • Loading branch information
zhanghai committed Dec 30, 2023
1 parent 3a09c1b commit 709b105
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ val BuiltinRuleList =
|| $.matches(url, 'www\\.reddit\\.com', '/r/[^/]+/s/.+')) {
const response = $.fetch(url, { redirect: 'manual' });
if ([301, 302, 303, 307, 308].includes(response.status)) {
const headers = response.headers;
for (name in headers) {
for ([name, value] of response.headers) {
if (name.toLowerCase() === 'location') {
return headers[name];
return value;
}
}
}
Expand Down
17 changes: 14 additions & 3 deletions app/src/main/java/me/zhanghai/android/untracker/Untracker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kotlinx.coroutines.runBlocking
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonArray
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
Expand Down Expand Up @@ -271,7 +272,10 @@ private class Builtins : IBuiltins {
}
val method = options?.get("method")?.jsonPrimitive?.content?.uppercase() ?: "GET"
val headers =
options?.get("headers")?.jsonObject?.mapValues { it.value.jsonPrimitive.content }
options?.get("headers")?.jsonArray?.map {
val nameAndValue = it.jsonArray
nameAndValue[0].jsonPrimitive.content to nameAndValue[1].jsonPrimitive.content
}
val body = options?.get("body")?.jsonPrimitive?.content
val redirect =
options?.get("redirect")?.jsonPrimitive?.content?.let {
Expand Down Expand Up @@ -312,8 +316,15 @@ private class Builtins : IBuiltins {
put("body", JsonPrimitive(response.body?.string()))
put(
"headers",
buildJsonObject {
response.headers.forEach { (name, value) -> put(name, JsonPrimitive(value)) }
buildJsonArray {
response.headers.forEach { (name, value) ->
add(
buildJsonArray {
add(JsonPrimitive(name))
add(JsonPrimitive(value))
}
)
}
}
)
put("ok", JsonPrimitive(response.isSuccessful))
Expand Down

0 comments on commit 709b105

Please sign in to comment.