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

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