lib/vines/stream/http/request.rb in vines-0.4.9 vs lib/vines/stream/http/request.rb in vines-0.4.10
- old
+ new
@@ -23,17 +23,26 @@
'manifest' => 'text/cache-manifest'
}.freeze
attr_reader :stream, :body, :headers, :method, :path, :url, :query
+ # Create a new request parsed from an HTTP client connection. We'll try
+ # to keep this request open until there are stanzas available to send
+ # as a response.
+ #
+ # stream - The Stream::Http client connection that received the request.
+ # parser - The Http::Parser that parsed the HTTP request.
+ # body - The String request body.
def initialize(stream, parser, body)
- @stream, @body = stream, body
+ uri = URI(parser.request_url)
+ @stream = stream
+ @body = body
@headers = parser.headers
@method = parser.http_method
- @path = parser.request_path
@url = parser.request_url
- @query = parser.query_string
+ @path = uri.path
+ @query = uri.query
@received = Time.now
end
# Return the number of seconds since this request was received.
def age
@@ -42,14 +51,16 @@
# Write the requested file to the client out of the given document root
# directory. Take care to prevent directory traversal attacks with paths
# like ../../../etc/passwd. Use the If-Modified-Since request header
# to implement caching.
+ #
+ # Returns nothing.
def reply_with_file(dir)
path = File.expand_path(File.join(dir, @path))
- # redirect requests missing a slash so relative links work
+ # Redirect requests missing a slash so relative links work.
if File.directory?(path) && !@path.end_with?('/')
send_status(301, MOVED, "Location: #{redirect_uri}")
return
end
@@ -67,10 +78,12 @@
end
end
# Send an HTTP 200 OK response wrapping the XMPP node content back
# to the client.
+ #
+ # Returns nothing.
def reply(node, content_type)
body = node.to_s
header = [
"HTTP/1.1 200 OK",
"Access-Control-Allow-Origin: *",
@@ -88,10 +101,12 @@
end
# Send a 200 OK response, allowing any origin domain to connect to the
# server, in response to CORS preflight OPTIONS requests. This allows
# any web application using strophe.js to connect to our BOSH port.
+ #
+ # Returns nothing.
def reply_to_options
allow = @headers['Access-Control-Request-Headers']
headers = [
"Access-Control-Allow-Origin: *",
"Access-Control-Allow-Methods: POST, GET, OPTIONS",
@@ -105,10 +120,12 @@
# Attempt to rebuild the full request URI from the Host header. If it
# wasn't sent by the client, just return the relative path that
# was requested. The Location response header must contain the fully
# qualified URI, but most browsers will accept relative paths as well.
+ #
+ # Returns the String URL.
def redirect_uri
host = headers['Host']
uri = "#{path}/"
uri = "#{uri}?#{query}" unless (query || '').empty?
uri = "http://#{host}#{uri}" if host
@@ -135,10 +152,12 @@
end
# Stream the contents of the file to the client in a 200 OK response.
# Send a Last-Modified response header so clients can send us an
# If-Modified-Since request header for caching.
+ #
+ # Returns nothing.
def send_file(path, status=200, message='OK')
header = [
"HTTP/1.1 #{status} #{message}",
"Content-Type: #{content_type(path)}",
"Content-Length: #{File.size(path)}",
@@ -160,13 +179,15 @@
# Provide a vroute cookie in each response that uniquely identifies this
# HTTP server. Reverse proxy servers (nginx/apache) can use this cookie
# to implement sticky sessions. Return nil if vroute was not set in
# config.rb and no cookie should be sent.
+ #
+ # Returns a String cookie value or nil if disabled.
def vroute_cookie
route = @stream.config[:http].vroute
route ? "Set-Cookie: vroute=#{route}; path=/; HttpOnly" : nil
end
end
end
end
-end
\ No newline at end of file
+end