Skip to content

Commit ae2a545

Browse files
author
Hemant Kumar
committed
Merge pull request #131 from code-mancers/return-bad-request-when-host-missing
When Host is missing Invoker should return 400 Bad request response
2 parents 5011519 + 76ec5db commit ae2a545

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ invoker_profile/
1414
.ruby-version
1515
Gemfile.lock
1616
.overcommit.yml
17+
Procfile

lib/invoker/power/balancer.rb

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ def headers_received(headers)
6464
return
6565
end
6666
@session = UUID.generate()
67+
if !headers['Host'] || headers['Host'].empty?
68+
return_error_page(400)
69+
return
70+
end
71+
6772
dns_check_response = UrlRewriter.new.select_backend_config(headers['Host'])
6873
if dns_check_response && dns_check_response.port
6974
connection.server(session, host: '0.0.0.0', port: dns_check_response.port)

lib/invoker/power/templates/400.html

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Invoker</title>
6+
<style>
7+
body {
8+
margin: 0;
9+
padding: 0;
10+
background: #fff;
11+
line-height: 18px;
12+
}
13+
div.page {
14+
padding: 36px 90px;
15+
}
16+
h1, h2, p, li {
17+
font-family: Helvetica, sans-serif;
18+
font-size: 13px;
19+
}
20+
h1 {
21+
line-height: 45px;
22+
font-size: 36px;
23+
margin: 0;
24+
}
25+
h2 {
26+
line-height: 27px;
27+
font-size: 18px;
28+
font-weight: normal;
29+
margin: 0;
30+
}
31+
</style>
32+
</head>
33+
<body class="">
34+
<div class="page">
35+
<h1>Bad request</h1>
36+
<hr>
37+
<h2>Invoker couldn't understand the request</h2>
38+
</div>
39+
</body>
40+
</html>

spec/invoker/power/balancer_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
require 'spec_helper'
22

33
describe Invoker::Power::Balancer do
4+
before do
5+
@http_connection = mock("connection")
6+
@balancer = Invoker::Power::Balancer.new(@http_connection, "http")
7+
end
48

9+
context "when Host field is missing in the request" do
10+
it "should return 400 as response when Host is missing" do
11+
headers = {}
12+
@http_connection.expects(:send_data).with() { |value| value =~ /400 Bad Request/i }
13+
@balancer.headers_received(headers)
14+
end
15+
16+
it "should return 400 as response when Host is empty" do
17+
headers = { 'Host' => '' }
18+
@http_connection.expects(:send_data).with() { |value| value =~ /400 Bad Request/i }
19+
@balancer.headers_received(headers)
20+
end
21+
end
522
end

0 commit comments

Comments
 (0)