<commit_before>var errors = require('../errors');

///--- Globals

var PreconditionFailedError = errors.PreconditionFailedError;


///--- Helpers

function isValidDate(d) {
  if (isNaN(Date.parse(d)))
    return false;
  return (new Date(d) < new Date());
}


/**
 * Returns a plugin that will compare an already set ETag header with
 * the client's If-Match and If-None-Match header, and an already set
 * Last-Modified header with the client's If-Modified-Since and
 * If-Unmodified-Since header.
 *
 * @return {Function} restify handler.
 */

function conditionalRequest() {
  return function checkConditions(req, res, next) {
    var clientETags;
    var currentETag;
    var etag = res.header('ETag') || res.etag;
    var clientDate;
    var modified = res.header('Last-Modified');
    var i;
    var matched = false;

    if (typeof (etag) === 'string' && etag.length !== 0) {
      if (req.headers['if-match']) {
        // RFC: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.24

        /* JSSTYLED */
        clientETags = req.headers['if-match'].split(/\s*,\s*/);

        for (i = 0; i < clientETags.length; i++) {
          // only strong comparison
          /* JSSTYLED */
          currentETag = clientETags[i].replace(/^"(\w*)"$/, '$1');

          if (currentETag === '*' || currentETag === etag) {
            matched = true;
            break;
          }
        }

        if (!matched)
          return next(new PreconditionFailedError('etag didn\'t match'));
      }

      if (req.headers['if-none-match']) {
        // RFC: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26

        /* JSSTYLED */
        clientETags = req.headers['if-none-match'].split(/\s*,\s*/);<commit_msg>Get rid of ETag quotes and W/ prefix for ETag header.<commit_after>var errors = require('../errors');

///--- Globals

var PreconditionFailedError = errors.PreconditionFailedError;


///--- Helpers

function isValidDate(d) {
  if (isNaN(Date.parse(d)))
    return false;
  return (new Date(d) < new Date());
}


/**
 * Returns a plugin that will compare an already set ETag header with
 * the client's If-Match and If-None-Match header, and an already set
 * Last-Modified header with the client's If-Modified-Since and
 * If-Unmodified-Since header.
 *
 * @return {Function} restify handler.
 */

function conditionalRequest() {
  return function checkConditions(req, res, next) {
    var clientETags;
    var currentETag;
    var etag = res.header('ETag') || res.etag;
    var clientDate;
    var modified = res.header('Last-Modified');
    var i;
    var matched = false;

    if (typeof (etag) === 'string' && etag.length !== 0) {
      etag =
        etag.replace(/^W\//, '').replace(/^"(\w*)"$/, '$1');

      if (req.headers['if-match']) {
        // RFC: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.24

        /* JSSTYLED */
        clientETags = req.headers['if-match'].split(/\s*,\s*/);

        for (i = 0; i < clientETags.length; i++) {
          // only strong comparison
          /* JSSTYLED */
          currentETag = clientETags[i].replace(/^"(\w*)"$/, '$1');

          if (currentETag === '*' || currentETag === etag) {
            matched = true;
            break;
          }
        }

        if (!matched)
          return next(new PreconditionFailedError('etag didn\'t match'));
      }

      if (req.headers['if-none-match']) {
        // RFC: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26

        /* JSSTYLED */
        clientETags = req.headers['if-none-match'].split(/\s*,\s*/);
{'fraction_non_alphanumeric': 0.16829745596868884, 'fraction_numerical': 0.018730779983226167, 'mean_word_length': 2.3946869070208727, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5267107', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1161, 'n_words': 265}