data Bezel::Conn

A connection: one request and the response being made for it, flowing through the plugs of an endpoint (Bezel::Plug). A Conn is a value: every change makes a new one (with, or the methods below), which a plug returns. A plug that answers (send_resp and friends) and stops the rest of the pipeline also calls halt.

Not Shareable: the request's body is read from its connection.

conn = Bezel::Conn.build(HTTP::Server::Request.get("/users?page=2"))
conn.params.int("page")                     # => 2
conn = conn.put_resp_header("x-frame-options", "DENY").text("hi")
conn.status                                 # => 200
conn.to_response.headers["x-frame-options"] # => "DENY"

src/conn.qz:82

Class methods

def self.build(req: HTTP::Server::Request, app: Bezel::App = …) -> Bezel::Conn

A connection for req in app, its parameters those of the query (Bezel::BadRequest when it does not decode).

src/conn.qz:120

def self.bare(req: HTTP::Server::Request, app: Bezel::App) -> Bezel::Conn

A connection for req in app, without parameters.

src/conn.qz:125

def self.status_of(error: Exception) -> Int

The status an exception is answered with: an HTTP::Error's own, else 500.

src/conn.qz:195

Methods

def request -> HTTP::Server::Request

The request, as the plugs left it (Plug::MethodOverride changes its method).

src/conn.qz:85

def params -> Bezel::Params

The parameters: query, body (once Plug::Parsers ran), path (once the router matched), merged.

src/conn.qz:88

def body_params -> Bezel::Params

The body's parameters alone (Bezel::Plug::Parsers).

src/conn.qz:90

def assigns -> Hash[Symbol, Object]

Values the plugs and the action pass on (the current user, say).

src/conn.qz:92

def session -> Bezel::Session

The session (empty without the session plug).

src/conn.qz:94

def flash -> Bezel::Flash

The flash.

src/conn.qz:96

def status -> Int

The response's status (200 until set).

src/conn.qz:98

def resp_headers -> HTTP::Headers

The response's header fields.

src/conn.qz:100

def resp_body -> HTTP::Content?

The response's body, or nil while none is set.

src/conn.qz:102

def halted -> Bool

Whether a plug stopped the pipeline: the plugs after it do not run.

src/conn.qz:104

def request_id -> String?

The request's id (Bezel::Plug::RequestId), or nil.

src/conn.qz:106

def app -> Bezel::App

The application.

src/conn.qz:108

def started -> Duration

When the endpoint took the request (Time.monotonic).

src/conn.qz:110

def error -> StandardError?

The exception a plug raised, which stopped the pipeline (Plug.run): the endpoint answers it with Bezel::ErrorPage.

src/conn.qz:113

def format -> Symbol?

The response's format Plug::Accepts chose (:html, :json), or nil.

src/conn.qz:116

def to_h -> Hash[Symbol, Object]

The fields as a Hash, by name.

src/conn.qz:120

def with(*, request: HTTP::Server::Request = …, params: Bezel::Params = …, body_params: Bezel::Params = …, assigns: Hash[Symbol, Object] = …, session: Bezel::Session = …, flash: Bezel::Flash = …, status: Int = …, resp_headers: HTTP::Headers = …, resp_body: HTTP::Content? = …, halted: Bool = …, request_id: String? = …, app: Bezel::App = …, started: Duration = …, error: StandardError? = …, format: Symbol? = …) -> Bezel::Conn

A copy with the fields given replaced.

src/conn.qz:120

def method -> String

The request's method ("GET").

src/conn.qz:131

def path -> String

The request's path, percent-decoded ("/users/1").

src/conn.qz:134

def header(name: String) -> String?

The request's header name, or nil.

src/conn.qz:137

def config -> Bezel::Config

The configuration.

src/conn.qz:140

def csrf_token -> String?

The CSRF token forms of this request carry, or nil: the session's CSRF secret (Plug::CSRF makes one), masked afresh at each call, so that it follows a renew or drop in this request; else the :csrf_token assign.

src/conn.qz:146

def url(path: String) -> String

path (a route helper's) as a URL of this request's scheme and host: Conn#url(Router.user_path(id: 1)). The host is the request's Host header (localhost without one), which the proxy in front sets: behind none, a client chooses it, so a URL meant for elsewhere (an email's link) should use a host of the configuration's instead.

A Host header that is not a host and an optional port (letters, digits, ., -, :, [ and ]) is Bezel::BadRequest, so that it cannot bring a path or a query of its own.

src/conn.qz:162

def repo -> Rotor::Repo

A repository over the application's database pool: Bezel::ConfigError when the application has none (no DATABASE_URL). Cheap: the pool is the application's, the repository a view of it.

src/conn.qz:172

def halted? -> Bool

Whether a plug stopped the pipeline.

src/conn.qz:179

def sent? -> Bool

Whether a response's body is set.

src/conn.qz:182

def final_status -> Int

The status the client gets: the error's (an HTTP::Error's own, 500 for another exception) when a plug raised, else 404 when no plug answered, else status.

src/conn.qz:187

def halt -> Bezel::Conn

The connection with the pipeline stopped after this plug.

src/conn.qz:198

def assign(key: Symbol, value: Object) -> Bezel::Conn

The connection with value assigned to key.

src/conn.qz:201

def assign?(key: Symbol) -> Object?

The value assigned to key, or nil (conn.assign?(:user).as?(User)).

src/conn.qz:204

def assign(key: Symbol) -> Object

The value assigned to key: KeyError when none is.

src/conn.qz:207

def put_status(status: Int) -> Bezel::Conn

The connection with the status status.

src/conn.qz:212

def put_resp_header(name: String, value: String) -> Bezel::Conn

The connection with the response header name set to value (replacing any value it had).

src/conn.qz:216

def add_resp_header(name: String, value: String) -> Bezel::Conn

The connection with the response header name added (Set-Cookie).

src/conn.qz:219

def delete_resp_header(name: String) -> Bezel::Conn

The connection without the response header name.

src/conn.qz:222

def put_session(session: Bezel::Session) -> Bezel::Conn

The connection with the session session.

src/conn.qz:225

def put_session(key: String, value: String) -> Bezel::Conn

The connection with the session's key set to value.

src/conn.qz:228

def put_flash(kind: Symbol, message: String) -> Bezel::Conn

The connection with message in the flash for the next request.

src/conn.qz:231

def merge_params(more: Bezel::Params) -> Bezel::Conn

The connection with params merged over its parameters.

src/conn.qz:234

def send_resp(status: Int, body: HTTP::Content, content_type: String? = …) -> Bezel::Conn

The connection answered with status and body (its content type content_type unless one is set), and halted.

src/conn.qz:238

def text(body: String, status: Int = …) -> Bezel::Conn

The connection answered with the text body (text/plain), and halted.

src/conn.qz:246

def html(page: Bezel::Html, status: Int = …) -> Bezel::Conn

The connection answered with the page page (text/html), and halted.

src/conn.qz:252

def html(body: String, status: Int = …) -> Bezel::Conn

The connection answered with the HTML body (text/html), and halted.

src/conn.qz:256

def json(value: JSON::Value, status: Int = …) -> Bezel::Conn

The connection answered with value as JSON (application/json), and halted.

src/conn.qz:262

def redirect(to: String, status: Int = …) -> Bezel::Conn

The connection answered with a redirect to to (302 Found, or status), and halted. A to that is a full URL must be one the application chose: redirecting to a URL from the request is an open redirect.

src/conn.qz:270

def to_response -> HTTP::Server::Response

The response: the status, the headers and the body (empty when none is set).

src/conn.qz:276