Class: UzuUzu::Response

Inherits:
Rack::Response
  • Object
show all
Includes:
Helper::Controller
Defined in:
lib/uzuuzu-core/response.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper::Controller

#action, #application, #controller, #dm, #h, #helper, #instance_variable_map, #logger, #query, #query_string, #request, #response, #route, #s3, #session, #u

Class Method Details

.currentObject



16
17
18
# File 'lib/uzuuzu-core/response.rb', line 16

def self.current
  Thread.current[:response]
end

Instance Method Details

#etag(value, kind = :strong) ⇒ Object

Raises:

  • (::TypeError)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/uzuuzu-core/response.rb', line 184

def etag(value, kind=:strong)
  raise ::TypeError, ":strong or :weak expected" if ![:strong,:weak].include?(kind)
  value = '"%s"' % value
  value = 'W/' + value if kind == :weak
  @header['ETag'] = value

  # Conditional GET check
  if etags = request.env['HTTP_IF_NONE_MATCH']
    etags = etags.split(/\s*,\s*/)
    if etags.include?(value) || etags.include?('*')
      @status = 304
      throw :finish
    end
  end
end

#last_modified(time) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/uzuuzu-core/response.rb', line 167

def last_modified(time)
  return unless time
  if time.respond_to?(:to_time)
      time = time.to_time
  else
      time = ::Time.parse(time.to_s)
  end
  @header['Last-Modified'] = time.httpdate
  if ::Time.httpdate(request.env['HTTP_IF_MODIFIED_SINCE']).to_i >= time.to_i
    @status = 304
    throw :finish
  end
end

#mime_type(mime) ⇒ Object



33
34
35
# File 'lib/uzuuzu-core/response.rb', line 33

def mime_type(mime)
  @header["Content-Type"] = rack_mime_type(mime) || mime if mime
end

#not_found(code = 404, options = {}) ⇒ Object



75
76
77
# File 'lib/uzuuzu-core/response.rb', line 75

def not_found(code=404, options={})
  server_error(code, options)
end

#rack_mime_type(type, value = nil) ⇒ Object



23
24
25
26
27
28
# File 'lib/uzuuzu-core/response.rb', line 23

def rack_mime_type(type, value=nil)
  return type if type.nil? || type.to_s.include?('/')
  type = ".#{type}" unless type.to_s[0] == ?.
  return Rack::Mime.mime_type(type, nil) if value.nil?
  Rack::Mime::MIME_TYPES[type] = value
end

#redirect(uri, options = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/uzuuzu-core/response.rb', line 82

def redirect(uri, options={})
  if not uri =~ /^https?:\/\//
    abs_uri = "#{request.scheme}://#{request.host}"

    if request.scheme == 'https' && request.port != 443 ||
    request.scheme == 'http' && request.port != 80
      abs_uri << ":#{request.port}"
    end
    uri = (abs_uri << uri)
  end
  uri << query_string(options)
  super(uri)
  throw :finish
end

#respond(string, mime = nil, status = 200) ⇒ Object



40
41
42
43
44
45
# File 'lib/uzuuzu-core/response.rb', line 40

def respond(string, mime=nil, status=200)
  @status = 200
  mime_type(mime)
  write string
  throw :finish
end

#respond_toObject



50
51
52
53
54
55
# File 'lib/uzuuzu-core/response.rb', line 50

def respond_to
  return unless block_given?
  @status = 200
  contents = yield(request.wish)
  respond(contents, request.wish) if contents and successful?
end

#reverse_proxy(url, opts = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/uzuuzu-core/response.rb', line 100

def reverse_proxy(url, opts={})
  uri = URI.parse(url)
  headers = opts['headers'] if opts['headers']
  headers ||= opts[:headers] if opts[:headers]
  headers ||= Rack::Utils::HeaderHash.new
  request.env.each do |key, value|
    if key =~ /HTTP_(.*)/
      headers[$1] = value
    end
  end
  headers['HOST'] = uri.host unless (opts[:host] || opts['host'])
  Net::HTTP.version_1_2
  http = Net::HTTP.new( uri.host, uri.port )
  http.read_timeout = opts[:timeout] if opts[:timeout]
  http.read_timeout = opts['timeout'] if opts['timeout']
  if uri.scheme == 'https' && (opts[:ssl] || opts['ssl'])
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  else
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  http.start do |http|
    m = request.request_method
    case m
    when 'GET', 'HEAD', 'DELETE', 'OPTIONS', 'TRACE'
      req = Net::HTTP.const_get(m.capitalize).new("#{uri.path}?#{uri.query}", headers)
      req.basic_auth opts[:username], opts[:password] if opts[:username] and opts[:password]
      req.basic_auth opts['username'], opts['password'] if opts['username'] and opts['password']
    when 'PUT', 'POST'
      req = Net::HTTP.const_get(m.capitalize).new("#{uri.path}?#{uri.query}", headers)
      req.basic_auth opts[:username], opts[:password] if opts[:username] and opts[:password]
      req.basic_auth opts['username'], opts['password'] if opts['username'] and opts['password']

      if request.body.respond_to?(:read) && request.body.respond_to?(:rewind)
        body = request.body.read
        req.content_length = body.size
        request.body.rewind
      else
        req.content_length = request.body.size
      end

      req.content_type = request.content_type unless request.content_type.nil?
      req.body_stream = request.body
    else
      raise "method not supported: #{m}"
    end # case m
    
    body = ''
    res = http.request(req) do |res|
      res.read_body do |segment|
        body << segment
      end
    end
    response_headers = Rack::Utils::HeaderHash.new(res.to_hash)
    response_headers.delete('status')
    response_headers.delete('transfer-encoding')
    response_headers.each do |key, value|
        response.header[key] = value
    end
    response.status = 200
    response.body = [body]
  end # http
  
  throw :finish
end

#send_binaryObject



61
62
63
# File 'lib/uzuuzu-core/response.rb', line 61

def send_binary()
  
end

#send_fileObject



57
58
59
# File 'lib/uzuuzu-core/response.rb', line 57

def send_file()
  
end

#server_error(code = 500, options = {}) ⇒ Object



68
69
70
# File 'lib/uzuuzu-core/response.rb', line 68

def server_error(code=500, options={})
  redirect "/error/#{code}"
end