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.

107 lines
5.2 KiB

4 years ago
  1. # Server-side integration
  2. There are some servers that are not compliant with the [RFC 6265](http://tools.ietf.org/html/rfc6265). For those, some characters that are not encoded by JavaScript Cookie might be treated differently.
  3. Here we document the most important server-side peculiarities and their workarounds. Feel free to send a [Pull Request](https://github.com/js-cookie/js-cookie/blob/master/CONTRIBUTING.md#pull-requests) if you see something that can be improved.
  4. *Disclaimer: This documentation is entirely based on community provided information. The examples below should be used only as a reference.*
  5. ## PHP
  6. In PHP, `setcookie()` function encodes cookie values using `urlencode()` function, which applies `%`-encoding but also encodes spaces as `+` signs, [for historical reasons](http://php.net/manual/en/function.urlencode.php#function.urlencode). When cookies are read back via `$_COOKIE` or `filter_input(INPUT_COOKIE)`, they would go trough a decoding process which decodes `%`-encoded sequences and also converts `+` signs back to spaces. However, the plus (`+`) sign is valid cookie character by itself, which means that libraries that adhere to standards will interpret `+` signs differently to PHP.
  7. This presents two types of problems:
  8. 1. PHP writes a cookie via `setcookie()` and all spaces get converted to `+` signs. JavaScript Cookie read `+` signs and uses them literally, since it is a valid cookie character.
  9. 2. JavaScript Cookie writes a cookie with a value that contains `+` signs and stores it as is, since it is a valid cookie character. PHP read a cookie and converts `+` signs to spaces.
  10. To make both PHP and JavaScript Cookie play nicely together?
  11. **In PHP**, use `setrawcookie()` instead of `setcookie()`:
  12. ```php
  13. setrawcookie($name, rawurlencode($value));
  14. ```
  15. **In JavaScript**, use a custom converter.
  16. **Example**:
  17. ```javascript
  18. var PHPCookies = Cookies.withConverter({
  19. write: function (value) {
  20. // Encode all characters according to the "encodeURIComponent" spec
  21. return encodeURIComponent(value)
  22. // Revert the characters that are unnecessarily encoded but are
  23. // allowed in a cookie value, except for the plus sign (%2B)
  24. .replace(/%(23|24|26|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
  25. },
  26. read: function (value) {
  27. return value
  28. // Decode the plus sign to spaces first, otherwise "legit" encoded pluses
  29. // will be replaced incorrectly
  30. .replace(/\+/g, ' ')
  31. // Decode all characters according to the "encodeURIComponent" spec
  32. .replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
  33. }
  34. });
  35. ```
  36. Rack seems to have [a similar problem](https://github.com/js-cookie/js-cookie/issues/70#issuecomment-132503017).
  37. ## Tomcat
  38. ### Version >= 7.x
  39. It seems that there is a situation where Tomcat does not [read the parens correctly](https://github.com/js-cookie/js-cookie/issues/92#issue-107743407). To fix this you need to write a custom write converter.
  40. **Example**:
  41. ```javascript
  42. var TomcatCookies = Cookies.withConverter({
  43. write: function (value) {
  44. // Encode all characters according to the "encodeURIComponent" spec
  45. return encodeURIComponent(value)
  46. // Revert the characters that are unnecessarily encoded but are
  47. // allowed in a cookie value
  48. .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent)
  49. // Encode the parens that are interpreted incorrectly by Tomcat
  50. .replace(/[\(\)]/g, escape);
  51. }
  52. });
  53. ```
  54. ### Version >= 8.0.15
  55. Since Tomcat 8.0.15, it is possible to configure RFC 6265 compliance by changing your `conf/context.xml` file and adding the new [CookieProcessor](https://tomcat.apache.org/tomcat-8.0-doc/config/cookie-processor.html) nested inside the Context element. It would be like this:
  56. ```xml
  57. <Context>
  58. <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"/>
  59. </context>
  60. ```
  61. And you're all done.
  62. Alternatively, you can check the [Java Cookie](https://github.com/js-cookie/java-cookie) project, which integrates nicely with JavaScript Cookie.
  63. ## JBoss 7.1.1
  64. It seems that the servlet implementation of JBoss 7.1.1 [does not read some characters correctly](https://github.com/js-cookie/js-cookie/issues/70#issuecomment-148944674), even though they are allowed as per [RFC 6265](https://tools.ietf.org/html/rfc6265#section-4.1.1). To fix this you need to write a custom converter to send those characters correctly.
  65. **Example**:
  66. ```javascript
  67. var JBossCookies = Cookies.withConverter({
  68. write: function (value) {
  69. // Encode all characters according to the "encodeURIComponent" spec
  70. return encodeURIComponent(value)
  71. // Revert the characters that are unnecessarily encoded but are
  72. // allowed in a cookie value
  73. .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent)
  74. // Encode again the characters that are not allowed in JBoss 7.1.1, like "[" and "]":
  75. .replace(/[\[\]]/g, encodeURIComponent);
  76. }
  77. });
  78. ```
  79. Alternatively, you can check the [Java Cookie](https://github.com/js-cookie/java-cookie) project, which integrates nicely with JavaScript Cookie.