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.

http_message.h 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright 2024 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * 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. #ifndef RSPAMD_HTTP_MESSAGE_H
  17. #define RSPAMD_HTTP_MESSAGE_H
  18. #include "config.h"
  19. #include "keypair.h"
  20. #include "keypairs_cache.h"
  21. #include "fstring.h"
  22. #include "ref.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. struct rspamd_http_connection;
  27. enum rspamd_http_message_type {
  28. HTTP_REQUEST = 0,
  29. HTTP_RESPONSE
  30. };
  31. /**
  32. * Extract the current message from a connection to deal with separately
  33. * @param conn
  34. * @return
  35. */
  36. struct rspamd_http_message *rspamd_http_connection_steal_msg(
  37. struct rspamd_http_connection *conn);
  38. /**
  39. * Copy the current message from a connection to deal with separately
  40. * @param conn
  41. * @return
  42. */
  43. struct rspamd_http_message *rspamd_http_connection_copy_msg(
  44. struct rspamd_http_message *msg, GError **err);
  45. /**
  46. * Create new HTTP message
  47. * @param type request or response
  48. * @return new http message
  49. */
  50. struct rspamd_http_message *rspamd_http_new_message(enum rspamd_http_message_type type);
  51. /**
  52. * Increase refcount number for an HTTP message
  53. * @param msg message to use
  54. * @return
  55. */
  56. struct rspamd_http_message *rspamd_http_message_ref(struct rspamd_http_message *msg);
  57. /**
  58. * Decrease number of refcounts for http message
  59. * @param msg
  60. */
  61. void rspamd_http_message_unref(struct rspamd_http_message *msg);
  62. /**
  63. * Sets a key for peer
  64. * @param msg
  65. * @param pk
  66. */
  67. void rspamd_http_message_set_peer_key(struct rspamd_http_message *msg,
  68. struct rspamd_cryptobox_pubkey *pk);
  69. /**
  70. * Create HTTP message from URL
  71. * @param url
  72. * @return new message or NULL
  73. */
  74. struct rspamd_http_message *rspamd_http_message_from_url(const gchar *url);
  75. /**
  76. * Returns body for a message
  77. * @param msg
  78. * @param blen pointer where to save body length
  79. * @return pointer to body start
  80. */
  81. const gchar *rspamd_http_message_get_body(struct rspamd_http_message *msg,
  82. gsize *blen);
  83. /**
  84. * Set message's body from the string
  85. * @param msg
  86. * @param data
  87. * @param len
  88. * @return TRUE if a message's body has been set
  89. */
  90. gboolean rspamd_http_message_set_body(struct rspamd_http_message *msg,
  91. const gchar *data, gsize len);
  92. /**
  93. * Set message's method by name
  94. * @param msg
  95. * @param method
  96. */
  97. void rspamd_http_message_set_method(struct rspamd_http_message *msg,
  98. const gchar *method);
  99. /**
  100. * Maps fd as message's body
  101. * @param msg
  102. * @param fd
  103. * @return TRUE if a message's body has been set
  104. */
  105. gboolean rspamd_http_message_set_body_from_fd(struct rspamd_http_message *msg,
  106. gint fd);
  107. /**
  108. * Uses rspamd_fstring_t as message's body, string is consumed by this operation
  109. * @param msg
  110. * @param fstr
  111. * @return TRUE if a message's body has been set
  112. */
  113. gboolean rspamd_http_message_set_body_from_fstring_steal(struct rspamd_http_message *msg,
  114. rspamd_fstring_t *fstr);
  115. /**
  116. * Uses rspamd_fstring_t as message's body, string is copied by this operation
  117. * @param msg
  118. * @param fstr
  119. * @return TRUE if a message's body has been set
  120. */
  121. gboolean rspamd_http_message_set_body_from_fstring_copy(struct rspamd_http_message *msg,
  122. const rspamd_fstring_t *fstr);
  123. /**
  124. * Appends data to message's body
  125. * @param msg
  126. * @param data
  127. * @param len
  128. * @return TRUE if a message's body has been set
  129. */
  130. gboolean rspamd_http_message_append_body(struct rspamd_http_message *msg,
  131. const gchar *data, gsize len);
  132. /**
  133. * Append a header to http message
  134. * @param rep
  135. * @param name
  136. * @param value
  137. */
  138. void rspamd_http_message_add_header(struct rspamd_http_message *msg,
  139. const gchar *name,
  140. const gchar *value);
  141. void rspamd_http_message_add_header_len(struct rspamd_http_message *msg,
  142. const gchar *name,
  143. const gchar *value,
  144. gsize len);
  145. void rspamd_http_message_add_header_fstr(struct rspamd_http_message *msg,
  146. const gchar *name,
  147. rspamd_fstring_t *value);
  148. /**
  149. * Search for a specified header in message
  150. * @param msg message
  151. * @param name name of header
  152. */
  153. const rspamd_ftok_t *rspamd_http_message_find_header(
  154. struct rspamd_http_message *msg,
  155. const gchar *name);
  156. /**
  157. * Search for a header that has multiple values
  158. * @param msg
  159. * @param name
  160. * @return list of rspamd_ftok_t * with values
  161. */
  162. GPtrArray *rspamd_http_message_find_header_multiple(
  163. struct rspamd_http_message *msg,
  164. const gchar *name);
  165. /**
  166. * Remove specific header from a message
  167. * @param msg
  168. * @param name
  169. * @return
  170. */
  171. gboolean rspamd_http_message_remove_header(struct rspamd_http_message *msg,
  172. const gchar *name);
  173. /**
  174. * Free HTTP message
  175. * @param msg
  176. */
  177. void rspamd_http_message_free(struct rspamd_http_message *msg);
  178. /**
  179. * Extract arguments from a message's URI contained inside query string decoding
  180. * them if needed
  181. * @param msg HTTP request message
  182. * @return new GHashTable which maps rspamd_ftok_t* to rspamd_ftok_t*
  183. * (table must be freed by a caller)
  184. */
  185. GHashTable *rspamd_http_message_parse_query(struct rspamd_http_message *msg);
  186. /**
  187. * Increase refcount for shared file (if any) to prevent early memory unlinking
  188. * @param msg
  189. */
  190. struct rspamd_storage_shmem *rspamd_http_message_shmem_ref(struct rspamd_http_message *msg);
  191. /**
  192. * Decrease external ref for shmem segment associated with a message
  193. * @param msg
  194. */
  195. void rspamd_http_message_shmem_unref(struct rspamd_storage_shmem *p);
  196. /**
  197. * Returns message's flags
  198. * @param msg
  199. * @return
  200. */
  201. guint rspamd_http_message_get_flags(struct rspamd_http_message *msg);
  202. /**
  203. * Returns an HTTP hostname for a message, derived from a header if it has it
  204. * or from a url if it doesn't
  205. * @param msg
  206. * @param hostlen output of the host length
  207. * @return
  208. */
  209. const gchar *rspamd_http_message_get_http_host(struct rspamd_http_message *msg,
  210. gsize *hostlen);
  211. /**
  212. * Returns true if a message has standard port (80 or 443 for https)
  213. * @param msg
  214. * @return
  215. */
  216. bool rspamd_http_message_is_standard_port(struct rspamd_http_message *msg);
  217. const gchar *rspamd_http_message_get_url(struct rspamd_http_message *msg, gsize *len);
  218. #ifdef __cplusplus
  219. }
  220. #endif
  221. #endif