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

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