<commit_before>defmodule Annon.Plugins.Proxy.Adapters.HTTP do
  @moduledoc """
  HTTP(s) requests adapter that sends body as-is via HTTPPoison.
  """
  alias Annon.Plugin.UpstreamRequest
  alias Plug.Conn
  require Logger

  @stream_opts [
    length: :infinity,      # Read whole response
    read_length: 1_049_600, # in 1 Mb chunks
    read_timeout: 15_000    # with 15 seconds timeout between chunks
  ]

  @buffer_opts [
    connect_timeout: 30_000,
    recv_timeout: 30_000,
    timeout: 30_000
  ]

  def dispatch(%UpstreamRequest{} = upstream_request, %Conn{body_params: %Plug.Conn.Unfetched{}} = conn) do
    upstream_url = UpstreamRequest.to_upstream_url!(upstream_request)
    method = String.to_atom(conn.method)

    with {:ok, client_ref} <- :hackney.request(method, upstream_url, upstream_request.headers, :stream, []),
         {:ok, client_ref, conn} <- stream_request_body(client_ref, upstream_request, conn),
         %Conn{} = conn <- stream_response_body(client_ref, conn) do
      {:ok, conn}
    else
      {:error, term} ->
        Conn.send_resp(conn, 502, Annon.Helpers.Response.build_upstream_error(to_string(term)))
    end
  end

  def dispatch(%UpstreamRequest{} = upstream_request, %Conn{method: method} = conn) do
    upstream_url = UpstreamRequest.to_upstream_url!(upstream_request)
    method = String.to_atom(method)

    body =
      conn
      |> Map.get(:body_params)
      |> Poison.encode!()
<commit_msg>Do not violate http proxy adapter contract for responses with errors [patch]<commit_after>defmodule Annon.Plugins.Proxy.Adapters.HTTP do
  @moduledoc """
  HTTP(s) requests adapter that sends body as-is via HTTPPoison.
  """
  alias Annon.Plugin.UpstreamRequest
  alias Plug.Conn
  require Logger

  @stream_opts [
    length: :infinity,      # Read whole response
    read_length: 1_049_600, # in 1 Mb chunks
    read_timeout: 15_000    # with 15 seconds timeout between chunks
  ]

  @buffer_opts [
    connect_timeout: 30_000,
    recv_timeout: 30_000,
    timeout: 30_000
  ]

  def dispatch(%UpstreamRequest{} = upstream_request, %Conn{body_params: %Plug.Conn.Unfetched{}} = conn) do
    upstream_url = UpstreamRequest.to_upstream_url!(upstream_request)
    method = String.to_atom(conn.method)

    with {:ok, client_ref} <- :hackney.request(method, upstream_url, upstream_request.headers, :stream, []),
         {:ok, client_ref, conn} <- stream_request_body(client_ref, upstream_request, conn),
         %Conn{} = conn <- stream_response_body(client_ref, conn) do
      {:ok, conn}
    else
      {:error, term} ->
        {:ok, Conn.send_resp(conn, 502, Annon.Helpers.Response.build_upstream_error(to_string(term)))}
    end
  end

  def dispatch(%UpstreamRequest{} = upstream_request, %Conn{method: method} = conn) do
    upstream_url = UpstreamRequest.to_upstream_url!(upstream_request)
    method = String.to_atom(method)

    body =
      conn
      |> Map.get(:body_params)
      |> Poison.encode!()

{'fraction_non_alphanumeric': 0.11291413116970926, 'fraction_numerical': 0.02231237322515213, 'mean_word_length': 3.7344, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4545168', 'n_tokens_mistral': 1063, 'n_tokens_neox': 961, 'n_words': 212}