Class: Zapp::HTTPContext::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/zapp/http_context/context.rb

Overview

Context containing request and response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket:, logger: Zapp::Logger) ⇒ Context

Returns a new instance of Context.



12
13
14
15
16
17
18
19
20
# File 'lib/zapp/http_context/context.rb', line 12

def initialize(socket:, logger: Zapp::Logger)
  @socket = socket
  @req = Zapp::HTTPContext::Request.new(socket: socket)
  @res = Zapp::HTTPContext::Response.new(socket: socket)
rescue Puma::HttpParserError => e
  res.write(data: "Invalid HTTP request", status: 400, headers: {})
  logger.warn("Puma parser error: #{e}")
  logger.debug("HTTP request raw: #{context.req.raw}")
end

Instance Attribute Details

#reqObject (readonly)

Returns the value of attribute req.



10
11
12
# File 'lib/zapp/http_context/context.rb', line 10

def req
  @req
end

#resObject (readonly)

Returns the value of attribute res.



10
11
12
# File 'lib/zapp/http_context/context.rb', line 10

def res
  @res
end

#socketObject (readonly)

Returns the value of attribute socket.



10
11
12
# File 'lib/zapp/http_context/context.rb', line 10

def socket
  @socket
end

Instance Method Details

#client_closed?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/zapp/http_context/context.rb', line 26

def client_closed?
  req.data["HTTP_CONNECTION"] == "close"
end

#closeObject



22
23
24
# File 'lib/zapp/http_context/context.rb', line 22

def close
  @socket.close
end