Last active
August 29, 2015 14:07
-
-
Save mateuszdw/6423d82128861b47fc18 to your computer and use it in GitHub Desktop.
goliath async upload validation CORS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Goliath | |
class Request | |
old_post_process = instance_method(:post_process) | |
define_method(:post_process) do |results| | |
status, headers, body = *results | |
# Include any data here | |
# All requests will pass here | |
if env['size-error'] | |
headers['Access-Control-Allow-Origin'] = '*' | |
if env[Goliath::Request::REQUEST_METHOD] == 'OPTIONS' | |
options_cors_headers = { | |
'Access-Control-Allow-Origin' => '*', | |
'Access-Control-Expose-Headers' => 'X-Error-Message,X-Error-Detail,X-RateLimit-Requests,X-RateLimit-MaxRequests', | |
'Access-Control-Max-Age' => '172800', | |
'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS', | |
'Access-Control-Allow-Headers' => 'X-Requested-With,Content-Type' | |
} | |
results = [200, headers.merge(options_cors_headers), ''] | |
end | |
end | |
old_post_process.bind(self).call(results) | |
end | |
end | |
end | |
class Upload < Goliath::API | |
include Goliath::Validation | |
def on_headers(env, headers) | |
env.logger.info 'received headers: ' + headers.inspect | |
env['async-headers'] = headers | |
end | |
def on_body(env, data) | |
(env['async-body'] ||= '') << data | |
size = env['async-body'].size | |
if size >= 100000 | |
env['size-error'] = true | |
raise Goliath::Validation::Error.new(422, | |
"Please send smaller file. Received size is #{size.inspect}") | |
end | |
end | |
def response(env) | |
[200, {},{}] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment