版博士V2.0程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

84 righe
2.5 KiB

  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const Cookie_1 = __importDefault(require("./Cookie"));
  7. /**
  8. * CookieJar.
  9. *
  10. * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie.
  11. * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie.
  12. */
  13. class CookieJar {
  14. constructor() {
  15. this.cookies = [];
  16. }
  17. /**
  18. * Validate cookie.
  19. *
  20. * @param cookie
  21. */
  22. validateCookie(cookie) {
  23. if (cookie.key.toLowerCase().startsWith('__secure-') && !cookie.isSecure()) {
  24. return false;
  25. }
  26. if (cookie.key.toLowerCase().startsWith('__host-') &&
  27. (!cookie.isSecure() || cookie.path !== '/' || cookie.domain)) {
  28. return false;
  29. }
  30. return true;
  31. }
  32. /**
  33. * Set cookie.
  34. *
  35. * @param cookieString
  36. */
  37. setCookiesString(cookieString) {
  38. if (!cookieString) {
  39. return;
  40. }
  41. const newCookie = new Cookie_1.default(cookieString);
  42. if (!this.validateCookie(newCookie)) {
  43. return;
  44. }
  45. this.cookies
  46. .filter((cookie) => cookie.key === newCookie.key)
  47. .forEach((cookie) => {
  48. this.cookies.splice(this.cookies.indexOf(cookie), 1);
  49. });
  50. this.cookies.push(newCookie);
  51. }
  52. /**
  53. * Get cookie.
  54. *
  55. * @param location Location.
  56. * @param fromDocument If true, the caller is a document.
  57. * @returns Cookie string.
  58. */
  59. getCookiesString(location, fromDocument) {
  60. const cookies = this.cookies.filter((cookie) => {
  61. // Skip when use document.cookie and the cookie is httponly.
  62. if (fromDocument && cookie.isHttpOnly()) {
  63. return false;
  64. }
  65. if (cookie.isExpired()) {
  66. return false;
  67. }
  68. if (cookie.isSecure() && location.protocol !== 'https:') {
  69. return false;
  70. }
  71. if (cookie.domain && !location.hostname.endsWith(cookie.domain)) {
  72. return false;
  73. }
  74. if (cookie.path && !location.pathname.startsWith(cookie.path)) {
  75. return false;
  76. }
  77. // TODO: check SameSite.
  78. return true;
  79. });
  80. return cookies.map((cookie) => cookie.cookieString()).join('; ');
  81. }
  82. }
  83. exports.default = CookieJar;
  84. //# sourceMappingURL=CookieJar.js.map