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_connection.h 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*-
  2. * Copyright 2016 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 HTTP_H_
  17. #define HTTP_H_
  18. /**
  19. * @file http.h
  20. *
  21. * This is an interface for HTTP client and conn.
  22. * This code uses HTTP parser written by Joyent Inc based on nginx code.
  23. */
  24. #include "config.h"
  25. #include "keypair.h"
  26. #include "keypairs_cache.h"
  27. #include "fstring.h"
  28. #include "ref.h"
  29. #include "http_message.h"
  30. #include "http_util.h"
  31. #include <event.h>
  32. enum rspamd_http_connection_type {
  33. RSPAMD_HTTP_SERVER,
  34. RSPAMD_HTTP_CLIENT
  35. };
  36. struct rspamd_http_header;
  37. struct rspamd_http_message;
  38. struct rspamd_http_connection_private;
  39. struct rspamd_http_connection;
  40. struct rspamd_http_connection_router;
  41. struct rspamd_http_connection_entry;
  42. struct rspamd_storage_shmem {
  43. gchar *shm_name;
  44. ref_entry_t ref;
  45. };
  46. /**
  47. * Legacy spamc protocol
  48. */
  49. #define RSPAMD_HTTP_FLAG_SPAMC (1 << 0)
  50. /**
  51. * Store body of the message in a shared memory segment
  52. */
  53. #define RSPAMD_HTTP_FLAG_SHMEM (1 << 2)
  54. /**
  55. * Store body of the message in an immutable shared memory segment
  56. */
  57. #define RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE (1 << 3)
  58. /**
  59. * Use tls for this message
  60. */
  61. #define RSPAMD_HTTP_FLAG_SSL (1 << 4)
  62. /**
  63. * Body has been set for a message
  64. */
  65. #define RSPAMD_HTTP_FLAG_HAS_BODY (1 << 5)
  66. /**
  67. * Do not verify server's certificate
  68. */
  69. #define RSPAMD_HTTP_FLAG_SSL_NOVERIFY (1 << 6)
  70. /**
  71. * Options for HTTP connection
  72. */
  73. enum rspamd_http_options {
  74. RSPAMD_HTTP_BODY_PARTIAL = 0x1, /**< Call body handler on all body data portions *///!< RSPAMD_HTTP_BODY_PARTIAL
  75. RSPAMD_HTTP_CLIENT_SIMPLE = 0x1u << 1, /**< Read HTTP client reply automatically */ //!< RSPAMD_HTTP_CLIENT_SIMPLE
  76. RSPAMD_HTTP_CLIENT_ENCRYPTED = 0x1u << 2, /**< Encrypt data for client */ //!< RSPAMD_HTTP_CLIENT_ENCRYPTED
  77. RSPAMD_HTTP_CLIENT_SHARED = 0x1u << 3, /**< Store reply in shared memory */ //!< RSPAMD_HTTP_CLIENT_SHARED
  78. RSPAMD_HTTP_REQUIRE_ENCRYPTION = 0x1u << 4
  79. };
  80. typedef int (*rspamd_http_body_handler_t) (struct rspamd_http_connection *conn,
  81. struct rspamd_http_message *msg,
  82. const gchar *chunk,
  83. gsize len);
  84. typedef void (*rspamd_http_error_handler_t) (struct rspamd_http_connection *conn,
  85. GError *err);
  86. typedef int (*rspamd_http_finish_handler_t) (struct rspamd_http_connection *conn,
  87. struct rspamd_http_message *msg);
  88. /**
  89. * HTTP connection structure
  90. */
  91. struct rspamd_http_connection {
  92. struct rspamd_http_connection_private *priv;
  93. rspamd_http_body_handler_t body_handler;
  94. rspamd_http_error_handler_t error_handler;
  95. rspamd_http_finish_handler_t finish_handler;
  96. struct rspamd_keypair_cache *cache;
  97. gpointer ud;
  98. gsize max_size;
  99. unsigned opts;
  100. enum rspamd_http_connection_type type;
  101. gboolean finished;
  102. gint fd;
  103. gint ref;
  104. };
  105. /**
  106. * Create new http connection
  107. * @param handler_t handler_t for body
  108. * @param opts options
  109. * @return new connection structure
  110. */
  111. struct rspamd_http_connection *rspamd_http_connection_new (
  112. rspamd_http_body_handler_t body_handler,
  113. rspamd_http_error_handler_t error_handler,
  114. rspamd_http_finish_handler_t finish_handler,
  115. unsigned opts,
  116. enum rspamd_http_connection_type type,
  117. struct rspamd_keypair_cache *cache,
  118. gpointer ssl_ctx);
  119. /**
  120. * Set key pointed by an opaque pointer
  121. * @param conn connection structure
  122. * @param key opaque key structure
  123. */
  124. void rspamd_http_connection_set_key (struct rspamd_http_connection *conn,
  125. struct rspamd_cryptobox_keypair *key);
  126. /**
  127. * Get peer's public key
  128. * @param conn connection structure
  129. * @return pubkey structure or NULL
  130. */
  131. const struct rspamd_cryptobox_pubkey* rspamd_http_connection_get_peer_key (
  132. struct rspamd_http_connection *conn);
  133. /**
  134. * Returns TRUE if a connection is encrypted
  135. * @param conn
  136. * @return
  137. */
  138. gboolean rspamd_http_connection_is_encrypted (struct rspamd_http_connection *conn);
  139. /**
  140. * Handle a request using socket fd and user data ud
  141. * @param conn connection structure
  142. * @param ud opaque user data
  143. * @param fd fd to read/write
  144. */
  145. void rspamd_http_connection_read_message (
  146. struct rspamd_http_connection *conn,
  147. gpointer ud,
  148. gint fd,
  149. struct timeval *timeout,
  150. struct event_base *base);
  151. void rspamd_http_connection_read_message_shared (
  152. struct rspamd_http_connection *conn,
  153. gpointer ud,
  154. gint fd,
  155. struct timeval *timeout,
  156. struct event_base *base);
  157. /**
  158. * Send reply using initialised connection
  159. * @param conn connection structure
  160. * @param msg HTTP message
  161. * @param ud opaque user data
  162. * @param fd fd to read/write
  163. */
  164. void rspamd_http_connection_write_message (
  165. struct rspamd_http_connection *conn,
  166. struct rspamd_http_message *msg,
  167. const gchar *host,
  168. const gchar *mime_type,
  169. gpointer ud,
  170. gint fd,
  171. struct timeval *timeout,
  172. struct event_base *base);
  173. void rspamd_http_connection_write_message_shared (
  174. struct rspamd_http_connection *conn,
  175. struct rspamd_http_message *msg,
  176. const gchar *host,
  177. const gchar *mime_type,
  178. gpointer ud,
  179. gint fd,
  180. struct timeval *timeout,
  181. struct event_base *base);
  182. /**
  183. * Free connection structure
  184. * @param conn
  185. */
  186. void rspamd_http_connection_free (struct rspamd_http_connection *conn);
  187. /**
  188. * Increase refcount for a connection
  189. * @param conn
  190. * @return
  191. */
  192. static inline struct rspamd_http_connection *
  193. rspamd_http_connection_ref (struct rspamd_http_connection *conn)
  194. {
  195. conn->ref++;
  196. return conn;
  197. }
  198. /**
  199. * Decrease a refcount for a connection and free it if refcount is equal to zero
  200. * @param conn
  201. */
  202. static void
  203. rspamd_http_connection_unref (struct rspamd_http_connection *conn)
  204. {
  205. if (--conn->ref <= 0) {
  206. rspamd_http_connection_free (conn);
  207. }
  208. }
  209. /**
  210. * Reset connection for a new request
  211. * @param conn
  212. */
  213. void rspamd_http_connection_reset (struct rspamd_http_connection *conn);
  214. /**
  215. * Sets global maximum size for HTTP message being processed
  216. * @param sz
  217. */
  218. void rspamd_http_connection_set_max_size (struct rspamd_http_connection *conn,
  219. gsize sz);
  220. void rspamd_http_connection_disable_encryption (struct rspamd_http_connection *conn);
  221. #endif /* HTTP_H_ */