Skip to content

Commit

Permalink
Merge branch 'dont_include_default_ports' of github.com:wader/em-http…
Browse files Browse the repository at this point in the history
…-request
  • Loading branch information
igrigorik committed Oct 26, 2013
2 parents 041c3e9 + a6ce53e commit eb24fc1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
11 changes: 4 additions & 7 deletions lib/em-http/http_client_options.rb
Expand Up @@ -35,17 +35,14 @@ def set_uri(uri, path = nil)

@uri = uri
@path = uri.path
@host = uri.host
@port = uri.port

# Make sure the ports are set as Addressable::URI doesn't
# set the port if it isn't there
if @uri.scheme == "https"
@uri.port ||= 443
else
@uri.port ||= 80
if @port.nil?
@port = @uri.scheme == "https" ? 443 : 80
end

@host = @uri.host
@port = @uri.port

end
end
2 changes: 1 addition & 1 deletion lib/em-http/http_encoding.rb
Expand Up @@ -39,7 +39,7 @@ def munge_header_keys(head)
end

def encode_host
if @req.uri.port == 80 || @req.uri.port == 443
if @req.uri.port.nil? || @req.uri.port == 80 || @req.uri.port == 443
return @req.uri.host
else
@req.uri.host + ":#{@req.uri.port}"
Expand Down
44 changes: 44 additions & 0 deletions spec/redirect_spec.rb
Expand Up @@ -318,4 +318,48 @@ def response(r)
}
end

it "should not add default http port to redirect url that don't include it" do
EventMachine.run {
conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/http_no_port')
http = conn.get :redirects => 1
http.errback {
http.last_effective_url.to_s.should == 'http://host/'
EM.stop
}
}
end

it "should not add default https port to redirect url that don't include it" do
EventMachine.run {
conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/https_no_port')
http = conn.get :redirects => 1
http.errback {
http.last_effective_url.to_s.should == 'https://host/'
EM.stop
}
}
end

it "should keep default http port in redirect url that include it" do
EventMachine.run {
conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/http_with_port')
http = conn.get :redirects => 1
http.errback {
http.last_effective_url.to_s.should == 'http://host:80/'
EM.stop
}
}
end

it "should keep default https port in redirect url that include it" do
EventMachine.run {
conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/https_with_port')
http = conn.get :redirects => 1
http.errback {
http.last_effective_url.to_s.should == 'https://host:443/'
EM.stop
}
}
end

end
16 changes: 16 additions & 0 deletions spec/stallion.rb
Expand Up @@ -185,6 +185,22 @@ def self.call(env)
stable.response.status = 301
stable.response["Location"] = "http://$$$@$!%&^"

elsif stable.request.path_info == '/redirect/http_no_port'
stable.response.status = 301
stable.response["Location"] = "http://host/"

elsif stable.request.path_info == '/redirect/https_no_port'
stable.response.status = 301
stable.response["Location"] = "https://host/"

elsif stable.request.path_info == '/redirect/http_with_port'
stable.response.status = 301
stable.response["Location"] = "http://host:80/"

elsif stable.request.path_info == '/redirect/https_with_port'
stable.response.status = 301
stable.response["Location"] = "https://host:443/"

elsif stable.request.path_info == '/gzip'
io = StringIO.new
gzip = Zlib::GzipWriter.new(io)
Expand Down

0 comments on commit eb24fc1

Please sign in to comment.