Skip to content

Commit b3ffb2a

Browse files
committed
Fixed #17: Removes Access-Control-Allow-Origin header from server
1 parent 08bd0da commit b3ffb2a

File tree

4 files changed

+9
-10
lines changed

4 files changed

+9
-10
lines changed

example/haproxy/cors.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ end
5151
-- allowed_headers: Comma-delimited list of allowed headers. (e.g. X-Header1,X-Header2)
5252
function preflight_request_ver1(txn, allowed_methods, allowed_headers)
5353
core.Debug("CORS: preflight request received")
54-
txn.http:res_add_header("Access-Control-Allow-Methods", allowed_methods)
55-
txn.http:res_add_header("Access-Control-Allow-Headers", allowed_headers)
56-
txn.http:res_add_header("Access-Control-Max-Age", 600)
54+
txn.http:res_set_header("Access-Control-Allow-Methods", allowed_methods)
55+
txn.http:res_set_header("Access-Control-Allow-Headers", allowed_headers)
56+
txn.http:res_set_header("Access-Control-Max-Age", 600)
5757
core.Debug("CORS: attaching allowed methods to response")
5858
end
5959

@@ -148,7 +148,7 @@ function cors_response(txn)
148148
end
149149

150150
core.Debug("CORS: " .. origin .. " allowed")
151-
txn.http:res_add_header("Access-Control-Allow-Origin", allowed_origin)
151+
txn.http:res_set_header("Access-Control-Allow-Origin", allowed_origin)
152152
end
153153
end
154154

example/haproxy/haproxy.cfg

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ listen ui
1818
listen api
1919
bind :8080
2020

21-
22-
2321
# Invoke the CORS service on the request to capture the Origin header
2422
http-request lua.cors "GET,PUT,POST", "localhost", "X-Custom-Header"
2523

example/web/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ app.get('/', function (req, res) {
1515

1616
// the API
1717
app.get('/getdata', function(req, res) {
18+
res.set("Access-Control-Allow-Origin", "blah.com") // this should be overwritten by HAProxy
1819
res.send('Message from the server!')
1920
})
2021

lib/cors.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ end
5151
-- allowed_headers: Comma-delimited list of allowed headers. (e.g. X-Header1,X-Header2)
5252
function preflight_request_ver1(txn, allowed_methods, allowed_headers)
5353
core.Debug("CORS: preflight request received")
54-
txn.http:res_add_header("Access-Control-Allow-Methods", allowed_methods)
55-
txn.http:res_add_header("Access-Control-Allow-Headers", allowed_headers)
56-
txn.http:res_add_header("Access-Control-Max-Age", 600)
54+
txn.http:res_set_header("Access-Control-Allow-Methods", allowed_methods)
55+
txn.http:res_set_header("Access-Control-Allow-Headers", allowed_headers)
56+
txn.http:res_set_header("Access-Control-Max-Age", 600)
5757
core.Debug("CORS: attaching allowed methods to response")
5858
end
5959

@@ -148,7 +148,7 @@ function cors_response(txn)
148148
end
149149

150150
core.Debug("CORS: " .. origin .. " allowed")
151-
txn.http:res_add_header("Access-Control-Allow-Origin", allowed_origin)
151+
txn.http:res_set_header("Access-Control-Allow-Origin", allowed_origin)
152152
end
153153
end
154154

0 commit comments

Comments
 (0)