Module: MiddleSquid::Helpers
- Included in:
- Runner
- Defined in:
- lib/middle_squid/helpers.rb
Predefined Helpers collapse
-
#download_like(request, uri) ⇒ Array
Download a resource with the same headers and body as a rack request.
Instance Method Details
#download_like(request, uri) ⇒ Array
Note:
This method must be called inside an active fiber. Actions#intercept does it automatically.
Download a resource with the same headers and body as a rack request.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/middle_squid/helpers.rb', line 35 def download_like(request, uri) fiber = Fiber.current method = request.request_method.downcase.to_sym headers = {'Content-Type' => request.env['CONTENT_TYPE']} request.env. select {|k| k.start_with? 'HTTP_' }. each {|key, val| headers[key[5..-1]] = val } headers.sanitize_headers! = { :head => headers, :body => request.body.read, } http = EM::HttpRequest.new(uri.to_s).send method, http.callback { status = http.response_header.status headers = http.response_header body = http.response headers.sanitize_headers! fiber.resume [status, headers, body] } http.errback { fiber.resume [ 520, {'Content-Type' => 'text/plain'}, "[MiddleSquid] #{http.error}" ] } Fiber.yield end |