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 5.9KB

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