dav4rack 1.1.0
This commit is contained in:
parent
07d272daa4
commit
ead480f720
@ -102,12 +102,14 @@ module DAV4Rack
|
|||||||
# Return response to HEAD
|
# Return response to HEAD
|
||||||
def head
|
def head
|
||||||
if(resource.exist?)
|
if(resource.exist?)
|
||||||
response['Etag'] = resource.etag
|
res = resource.head(request, response)
|
||||||
response['Content-Type'] = resource.content_type
|
if(res == OK)
|
||||||
response['Content-Length'] = resource.content_length.to_s
|
response['Etag'] ||= resource.etag
|
||||||
response['Last-Modified'] = resource.last_modified.httpdate
|
response['Content-Type'] ||= resource.content_type
|
||||||
resource.head(request, response)
|
response['Content-Length'] ||= resource.content_length.to_s
|
||||||
OK
|
response['Last-Modified'] ||= resource.last_modified.httpdate
|
||||||
|
end
|
||||||
|
res
|
||||||
else
|
else
|
||||||
NotFound
|
NotFound
|
||||||
end
|
end
|
||||||
@ -197,7 +199,8 @@ module DAV4Rack
|
|||||||
return BadRequest unless request.depth == :infinity
|
return BadRequest unless request.depth == :infinity
|
||||||
|
|
||||||
return BadRequest unless dest = request.destination
|
return BadRequest unless dest = request.destination
|
||||||
if status = dest.validate
|
if status = dest.validate(host: request.host,
|
||||||
|
resource_path: resource.path)
|
||||||
return status
|
return status
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -5,24 +5,13 @@ module DAV4Rack
|
|||||||
|
|
||||||
attr_reader :host, :path, :path_info
|
attr_reader :host, :path, :path_info
|
||||||
|
|
||||||
def initialize(value, script_name: nil)
|
# uri is expected to be a DAV4Rack::Uri instance
|
||||||
@script_name = script_name.to_s
|
def initialize(uri)
|
||||||
@value = value.to_s.strip
|
|
||||||
parse
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse
|
|
||||||
uri = Addressable::URI.parse @value
|
|
||||||
|
|
||||||
@host = uri.host
|
@host = uri.host
|
||||||
@path = Addressable::URI.unencode uri.path
|
@path = uri.path
|
||||||
|
unless @path_info = uri.path_info
|
||||||
if @script_name
|
# nil path info means path is outside the realm of script_name
|
||||||
if @path =~ /\A(?<path>#{Regexp.escape @script_name}(?<path_info>\/.*))\z/
|
raise ArgumentError, "invalid destination header value: #{uri.to_s}"
|
||||||
@path_info = $~[:path_info]
|
|
||||||
else
|
|
||||||
raise ArgumentError, 'invalid destination header value'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -30,7 +19,7 @@ module DAV4Rack
|
|||||||
def validate(host: nil, resource_path: nil)
|
def validate(host: nil, resource_path: nil)
|
||||||
if host and self.host and self.host != host
|
if host and self.host and self.host != host
|
||||||
DAV4Rack::HTTPStatus::BadGateway
|
DAV4Rack::HTTPStatus::BadGateway
|
||||||
elsif self.path == resource_path
|
elsif resource_path and self.path_info == resource_path
|
||||||
DAV4Rack::HTTPStatus::Forbidden
|
DAV4Rack::HTTPStatus::Forbidden
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
require 'uri'
|
require 'uri'
|
||||||
require 'addressable/uri'
|
require 'addressable/uri'
|
||||||
require 'dav4rack/logger'
|
require 'dav4rack/logger'
|
||||||
|
require 'dav4rack/uri'
|
||||||
|
|
||||||
module DAV4Rack
|
module DAV4Rack
|
||||||
class Request < Rack::Request
|
class Request < Rack::Request
|
||||||
@ -84,7 +85,7 @@ module DAV4Rack
|
|||||||
# Destination header
|
# Destination header
|
||||||
def destination
|
def destination
|
||||||
@destination ||= if h = get_header('HTTP_DESTINATION')
|
@destination ||= if h = get_header('HTTP_DESTINATION')
|
||||||
DestinationHeader.new h, script_name: script_name
|
DestinationHeader.new DAV4Rack::Uri.new(h, script_name: script_name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -123,6 +124,13 @@ module DAV4Rack
|
|||||||
"#{script_name}#{expand_path path}"
|
"#{script_name}#{expand_path path}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# returns the given path, but with the leading script_name removed. Will
|
||||||
|
# return nil if the path does not begin with the script_name
|
||||||
|
def path_info_for(full_path, script_name: self.script_name)
|
||||||
|
uri = DAV4Rack::Uri.new full_path, script_name: script_name
|
||||||
|
return uri.path_info
|
||||||
|
end
|
||||||
|
|
||||||
# expands '/foo/../bar' to '/bar'
|
# expands '/foo/../bar' to '/bar'
|
||||||
def expand_path(path)
|
def expand_path(path)
|
||||||
path.squeeze! '/'
|
path.squeeze! '/'
|
||||||
|
|||||||
@ -208,8 +208,12 @@ module DAV4Rack
|
|||||||
NotImplemented
|
NotImplemented
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# HTTP HEAD request.
|
||||||
|
#
|
||||||
|
# Like GET, but without content. Override if you set custom headers in GET
|
||||||
|
# to set them here as well.
|
||||||
def head(request, response)
|
def head(request, response)
|
||||||
#no-op, but called by the controller
|
OK
|
||||||
end
|
end
|
||||||
|
|
||||||
# HTTP PUT request.
|
# HTTP PUT request.
|
||||||
|
|||||||
42
lib/dav4rack/uri.rb
Normal file
42
lib/dav4rack/uri.rb
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
require 'addressable/uri'
|
||||||
|
|
||||||
|
module DAV4Rack
|
||||||
|
|
||||||
|
# adds a bit of parsing logic around a header URI or path value
|
||||||
|
class Uri
|
||||||
|
|
||||||
|
attr_reader :host, :path, :path_info, :script_name
|
||||||
|
|
||||||
|
def initialize(uri_or_path, script_name: nil)
|
||||||
|
# more than one leading slash confuses Addressable::URI, resulting e.g.
|
||||||
|
# with //remote.php/dav/files in a path of /dav/files with a host
|
||||||
|
# remote.php.
|
||||||
|
@uri_or_path = uri_or_path.to_s.strip.sub %r{\A/+}, '/'
|
||||||
|
|
||||||
|
@script_name = script_name
|
||||||
|
parse
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
@uri_or_path
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def parse
|
||||||
|
uri = Addressable::URI.parse @uri_or_path
|
||||||
|
|
||||||
|
@host = uri.host
|
||||||
|
@path = Addressable::URI.unencode uri.path
|
||||||
|
|
||||||
|
if @script_name
|
||||||
|
if @path =~ /\A(?<path>#{Regexp.escape @script_name}(?<path_info>\/.*))\z/
|
||||||
|
@path_info = $~[:path_info]
|
||||||
|
end
|
||||||
|
else
|
||||||
|
@path_info = @path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -13,5 +13,5 @@ module DAV4Rack
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
VERSION = Version.new('1.0.0')
|
VERSION = Version.new('1.1.0')
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user