From 0ce1eb4d972316d0df4eab637cc95fa8045eba64 Mon Sep 17 00:00:00 2001 From: Nicol Acosta Date: Fri, 1 Jul 2022 17:33:19 -0500 Subject: [PATCH 01/13] forced https --- lib/soap/request.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/soap/request.ex b/lib/soap/request.ex index 65ef3e7..f918be1 100644 --- a/lib/soap/request.ex +++ b/lib/soap/request.ex @@ -31,5 +31,6 @@ defmodule Soap.Request do @spec get_url(wsdl :: map()) :: String.t() defp get_url(wsdl) do wsdl.endpoint + |> String.repalce("http://", "https://") end end From f8bd0a29e7761fb3f54be33bce59fc32e27a34e5 Mon Sep 17 00:00:00 2001 From: Nicol Acosta Date: Fri, 1 Jul 2022 17:52:33 -0500 Subject: [PATCH 02/13] forced https fix --- lib/soap/request.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/soap/request.ex b/lib/soap/request.ex index f918be1..cea6e02 100644 --- a/lib/soap/request.ex +++ b/lib/soap/request.ex @@ -31,6 +31,6 @@ defmodule Soap.Request do @spec get_url(wsdl :: map()) :: String.t() defp get_url(wsdl) do wsdl.endpoint - |> String.repalce("http://", "https://") + |> String.replace("http://", "https://") end end From c025416494ef901836749576b81475a0fdf4aa17 Mon Sep 17 00:00:00 2001 From: Nicol Acosta Date: Mon, 4 Jul 2022 16:19:52 -0500 Subject: [PATCH 03/13] added option to enable force https --- README.md | 6 ++++++ lib/soap/request.ex | 21 ++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index bc436ea..dd186b5 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,12 @@ Configure version of SOAP protocol. Supported versions `1.1`(default) and `1.2`: config :soap, :globals, version: "1.1" ``` +To force https on the soap calls, just calls + +```elixir +config :soap, :globals, force_https: true +``` + ## Usage The documentation is available on [HexDocs](https://hexdocs.pm/soap/api-reference.html). diff --git a/lib/soap/request.ex b/lib/soap/request.ex index cea6e02..2e3be94 100644 --- a/lib/soap/request.ex +++ b/lib/soap/request.ex @@ -10,27 +10,30 @@ defmodule Soap.Request do Calling HTTPoison request by Map with method, url, body, headers, options keys. """ @spec call(wsdl :: map(), operation :: String.t(), params :: any(), headers :: any(), opts :: any()) :: any() - def call(wsdl, operation, soap_headers_and_params, request_headers \\ [], opts \\ []) + def call(wsdl, operation, soap_headers_and_params, https? \\ force_https?(), request_headers \\ [], opts \\ []) - def call(wsdl, operation, {soap_headers, params}, request_headers, opts) do - url = get_url(wsdl) + def call(wsdl, operation, {soap_headers, params}, https?, request_headers, opts) do + url = get_url(wsdl, https?) request_headers = Headers.build(wsdl, operation, request_headers) body = Params.build_body(wsdl, operation, params, soap_headers) get_http_client().post(url, body, request_headers, opts) end - def call(wsdl, operation, params, request_headers, opts), - do: call(wsdl, operation, {%{}, params}, request_headers, opts) + def call(wsdl, operation, params, https?, request_headers, opts), + do: call(wsdl, operation, {%{}, params}, https?, request_headers, opts) @spec get_http_client() :: HTTPoison.Base def get_http_client do Application.get_env(:soap, :globals)[:http_client] || HTTPoison end - @spec get_url(wsdl :: map()) :: String.t() - defp get_url(wsdl) do - wsdl.endpoint - |> String.replace("http://", "https://") + @spec force_https?() :: boolean() + def force_https? do + Application.get_env(:soap, :globals)[:force_https] || false end + + @spec get_url(wsdl :: map(), boolean()) :: String.t() + defp get_url(wsdl, true), do: String.replace(wsdl.endpoint, "http://", "https://") + defp get_url(wsdl, _), do: wsdl.endpoint end From cf8883c3bf1c6b3afaba3698b223941abd2c10cb Mon Sep 17 00:00:00 2001 From: Nicol Acosta Date: Mon, 4 Jul 2022 16:53:44 -0500 Subject: [PATCH 04/13] fix get default values before env vars from exec --- lib/soap/request.ex | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/soap/request.ex b/lib/soap/request.ex index 2e3be94..d3ac05c 100644 --- a/lib/soap/request.ex +++ b/lib/soap/request.ex @@ -10,9 +10,10 @@ defmodule Soap.Request do Calling HTTPoison request by Map with method, url, body, headers, options keys. """ @spec call(wsdl :: map(), operation :: String.t(), params :: any(), headers :: any(), opts :: any()) :: any() - def call(wsdl, operation, soap_headers_and_params, https? \\ force_https?(), request_headers \\ [], opts \\ []) + def call(wsdl, operation, soap_headers_and_params, https?, request_headers \\ [], opts \\ []) def call(wsdl, operation, {soap_headers, params}, https?, request_headers, opts) do + https? = if is_nil(https?), do: force_https?(), else: https? url = get_url(wsdl, https?) request_headers = Headers.build(wsdl, operation, request_headers) body = Params.build_body(wsdl, operation, params, soap_headers) @@ -21,7 +22,7 @@ defmodule Soap.Request do end def call(wsdl, operation, params, https?, request_headers, opts), - do: call(wsdl, operation, {%{}, params}, https?, request_headers, opts) + do: call(wsdl, operation, {%{}, params}, https?, request_headers, opts) @spec get_http_client() :: HTTPoison.Base def get_http_client do From 40674704fbe63310c82ff2b0001e88cf72d8c200 Mon Sep 17 00:00:00 2001 From: Nicol Acosta Date: Mon, 4 Jul 2022 17:14:54 -0500 Subject: [PATCH 05/13] removed option from call func, just the env var can force the ssl --- lib/soap/request.ex | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/soap/request.ex b/lib/soap/request.ex index d3ac05c..34275ed 100644 --- a/lib/soap/request.ex +++ b/lib/soap/request.ex @@ -10,19 +10,18 @@ defmodule Soap.Request do Calling HTTPoison request by Map with method, url, body, headers, options keys. """ @spec call(wsdl :: map(), operation :: String.t(), params :: any(), headers :: any(), opts :: any()) :: any() - def call(wsdl, operation, soap_headers_and_params, https?, request_headers \\ [], opts \\ []) + def call(wsdl, operation, soap_headers_and_params, request_headers \\ [], opts \\ []) - def call(wsdl, operation, {soap_headers, params}, https?, request_headers, opts) do - https? = if is_nil(https?), do: force_https?(), else: https? - url = get_url(wsdl, https?) + def call(wsdl, operation, {soap_headers, params}, request_headers, opts) do + url = get_url(wsdl, force_https?()) request_headers = Headers.build(wsdl, operation, request_headers) body = Params.build_body(wsdl, operation, params, soap_headers) get_http_client().post(url, body, request_headers, opts) end - def call(wsdl, operation, params, https?, request_headers, opts), - do: call(wsdl, operation, {%{}, params}, https?, request_headers, opts) + def call(wsdl, operation, params, request_headers, opts), + do: call(wsdl, operation, {%{}, params}, request_headers, opts) @spec get_http_client() :: HTTPoison.Base def get_http_client do From 3c7673c014deafea27beba2f0da53f64ae5b25c4 Mon Sep 17 00:00:00 2001 From: Nicol Acosta Date: Thu, 25 Aug 2022 10:34:00 -0500 Subject: [PATCH 06/13] added debug flag --- .gitignore | 1 + lib/soap/request.ex | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index a669b7e..7ddb9a9 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ soap-*.tar # Misc. /priv /.DS_Store +/.idea \ No newline at end of file diff --git a/lib/soap/request.ex b/lib/soap/request.ex index 34275ed..3cf7d1f 100644 --- a/lib/soap/request.ex +++ b/lib/soap/request.ex @@ -2,6 +2,8 @@ defmodule Soap.Request do @moduledoc """ Documentation for Soap.Request. """ + require Logger + alias Soap.Request.{Headers, Params} @doc """ @@ -10,18 +12,22 @@ defmodule Soap.Request do Calling HTTPoison request by Map with method, url, body, headers, options keys. """ @spec call(wsdl :: map(), operation :: String.t(), params :: any(), headers :: any(), opts :: any()) :: any() - def call(wsdl, operation, soap_headers_and_params, request_headers \\ [], opts \\ []) + def call(wsdl, operation, soap_headers_and_params, request_headers \\ [], opts \\ [], flags \\ []) - def call(wsdl, operation, {soap_headers, params}, request_headers, opts) do + def call(wsdl, operation, {soap_headers, params}, request_headers, opts, flags) do url = get_url(wsdl, force_https?()) request_headers = Headers.build(wsdl, operation, request_headers) body = Params.build_body(wsdl, operation, params, soap_headers) + if flags[:debug] do + Logger.debug("soap-call-debug", %{url: url, body: body, request_headers: request_headers, opts: opts}) + end + get_http_client().post(url, body, request_headers, opts) end - def call(wsdl, operation, params, request_headers, opts), - do: call(wsdl, operation, {%{}, params}, request_headers, opts) + def call(wsdl, operation, params, request_headers, opts, flags), + do: call(wsdl, operation, {%{}, params}, request_headers, opts, flags) @spec get_http_client() :: HTTPoison.Base def get_http_client do From aef9c16b921400492f541a6d333e48db857008c3 Mon Sep 17 00:00:00 2001 From: Nicol Acosta Date: Thu, 25 Aug 2022 11:15:18 -0500 Subject: [PATCH 07/13] added missing parameter on soap file --- lib/soap.ex | 6 +++--- lib/soap/request.ex | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/soap.ex b/lib/soap.ex index a2248e2..4c507d2 100644 --- a/lib/soap.ex +++ b/lib/soap.ex @@ -106,11 +106,11 @@ defmodule Soap do {:ok, %Soap.Response{}} """ - @spec call(wsdl :: map(), operation :: String.t(), params :: map(), headers :: any(), opts :: any()) :: any() - def call(wsdl, operation, params, headers \\ [], opts \\ []) do + @spec call(wsdl :: map(), operation :: String.t(), params :: map(), headers :: any(), opts :: any(), flags :: any()) :: any() + def call(wsdl, operation, params, headers \\ [], opts \\ [], flags \\ []) do wsdl |> validate_operation(operation) - |> Request.call(operation, params, headers, opts) + |> Request.call(operation, params, headers, opts, flags) |> handle_response end diff --git a/lib/soap/request.ex b/lib/soap/request.ex index 3cf7d1f..a3bc417 100644 --- a/lib/soap/request.ex +++ b/lib/soap/request.ex @@ -11,7 +11,7 @@ defmodule Soap.Request do Calling HTTPoison request by Map with method, url, body, headers, options keys. """ - @spec call(wsdl :: map(), operation :: String.t(), params :: any(), headers :: any(), opts :: any()) :: any() + @spec call(wsdl :: map(), operation :: String.t(), params :: any(), headers :: any(), opts :: any(), flags :: any()) :: any() def call(wsdl, operation, soap_headers_and_params, request_headers \\ [], opts \\ [], flags \\ []) def call(wsdl, operation, {soap_headers, params}, request_headers, opts, flags) do From 88e2a67116643debe090f08cf21e4d80e1f82562 Mon Sep 17 00:00:00 2001 From: Nicol Acosta Date: Thu, 25 Aug 2022 11:20:28 -0500 Subject: [PATCH 08/13] changed message on log --- lib/soap/request.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/soap/request.ex b/lib/soap/request.ex index a3bc417..82b8b29 100644 --- a/lib/soap/request.ex +++ b/lib/soap/request.ex @@ -20,7 +20,7 @@ defmodule Soap.Request do body = Params.build_body(wsdl, operation, params, soap_headers) if flags[:debug] do - Logger.debug("soap-call-debug", %{url: url, body: body, request_headers: request_headers, opts: opts}) + Logger.debug(%{log_id: "soap-call-debug", url: url, body: body, request_headers: request_headers, opts: opts}) end get_http_client().post(url, body, request_headers, opts) From 5babfadecd9fb03421767cb772ff2277c8ebe3b5 Mon Sep 17 00:00:00 2001 From: Nicol Acosta Date: Thu, 25 Aug 2022 17:32:23 -0500 Subject: [PATCH 09/13] added more headers --- lib/soap/request/headers.ex | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/soap/request/headers.ex b/lib/soap/request/headers.ex index 776d006..3ab6ae4 100644 --- a/lib/soap/request/headers.ex +++ b/lib/soap/request/headers.ex @@ -14,7 +14,7 @@ defmodule Soap.Request.Headers do def build(wsdl, operation, custom_headers) do wsdl |> extract_soap_action_by_operation(operation) - |> extract_headers(custom_headers) + |> extract_headers(custom_headers, wsdl) end @spec extract_soap_action_by_operation(map(), String.t()) :: String.t() @@ -22,12 +22,23 @@ defmodule Soap.Request.Headers do Enum.find(wsdl[:operations], fn x -> x[:name] == operation end)[:soap_action] end - @spec extract_headers(String.t(), list()) :: list() - defp extract_headers(soap_action, []), do: base_headers(soap_action) - defp extract_headers(_, custom_headers), do: custom_headers + @spec extract_headers(String.t(), list(), map()) :: list() + defp extract_headers(soap_action, [], wsdl), do: base_headers(soap_action, wsdl) + defp extract_headers(_, custom_headers, _), do: custom_headers - @spec base_headers(String.t()) :: list() - defp base_headers(soap_action) do - [{"SOAPAction", soap_action}, {"Content-Type", "text/xml;charset=utf-8"}] + @spec base_headers(String.t(), map()) :: list() + defp base_headers(soap_action, wsdl) do + uri = URI.parse(wsdl[:endpoint]) + [ +# {"Content-Type", "text/xml;charset=utf-8"}, +# {"User-Agent", "strong-soap/3.4.0"}, + {"Accept", "text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8"}, + {"Accept-Encoding", "none"}, + {"Accept-Charset", "utf-8"}, + {"GET", "#{uri.path} HTTP/1.1}"}, + {"Host", uri.host}, + {"SOAPAction", soap_action}, + {"referer", wsdl[:endpoint]}, + ] end end From f37e7a4f769f9f538acba382cfa7a6503f651d12 Mon Sep 17 00:00:00 2001 From: Nicol Acosta Date: Thu, 25 Aug 2022 17:36:42 -0500 Subject: [PATCH 10/13] added more headers --- lib/soap/request/headers.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/soap/request/headers.ex b/lib/soap/request/headers.ex index 3ab6ae4..edef45c 100644 --- a/lib/soap/request/headers.ex +++ b/lib/soap/request/headers.ex @@ -30,8 +30,8 @@ defmodule Soap.Request.Headers do defp base_headers(soap_action, wsdl) do uri = URI.parse(wsdl[:endpoint]) [ -# {"Content-Type", "text/xml;charset=utf-8"}, -# {"User-Agent", "strong-soap/3.4.0"}, + {"Content-Type", "text/xml;charset=utf-8"}, + {"User-Agent", "strong-soap/3.4.0"}, {"Accept", "text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8"}, {"Accept-Encoding", "none"}, {"Accept-Charset", "utf-8"}, From d3cd468e888a08cc0963097e888d8cef9c1456ab Mon Sep 17 00:00:00 2001 From: Nicol Acosta Date: Thu, 25 Aug 2022 17:51:02 -0500 Subject: [PATCH 11/13] added more headers --- lib/soap/request/headers.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/soap/request/headers.ex b/lib/soap/request/headers.ex index edef45c..29bb0be 100644 --- a/lib/soap/request/headers.ex +++ b/lib/soap/request/headers.ex @@ -30,11 +30,12 @@ defmodule Soap.Request.Headers do defp base_headers(soap_action, wsdl) do uri = URI.parse(wsdl[:endpoint]) [ - {"Content-Type", "text/xml;charset=utf-8"}, + {"Content-Type", "text/xml; charset=utf-8"}, {"User-Agent", "strong-soap/3.4.0"}, {"Accept", "text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8"}, {"Accept-Encoding", "none"}, {"Accept-Charset", "utf-8"}, + {"Connection", "close"}, {"GET", "#{uri.path} HTTP/1.1}"}, {"Host", uri.host}, {"SOAPAction", soap_action}, From c21df0dad58ac4af33a159b1e99c3020c76a80f5 Mon Sep 17 00:00:00 2001 From: Nicol Acosta Date: Thu, 25 Aug 2022 17:58:32 -0500 Subject: [PATCH 12/13] adapted headers --- lib/soap/request/headers.ex | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/soap/request/headers.ex b/lib/soap/request/headers.ex index 29bb0be..8404ff0 100644 --- a/lib/soap/request/headers.ex +++ b/lib/soap/request/headers.ex @@ -10,6 +10,11 @@ defmodule Soap.Request.Headers do """ + @content_types %{ + "1.1" => "text/xml; charset=utf-8", + "1.2" => "application/soap+xml; charset=utf-8", + } + @spec build(map(), String.t(), list()) :: list() def build(wsdl, operation, custom_headers) do wsdl @@ -30,7 +35,7 @@ defmodule Soap.Request.Headers do defp base_headers(soap_action, wsdl) do uri = URI.parse(wsdl[:endpoint]) [ - {"Content-Type", "text/xml; charset=utf-8"}, + {"Content-Type", Map.get(@content_types, wsdl[:soap_version])}, {"User-Agent", "strong-soap/3.4.0"}, {"Accept", "text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8"}, {"Accept-Encoding", "none"}, @@ -38,7 +43,7 @@ defmodule Soap.Request.Headers do {"Connection", "close"}, {"GET", "#{uri.path} HTTP/1.1}"}, {"Host", uri.host}, - {"SOAPAction", soap_action}, + {"SOAPAction", "\"#{soap_action}\""}, {"referer", wsdl[:endpoint]}, ] end From 9e013dc98354eae3155962ad2ed2ac6e273bc1c0 Mon Sep 17 00:00:00 2001 From: Nicol Acosta Date: Fri, 26 Aug 2022 13:03:19 -0500 Subject: [PATCH 13/13] more headers --- lib/soap/request.ex | 2 +- lib/soap/request/headers.ex | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/soap/request.ex b/lib/soap/request.ex index 82b8b29..c141ac8 100644 --- a/lib/soap/request.ex +++ b/lib/soap/request.ex @@ -16,8 +16,8 @@ defmodule Soap.Request do def call(wsdl, operation, {soap_headers, params}, request_headers, opts, flags) do url = get_url(wsdl, force_https?()) - request_headers = Headers.build(wsdl, operation, request_headers) body = Params.build_body(wsdl, operation, params, soap_headers) + request_headers = Headers.build(wsdl, operation, request_headers, body) if flags[:debug] do Logger.debug(%{log_id: "soap-call-debug", url: url, body: body, request_headers: request_headers, opts: opts}) diff --git a/lib/soap/request/headers.ex b/lib/soap/request/headers.ex index 8404ff0..a8cfe91 100644 --- a/lib/soap/request/headers.ex +++ b/lib/soap/request/headers.ex @@ -15,11 +15,11 @@ defmodule Soap.Request.Headers do "1.2" => "application/soap+xml; charset=utf-8", } - @spec build(map(), String.t(), list()) :: list() - def build(wsdl, operation, custom_headers) do + @spec build(map(), String.t(), list(), String.t()) :: list() + def build(wsdl, operation, custom_headers, body) do wsdl |> extract_soap_action_by_operation(operation) - |> extract_headers(custom_headers, wsdl) + |> extract_headers(custom_headers, wsdl, body) end @spec extract_soap_action_by_operation(map(), String.t()) :: String.t() @@ -27,14 +27,15 @@ defmodule Soap.Request.Headers do Enum.find(wsdl[:operations], fn x -> x[:name] == operation end)[:soap_action] end - @spec extract_headers(String.t(), list(), map()) :: list() - defp extract_headers(soap_action, [], wsdl), do: base_headers(soap_action, wsdl) - defp extract_headers(_, custom_headers, _), do: custom_headers + @spec extract_headers(String.t(), list(), map(), String.t()) :: list() + defp extract_headers(soap_action, [], wsdl, body), do: base_headers(soap_action, wsdl, body) + defp extract_headers(_, custom_headers, _, _), do: custom_headers - @spec base_headers(String.t(), map()) :: list() - defp base_headers(soap_action, wsdl) do + @spec base_headers(String.t(), map(), String.t()) :: list() + defp base_headers(soap_action, wsdl, body) do uri = URI.parse(wsdl[:endpoint]) [ + {"Content-Length", byte_size(body)}, {"Content-Type", Map.get(@content_types, wsdl[:soap_version])}, {"User-Agent", "strong-soap/3.4.0"}, {"Accept", "text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8"},