Class: FakeS3::Servlet

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

Instance Method Summary collapse

Constructor Details

#initialize(server, store, hostname) ⇒ Servlet

Returns a new instance of Servlet.



42
43
44
45
46
47
# File 'lib/fakes3/server.rb', line 42

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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/fakes3/server.rb', line 158

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

  case s_req.type
  when Request::DELETE_OBJECT
    bucket_obj = @store.get_bucket(s_req.bucket)
    @store.delete_object(bucket_obj,s_req.object,s_req.webrick_request)
  when Request::DELETE_BUCKET
    @store.delete_bucket(s_req.bucket)
  end

  response.status = 204
  response.body = ""
end

#do_GET(request, response) ⇒ Object



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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fakes3/server.rb', line 49

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['Content-Type'] = "application/xml"
      query = {
        :marker => s_req.query["marker"] ? s_req.query["marker"].to_s : nil,
        :prefix => s_req.query["prefix"] ? s_req.query["prefix"].to_s : nil,
        :max_keys => s_req.query["max_keys"] ? s_req.query["max_keys"].to_s : nil,
        :delimiter => s_req.query["delimiter"] ? s_req.query["delimiter"].to_s : nil
      }
      bq = bucket_obj.query_for_range(query)
      response.body = XmlAdapter.bucket_query(bq)
    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
    stat = File::Stat.new(real_obj.io.path)

    response['Last-Modified'] = stat.mtime.iso8601()
    response['Etag'] = "\"#{real_obj.md5}\""
    response['Accept-Ranges'] = "bytes"
    response['Last-Ranges'] = "bytes"

    content_length = stat.size

    # 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['Last-Modified'] = real_obj.modified_date
    if s_req.http_verb == 'HEAD'
      response.body = ""
    else
      response.body = real_obj.io
    end
  end
end

#do_POST(request, response) ⇒ Object

Posts aren’t supported yet



155
156
# File 'lib/fakes3/server.rb', line 155

def do_POST(request,response)
end

#do_PUT(request, response) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/fakes3/server.rb', line 130

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
    bucket_obj = @store.get_bucket(s_req.bucket)
    if !bucket_obj
      # Lazily create a bucket.  TODO fix this to return the proper error
      bucket_obj = @store.create_bucket(s_req.bucket)
    end

    real_obj = @store.store_object(bucket_obj,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