Class: Waxx::Res
- Inherits:
-
Struct
- Object
- Struct
- Waxx::Res
- Defined in:
- waxx/res.rb
Overview
The Response Struct gets instanciated with each request (x.res)
“` x.res = value # Set a response header x.res.as(extention) # Set the Content-Type header based on extension x.res.redirect '/path' # Redirect the client with 302 / Location header x.res.location '/path' # Redirect the client with 302 / Location header x.res.cookie( # Set a cookie
name: "",
value: nil,
domain: nil,
expires: nil,
path: "/",
secure: true,
http_only: false,
same_site: "Lax"
) x << “ouput” # Append output to the response body x.res << “output” # Append output to the response body “`
Instance Attribute Summary collapse
-
#cookies ⇒ Object
Returns the value of attribute cookies.
-
#error ⇒ Object
Returns the value of attribute error.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#no_cookies ⇒ Object
Returns the value of attribute no_cookies.
-
#out ⇒ Object
Returns the value of attribute out.
-
#server ⇒ Object
Returns the value of attribute server.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
-
#<<(str) ⇒ Object
Send output to the client (may be buffered).
- #[](n, v) ⇒ Object
- #[]=(n, v) ⇒ Object
- #as(ext) ⇒ Object
-
#complete ⇒ Object
Output the headers and the body.
- #cookie(name: "", value: nil, domain: nil, expires: nil, path: "/", secure: true, http_only: false, same_site: "Lax") ⇒ Object
-
#head ⇒ Object
Return the response headers.
- #location(uri) ⇒ Object (also: #redirect)
Instance Attribute Details
#cookies ⇒ Object
Returns the value of attribute cookies
38 39 40 |
# File 'waxx/res.rb', line 38 def @cookies end |
#error ⇒ Object
Returns the value of attribute error
38 39 40 |
# File 'waxx/res.rb', line 38 def error @error end |
#headers ⇒ Object
Returns the value of attribute headers
38 39 40 |
# File 'waxx/res.rb', line 38 def headers @headers end |
#no_cookies ⇒ Object
Returns the value of attribute no_cookies
38 39 40 |
# File 'waxx/res.rb', line 38 def @no_cookies end |
#out ⇒ Object
Returns the value of attribute out
38 39 40 |
# File 'waxx/res.rb', line 38 def out @out end |
#server ⇒ Object
Returns the value of attribute server
38 39 40 |
# File 'waxx/res.rb', line 38 def server @server end |
#status ⇒ Object
Returns the value of attribute status
38 39 40 |
# File 'waxx/res.rb', line 38 def status @status end |
Instance Method Details
#<<(str) ⇒ Object
Send output to the client (may be buffered)
49 50 51 |
# File 'waxx/res.rb', line 49 def << str out << str end |
#[](n, v) ⇒ Object
53 54 55 |
# File 'waxx/res.rb', line 53 def [](n,v) headers[n] end |
#[]=(n, v) ⇒ Object
57 58 59 |
# File 'waxx/res.rb', line 57 def []=(n,v) headers[n] = v end |
#as(ext) ⇒ Object
61 62 63 |
# File 'waxx/res.rb', line 61 def as(ext) headers['Content-Type'] = Waxx::Http.ctype(ext) end |
#complete ⇒ Object
Output the headers and the body
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'waxx/res.rb', line 84 def complete re = out.join headers["Content-Length"] = re.bytesize begin server.print head server.print re # Connection reset by peer rescue Errno::ECONNRESET => e Waxx.debug(e.class) Waxx.debug(e) Waxx.debug(e.backtrace.join("\n")) # Broken pipe rescue Errno::EPIPE => e Waxx.debug(e.class) Waxx.debug(e) Waxx.debug(e.backtrace.join("\n")) end end |
#cookie(name: "", value: nil, domain: nil, expires: nil, path: "/", secure: true, http_only: false, same_site: "Lax") ⇒ Object
103 104 105 106 |
# File 'waxx/res.rb', line 103 def (name:"", value:nil, domain:nil, expires:nil, path:"/", secure:true, http_only: false, same_site: "Lax") expires = expires.nil? ? "" : "expires=#{Time === expires ? expires.rfc2822 : expires}; " << "#{name}=#{Waxx::Http.escape(value.to_s)}; #{expires}#{";domain=#{domain}" if domain}; path=#{path}#{"; secure" if secure}#{"; HttpOnly" if http_only}; SameSite=#{same_site}" end |
#head ⇒ Object
Return the response headers
72 73 74 75 76 77 78 79 80 81 |
# File 'waxx/res.rb', line 72 def head [ "HTTP/1.1 #{status} #{Waxx::Http::Status[status.to_s]}", headers.map{|n,v| "#{n}: #{v}"}, (.map{|c| "Set-Cookie: #{c}" } unless ), "\r\n" ].flatten.join("\r\n") end |
#location(uri) ⇒ Object Also known as: redirect
65 66 67 68 |
# File 'waxx/res.rb', line 65 def location(uri) self.status = 302 headers['Location'] = uri end |