summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHubert Chathi <hubert@uhoreg.ca>2020-10-22 22:08:46 -0400
committerHubert Chathi <hubert@uhoreg.ca>2020-10-22 22:08:46 -0400
commit3f94502e2d3aa544f4e32446783aa47b59d28b14 (patch)
treee3d7e501a6443cb1531f6d10c4d4d7a6e0bab2cd
parentadd a method to create delete requests (diff)
allow compressed server responses
-rw-r--r--lib/polyjuice/client/endpoint.ex32
-rw-r--r--lib/polyjuice/client/sync.ex28
-rw-r--r--test/polyjuice/client/endpoint/get_rooms_messages_test.exs6
-rw-r--r--test/polyjuice/client/endpoint/get_rooms_state_test.exs6
-rw-r--r--test/polyjuice/client/endpoint/get_sync_test.exs9
-rw-r--r--test/polyjuice/client/endpoint/post_join_test.exs2
-rw-r--r--test/polyjuice/client/endpoint/post_login_test.exs1
-rw-r--r--test/polyjuice/client/endpoint/post_logout_test.exs1
-rw-r--r--test/polyjuice/client/endpoint/post_rooms_forget_test.exs1
-rw-r--r--test/polyjuice/client/endpoint/post_rooms_leave_test.exs1
-rw-r--r--test/polyjuice/client/endpoint/post_rooms_receipt_test.exs1
-rw-r--r--test/polyjuice/client/endpoint/post_user_filter_test.exs1
-rw-r--r--test/polyjuice/client/endpoint/put_rooms_send_test.exs1
-rw-r--r--test/polyjuice/client/endpoint/put_rooms_state_test.exs1
14 files changed, 73 insertions, 18 deletions
diff --git a/lib/polyjuice/client/endpoint.ex b/lib/polyjuice/client/endpoint.ex
index 7869ec6..e22f758 100644
--- a/lib/polyjuice/client/endpoint.ex
+++ b/lib/polyjuice/client/endpoint.ex
@@ -54,11 +54,17 @@ defmodule Polyjuice.Client.Endpoint do
def prefix_media_r0, do: "_matrix/media/r0"
@doc "Headers for endpoints that accept JSON."
- def accept_json, do: [{"Accept", "application/json"}]
+ def accept_json,
+ do: [
+ {"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"}
+ ]
+
@doc "Headers for endpoints that send and accept JSON."
def send_json,
do: [
{"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"},
{"Content-Type", "application/json"}
]
@@ -168,9 +174,10 @@ defmodule Polyjuice.Client.Endpoint do
) :: any
def parse_response(%{} = endpoint_args, status_code, headers, body)
when is_integer(status_code) and is_list(headers) and is_binary(body) do
+ {:ok, decoded_body} = content_decode(body, headers)
# make sure it's JSON content
with "application/json" <- get_header(headers, "content-type"),
- {:ok, json} <- Jason.decode(body) do
+ {:ok, json} <- Jason.decode(decoded_body) do
case status_code do
200 ->
Polyjuice.Client.Endpoint.BodyParser.parse(endpoint_args, json)
@@ -181,7 +188,7 @@ defmodule Polyjuice.Client.Endpoint do
else
_ ->
{:error, if(status_code == 200, do: 500, else: status_code),
- %{"errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE", "body" => body}}
+ %{"errcode" => "CA_UHOREG_POLYJUICE_BAD_RESPONSE", "body" => decoded_body}}
end
end
@@ -202,4 +209,23 @@ defmodule Polyjuice.Client.Endpoint do
value
end
+
+ def content_decode(encoded, headers) do
+ case get_header(headers, "content-encoding", "identity") do
+ "identity" ->
+ {:ok, encoded}
+
+ "gzip" ->
+ {:ok, :zlib.gunzip(encoded)}
+
+ "deflate" ->
+ {:ok, :zlib.uncompress(encoded)}
+
+ "x-gzip" ->
+ {:ok, :zlib.gunzip(encoded)}
+
+ _ ->
+ {:error, :unknown_method}
+ end
+ end
end
diff --git a/lib/polyjuice/client/sync.ex b/lib/polyjuice/client/sync.ex
index 7a9ebf9..4671260 100644
--- a/lib/polyjuice/client/sync.ex
+++ b/lib/polyjuice/client/sync.ex
@@ -147,6 +147,7 @@ defmodule Polyjuice.Client.Sync do
headers = [
{"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"},
{"Content-Type", "application/json"},
{"Authorization", "Bearer #{access_token}"}
]
@@ -163,8 +164,10 @@ defmodule Polyjuice.Client.Sync do
with "application/json" <-
Polyjuice.Client.Endpoint.get_header(resp_headers, "content-type"),
- {:ok, %{} = json_body} <- Jason.decode(body),
- filter_id = Map.get(json_body, "filter_id") do
+ {:ok, decoded_body} <-
+ Polyjuice.Client.Endpoint.content_decode(body, resp_headers),
+ {:ok, %{} = json_body} <- Jason.decode(decoded_body),
+ {:ok, filter_id} = Map.fetch(json_body, "filter_id") do
Logger.debug("got filter id #{filter_id}")
Polyjuice.Client.Storage.set_filter_id(
@@ -187,10 +190,11 @@ defmodule Polyjuice.Client.Sync do
401 ->
{:ok, body} = :hackney.body(client_ref)
+ {:ok, decoded_body} = Polyjuice.Client.Endpoint.content_decode(body, resp_headers)
with "application/json" <-
Polyjuice.Client.Endpoint.get_header(resp_headers, "content-type"),
- {:ok, %{} = json_body} <- Jason.decode(body),
+ {:ok, %{} = json_body} <- Jason.decode(decoded_body),
"M_UNKNOWN_TOKEN" <- Map.get(json_body, "errcode") do
:hackney.close(state.conn_ref)
@@ -204,14 +208,17 @@ defmodule Polyjuice.Client.Sync do
# don't recurse -- we're terminating
else
_ ->
- {:ok, body} = :hackney.body(client_ref)
- Logger.warn("Unable to set filter for sync. Ignoring. Got message: #{body}")
+ Logger.warn(
+ "Unable to set filter for sync. Ignoring. Got message: #{decoded_body}"
+ )
+
do_sync(%{state | set_filter: nil})
end
_ ->
{:ok, body} = :hackney.body(client_ref)
- Logger.warn("Unable to set filter for sync. Ignoring. Got message: #{body}")
+ {:ok, decoded_body} = Polyjuice.Client.Endpoint.content_decode(body, resp_headers)
+ Logger.warn("Unable to set filter for sync. Ignoring. Got message: #{decoded_body}")
do_sync(%{state | set_filter: nil})
end
@@ -242,6 +249,7 @@ defmodule Polyjuice.Client.Sync do
headers = [
{"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"},
{"Authorization", "Bearer #{access_token}"}
]
@@ -259,7 +267,9 @@ defmodule Polyjuice.Client.Sync do
with "application/json" <-
Polyjuice.Client.Endpoint.get_header(resp_headers, "content-type"),
- {:ok, json_body} <- Jason.decode(body),
+ {:ok, decoded_body} <-
+ Polyjuice.Client.Endpoint.content_decode(body, resp_headers),
+ {:ok, %{} = json_body} <- Jason.decode(decoded_body),
%{"next_batch" => next_batch} <- json_body do
if state.backoff, do: Logger.info("Sync resumed")
process_body(json_body, state)
@@ -282,7 +292,9 @@ defmodule Polyjuice.Client.Sync do
with "application/json" <-
Polyjuice.Client.Endpoint.get_header(resp_headers, "content-type"),
- {:ok, %{} = json_body} <- Jason.decode(body),
+ {:ok, decoded_body} <-
+ Polyjuice.Client.Endpoint.content_decode(body, resp_headers),
+ {:ok, %{} = json_body} <- Jason.decode(decoded_body),
"M_UNKNOWN_TOKEN" <- Map.get(json_body, "errcode") do
:hackney.close(state.conn_ref)
diff --git a/test/polyjuice/client/endpoint/get_rooms_messages_test.exs b/test/polyjuice/client/endpoint/get_rooms_messages_test.exs
index f1d8787..34fbdb9 100644
--- a/test/polyjuice/client/endpoint/get_rooms_messages_test.exs
+++ b/test/polyjuice/client/endpoint/get_rooms_messages_test.exs
@@ -27,7 +27,8 @@ defmodule Polyjuice.Client.Endpoint.GetRoomsMessagesTest do
auth_required: true,
body: "",
headers: [
- {"Accept", "application/json"}
+ {"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"}
],
method: :get,
path: "_matrix/client/r0/rooms/%21roomid/messages",
@@ -65,7 +66,8 @@ defmodule Polyjuice.Client.Endpoint.GetRoomsMessagesTest do
auth_required: true,
body: "",
headers: [
- {"Accept", "application/json"}
+ {"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"}
],
method: :get,
path: "_matrix/client/r0/rooms/%21roomid/messages",
diff --git a/test/polyjuice/client/endpoint/get_rooms_state_test.exs b/test/polyjuice/client/endpoint/get_rooms_state_test.exs
index 3c59b80..8cdc312 100644
--- a/test/polyjuice/client/endpoint/get_rooms_state_test.exs
+++ b/test/polyjuice/client/endpoint/get_rooms_state_test.exs
@@ -28,7 +28,8 @@ defmodule Polyjuice.Client.Endpoint.GetRoomsStateTest do
auth_required: true,
body: "",
headers: [
- {"Accept", "application/json"}
+ {"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"}
],
method: :get,
path: "_matrix/client/r0/rooms/%21room_id/state"
@@ -63,7 +64,8 @@ defmodule Polyjuice.Client.Endpoint.GetRoomsStateTest do
auth_required: true,
body: "",
headers: [
- {"Accept", "application/json"}
+ {"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"}
],
method: :get,
path: "_matrix/client/r0/rooms/%21room_id/state/m.room.name/"
diff --git a/test/polyjuice/client/endpoint/get_sync_test.exs b/test/polyjuice/client/endpoint/get_sync_test.exs
index 02aa580..4f86041 100644
--- a/test/polyjuice/client/endpoint/get_sync_test.exs
+++ b/test/polyjuice/client/endpoint/get_sync_test.exs
@@ -23,7 +23,8 @@ defmodule Polyjuice.Client.Endpoint.GetSyncTest do
auth_required: true,
body: "",
headers: [
- {"Accept", "application/json"}
+ {"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"}
],
method: :get,
path: "_matrix/client/r0/sync",
@@ -60,7 +61,8 @@ defmodule Polyjuice.Client.Endpoint.GetSyncTest do
auth_required: true,
body: "",
headers: [
- {"Accept", "application/json"}
+ {"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"}
],
method: :get,
path: "_matrix/client/r0/sync",
@@ -84,7 +86,8 @@ defmodule Polyjuice.Client.Endpoint.GetSyncTest do
auth_required: true,
body: "",
headers: [
- {"Accept", "application/json"}
+ {"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"}
],
method: :get,
path: "_matrix/client/r0/sync",
diff --git a/test/polyjuice/client/endpoint/post_join_test.exs b/test/polyjuice/client/endpoint/post_join_test.exs
index 9f21c52..abf9914 100644
--- a/test/polyjuice/client/endpoint/post_join_test.exs
+++ b/test/polyjuice/client/endpoint/post_join_test.exs
@@ -36,6 +36,7 @@ defmodule Polyjuice.Client.Endpoint.PostJoinTest do
body: nil,
headers: [
{"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"},
{"Content-Type", "application/json"}
],
method: :post,
@@ -83,6 +84,7 @@ defmodule Polyjuice.Client.Endpoint.PostJoinTest do
body: "{}",
headers: [
{"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"},
{"Content-Type", "application/json"}
],
method: :post,
diff --git a/test/polyjuice/client/endpoint/post_login_test.exs b/test/polyjuice/client/endpoint/post_login_test.exs
index 4a2fa96..8bc22d8 100644
--- a/test/polyjuice/client/endpoint/post_login_test.exs
+++ b/test/polyjuice/client/endpoint/post_login_test.exs
@@ -32,6 +32,7 @@ defmodule Polyjuice.Client.Endpoint.PostLoginTest do
body: nil,
headers: [
{"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"},
{"Content-Type", "application/json"}
],
method: :post,
diff --git a/test/polyjuice/client/endpoint/post_logout_test.exs b/test/polyjuice/client/endpoint/post_logout_test.exs
index 7f7835c..536c523 100644
--- a/test/polyjuice/client/endpoint/post_logout_test.exs
+++ b/test/polyjuice/client/endpoint/post_logout_test.exs
@@ -25,6 +25,7 @@ defmodule Polyjuice.Client.Endpoint.PostLogoutTest do
body: "{}",
headers: [
{"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"},
{"Content-Type", "application/json"}
],
method: :post,
diff --git a/test/polyjuice/client/endpoint/post_rooms_forget_test.exs b/test/polyjuice/client/endpoint/post_rooms_forget_test.exs
index a2ce841..e3300cd 100644
--- a/test/polyjuice/client/endpoint/post_rooms_forget_test.exs
+++ b/test/polyjuice/client/endpoint/post_rooms_forget_test.exs
@@ -24,6 +24,7 @@ defmodule Polyjuice.Client.Endpoint.PostRoomsForgetTest do
body: "{}",
headers: [
{"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"},
{"Content-Type", "application/json"}
],
method: :post,
diff --git a/test/polyjuice/client/endpoint/post_rooms_leave_test.exs b/test/polyjuice/client/endpoint/post_rooms_leave_test.exs
index d3448ef..7b7b9e2 100644
--- a/test/polyjuice/client/endpoint/post_rooms_leave_test.exs
+++ b/test/polyjuice/client/endpoint/post_rooms_leave_test.exs
@@ -24,6 +24,7 @@ defmodule Polyjuice.Client.Endpoint.PostRoomsLeaveTest do
body: "{}",
headers: [
{"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"},
{"Content-Type", "application/json"}
],
method: :post,
diff --git a/test/polyjuice/client/endpoint/post_rooms_receipt_test.exs b/test/polyjuice/client/endpoint/post_rooms_receipt_test.exs
index 1d126a2..e829167 100644
--- a/test/polyjuice/client/endpoint/post_rooms_receipt_test.exs
+++ b/test/polyjuice/client/endpoint/post_rooms_receipt_test.exs
@@ -28,6 +28,7 @@ defmodule Polyjuice.Client.Endpoint.PostRoomsReceiptTest do
body: "{}",
headers: [
{"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"},
{"Content-Type", "application/json"}
],
method: :post,
diff --git a/test/polyjuice/client/endpoint/post_user_filter_test.exs b/test/polyjuice/client/endpoint/post_user_filter_test.exs
index 947aa1e..80f22e4 100644
--- a/test/polyjuice/client/endpoint/post_user_filter_test.exs
+++ b/test/polyjuice/client/endpoint/post_user_filter_test.exs
@@ -32,6 +32,7 @@ defmodule Polyjuice.Client.Endpoint.PostUserfilterTest do
body: ~s({"presence":{"types":[]}}),
headers: [
{"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"},
{"Content-Type", "application/json"}
],
method: :post,
diff --git a/test/polyjuice/client/endpoint/put_rooms_send_test.exs b/test/polyjuice/client/endpoint/put_rooms_send_test.exs
index db4a4bc..98d5876 100644
--- a/test/polyjuice/client/endpoint/put_rooms_send_test.exs
+++ b/test/polyjuice/client/endpoint/put_rooms_send_test.exs
@@ -32,6 +32,7 @@ defmodule Polyjuice.Client.Endpoint.PutRoomsSendTest do
body: ~s({"body":"Hello World!"}),
headers: [
{"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"},
{"Content-Type", "application/json"}
],
method: :put,
diff --git a/test/polyjuice/client/endpoint/put_rooms_state_test.exs b/test/polyjuice/client/endpoint/put_rooms_state_test.exs
index cb745e3..e541aee 100644
--- a/test/polyjuice/client/endpoint/put_rooms_state_test.exs
+++ b/test/polyjuice/client/endpoint/put_rooms_state_test.exs
@@ -32,6 +32,7 @@ defmodule Polyjuice.Client.Endpoint.PutRoomsStateTest do
body: ~s({"name":"foo"}),
headers: [
{"Accept", "application/json"},
+ {"Accept-Encoding", "gzip, deflate"},
{"Content-Type", "application/json"}
],
method: :put,