Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # cookie
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Node.js Version][node-version-image]][node-version-url]
  5. [![Build Status][travis-image]][travis-url]
  6. [![Test Coverage][coveralls-image]][coveralls-url]
  7. Basic HTTP cookie parser and serializer for HTTP servers.
  8. ## Installation
  9. ```sh
  10. $ npm install cookie
  11. ```
  12. ## API
  13. ```js
  14. var cookie = require('cookie');
  15. ```
  16. ### cookie.parse(str, options)
  17. Parse an HTTP `Cookie` header string and returning an object of all cookie name-value pairs.
  18. The `str` argument is the string representing a `Cookie` header value and `options` is an
  19. optional object containing additional parsing options.
  20. ```js
  21. var cookies = cookie.parse('foo=bar; equation=E%3Dmc%5E2');
  22. // { foo: 'bar', equation: 'E=mc^2' }
  23. ```
  24. #### Options
  25. `cookie.parse` accepts these properties in the options object.
  26. ##### decode
  27. Specifies a function that will be used to decode a cookie's value. Since the value of a cookie
  28. has a limited character set (and must be a simple string), this function can be used to decode
  29. a previously-encoded cookie value into a JavaScript string or other object.
  30. The default function is the global `decodeURIComponent`, which will decode any URL-encoded
  31. sequences into their byte representations.
  32. **note** if an error is thrown from this function, the original, non-decoded cookie value will
  33. be returned as the cookie's value.
  34. ### cookie.serialize(name, value, options)
  35. Serialize a cookie name-value pair into a `Set-Cookie` header string. The `name` argument is the
  36. name for the cookie, the `value` argument is the value to set the cookie to, and the `options`
  37. argument is an optional object containing additional serialization options.
  38. ```js
  39. var setCookie = cookie.serialize('foo', 'bar');
  40. // foo=bar
  41. ```
  42. #### Options
  43. `cookie.serialize` accepts these properties in the options object.
  44. ##### domain
  45. Specifies the value for the [`Domain` `Set-Cookie` attribute][rfc-6266-5.2.3]. By default, no
  46. domain is set, and most clients will consider the cookie to apply to only the current domain.
  47. ##### encode
  48. Specifies a function that will be used to encode a cookie's value. Since value of a cookie
  49. has a limited character set (and must be a simple string), this function can be used to encode
  50. a value into a string suited for a cookie's value.
  51. The default function is the global `ecodeURIComponent`, which will encode a JavaScript string
  52. into UTF-8 byte sequences and then URL-encode any that fall outside of the cookie range.
  53. ##### expires
  54. Specifies the `Date` object to be the value for the [`Expires` `Set-Cookie` attribute][rfc-6266-5.2.1].
  55. By default, no expiration is set, and most clients will consider this a "non-persistent cookie" and
  56. will delete it on a condition like exiting a web browser application.
  57. **note** the [cookie storage model specification][rfc-6266-5.3] states that if both `expires` and
  58. `magAge` are set, then `maxAge` takes precedence, but it is possiblke not all clients by obey this,
  59. so if both are set, they should point to the same date and time.
  60. ##### httpOnly
  61. Specifies the `boolean` value for the [`HttpOnly` `Set-Cookie` attribute][rfc-6266-5.2.6]. When truthy,
  62. the `HttpOnly` attribute is set, otherwise it is not. By default, the `HttpOnly` attribute is not set.
  63. **note** be careful when setting this to `true`, as compliant clients will not allow client-side
  64. JavaScript to see the cookie in `document.cookie`.
  65. ##### maxAge
  66. Specifies the `number` (in seconds) to be the value for the [`Max-Age` `Set-Cookie` attribute][rfc-6266-5.2.2].
  67. The given number will be converted to an integer by rounding down. By default, no maximum age is set.
  68. **note** the [cookie storage model specification][rfc-6266-5.3] states that if both `expires` and
  69. `magAge` are set, then `maxAge` takes precedence, but it is possiblke not all clients by obey this,
  70. so if both are set, they should point to the same date and time.
  71. ##### path
  72. Specifies the value for the [`Path` `Set-Cookie` attribute][rfc-6266-5.2.4]. By default, the path
  73. is considered the ["default path"][rfc-6266-5.1.4]. By default, no maximum age is set, and most
  74. clients will consider this a "non-persistent cookie" and will delete it on a condition like exiting
  75. a web browser application.
  76. ##### sameSite
  77. Specifies the `boolean` or `string` to be the value for the [`SameSite` `Set-Cookie` attribute][draft-west-first-party-cookies-07].
  78. - `true` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
  79. - `false` will not set the `SameSite` attribute.
  80. - `'lax'` will set the `SameSite` attribute to `Lax` for lax same site enforcement.
  81. - `'strict'` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
  82. More information about the different enforcement levels can be found in the specification
  83. https://tools.ietf.org/html/draft-west-first-party-cookies-07#section-4.1.1
  84. **note** This is an attribute that has not yet been fully standardized, and may change in the future.
  85. This also means many clients may ignore this attribute until they understand it.
  86. ##### secure
  87. Specifies the `boolean` value for the [`Secure` `Set-Cookie` attribute][rfc-6266-5.2.5]. When truthy,
  88. the `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.
  89. **note** be careful when setting this to `true`, as compliant clients will not send the cookie back to
  90. the server in the future if the browser does not have an HTTPS connection.
  91. ## Example
  92. The following example uses this module in conjunction with the Node.js core HTTP server
  93. to prompt a user for their name and display it back on future visits.
  94. ```js
  95. var cookie = require('cookie');
  96. var escapeHtml = require('escape-html');
  97. var http = require('http');
  98. var url = require('url');
  99. function onRequest(req, res) {
  100. // Parse the query string
  101. var query = url.parse(req.url, true, true).query;
  102. if (query && query.name) {
  103. // Set a new cookie with the name
  104. res.setHeader('Set-Cookie', cookie.serialize('name', String(query.name), {
  105. httpOnly: true,
  106. maxAge: 60 * 60 * 24 * 7 // 1 week
  107. }));
  108. // Redirect back after setting cookie
  109. res.statusCode = 302;
  110. res.setHeader('Location', req.headers.referer || '/');
  111. res.end();
  112. return;
  113. }
  114. // Parse the cookies on the request
  115. var cookies = cookie.parse(req.headers.cookie || '');
  116. // Get the visitor name set in the cookie
  117. var name = cookies.name;
  118. res.setHeader('Content-Type', 'text/html; charset=UTF-8');
  119. if (name) {
  120. res.write('<p>Welcome back, <b>' + escapeHtml(name) + '</b>!</p>');
  121. } else {
  122. res.write('<p>Hello, new visitor!</p>');
  123. }
  124. res.write('<form method="GET">');
  125. res.write('<input placeholder="enter your name" name="name"> <input type="submit" value="Set Name">');
  126. res.end('</form');
  127. }
  128. http.createServer(onRequest).listen(3000);
  129. ```
  130. ## Testing
  131. ```sh
  132. $ npm test
  133. ```
  134. ## References
  135. - [RFC 6266: HTTP State Management Mechanism][rfc-6266]
  136. - [Same-site Cookies][draft-west-first-party-cookies-07]
  137. [draft-west-first-party-cookies-07]: https://tools.ietf.org/html/draft-west-first-party-cookies-07
  138. [rfc-6266]: https://tools.ietf.org/html/rfc6266
  139. [rfc-6266-5.1.4]: https://tools.ietf.org/html/rfc6266#section-5.1.4
  140. [rfc-6266-5.2.1]: https://tools.ietf.org/html/rfc6266#section-5.2.1
  141. [rfc-6266-5.2.2]: https://tools.ietf.org/html/rfc6266#section-5.2.2
  142. [rfc-6266-5.2.3]: https://tools.ietf.org/html/rfc6266#section-5.2.3
  143. [rfc-6266-5.2.4]: https://tools.ietf.org/html/rfc6266#section-5.2.4
  144. [rfc-6266-5.3]: https://tools.ietf.org/html/rfc6266#section-5.3
  145. ## License
  146. [MIT](LICENSE)
  147. [npm-image]: https://img.shields.io/npm/v/cookie.svg
  148. [npm-url]: https://npmjs.org/package/cookie
  149. [node-version-image]: https://img.shields.io/node/v/cookie.svg
  150. [node-version-url]: https://nodejs.org/en/download
  151. [travis-image]: https://img.shields.io/travis/jshttp/cookie/master.svg
  152. [travis-url]: https://travis-ci.org/jshttp/cookie
  153. [coveralls-image]: https://img.shields.io/coveralls/jshttp/cookie/master.svg
  154. [coveralls-url]: https://coveralls.io/r/jshttp/cookie?branch=master
  155. [downloads-image]: https://img.shields.io/npm/dm/cookie.svg
  156. [downloads-url]: https://npmjs.org/package/cookie