Class: Strobe::Middleware::Proxy::ProxyRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/strobe/middleware/proxy.rb

Defined Under Namespace

Classes: DeferrableBody

Constant Summary collapse

KEEP =
[ 'CONTENT_LENGTH', 'CONTENT_TYPE' ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, url, opts = {}) ⇒ ProxyRequest

Returns a new instance of ProxyRequest.


167
168
169
170
171
172
173
174
175
176
177
# File 'lib/strobe/middleware/proxy.rb', line 167

def initialize(env, url, opts = {})
  @env  = env
  @url  = url
  @opts = opts

  # STATE options
  @http      = nil
  @body      = nil
  @redirect  = false
  @responded = false
end

Instance Attribute Details

#response_headersObject (readonly)

Returns the value of attribute response_headers


165
166
167
# File 'lib/strobe/middleware/proxy.rb', line 165

def response_headers
  @response_headers
end

#urlObject (readonly)

Returns the value of attribute url


165
166
167
# File 'lib/strobe/middleware/proxy.rb', line 165

def url
  @url
end

Class Method Details

.x_strobe_proxy(url, env, opts) ⇒ Object


128
129
130
131
132
133
134
# File 'lib/strobe/middleware/proxy.rb', line 128

def self.x_strobe_proxy(url, env, opts)
  request = ProxyRequest.new(env, url, opts)

  request.handle!

  throw :async
end

Instance Method Details

#block_cookies?Boolean

Returns:

  • (Boolean)

358
359
360
# File 'lib/strobe/middleware/proxy.rb', line 358

def block_cookies?
  @opts[:block_cookies]
end

#block_headersObject


362
363
364
# File 'lib/strobe/middleware/proxy.rb', line 362

def block_headers
  @opts[:block_headers] || []
end

#chunked_response_body?Boolean

Returns:

  • (Boolean)

299
300
301
302
# File 'lib/strobe/middleware/proxy.rb', line 299

def chunked_response_body?
  has_response_body? && response_headers['Transfer-Encoding'] &&
    response_headers['Transfer-Encoding'].downcase == 'chunked'
end

#extra_headersObject


366
367
368
# File 'lib/strobe/middleware/proxy.rb', line 366

def extra_headers
  @opts[:extra_headers] || {}
end

#handle!Object


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/strobe/middleware/proxy.rb', line 179

def handle!
  EM.next_tick do
    begin
      conn = EM::HttpRequest.new(url, {:ssl => { :verify_peer => false }})

      @http = case request_method
      when 'GET'    then conn.get    request_options
      when 'POST'   then conn.post   request_options
      when 'PUT'    then conn.put    request_options
      when 'DELETE' then conn.delete request_options
      when 'HEAD'   then conn.head   request_options
      else raise "Unknown HTTP method '#{request_method}'"
      end

      @http.headers  { |hdrs| handle_headers(hdrs) }
      @http.stream   { |data| handle_chunk(data)   }
      @http.callback { handle_done  }
      @http.errback  { handle_error }
    end
  end
end

#handle_chunk(chunk) ⇒ Object


265
266
267
268
# File 'lib/strobe/middleware/proxy.rb', line 265

def handle_chunk(chunk)
  return if redirect?
  @body.call(chunk) if @body
end

#handle_cookies(value) ⇒ Object


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/strobe/middleware/proxy.rb', line 231

def handle_cookies(value)
  cookies = []
  uri = URI.parse(url)

  case value
  when Array then value.each { |c| cookies << c }
  when Hash  then value.each { |_, c| cookies << c }
  else            cookies << value
  end

  result = cookies.flatten.join(",").split(/,(?=[^;,]*=)|,$/).map do |cookie|
    parts = cookie.split(/;+/).map!(&:strip)

    # delete domain and extract path from the cookie
    parts.delete_if { |p| p.start_with?("domain=") }
    path_part = parts.find { |p| p.start_with?("path=") }.tap { |p| parts.delete(p) }

    if (uri.scheme == "http" && uri.port != 80) || (uri.scheme == "https" && uri.port != 443)
      domain = "#{uri.host}:#{uri.port}"
    else
      domain = uri.host
    end

    path = path_part.to_s.split("=", 2)[1] || "/"

    # add a new path
    parts << "path=/_strobe/proxy/#{domain.sub(/^[.]/, "")}#{path.sub(/\/$/, "")}"

    parts.join("; ")
  end

  result.join(", ")
end

#handle_doneObject


