Class: Waxx::Res

Inherits:
Struct
  • Object
show all
Defined in:
lib/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 'lib/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 'lib/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 'lib/waxx/res.rb', line 38

def headers
  @headers
end

#headers_sentObject (readonly)

Returns the value of attribute headers_sent.



48
49
50
# File 'lib/waxx/res.rb', line 48

def headers_sent
  @headers_sent
end

#no_cookiesObject

Returns the value of attribute no_cookies

Returns:

  • (Object)

    the current value of no_cookies



38
39
40
# File 'lib/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 'lib/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 'lib/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 'lib/waxx/res.rb', line 38

def status
  @status
end

Instance Method Details

#<<(str) ⇒ Object

Send output to the client buffered until flush() or complete()



51
52
53
# File 'lib/waxx/res.rb', line 51

def << str
  out << str
end

#[](n, v) ⇒ Object



55
56
57
# File 'lib/waxx/res.rb', line 55

def [](n,v)
  headers[n]
end

#[]=(n, v) ⇒ Object



59
60
61
# File 'lib/waxx/res.rb', line 59

def []=(n,v)
  headers[n] = v
end

#as(ext) ⇒ Object



63
64
65
# File 'lib/waxx/res.rb', line 63

def as(ext)
  headers['content-type'] = Waxx::Http.ctype(ext)
end

#completeObject

Output the headers and the body



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/waxx/res.rb', line 99

def complete
  re = out.join
  headers["content-length"] = re.bytesize if headers['content-length'].nil?
  begin
    unless @headers_sent
      server.print head
      @headers_sent = true
    end
    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


121
122
123
124
125
126
127
128
129
130
# File 'lib/waxx/res.rb', line 121

def cookie(name: "", value: nil, domain: nil, expires: nil, path: "/", secure: true, http_only: false, same_site: "Lax")
  c = ["#{name}=#{Waxx::Http.escape(value.to_s)}"]
  c << "Path=#{path}"
  c << "SameSite=#{same_site}"
  c << "Expires=#{Time === expires ? expires.rfc2822 : expires}" if expires
  c << "Domain=#{domain}" if domain
  c << "Secure" if secure
  c << "HttpOnly" if http_only
  cookies << c.join("; ")
end

#flush(content = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/waxx/res.rb', line 85

def flush(content=nil)
  unless @headers_sent
    server.print head
    @headers_sent = true
  end
  if content.nil?
    server.print out.join
    out.clear
  else
    server.print content
  end
end

#headObject

Return the response headers



74
75
76
77
78
79
80
81
82
83
# File 'lib/waxx/res.rb', line 74

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



67
68
69
70
# File 'lib/waxx/res.rb', line 67

def location(uri)
  self.status = 302
  headers['Location'] = uri
end