Class: Shuck::Servlet

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/shuck/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(server, store, hostname) ⇒ Servlet

Returns a new instance of Servlet.



34
35
36
37
38
39
# File 'lib/shuck/server.rb', line 34

def initialize(server,store,hostname)
  super(server)
  @store = store
  @hostname = hostname
  @root_hostnames = [hostname,'localhost','s3.amazonaws.com','s3.localhost']
end

Instance Method Details

#do_DELETE(request, response) ⇒ Object



128
129
130
# File 'lib/shuck/server.rb', line 128

def do_DELETE(request,response)
  p request
end

#do_GET(request, response) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/shuck/server.rb', line 41

def do_GET(request, response)
  s_req = normalize_request(request)

  case s_req.type
  when 'LIST_BUCKETS'
    response.status = 200
    response['Content-Type'] = 'application/xml'
    buckets = @store.buckets
    response.body = XmlAdapter.buckets(buckets)
  when 'LS_BUCKET'
    bucket_obj = @store.get_bucket(s_req.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(s_req.bucket)
      response['Content-Type'] = "application/xml"
    end
  when 'GET_ACL'
    response.status = 200
    response.body = XmlAdapter.acl()
    response['Content-Type'] = 'application/xml'
  when 'GET'
    real_obj = @store.get_object(s_req.bucket,s_req.object,request)
    if !real_obj
      response.status = 404
      response.body = ""
      return
    end

    response.status = 200
    response['Content-Type'] = real_obj.content_type
    content_length = File::Stat.new(real_obj.io.path).size
    response['Etag'] = real_obj.md5
    response['Accept-Ranges'] = "bytes"

    # Added Range Query support
    if range = request.header["range"].first
      response.status = 206
      if range =~ /bytes=(\d*)-(\d*)/
        start = $1.to_i
        finish = $2.to_i
        finish_str = ""
        if finish == 0
          finish = content_length - 1
          finish_str = "#{finish}"
        else
          finish_str = finish.to_s
        end

        bytes_to_read = finish - start + 1
        response['Content-Range'] = "bytes #{start}-#{finish_str}/#{content_length}"
        real_obj.io.pos = start
        response.body = real_obj.io.read(bytes_to_read)
        return
      end
    end
    response['Content-Length'] = File::Stat.new(real_obj.io.path).size
    response.body = real_obj.io
  end
end

#do_POST(request, response) ⇒ Object



124
125
126
# File 'lib/shuck/server.rb', line 124

def do_POST(request,response)
  p request
end

#do_PUT(request, response) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/shuck/server.rb', line 105

def do_PUT(request,response)
  s_req = normalize_request(request)


  case s_req.type
  when Request::COPY
    @store.copy_object(s_req.src_bucket,s_req.src_object,s_req.bucket,s_req.object)
  when Request::STORE
    real_obj = @store.store_object(s_req.bucket,s_req.object,s_req.webrick_request)
    response['Etag'] = real_obj.md5
  when Request::CREATE_BUCKET
    @store.create_bucket(s_req.bucket)
  end

  response.status = 200
  response.body = ""
  response['Content-Type'] = "text/xml"
end