lib/fakes3/server.rb in fakes3-0.2.5 vs lib/fakes3/server.rb in fakes3-1.0.0
- old
+ new
@@ -2,10 +2,11 @@
require 'webrick'
require 'webrick/https'
require 'openssl'
require 'securerandom'
require 'cgi'
+require 'fakes3/util'
require 'fakes3/file_store'
require 'fakes3/xml_adapter'
require 'fakes3/bucket_query'
require 'fakes3/unsupported_operation'
require 'fakes3/errors'
@@ -24,13 +25,13 @@
SET_ACL = "SET_ACL"
MOVE = "MOVE"
DELETE_OBJECT = "DELETE_OBJECT"
DELETE_BUCKET = "DELETE_BUCKET"
- attr_accessor :bucket,:object,:type,:src_bucket,
- :src_object,:method,:webrick_request,
- :path,:is_path_style,:query,:http_verb
+ attr_accessor :bucket, :object, :type, :src_bucket,
+ :src_object, :method, :webrick_request,
+ :path, :is_path_style, :query, :http_verb
def inspect
puts "-----Inspect FakeS3 Request"
puts "Type: #{@type}"
puts "Is Path Style: #{@is_path_style}"
@@ -87,14 +88,14 @@
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.body = XmlAdapter.acl
response['Content-Type'] = 'application/xml'
when 'GET'
- real_obj = @store.get_object(s_req.bucket,s_req.object,request)
+ real_obj = @store.get_object(s_req.bucket, s_req.object, request)
if !real_obj
response.status = 404
response.body = XmlAdapter.error_no_such_key(s_req.object)
response['Content-Type'] = "application/xml"
return
@@ -115,13 +116,19 @@
end
end
response.status = 200
response['Content-Type'] = real_obj.content_type
+
+ if real_obj.content_encoding
+ response.header['X-Content-Encoding'] = real_obj.content_encoding
+ response.header['Content-Encoding'] = real_obj.content_encoding
+ end
+
stat = File::Stat.new(real_obj.io.path)
- response['Last-Modified'] = Time.iso8601(real_obj.modified_date).httpdate()
+ response['Last-Modified'] = Time.iso8601(real_obj.modified_date).httpdate
response.header['ETag'] = "\"#{real_obj.md5}\""
response['Accept-Ranges'] = "bytes"
response['Last-Ranges'] = "bytes"
response['Access-Control-Allow-Origin'] = '*'
@@ -130,11 +137,12 @@
end
content_length = stat.size
# Added Range Query support
- if range = request.header["range"].first
+ range = request.header["range"].first
+ if range
response.status = 206
if range =~ /bytes=(\d*)-(\d*)/
start = $1.to_i
finish = $2.to_i
finish_str = ""
@@ -153,10 +161,11 @@
end
end
response['Content-Length'] = File::Stat.new(real_obj.io.path).size
if s_req.http_verb == 'HEAD'
response.body = ""
+ real_obj.io.close
else
response.body = real_obj.io
end
end
end
@@ -172,20 +181,20 @@
response['Content-Type'] = "text/xml"
response['Access-Control-Allow-Origin'] = '*'
case s_req.type
when Request::COPY
- object = @store.copy_object(s_req.src_bucket,s_req.src_object,s_req.bucket,s_req.object,request)
+ object = @store.copy_object(s_req.src_bucket, s_req.src_object, s_req.bucket, s_req.object, request)
response.body = XmlAdapter.copy_object_result(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)
+ real_obj = @store.store_object(bucket_obj, s_req.object, s_req.webrick_request)
response.header['ETag'] = "\"#{real_obj.md5}\""
when Request::CREATE_BUCKET
@store.create_bucket(s_req.bucket)
end
end
@@ -318,11 +327,11 @@
def do_OPTIONS(request, response)
super
response['Access-Control-Allow-Origin'] = '*'
response['Access-Control-Allow-Methods'] = 'PUT, POST, HEAD, GET, OPTIONS'
- response['Access-Control-Allow-Headers'] = 'Accept, Content-Type, Authorization, Content-Length, ETag'
+ response['Access-Control-Allow-Headers'] = 'Accept, Content-Type, Authorization, Content-Length, ETag, X-CSRF-Token'
response['Access-Control-Expose-Headers'] = 'ETag'
end
private
@@ -479,16 +488,16 @@
def parse_complete_multipart_upload(request)
parts_xml = ""
request.body { |chunk| parts_xml << chunk }
- # TODO: I suck at parsing xml
- parts_xml = parts_xml.scan /\<Part\>.*?<\/Part\>/m
+ # TODO: improve parsing xml
+ parts_xml = parts_xml.scan(/<Part>.*?<\/Part>/m)
parts_xml.collect do |xml|
{
- number: xml[/\<PartNumber\>(\d+)\<\/PartNumber\>/, 1].to_i,
- etag: xml[/\<ETag\>\"(.+)\"\<\/ETag\>/, 1]
+ number: xml[/<PartNumber>(\d+)<\/PartNumber>/, 1].to_i,
+ etag: FakeS3::Util.strip_before_and_after(xml[/\<ETag\>(.+)<\/ETag>/, 1], '"')
}
end
end
def dump_request(request)