require 'webrick' require 'shuck/file_store' require 'shuck/xml_adapter' module Shuck class Servlet < WEBrick::HTTPServlet::AbstractServlet def initialize(server,store,hostname) super(server) @store = store @hostname = hostname end def do_GET(request, response) path = request.path path_len = path.size host = request["HOST"] path_style_request = true bucket = nil if host != @hostname bucket = host.split(".")[0] path_style_request = false end if path == "/" and path_style_request response.status = 200 response['Content-Type'] = 'text/xml' buckets = @store.buckets response.body = XmlAdapter.buckets(buckets) else if path_style_request elems = path[1,path_len].split("/") bucket = elems[0] else elems = path.split("/") end if elems.size == 1 bucket_obj = @store.get_bucket(bucket) if bucket_obj response.status = 200 response.body = XmlAdapter.bucket(bucket_obj) response['Content-Type'] = "application/xml" else response.status = 404 response.body = XmlAdapter.error_no_such_bucket(bucket) response['Content-Type'] = "application/xml" end else object = elems[1,elems.size].join('/') io = @store.get_object(bucket,object) if io response.status = 200 response['Content-Type'] = "application/octet-stream" response.body = io response.chunked = true else response.status = 404 response.body = "" end end end end def do_PUT(request,response) path = request.path path_len = path.size path_style_request = true host = request["Host"] bucket = nil path_style_request = true if host != @hostname bucket = host.split(".")[0] path_style_request = false end if path == "/" if bucket @store.create_bucket(bucket) else puts "Unsure what to do" end else if path_style_request elems = path[1,path_len].split("/") bucket = elems[0] else elems = path.split("/") end if path_style_request if elems.size == 1 @store.create_bucket(bucket) else object = elems[1,elems.size].join('/') @store.store_object(bucket,object,request) end else @store.store_object(bucket,path,request) end end response.status = 200 response.body = "" response['Content-Type'] = "text/xml" end def do_POST(request,response) puts "Put" p request end def do_DELETE(request,response) puts "Delete" p request end end class Server def initialize(port,store,hostname = "localhost") @port = port @store = store @hostname = hostname end def serve @server = WEBrick::HTTPServer.new(:Port => @port) @server.mount "/", Servlet, @store,@hostname trap "INT" do @server.shutdown end @server.start end def shutdown @server.shutdown end end end