Stricter handling of warning headers

This commit is contained in:
Jason Tedor 2017-02-24 14:08:28 -05:00
parent 7f98c48828
commit f14055cb8d
3 changed files with 38 additions and 2 deletions

View file

@ -179,9 +179,9 @@ export function initializeInput($el, $actionsEl, $copyAsCurlEl, output) {
var warnings = xhr.getResponseHeader("warning");
if (warnings) {
// pattern for valid warning header
var re = /\d{3} [^ ]+ \"([^"]*)\"( \"[^"]*\")/
var re = /\d{3} [0-9a-zA-Z!#$%&'*+-.^_`|~]+ \"((?:\t| |!|[\x23-\x5b]|[\x5d-\x7e]|[\x80-\xff]|\\\\|\\")*)\"(?: \"[^"]*\")/
// split on any comma that is followed by an even number of quotes
warnings = _.map(warnings.split(/, (?=(?:[^"]*\"[^"]*\")*[^"]*$)/), function (warning) {
warnings = _.map(utils.splitOnUnquotedCommaSpace(warnings), function (warning) {
var match = re.exec(warning)
// extract the actual warning if there was a match
return "#! Deprecation: " + (match !== null ? match[1] : warning)

View file

@ -56,4 +56,29 @@ utils.expandLiteralStrings = function (data) {
});
}
utils.splitOnUnquotedCommaSpace = function (s) {
var quoted = false;
var arr = [];
var buffer = '';
var i = 0
while (i < s.length) {
var token = s.charAt(i++)
if (token == '\\' && i < s.length) {
token += s.charAt(i++)
} else if (token == ',' && i < s.length && s.charAt(i) == ' ') {
token += s.charAt(i++);
}
if (token == '"') {
quoted = !quoted
} else if (!quoted && token == ', ') {
arr.push(buffer);
buffer = '';
continue
}
buffer += token;
}
arr.push(buffer)
return arr;
}
module.exports = utils;

View file

@ -33,4 +33,15 @@ _.each(expandingTests.split(/^=+$/m), function (fixture) {
test("Literal expand - " + name, function () {
deepEqual(utils.expandLiteralStrings(collapsed), expanded);
});
test("split on unquoted comma followed by space", function () {
deepEqual(utils.splitOnUnquotedCommaSpace('a, b'), ['a', 'b']);
deepEqual(utils.splitOnUnquotedCommaSpace('a,b, c'), ['a,b', 'c']);
deepEqual(utils.splitOnUnquotedCommaSpace('"a, b"'), ['"a, b"']);
deepEqual(utils.splitOnUnquotedCommaSpace('"a, b", c'), ['"a, b"', 'c']);
deepEqual(utils.splitOnUnquotedCommaSpace('"a, b\\", c"'), ['"a, b\\", c"']);
deepEqual(utils.splitOnUnquotedCommaSpace(', a, b'), ['', 'a', 'b']);
deepEqual(utils.splitOnUnquotedCommaSpace('a, b, '), ['a', 'b', '']);
deepEqual(utils.splitOnUnquotedCommaSpace('\\"a, b", "c, d\\", e", f"'), ['\\"a', 'b", "c', 'd\\"', 'e", f"']);
});
});