Class: Linzer::Message

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/linzer/message.rb,
lib/linzer/message/field.rb,
lib/linzer/message/adapter.rb,
lib/linzer/message/wrapper.rb,
lib/linzer/message/field/parser.rb,
lib/linzer/message/adapter/abstract.rb,
lib/linzer/message/adapter/rack/common.rb,
lib/linzer/message/adapter/rack/request.rb,
lib/linzer/message/adapter/rack/response.rb,
lib/linzer/message/adapter/generic/request.rb,
lib/linzer/message/adapter/generic/response.rb,
lib/linzer/message/adapter/http_gem/request.rb,
lib/linzer/message/adapter/net_http/request.rb,
lib/linzer/message/adapter/http_gem/response.rb,
lib/linzer/message/adapter/net_http/response.rb

Overview

Wraps an HTTP request or response for signing and verification.

Message provides a unified interface for accessing HTTP message components regardless of the underlying HTTP library (Rack, Net::HTTP, http.rb, etc.). It handles the extraction of both regular header fields and derived components (like ‘@method`, `@path`, `@authority`).

Examples:

Wrapping a Rack request

request = Rack::Request.new(env)
message = Linzer::Message.new(request)
message.request?  # => true
message["@method"] # => "GET"

Wrapping a Net::HTTP request

request = Net::HTTP::Post.new(uri)
request["content-type"] = "application/json"
message = Linzer::Message.new(request)
message["content-type"]  # => "application/json"

Wrapping a response with an attached request

response = Net::HTTPOK.new("1.1", "200", "OK")
message = Linzer::Message.new(response, attached_request: request)
message["@status"]  # => 200
message['"content-type";req']  # => value from the attached request

See Also:

Defined Under Namespace

Modules: Adapter, Wrapper Classes: Field

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation, attached_request: nil) ⇒ Message

Creates a new Message wrapper.

Examples:

Basic usage

message = Linzer::Message.new(request)

Response with attached request (for ‘;req` parameter support)

message = Linzer::Message.new(response, attached_request: original_request)

Raises:

  • (Error)

    If the operation class is not supported and no adapter has been registered for it.



52
53
54
55
# File 'lib/linzer/message.rb', line 52

def initialize(operation, attached_request: nil)
  @adapter = Wrapper.wrap(operation, attached_request: attached_request)
  freeze
end

Class Method Details

.register_adapter(operation_class, adapter_class) ⇒ Object

Registers a custom adapter for an HTTP message class.

Use this to add support for HTTP libraries not built into Linzer. The adapter class must inherit from Linzer::Message::Adapter::Abstract and implement the required interface.

Examples:

Registering a custom adapter

class MyHttpRequest; end
class MyAdapter < Linzer::Message::Adapter::Abstract
  # ... implementation
end
Linzer::Message.register_adapter(MyHttpRequest, MyAdapter)

See Also:



134
135
136
# File 'lib/linzer/message.rb', line 134

def register_adapter(operation_class, adapter_class)
  Wrapper.register_adapter(operation_class, adapter_class)
end

Instance Method Details

#[](component) ⇒ String, ...

Retrieves a component value from the message.

Supports both regular header fields and derived components:

  • Header fields: ‘“content-type”`, `“date”`, `“x-custom-header”`

  • Derived components: ‘“@method”`, `“@path”`, `“@authority”`, `“@status”`

  • With parameters: ‘“content-type”;bs`, `“example-dict”;key=“a”`, `“date”;req`

Examples:

Accessing headers

message["content-type"]  # => "application/json"

Accessing derived components

message["@method"]  # => "POST"
message["@path"]    # => "/api/resource"

With parameters

message['"content-type";bs']  # => base64-encoded value


100
# File 'lib/linzer/message.rb', line 100

def_delegators :@adapter, :header, :field?, :[]

#attach!(signature) ⇒ Object

Attaches a signature to the underlying HTTP message.

Modifies the original HTTP message by adding the signature and signature-input headers from the signature.

Examples:

signature = Linzer.sign(key, message, components)
message.attach!(signature)


114
# File 'lib/linzer/message.rb', line 114

def_delegators :@adapter, :attach!

#attached_request?Boolean

Checks if this response message has an attached request.



68
# File 'lib/linzer/message.rb', line 68

def_delegators :@adapter, :request?, :response?, :attached_request?

#field?(component) ⇒ Boolean

Checks if a component exists in the message.



# File 'lib/linzer/message.rb', line 75


#header(name) ⇒ String?

Retrieves a header value by name.



# File 'lib/linzer/message.rb', line 70


#request?Boolean

Checks if this message wraps an HTTP request.



# File 'lib/linzer/message.rb', line 57


#response?Boolean

Checks if this message wraps an HTTP response.



# File 'lib/linzer/message.rb', line 61