Class: Waxx::Res

Inherits:
Struct
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#cookiesObject

Returns the value of attribute cookies

Returns:

  • (Object)

    the current value of cookies



38
39
40
# File 'waxx/res.rb', line 38

def cookies
  @cookies
end

#errorObject

Returns the value of attribute error

Returns:

  • (Object)

    the current value of error



38
39
40
# File 'waxx/res.rb', line 38

def error
  @error
end

#headersObject

Returns the value of attribute headers

Returns:

  • (Object)

    the current value of headers



38
39
40
# File 'waxx/res.rb', line 38

def headers
  @headers
end

#no_cookiesObject

Returns the value of attribute no_cookies

Returns:

  • (Object)

    the current value of no_cookies



38
39
40
# File 'waxx/res.rb', line 38

def no_cookies
  @no_cookies
end

#outObject

Returns the value of attribute out

Returns:

  • (Object)

    the current value of out



38
39
40
# File 'waxx/res.rb', line 38

def out
  @out
end

#serverObject

Returns the value of attribute server

Returns:

  • (Object)

    the current value of server



38
39
40
# File 'waxx/res.rb', line 38

def server
  @server
end

#statusObject

Returns the value of attribute status

Returns:

  • (Object)

    the current value of 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

#completeObject

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


103
104
105
106
# File 'waxx/res.rb', line 103

def cookie(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}; "
  cookies << "#{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

#headObject

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}"},
    (cookies.map{|c| 
      "Set-Cookie: #{c}"
    } unless no_cookies),
    "\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