Passby.Conn (passby v0.1.1)

Copy Markdown View Source

Represents an HTTP connection in Passby.

This struct and its helper functions provide a familiar, lightweight API compatible with Plug.Conn and Bypass handler functions without requiring Plug as a dependency.

Summary

Functions

Gets the values of the specified request header.

Sets or adds a response header.

Sets the response status and body without immediately transmitting it.

Sends the response configured in the connection struct over the socket.

Sends the response with the given status and body over the underlying socket.

Types

t()

@type t() :: %Passby.Conn{
  adapter: {module(), any()},
  method: String.t(),
  path_info: [String.t()],
  query_string: String.t(),
  req_body: binary(),
  req_headers: [{String.t(), String.t()}],
  request_path: String.t(),
  resp_body: binary() | nil,
  resp_headers: [{String.t(), String.t()}],
  state: :unset | :set | :sent,
  status: integer() | nil
}

Functions

get_req_header(conn, header_name)

@spec get_req_header(t(), String.t()) :: [String.t()]

Gets the values of the specified request header.

Header names are case-insensitive. Returns a list of string values matching the header name (or an empty list if not present).

Examples

iex> conn = %Passby.Conn{req_headers: [{"soapaction", "sessionOpen"}]}
iex> Passby.Conn.get_req_header(conn, "SoapAction")
["sessionOpen"]

put_resp_header(conn, key, value)

@spec put_resp_header(t(), String.t(), String.t()) :: t()

Sets or adds a response header.

Examples

iex> conn = %Passby.Conn{}
iex> conn = Passby.Conn.put_resp_header(conn, "content-type", "application/json")
iex> conn.resp_headers
[{"content-type", "application/json"}]

resp(conn, status, body)

@spec resp(t(), integer(), binary()) :: t()

Sets the response status and body without immediately transmitting it.

Examples

iex> conn = %Passby.Conn{}
iex> conn = Passby.Conn.resp(conn, 200, "Hello World")
iex> conn.status
200
iex> conn.resp_body
"Hello World"

send_resp(conn)

@spec send_resp(t()) :: t()

Sends the response configured in the connection struct over the socket.

send_resp(conn, status, body)

@spec send_resp(t(), integer(), binary()) :: t()

Sends the response with the given status and body over the underlying socket.

If no status and body are passed, it sends the response configured in the struct.