util_cookies.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file util_cookies.h
  18. * @brief Apache cookie library
  19. */
  20. #ifndef UTIL_COOKIES_H
  21. #define UTIL_COOKIES_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /**
  26. * @defgroup APACHE_CORE_COOKIE Cookies
  27. * @ingroup APACHE_CORE
  28. *
  29. * RFC2109 and RFC2965 compliant HTTP cookies can be read from and written
  30. * to using this set of functions.
  31. *
  32. * @{
  33. *
  34. */
  35. #include "apr_errno.h"
  36. #include "httpd.h"
  37. #define SET_COOKIE "Set-Cookie"
  38. #define SET_COOKIE2 "Set-Cookie2"
  39. #define DEFAULT_ATTRS "HttpOnly;Secure;Version=1"
  40. #define CLEAR_ATTRS "Version=1"
  41. typedef struct {
  42. request_rec *r;
  43. const char *name;
  44. const char *encoded;
  45. apr_table_t *new_cookies;
  46. int duplicated;
  47. } ap_cookie_do;
  48. /**
  49. * Write an RFC2109 compliant cookie.
  50. *
  51. * @param r The request
  52. * @param name The name of the cookie.
  53. * @param val The value to place in the cookie.
  54. * @param attrs The string containing additional cookie attributes. If NULL, the
  55. * DEFAULT_ATTRS will be used.
  56. * @param maxage If non zero, a Max-Age header will be added to the cookie.
  57. * @param ... A varargs array of zero or more (apr_table_t *) tables followed by NULL
  58. * to which the cookies should be added.
  59. */
  60. AP_DECLARE(apr_status_t) ap_cookie_write(request_rec * r, const char *name,
  61. const char *val, const char *attrs,
  62. long maxage, ...)
  63. AP_FN_ATTR_SENTINEL;
  64. /**
  65. * Write an RFC2965 compliant cookie.
  66. *
  67. * @param r The request
  68. * @param name2 The name of the cookie.
  69. * @param val The value to place in the cookie.
  70. * @param attrs2 The string containing additional cookie attributes. If NULL, the
  71. * DEFAULT_ATTRS will be used.
  72. * @param maxage If non zero, a Max-Age header will be added to the cookie.
  73. * @param ... A varargs array of zero or more (apr_table_t *) tables followed by NULL
  74. * to which the cookies should be added.
  75. */
  76. AP_DECLARE(apr_status_t) ap_cookie_write2(request_rec * r, const char *name2,
  77. const char *val, const char *attrs2,
  78. long maxage, ...)
  79. AP_FN_ATTR_SENTINEL;
  80. /**
  81. * Remove an RFC2109 compliant cookie.
  82. *
  83. * @param r The request
  84. * @param name The name of the cookie.
  85. * @param attrs The string containing additional cookie attributes. If NULL, the
  86. * CLEAR_ATTRS will be used.
  87. * @param ... A varargs array of zero or more (apr_table_t *) tables followed by NULL
  88. * to which the cookies should be added.
  89. */
  90. AP_DECLARE(apr_status_t) ap_cookie_remove(request_rec * r, const char *name,
  91. const char *attrs, ...)
  92. AP_FN_ATTR_SENTINEL;
  93. /**
  94. * Remove an RFC2965 compliant cookie.
  95. *
  96. * @param r The request
  97. * @param name2 The name of the cookie.
  98. * @param attrs2 The string containing additional cookie attributes. If NULL, the
  99. * CLEAR_ATTRS will be used.
  100. * @param ... A varargs array of zero or more (apr_table_t *) tables followed by NULL
  101. * to which the cookies should be added.
  102. */
  103. AP_DECLARE(apr_status_t) ap_cookie_remove2(request_rec * r, const char *name2,
  104. const char *attrs2, ...)
  105. AP_FN_ATTR_SENTINEL;
  106. /**
  107. * Read a cookie called name, placing its value in val.
  108. *
  109. * Both the Cookie and Cookie2 headers are scanned for the cookie.
  110. *
  111. * If the cookie is duplicated, this function returns APR_EGENERAL. If found,
  112. * and if remove is non zero, the cookie will be removed from the headers, and
  113. * thus kept private from the backend.
  114. */
  115. AP_DECLARE(apr_status_t) ap_cookie_read(request_rec * r, const char *name, const char **val,
  116. int remove);
  117. /**
  118. * Sanity check a given string that it exists, is not empty,
  119. * and does not contain the special characters '=', ';' and '&'.
  120. *
  121. * It is used to sanity check the cookie names.
  122. */
  123. AP_DECLARE(apr_status_t) ap_cookie_check_string(const char *string);
  124. /**
  125. * @}
  126. */
  127. #ifdef __cplusplus
  128. }
  129. #endif
  130. #endif /* !UTIL_COOKIES_H */