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_router.h 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_ROUTER_H
  17. #define RSPAMD_HTTP_ROUTER_H
  18. #include "config.h"
  19. #include "http_connection.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. struct rspamd_http_connection_router;
  24. struct rspamd_http_connection_entry;
  25. typedef int (*rspamd_http_router_handler_t) (struct rspamd_http_connection_entry
  26. *conn_ent,
  27. struct rspamd_http_message *msg);
  28. typedef void (*rspamd_http_router_error_handler_t) (struct rspamd_http_connection_entry *conn_ent,
  29. GError *err);
  30. typedef void (*rspamd_http_router_finish_handler_t) (struct rspamd_http_connection_entry *conn_ent);
  31. struct rspamd_http_connection_entry {
  32. struct rspamd_http_connection_router *rt;
  33. struct rspamd_http_connection *conn;
  34. gpointer ud;
  35. gboolean is_reply;
  36. gboolean support_gzip;
  37. struct rspamd_http_connection_entry *prev, *next;
  38. };
  39. struct rspamd_http_connection_router {
  40. struct rspamd_http_connection_entry *conns;
  41. GHashTable *paths;
  42. GHashTable *response_headers;
  43. GPtrArray *regexps;
  44. ev_tstamp timeout;
  45. struct ev_loop *event_loop;
  46. struct rspamd_http_context *ctx;
  47. gchar *default_fs_path;
  48. rspamd_http_router_handler_t unknown_method_handler;
  49. struct rspamd_cryptobox_keypair *key;
  50. rspamd_http_router_error_handler_t error_handler;
  51. rspamd_http_router_finish_handler_t finish_handler;
  52. };
  53. /**
  54. * Create new http connection router and the associated HTTP connection
  55. * @param eh error handler callback
  56. * @param fh finish handler callback
  57. * @param default_fs_path if not NULL try to serve static files from
  58. * the specified directory
  59. * @return
  60. */
  61. struct rspamd_http_connection_router *rspamd_http_router_new (
  62. rspamd_http_router_error_handler_t eh,
  63. rspamd_http_router_finish_handler_t fh,
  64. ev_tstamp timeout,
  65. const char *default_fs_path,
  66. struct rspamd_http_context *ctx);
  67. /**
  68. * Set encryption key for the HTTP router
  69. * @param router router structure
  70. * @param key opaque key structure
  71. */
  72. void rspamd_http_router_set_key (struct rspamd_http_connection_router *router,
  73. struct rspamd_cryptobox_keypair *key);
  74. /**
  75. * Add new path to the router
  76. */
  77. void rspamd_http_router_add_path (struct rspamd_http_connection_router *router,
  78. const gchar *path, rspamd_http_router_handler_t handler);
  79. /**
  80. * Add custom header to append to router replies
  81. * @param router
  82. * @param name
  83. * @param value
  84. */
  85. void rspamd_http_router_add_header (struct rspamd_http_connection_router *router,
  86. const gchar *name, const gchar *value);
  87. /**
  88. * Sets method to handle unknown request methods
  89. * @param router
  90. * @param handler
  91. */
  92. void rspamd_http_router_set_unknown_handler (struct rspamd_http_connection_router *router,
  93. rspamd_http_router_handler_t handler);
  94. /**
  95. * Inserts router headers to the outbound message
  96. * @param router
  97. * @param msg
  98. */
  99. void rspamd_http_router_insert_headers (struct rspamd_http_connection_router *router,
  100. struct rspamd_http_message *msg);
  101. struct rspamd_regexp_s;
  102. /**
  103. * Adds new pattern to router, regexp object is refcounted by this function
  104. * @param router
  105. * @param re
  106. * @param handler
  107. */
  108. void rspamd_http_router_add_regexp (struct rspamd_http_connection_router *router,
  109. struct rspamd_regexp_s *re, rspamd_http_router_handler_t handler);
  110. /**
  111. * Handle new accepted socket
  112. * @param router router object
  113. * @param fd server socket
  114. * @param ud opaque userdata
  115. */
  116. void rspamd_http_router_handle_socket (
  117. struct rspamd_http_connection_router *router,
  118. gint fd,
  119. gpointer ud);
  120. /**
  121. * Free router and all connections associated
  122. * @param router
  123. */
  124. void rspamd_http_router_free (struct rspamd_http_connection_router *router);
  125. #ifdef __cplusplus
  126. }
  127. #endif
  128. #endif