cookie.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #ifndef HEADER_CURL_COOKIE_H
  2. #define HEADER_CURL_COOKIE_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #include <curl/curl.h>
  28. struct Cookie {
  29. struct Cookie *next; /* next in the chain */
  30. char *name; /* <this> = value */
  31. char *value; /* name = <this> */
  32. char *path; /* path = <this> which is in Set-Cookie: */
  33. char *spath; /* sanitized cookie path */
  34. char *domain; /* domain = <this> */
  35. curl_off_t expires; /* expires = <this> */
  36. char *expirestr; /* the plain text version */
  37. /* RFC 2109 keywords. Version=1 means 2109-compliant cookie sending */
  38. char *version; /* Version = <value> */
  39. char *maxage; /* Max-Age = <value> */
  40. bool tailmatch; /* whether we do tail-matching of the domain name */
  41. bool secure; /* whether the 'secure' keyword was used */
  42. bool livecookie; /* updated from a server, not a stored file */
  43. bool httponly; /* true if the httponly directive is present */
  44. int creationtime; /* time when the cookie was written */
  45. unsigned char prefix; /* bitmap fields indicating which prefix are set */
  46. };
  47. /*
  48. * Available cookie prefixes, as defined in
  49. * draft-ietf-httpbis-rfc6265bis-02
  50. */
  51. #define COOKIE_PREFIX__SECURE (1<<0)
  52. #define COOKIE_PREFIX__HOST (1<<1)
  53. #define COOKIE_HASH_SIZE 256
  54. struct CookieInfo {
  55. /* linked list of cookies we know of */
  56. struct Cookie *cookies[COOKIE_HASH_SIZE];
  57. char *filename; /* file we read from/write to */
  58. long numcookies; /* number of cookies in the "jar" */
  59. bool running; /* state info, for cookie adding information */
  60. bool newsession; /* new session, discard session cookies on load */
  61. int lastct; /* last creation-time used in the jar */
  62. curl_off_t next_expiration; /* the next time at which expiration happens */
  63. };
  64. /* The maximum sizes we accept for cookies. RFC 6265 section 6.1 says
  65. "general-use user agents SHOULD provide each of the following minimum
  66. capabilities":
  67. - At least 4096 bytes per cookie (as measured by the sum of the length of
  68. the cookie's name, value, and attributes).
  69. In the 6265bis draft document section 5.4 it is phrased even stronger: "If
  70. the sum of the lengths of the name string and the value string is more than
  71. 4096 octets, abort these steps and ignore the set-cookie-string entirely."
  72. */
  73. /** Limits for INCOMING cookies **/
  74. /* The longest we allow a line to be when reading a cookie from a HTTP header
  75. or from a cookie jar */
  76. #define MAX_COOKIE_LINE 5000
  77. /* Maximum length of an incoming cookie name or content we deal with. Longer
  78. cookies are ignored. */
  79. #define MAX_NAME 4096
  80. /* Maximum number of Set-Cookie: lines accepted in a single response. If more
  81. such header lines are received, they are ignored. This value must be less
  82. than 256 since an unsigned char is used to count. */
  83. #define MAX_SET_COOKIE_AMOUNT 50
  84. /** Limits for OUTGOING cookies **/
  85. /* Maximum size for an outgoing cookie line libcurl will use in an http
  86. request. This is the default maximum length used in some versions of Apache
  87. httpd. */
  88. #define MAX_COOKIE_HEADER_LEN 8190
  89. /* Maximum number of cookies libcurl will send in a single request, even if
  90. there might be more cookies that match. One reason to cap the number is to
  91. keep the maximum HTTP request within the maximum allowed size. */
  92. #define MAX_COOKIE_SEND_AMOUNT 150
  93. struct Curl_easy;
  94. /*
  95. * Add a cookie to the internal list of cookies. The domain and path arguments
  96. * are only used if the header boolean is TRUE.
  97. */
  98. struct Cookie *Curl_cookie_add(struct Curl_easy *data,
  99. struct CookieInfo *c, bool header,
  100. bool noexpiry, char *lineptr,
  101. const char *domain, const char *path,
  102. bool secure);
  103. struct Cookie *Curl_cookie_getlist(struct Curl_easy *data,
  104. struct CookieInfo *c, const char *host,
  105. const char *path, bool secure);
  106. void Curl_cookie_freelist(struct Cookie *cookies);
  107. void Curl_cookie_clearall(struct CookieInfo *cookies);
  108. void Curl_cookie_clearsess(struct CookieInfo *cookies);
  109. #if defined(CURL_DISABLE_HTTP) || defined(CURL_DISABLE_COOKIES)
  110. #define Curl_cookie_list(x) NULL
  111. #define Curl_cookie_loadfiles(x) Curl_nop_stmt
  112. #define Curl_cookie_init(x,y,z,w) NULL
  113. #define Curl_cookie_cleanup(x) Curl_nop_stmt
  114. #define Curl_flush_cookies(x,y) Curl_nop_stmt
  115. #else
  116. void Curl_flush_cookies(struct Curl_easy *data, bool cleanup);
  117. void Curl_cookie_cleanup(struct CookieInfo *c);
  118. struct CookieInfo *Curl_cookie_init(struct Curl_easy *data,
  119. const char *file, struct CookieInfo *inc,
  120. bool newsession);
  121. struct curl_slist *Curl_cookie_list(struct Curl_easy *data);
  122. void Curl_cookie_loadfiles(struct Curl_easy *data);
  123. #endif
  124. #endif /* HEADER_CURL_COOKIE_H */