Skip to content

Instantly share code, notes, and snippets.

@bigcurl
Created May 16, 2011 21:27
Show Gist options
  • Save bigcurl/975413 to your computer and use it in GitHub Desktop.
Save bigcurl/975413 to your computer and use it in GitHub Desktop.
A simple goliath API that returns the sha1 hash of an uploaded file.
#!/usr/bin/env ruby
# encoding: utf-8
require 'goliath'
require 'digest/sha1'
class ShaStream < Goliath::API
def on_headers(env, headers)
env['async-digest'] = Digest::SHA1.new # SHA1 or MD5
str = env['CONTENT_TYPE'].match(/multipart\/form-data.*boundary\=?([^"\;,]+)?/)
env['request-boundary'] = str[1] if str
end
def on_body(env, data)
data.gsub!(/.*#{env['request-boundary']}.*\r\n\r\n/m, '')
data.gsub!(/\r\n.*#{env['request-boundary']}--\r\n/m, '')
env['async-digest'] << data
end
def response(env)
[200, {}, ["works #{env['async-digest'].hexdigest}\n"]]
end
end
# run with ruby sha_stream.rb -sv -p 9000
# call with curl -F "file=@goliath/github.rb" "localhost:9000/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment