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.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. #include "http_router.h"
  17. #include "http_connection.h"
  18. #include "http_private.h"
  19. #include "libutil/regexp.h"
  20. #include "libutil/printf.h"
  21. #include "libserver/logger.h"
  22. #include "utlist.h"
  23. #include "unix-std.h"
  24. enum http_magic_type {
  25. HTTP_MAGIC_PLAIN = 0,
  26. HTTP_MAGIC_HTML,
  27. HTTP_MAGIC_CSS,
  28. HTTP_MAGIC_JS,
  29. HTTP_MAGIC_PNG,
  30. HTTP_MAGIC_JPG
  31. };
  32. static const struct _rspamd_http_magic {
  33. const gchar *ext;
  34. const gchar *ct;
  35. } http_file_types[] = {
  36. [HTTP_MAGIC_PLAIN] = { "txt", "text/plain" },
  37. [HTTP_MAGIC_HTML] = { "html", "text/html" },
  38. [HTTP_MAGIC_CSS] = { "css", "text/css" },
  39. [HTTP_MAGIC_JS] = { "js", "application/javascript" },
  40. [HTTP_MAGIC_PNG] = { "png", "image/png" },
  41. [HTTP_MAGIC_JPG] = { "jpg", "image/jpeg" },
  42. };
  43. /*
  44. * HTTP router functions
  45. */
  46. static void
  47. rspamd_http_entry_free (struct rspamd_http_connection_entry *entry)
  48. {
  49. if (entry != NULL) {
  50. close (entry->conn->fd);
  51. rspamd_http_connection_unref (entry->conn);
  52. if (entry->rt->finish_handler) {
  53. entry->rt->finish_handler (entry);
  54. }
  55. DL_DELETE (entry->rt->conns, entry);
  56. g_free (entry);
  57. }
  58. }
  59. static void
  60. rspamd_http_router_error_handler (struct rspamd_http_connection *conn,
  61. GError *err)
  62. {
  63. struct rspamd_http_connection_entry *entry = conn->ud;
  64. struct rspamd_http_message *msg;
  65. if (entry->is_reply) {
  66. /* At this point we need to finish this session and close owned socket */
  67. if (entry->rt->error_handler != NULL) {
  68. entry->rt->error_handler (entry, err);
  69. }
  70. rspamd_http_entry_free (entry);
  71. }
  72. else {
  73. /* Here we can write a reply to a client */
  74. if (entry->rt->error_handler != NULL) {
  75. entry->rt->error_handler (entry, err);
  76. }
  77. msg = rspamd_http_new_message (HTTP_RESPONSE);
  78. msg->date = time (NULL);
  79. msg->code = err->code;
  80. rspamd_http_message_set_body (msg, err->message, strlen (err->message));
  81. rspamd_http_connection_reset (entry->conn);
  82. rspamd_http_connection_write_message (entry->conn,
  83. msg,
  84. NULL,
  85. "text/plain",
  86. entry,
  87. entry->rt->timeout);
  88. entry->is_reply = TRUE;
  89. }
  90. }
  91. static const gchar *
  92. rspamd_http_router_detect_ct (const gchar *path)
  93. {
  94. const gchar *dot;
  95. guint i;
  96. dot = strrchr (path, '.');
  97. if (dot == NULL) {
  98. return http_file_types[HTTP_MAGIC_PLAIN].ct;
  99. }
  100. dot++;
  101. for (i = 0; i < G_N_ELEMENTS (http_file_types); i++) {
  102. if (strcmp (http_file_types[i].ext, dot) == 0) {
  103. return http_file_types[i].ct;
  104. }
  105. }
  106. return http_file_types[HTTP_MAGIC_PLAIN].ct;
  107. }
  108. static gboolean
  109. rspamd_http_router_is_subdir (const gchar *parent, const gchar *sub)
  110. {
  111. if (parent == NULL || sub == NULL || *parent == '\0') {
  112. return FALSE;
  113. }
  114. while (*parent != '\0') {
  115. if (*sub != *parent) {
  116. return FALSE;
  117. }
  118. parent++;
  119. sub++;
  120. }
  121. parent--;
  122. if (*parent == G_DIR_SEPARATOR) {
  123. return TRUE;
  124. }
  125. return (*sub == G_DIR_SEPARATOR || *sub == '\0');
  126. }
  127. static gboolean
  128. rspamd_http_router_try_file (struct rspamd_http_connection_entry *entry,
  129. rspamd_ftok_t *lookup, gboolean expand_path)
  130. {
  131. struct stat st;
  132. gint fd;
  133. gchar filebuf[PATH_MAX], realbuf[PATH_MAX], *dir;
  134. struct rspamd_http_message *reply_msg;
  135. rspamd_snprintf (filebuf, sizeof (filebuf), "%s%c%T",
  136. entry->rt->default_fs_path, G_DIR_SEPARATOR, lookup);
  137. if (realpath (filebuf, realbuf) == NULL ||
  138. lstat (realbuf, &st) == -1) {
  139. return FALSE;
  140. }
  141. if (S_ISDIR (st.st_mode) && expand_path) {
  142. /* Try to append 'index.html' to the url */
  143. rspamd_fstring_t *nlookup;
  144. rspamd_ftok_t tok;
  145. gboolean ret;
  146. nlookup = rspamd_fstring_sized_new (lookup->len + sizeof ("index.html"));
  147. rspamd_printf_fstring (&nlookup, "%T%c%s", lookup, G_DIR_SEPARATOR,
  148. "index.html");
  149. tok.begin = nlookup->str;
  150. tok.len = nlookup->len;
  151. ret = rspamd_http_router_try_file (entry, &tok, FALSE);
  152. rspamd_fstring_free (nlookup);
  153. return ret;
  154. }
  155. else if (!S_ISREG (st.st_mode)) {
  156. return FALSE;
  157. }
  158. /* We also need to ensure that file is inside the defined dir */
  159. rspamd_strlcpy (filebuf, realbuf, sizeof (filebuf));
  160. dir = dirname (filebuf);
  161. if (dir == NULL ||
  162. !rspamd_http_router_is_subdir (entry->rt->default_fs_path,
  163. dir)) {
  164. return FALSE;
  165. }
  166. fd = open (realbuf, O_RDONLY);
  167. if (fd == -1) {
  168. return FALSE;
  169. }
  170. reply_msg = rspamd_http_new_message (HTTP_RESPONSE);
  171. reply_msg->date = time (NULL);
  172. reply_msg->code = 200;
  173. rspamd_http_router_insert_headers (entry->rt, reply_msg);
  174. if (!rspamd_http_message_set_body_from_fd (reply_msg, fd)) {
  175. close (fd);
  176. return FALSE;
  177. }
  178. close (fd);
  179. rspamd_http_connection_reset (entry->conn);
  180. msg_debug ("requested file %s", realbuf);
  181. rspamd_http_connection_write_message (entry->conn, reply_msg, NULL,
  182. rspamd_http_router_detect_ct (realbuf), entry,
  183. entry->rt->timeout);
  184. return TRUE;
  185. }
  186. static void
  187. rspamd_http_router_send_error (GError *err,
  188. struct rspamd_http_connection_entry *entry)
  189. {
  190. struct rspamd_http_message *err_msg;
  191. err_msg = rspamd_http_new_message (HTTP_RESPONSE);
  192. err_msg->date = time (NULL);
  193. err_msg->code = err->code;
  194. rspamd_http_message_set_body (err_msg, err->message,
  195. strlen (err->message));
  196. entry->is_reply = TRUE;
  197. err_msg->status = rspamd_fstring_new_init (err->message, strlen (err->message));
  198. rspamd_http_router_insert_headers (entry->rt, err_msg);
  199. rspamd_http_connection_reset (entry->conn);
  200. rspamd_http_connection_write_message (entry->conn,
  201. err_msg,
  202. NULL,
  203. "text/plain",
  204. entry,
  205. entry->rt->timeout);
  206. }
  207. static int
  208. rspamd_http_router_finish_handler (struct rspamd_http_connection *conn,
  209. struct rspamd_http_message *msg)
  210. {
  211. struct rspamd_http_connection_entry *entry = conn->ud;
  212. rspamd_http_router_handler_t handler = NULL;
  213. gpointer found;
  214. GError *err;
  215. rspamd_ftok_t lookup;
  216. const rspamd_ftok_t *encoding;
  217. struct http_parser_url u;
  218. guint i;
  219. rspamd_regexp_t *re;
  220. struct rspamd_http_connection_router *router;
  221. G_STATIC_ASSERT (sizeof (rspamd_http_router_handler_t) ==
  222. sizeof (gpointer));
  223. memset (&lookup, 0, sizeof (lookup));
  224. router = entry->rt;
  225. if (entry->is_reply) {
  226. /* Request is finished, it is safe to free a connection */
  227. rspamd_http_entry_free (entry);
  228. }
  229. else {
  230. if (G_UNLIKELY (msg->method != HTTP_GET && msg->method != HTTP_POST)) {
  231. if (router->unknown_method_handler) {
  232. return router->unknown_method_handler (entry, msg);
  233. }
  234. else {
  235. err = g_error_new (HTTP_ERROR, 500,
  236. "Invalid method");
  237. if (entry->rt->error_handler != NULL) {
  238. entry->rt->error_handler (entry, err);
  239. }
  240. rspamd_http_router_send_error (err, entry);
  241. g_error_free (err);
  242. return 0;
  243. }
  244. }
  245. /* Search for path */
  246. if (msg->url != NULL && msg->url->len != 0) {
  247. http_parser_parse_url (msg->url->str, msg->url->len, TRUE, &u);
  248. if (u.field_set & (1 << UF_PATH)) {
  249. guint unnorm_len;
  250. lookup.begin = msg->url->str + u.field_data[UF_PATH].off;
  251. lookup.len = u.field_data[UF_PATH].len;
  252. rspamd_http_normalize_path_inplace ((gchar *)lookup.begin,
  253. lookup.len,
  254. &unnorm_len);
  255. lookup.len = unnorm_len;
  256. }
  257. else {
  258. lookup.begin = msg->url->str;
  259. lookup.len = msg->url->len;
  260. }
  261. found = g_hash_table_lookup (entry->rt->paths, &lookup);
  262. memcpy (&handler, &found, sizeof (found));
  263. msg_debug ("requested known path: %T", &lookup);
  264. }
  265. else {
  266. err = g_error_new (HTTP_ERROR, 404,
  267. "Empty path requested");
  268. if (entry->rt->error_handler != NULL) {
  269. entry->rt->error_handler (entry, err);
  270. }
  271. rspamd_http_router_send_error (err, entry);
  272. g_error_free (err);
  273. return 0;
  274. }
  275. entry->is_reply = TRUE;
  276. encoding = rspamd_http_message_find_header (msg, "Accept-Encoding");
  277. if (encoding && rspamd_substring_search (encoding->begin, encoding->len,
  278. "gzip", 4) != -1) {
  279. entry->support_gzip = TRUE;
  280. }
  281. if (handler != NULL) {
  282. return handler (entry, msg);
  283. }
  284. else {
  285. /* Try regexps */
  286. for (i = 0; i < router->regexps->len; i ++) {
  287. re = g_ptr_array_index (router->regexps, i);
  288. if (rspamd_regexp_match (re, lookup.begin, lookup.len,
  289. TRUE)) {
  290. found = rspamd_regexp_get_ud (re);
  291. memcpy (&handler, &found, sizeof (found));
  292. return handler (entry, msg);
  293. }
  294. }
  295. /* Now try plain file */
  296. if (entry->rt->default_fs_path == NULL || lookup.len == 0 ||
  297. !rspamd_http_router_try_file (entry, &lookup, TRUE)) {
  298. err = g_error_new (HTTP_ERROR, 404,
  299. "Not found");
  300. if (entry->rt->error_handler != NULL) {
  301. entry->rt->error_handler (entry, err);
  302. }
  303. msg_info ("path: %T not found", &lookup);
  304. rspamd_http_router_send_error (err, entry);
  305. g_error_free (err);
  306. }
  307. }
  308. }
  309. return 0;
  310. }
  311. struct rspamd_http_connection_router *
  312. rspamd_http_router_new (rspamd_http_router_error_handler_t eh,
  313. rspamd_http_router_finish_handler_t fh,
  314. ev_tstamp timeout,
  315. const char *default_fs_path,
  316. struct rspamd_http_context *ctx)
  317. {
  318. struct rspamd_http_connection_router *nrouter;
  319. struct stat st;
  320. nrouter = g_malloc0 (sizeof (struct rspamd_http_connection_router));
  321. nrouter->paths = g_hash_table_new_full (rspamd_ftok_icase_hash,
  322. rspamd_ftok_icase_equal, rspamd_fstring_mapped_ftok_free, NULL);
  323. nrouter->regexps = g_ptr_array_new ();
  324. nrouter->conns = NULL;
  325. nrouter->error_handler = eh;
  326. nrouter->finish_handler = fh;
  327. nrouter->response_headers = g_hash_table_new_full (rspamd_strcase_hash,
  328. rspamd_strcase_equal, g_free, g_free);
  329. nrouter->event_loop = ctx->event_loop;
  330. nrouter->timeout = timeout;
  331. nrouter->default_fs_path = NULL;
  332. if (default_fs_path != NULL) {
  333. if (stat (default_fs_path, &st) == -1) {
  334. msg_err ("cannot stat %s", default_fs_path);
  335. }
  336. else {
  337. if (!S_ISDIR (st.st_mode)) {
  338. msg_err ("path %s is not a directory", default_fs_path);
  339. }
  340. else {
  341. nrouter->default_fs_path = realpath (default_fs_path, NULL);
  342. }
  343. }
  344. }
  345. nrouter->ctx = ctx;
  346. return nrouter;
  347. }
  348. void
  349. rspamd_http_router_set_key (struct rspamd_http_connection_router *router,
  350. struct rspamd_cryptobox_keypair *key)
  351. {
  352. g_assert (key != NULL);
  353. router->key = rspamd_keypair_ref (key);
  354. }
  355. void
  356. rspamd_http_router_add_path (struct rspamd_http_connection_router *router,
  357. const gchar *path, rspamd_http_router_handler_t handler)
  358. {
  359. gpointer ptr;
  360. rspamd_ftok_t *key;
  361. rspamd_fstring_t *storage;
  362. G_STATIC_ASSERT (sizeof (rspamd_http_router_handler_t) ==
  363. sizeof (gpointer));
  364. if (path != NULL && handler != NULL && router != NULL) {
  365. memcpy (&ptr, &handler, sizeof (ptr));
  366. storage = rspamd_fstring_new_init (path, strlen (path));
  367. key = g_malloc0 (sizeof (*key));
  368. key->begin = storage->str;
  369. key->len = storage->len;
  370. g_hash_table_insert (router->paths, key, ptr);
  371. }
  372. }
  373. void
  374. rspamd_http_router_set_unknown_handler (struct rspamd_http_connection_router *router,
  375. rspamd_http_router_handler_t handler)
  376. {
  377. if (router != NULL) {
  378. router->unknown_method_handler = handler;
  379. }
  380. }
  381. void
  382. rspamd_http_router_add_header (struct rspamd_http_connection_router *router,
  383. const gchar *name, const gchar *value)
  384. {
  385. if (name != NULL && value != NULL && router != NULL) {
  386. g_hash_table_replace (router->response_headers, g_strdup (name),
  387. g_strdup (value));
  388. }
  389. }
  390. void
  391. rspamd_http_router_insert_headers (struct rspamd_http_connection_router *router,
  392. struct rspamd_http_message *msg)
  393. {
  394. GHashTableIter it;
  395. gpointer k, v;
  396. if (router && msg) {
  397. g_hash_table_iter_init (&it, router->response_headers);
  398. while (g_hash_table_iter_next (&it, &k, &v)) {
  399. rspamd_http_message_add_header (msg, k, v);
  400. }
  401. }
  402. }
  403. void
  404. rspamd_http_router_add_regexp (struct rspamd_http_connection_router *router,
  405. struct rspamd_regexp_s *re, rspamd_http_router_handler_t handler)
  406. {
  407. gpointer ptr;
  408. G_STATIC_ASSERT (sizeof (rspamd_http_router_handler_t) ==
  409. sizeof (gpointer));
  410. if (re != NULL && handler != NULL && router != NULL) {
  411. memcpy (&ptr, &handler, sizeof (ptr));
  412. rspamd_regexp_set_ud (re, ptr);
  413. g_ptr_array_add (router->regexps, rspamd_regexp_ref (re));
  414. }
  415. }
  416. void
  417. rspamd_http_router_handle_socket (struct rspamd_http_connection_router *router,
  418. gint fd, gpointer ud)
  419. {
  420. struct rspamd_http_connection_entry *conn;
  421. conn = g_malloc0 (sizeof (struct rspamd_http_connection_entry));
  422. conn->rt = router;
  423. conn->ud = ud;
  424. conn->is_reply = FALSE;
  425. conn->conn = rspamd_http_connection_new_server (router->ctx,
  426. fd,
  427. NULL,
  428. rspamd_http_router_error_handler,
  429. rspamd_http_router_finish_handler,
  430. 0);
  431. if (router->key) {
  432. rspamd_http_connection_set_key (conn->conn, router->key);
  433. }
  434. rspamd_http_connection_read_message (conn->conn, conn, router->timeout);
  435. DL_PREPEND (router->conns, conn);
  436. }
  437. void
  438. rspamd_http_router_free (struct rspamd_http_connection_router *router)
  439. {
  440. struct rspamd_http_connection_entry *conn, *tmp;
  441. rspamd_regexp_t *re;
  442. guint i;
  443. if (router) {
  444. DL_FOREACH_SAFE (router->conns, conn, tmp) {
  445. rspamd_http_entry_free (conn);
  446. }
  447. if (router->key) {
  448. rspamd_keypair_unref (router->key);
  449. }
  450. if (router->default_fs_path != NULL) {
  451. g_free (router->default_fs_path);
  452. }
  453. for (i = 0; i < router->regexps->len; i ++) {
  454. re = g_ptr_array_index (router->regexps, i);
  455. rspamd_regexp_unref (re);
  456. }
  457. g_ptr_array_free (router->regexps, TRUE);
  458. g_hash_table_unref (router->paths);
  459. g_hash_table_unref (router->response_headers);
  460. g_free (router);
  461. }
  462. }