Skip to content

Commit

Permalink
perf: improve If-None-Match token parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 14, 2017
1 parent ff5f257 commit 21a0f0c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ unreleased
==========

* Fix regression matching multiple ETags in `If-None-Match`
* perf: improve `If-None-Match` token parsing

0.5.1 / 2017-09-11
==================
Expand Down
45 changes: 37 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@

var CACHE_CONTROL_NO_CACHE_REGEXP = /(?:^|,)\s*?no-cache\s*?(?:,|$)/

/**
* Simple expression to split token list.
* @private
*/

var TOKEN_LIST_REGEXP = / *, */

/**
* Module exports.
* @public
Expand Down Expand Up @@ -64,7 +57,7 @@ function fresh (reqHeaders, resHeaders) {
}

var etagStale = true
var matches = noneMatch.split(TOKEN_LIST_REGEXP)
var matches = parseTokenList(noneMatch)
for (var i = 0; i < matches.length; i++) {
var match = matches[i]
if (match === etag || match === 'W/' + etag || 'W/' + match === etag) {
Expand Down Expand Up @@ -106,3 +99,39 @@ function parseHttpDate (date) {
? timestamp
: NaN
}

/**
* Parse a HTTP token list.
*
* @param {string} str
* @private
*/

function parseTokenList (str) {
var end = 0
var list = []
var start = 0

// gather tokens
for (var i = 0, len = str.length; i < len; i++) {
switch (str.charCodeAt(i)) {
case 0x20: /* */
if (start === end) {
start = end = i + 1
}
break
case 0x2c: /* , */
list.push(str.substring(start, end))
start = end = i + 1
break
default:
end = i + 1
break
}
}

// final token
list.push(str.substring(start, end))

return list
}

0 comments on commit 21a0f0c

Please sign in to comment.