270
271
272
273
274
275
# File 'lib/strobe/middleware/proxy.rb', line 270

def handle_done
  if has_response_body? && @body
    @body.call ['']
    @body.succeed
  end
end

#handle_errorObject


277
278
279
280
281
282
283
# File 'lib/strobe/middleware/proxy.rb', line 277

def handle_error
  unless @responded
    status  = @http.response_header.status
    headers = { "Connection" => "close", "Content-Length" => "0" }
    respond(status, headers, "")
  end
end

#handle_headers(hdrs) ⇒ Object


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/strobe/middleware/proxy.rb', line 201

def handle_headers(hdrs)
  headers = {}

  hdrs.each do |key, value|
    key = headerize(key)
    if key == "Set-Cookie"
      headers["Set-Cookie"] = handle_cookies(value)
    else
      headers[key] = value
    end
  end

  # Let's simplify things by always chunking the response
  # Also, responses will never be compressed
  headers.delete("Content-Length")
  headers.delete("Content-Encoding")
  headers['Transfer-Encoding'] = 'chunked'

  @redirect = headers['Location'] && @http.req.follow_redirect?

  unless @redirect
    h = {}
    headers.each { |k,v| h[k] = v if !block_headers.include?(k.downcase) }
    headers = h.merge(extra_headers)
    @response_headers = ::Rack::Utils::HeaderHash.new(headers)
    @body = DeferrableBody.new(self) if has_response_body?
    respond(@http.response_header.status, @response_headers)
  end
end

#has_request_body?Boolean

Returns:

  • (Boolean)

339
340
341
# File 'lib/strobe/middleware/proxy.rb', line 339

def has_request_body?
  @env['CONTENT_LENGTH'] || @env['HTTP_TRANSFER_ENCODING']
end

#has_response_body?Boolean

Returns:

  • (Boolean)

294
295
296
297
# File 'lib/strobe/middleware/proxy.rb', line 294

def has_response_body?
  status = @http.response_header.status
  status >= 200 && status != 204 && status != 304
end

#headerize(str) ⇒ Object


353
354
355
356
# File 'lib/strobe/middleware/proxy.rb', line 353

def headerize(str)
  parts = str.gsub(/^HTTP_/, '').split('_')
  parts.map! { |p| p.downcase.capitalize }.join('-')
end

#redirect?Boolean

Returns:

  • (Boolean)

290
291
292
# File 'lib/strobe/middleware/proxy.rb', line 290

def redirect?
  @redirect
end

#request_bodyObject


327
328
329
330
331
332
333
# File 'lib/strobe/middleware/proxy.rb', line 327

def request_body
  return unless has_request_body?
  return @request_body if @request_body
  @request_body = @env['rack.input'].read
  @env['rack.input'].rewind
  @request_body
end

#request_headersObject


308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/strobe/middleware/proxy.rb', line 308

def request_headers
  return @request_headers if @request_headers
  @request_headers = {}
  @env.each do |key, value|
    next unless key.is_a?(String)
    next if     key =~ /^HTTP_(HOST|VERSION|X_STROBE_PROXY_PROTOCOL)$/i
    next unless key =~ /^HTTP_/ || KEEP.include?(key)

    key = headerize(key)
    @request_headers[key] = value
  end

  if requires_request_body? && !has_request_body?
    @request_headers['Content-Length'] = '0'
  end

  @request_headers
end

#request_methodObject


304
305
306
# File 'lib/strobe/middleware/proxy.rb', line 304

def request_method
  @env['REQUEST_METHOD'].upcase
end

#request_optionsObject


343
344
345
346
347
348
349
350
351
# File 'lib/strobe/middleware/proxy.rb', line 343

def request_options
  return @request_options if @request_options
  @request_options = {}
  @request_options[:head]      = request_headers
  @request_options[:body]      = request_body if has_request_body?
  @request_options[:timeout]   = 60
  @request_options[:redirects] = 10
  @request_options
end

#requires_request_body?Boolean

Returns:

  • (Boolean)

335
336
337
# File 'lib/strobe/middleware/proxy.rb', line 335

def requires_request_body?
  ['POST', 'PUT'].include?(@env['REQUEST_METHOD'])
end

#respond(status, hdrs, body = @body) ⇒ Object


285
286
287
288
# File 'lib/strobe/middleware/proxy.rb', line 285

def respond(status, hdrs, body = @body)
  @responded = true
  @env['async.callback'].call([status, hdrs, body || ['']])
end