-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Embed the nginx extension's Lua code in its assets.
- Loading branch information
1 parent
c58ca9e
commit 78021b9
Showing
14 changed files
with
179 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
return { | ||
default = { | ||
ROOT = { './busted' } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FROM openresty/openresty:alpine | ||
|
||
RUN mkdir /betty-lua | ||
COPY content_negotiation.lua /betty-lua/content_negotiation.lua | ||
RUN echo "lua_package_path '/betty-lua/?.lua;;';" > /etc/nginx/conf.d/default.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
local Cone = {} | ||
|
||
function Cone.negotiate(header, available_values) | ||
if available_values == nil or available_values == {} then | ||
return nil | ||
end | ||
|
||
if header == nil or header == '' then | ||
return available_values[1] | ||
end | ||
|
||
header = header:gsub('%s+', '') | ||
|
||
acceptable_values = {} | ||
unacceptable_values = {} | ||
for qualified_value in header:gmatch('([^,]+)') do | ||
value, quality = Cone.parse_qualified_value(qualified_value) | ||
if quality == 0 then | ||
table.insert(unacceptable_values, value) | ||
else | ||
table.insert(acceptable_values, { value, quality}) | ||
end | ||
end | ||
-- Sort the values by quality in descending order. | ||
table.sort(acceptable_values, function(a, b) return a[2] > b[2] end) | ||
|
||
for _, qualified_acceptable_value in ipairs(acceptable_values) do | ||
acceptable_value = qualified_acceptable_value[1] | ||
for _, available_value in pairs(available_values) do | ||
if acceptable_value == available_value then | ||
return acceptable_value | ||
end | ||
end | ||
end | ||
|
||
for _, available_value in ipairs(available_values) do | ||
if not Cone._contains(available_value, unacceptable_values) then | ||
return available_value | ||
end | ||
end | ||
|
||
return available_values[1] | ||
end | ||
|
||
function Cone.parse_qualified_value(qualified_value) | ||
if qualified_value:find(';q=') then | ||
value, quality = qualified_value:match("(.*)%;q=(.*)") | ||
quality = tonumber(quality) | ||
else | ||
value = qualified_value | ||
quality = 1 | ||
end | ||
return value, quality | ||
end | ||
|
||
function Cone._contains(needle, haystack) | ||
for _, haystack_value in pairs(haystack) do | ||
if haystack_value == needle then | ||
return true | ||
end | ||
end | ||
return false | ||
end | ||
|
||
return Cone |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -Eeuo pipefail | ||
|
||
cd "$(dirname "$0")/.." | ||
|
||
echo 'Running Busted...' | ||
|
||
busted "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
local cone = require('../betty/extension/nginx/assets/content_negotiation') | ||
|
||
describe('negotiate', function () | ||
it('nil header, with nil available, should not return', function () | ||
assert.is_nil(cone.negotiate(nil, nil)) | ||
end) | ||
|
||
it('nil header, with none available, should not return', function () | ||
assert.is_nil(cone.negotiate(nil, {})) | ||
end) | ||
|
||
it('empty header, with nil available, should not return', function () | ||
assert.is_nil(cone.negotiate('', nil)) | ||
end) | ||
|
||
it('empty header, with none available, should not return', function () | ||
assert.is_nil(cone.negotiate('', {})) | ||
end) | ||
|
||
it('empty header containing spaces, with none available, should not return', function () | ||
assert.is_nil(cone.negotiate(' ', {})) | ||
end) | ||
|
||
it('empty header containing tabs, with none available, should not return', function () | ||
assert.is_nil(cone.negotiate(' ', {})) | ||
end) | ||
|
||
it('empty header, with one available, should return the default available', function () | ||
assert.are.equal('apples', cone.negotiate('', {'apples'})) | ||
end) | ||
|
||
it('empty header, with multiple available, should return the default available', function () | ||
assert.are.equal('apples', cone.negotiate('', {'apples', 'oranges', 'bananas'})) | ||
end) | ||
|
||
it('header with multiple, with multiple others available, should return the default available', function () | ||
assert.are.equal('apples', cone.negotiate('uk,fr,la', {'apples', 'oranges', 'bananas'})) | ||
end) | ||
|
||
it('header with one value, with none available, should not return', function () | ||
assert.is_nil(cone.negotiate('apples', {})) | ||
end) | ||
|
||
it('header with multiple values, with none available, should not return', function () | ||
assert.is_nil(cone.negotiate('apples,oranges,bananas', {})) | ||
end) | ||
|
||
it('header with one value, with one available, should not return', function () | ||
assert.are.equal('apples', cone.negotiate('apples', {'apples'})) | ||
end) | ||
|
||
it('header with one value with a default quality, being the last available, should return the one header value', function () | ||
assert.are.equal('bananas', cone.negotiate('bananas', {'apples', 'oranges', 'bananas'})) | ||
end) | ||
|
||
it('header with one value with an explicit quality, being the last available, should return the one header value', function () | ||
assert.are.equal('bananas', cone.negotiate('bananas;q=0.5', {'apples', 'oranges', 'bananas'})) | ||
end) | ||
|
||
it('header with one value with an unacceptable quality, being the last available, should return the one header value', function () | ||
assert.are.equal('oranges', cone.negotiate('apples;q=0', {'apples', 'oranges', 'bananas'})) | ||
end) | ||
|
||
it('header with multiple values and whitespace, should return the preferred header value', function () | ||
assert.are.equal('bananas', cone.negotiate('bananas , oranges', {'apples', 'oranges', 'bananas'})) | ||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters