Permalink
Browse files

extend server with protocol information

This extends the server class with the information, if it is serving
http or https. This can then be used to generate a root uri to the web
application.
  • Loading branch information...
1 parent 89e2efe commit e2f3e29a341945bb6a2ee3a9dc31cce71de3a58b @Gibheer Gibheer committed Jul 26, 2013
View
47 lib/zero/request/server.rb
@@ -14,6 +14,30 @@ class Server
# the key for the server software
# @api private
KEY_SERVER_SOFTWARE = 'SERVER_SOFTWARE'
+ # the key for https
+ # @api private
+ KEY_HTTPS = 'HTTPS'
+ # the https on switch
+ # @api private
+ KEY_HTTPS_ON_SWITCH = ['on', 'yes', '1']
+ # the string for http
+ # @api private
+ HTTP = 'http'
+ # the string for https
+ # @api private
+ HTTPS = 'https'
+ # protocol seperator
+ # @api private
+ URI_SEP_PROTOCOL = '://'
+ # port separator
+ # @api private
+ URI_SEP_PORT = ':'
+ # default http port
+ # @api private
+ DEFAULT_PORT_HTTP = 80
+ # default https port
+ # @api private
+ DEFAULT_PORT_HTTPS = 443
# This creates a new server instance extracting all server related
# information from the environment.
@@ -22,6 +46,12 @@ def initialize(environment)
@port = environment[KEY_SERVER_PORT].to_i
@protocol = environment[KEY_SERVER_PROTOCOL]
@software = environment[KEY_SERVER_SOFTWARE]
+ p environment[KEY_HTTPS]
+ if KEY_HTTPS_ON_SWITCH.include?(environment[KEY_HTTPS])
+ @is_https = true
+ else
+ @is_https = false
+ end
end
# get the port
@@ -36,6 +66,23 @@ def initialize(environment)
# get the server software
# @return [String] the server software name
attr_reader :software
+ # get if the request is served through https
+ # @return [Boolean] true if server got the request through https
+ def is_https?; @is_https; end
+ # get if the request is served through http
+ # @return [Boolean] true if server got the request though http
+ def is_http?; !@is_https; end
+
+ # return the uri to the server
+ # @return [String] the root uri to the server
+ def uri
+ uri = (is_https? ? HTTPS : HTTP) + URI_SEP_PROTOCOL + hostname
+ if (port == DEFAULT_PORT_HTTP && is_http?) ||
+ (port == DEFAULT_PORT_HTTPS && is_https?)
+ return uri
+ end
+ uri + URI_SEP_PORT + port.to_s
+ end
end
end
end
View
1 spec/unit/zero/request/server/hostname_spec.rb
@@ -6,4 +6,3 @@
let(:env) { EnvGenerator.get('/foo', {'SERVER_NAME' => hostname}) }
its(:hostname) { should be(hostname) }
end
-
View
20 spec/unit/zero/request/server/is_http_spec.rb
@@ -0,0 +1,20 @@
+require 'spec_helper'
+
+describe Zero::Request::Server, '#is_http?' do
+ subject { Zero::Request::Server.new(env) }
+ let(:hostname) { 'FooName' }
+
+ context 'with http' do
+ let(:env) { EnvGenerator.get('/foo', {'SERVER_NAME' => hostname}) }
+ its(:is_http?) { should be(true) }
+ end
+
+ context 'with https' do
+ let(:env) { EnvGenerator.get('/foo', {
+ 'SERVER_NAME' => hostname,
+ 'HTTPS' => 'on'
+ }) }
+ its(:is_http?) { should be(false) }
+ end
+end
+
View
20 spec/unit/zero/request/server/is_https_spec.rb
@@ -0,0 +1,20 @@
+require 'spec_helper'
+
+describe Zero::Request::Server, '#is_https?' do
+ subject { Zero::Request::Server.new(env) }
+ let(:hostname) { 'FooName' }
+
+ context 'with http' do
+ let(:env) { EnvGenerator.get('/foo', {'SERVER_NAME' => hostname}) }
+ its(:is_https?) { should be(false) }
+ end
+
+ context 'with https' do
+ let(:env) { EnvGenerator.get('/foo', {
+ 'SERVER_NAME' => hostname,
+ 'HTTPS' => 'on'
+ }) }
+ its(:is_https?) { should be(true) }
+ end
+end
+
View
23 spec/unit/zero/request/server/uri_spec.rb
@@ -0,0 +1,23 @@
+require 'spec_helper'
+
+describe Zero::Request::Server, '#uri' do
+ subject { Zero::Request::Server.new(env) }
+ let(:hostname) { 'FooName' }
+ let(:port) { '80' }
+ let(:protocol) { 'http' }
+ let(:env) { EnvGenerator.get('/foo', {
+ 'SERVER_NAME' => hostname,
+ 'SERVER_PORT' => port
+ }) }
+
+ context 'with standard port' do
+ let(:result) { "#{protocol}://#{hostname}" }
+ its(:uri) { should eq(result) }
+ end
+
+ context 'with different port' do
+ let(:port) { '9292' }
+ let(:result) { "#{protocol}://#{hostname}:#{port}" }
+ its(:uri) { should eq(result) }
+ end
+end

0 comments on commit e2f3e29

Please sign in to comment.