text: 'use strict';
  
  var tough = require('tough-cookie');
  var sessionExt = require('../utils/session-ext');
  
  exports.processCookies = function (setCookieHeader) {
    var sessionCookies = [];
    var nonSessionCookies = [];
    if (setCookieHeader) {
      for (var i = 0; i < setCookieHeader.length; ++i) {
        var oCookie = tough.Cookie.parse(setCookieHeader[i]);
        if (oCookie && isSessionCookie(oCookie)) {
          sessionCookies.push(oCookie);
        } else {
          nonSessionCookies.push(setCookieHeader[i]);
        }
      }
    }
    return {
      sessionCookies: sessionCookies,
      nonSessionCookies: nonSessionCookies
    };
  };
  
  exports.storeSessionCookies = function(sessionCookies, req) {
    if (sessionCookies.length === 0) {
      return;
    }
    var logger = req.loggingContext.getLogger('/StoreSessionCookies');
    sessionExt.update(req.session, function (session) {
      var cookieJar = session._cookieJar ? tough.CookieJar.deserializeSync(session._cookieJar) : new tough.CookieJar();
      sessionCookies.forEach(function (oCookie) {
        try {
          cookieJar.setCookieSync(oCookie, req.internalUrl.href);
        } catch (err) {
          logger.warning('Could not set session cookie: ', err.message);
        }
      });
      session._cookieJar = cookieJar.serializeSync();
    });
  };
  
  function isSessionCookie(oCookie) {
    return (!oCookie.expires || oCookie.expires === 'Infinity') && (!oCookie.maxAge || oCookie.maxAge === 'Infinity');
  }
  