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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*
  2. * Copyright 2024 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_message.h"
  17. #include "http_connection.h"
  18. #include "http_private.h"
  19. #include "libutil/printf.h"
  20. #include "libserver/logger.h"
  21. #include "utlist.h"
  22. #include "unix-std.h"
  23. struct rspamd_http_message *
  24. rspamd_http_new_message(enum rspamd_http_message_type type)
  25. {
  26. struct rspamd_http_message *new;
  27. new = g_malloc0(sizeof(struct rspamd_http_message));
  28. if (type == HTTP_REQUEST) {
  29. new->url = rspamd_fstring_new();
  30. }
  31. else {
  32. new->url = NULL;
  33. new->code = 200;
  34. }
  35. new->port = 80;
  36. new->type = type;
  37. new->method = HTTP_INVALID;
  38. new->headers = kh_init(rspamd_http_headers_hash);
  39. REF_INIT_RETAIN(new, rspamd_http_message_free);
  40. return new;
  41. }
  42. struct rspamd_http_message *
  43. rspamd_http_message_from_url(const char *url)
  44. {
  45. struct http_parser_url pu;
  46. struct rspamd_http_message *msg;
  47. const char *host, *path;
  48. size_t pathlen, urllen;
  49. unsigned int flags = 0;
  50. if (url == NULL) {
  51. return NULL;
  52. }
  53. urllen = strlen(url);
  54. memset(&pu, 0, sizeof(pu));
  55. if (http_parser_parse_url(url, urllen, FALSE, &pu) != 0) {
  56. msg_warn("cannot parse URL: %s", url);
  57. return NULL;
  58. }
  59. if ((pu.field_set & (1 << UF_HOST)) == 0) {
  60. msg_warn("no host argument in URL: %s", url);
  61. return NULL;
  62. }
  63. if ((pu.field_set & (1 << UF_SCHEMA))) {
  64. if (pu.field_data[UF_SCHEMA].len == sizeof("https") - 1 &&
  65. memcmp(url + pu.field_data[UF_SCHEMA].off, "https", 5) == 0) {
  66. flags |= RSPAMD_HTTP_FLAG_WANT_SSL;
  67. }
  68. }
  69. if ((pu.field_set & (1 << UF_PATH)) == 0) {
  70. path = "/";
  71. pathlen = 1;
  72. }
  73. else {
  74. path = url + pu.field_data[UF_PATH].off;
  75. pathlen = pu.field_data[UF_PATH].len;
  76. if (path > url && *(path - 1) == '/') {
  77. path--;
  78. pathlen++;
  79. }
  80. /* Include query if needed */
  81. if ((pu.field_set & (1 << UF_QUERY)) != 0) {
  82. /* Include both ? and query */
  83. pathlen += pu.field_data[UF_QUERY].len + 1;
  84. }
  85. /* Do not include fragment here! */
  86. }
  87. msg = rspamd_http_new_message(HTTP_REQUEST);
  88. host = url + pu.field_data[UF_HOST].off;
  89. msg->flags = flags;
  90. if ((pu.field_set & (1 << UF_PORT)) != 0) {
  91. msg->port = pu.port;
  92. }
  93. else {
  94. /* XXX: magic constant */
  95. if (flags & RSPAMD_HTTP_FLAG_WANT_SSL) {
  96. msg->port = 443;
  97. }
  98. else {
  99. msg->port = 80;
  100. }
  101. }
  102. msg->host = g_string_new_len(host, pu.field_data[UF_HOST].len);
  103. msg->url = rspamd_fstring_append(msg->url, path, pathlen);
  104. REF_INIT_RETAIN(msg, rspamd_http_message_free);
  105. return msg;
  106. }
  107. const char *
  108. rspamd_http_message_get_body(struct rspamd_http_message *msg,
  109. gsize *blen)
  110. {
  111. const char *ret = NULL;
  112. if (msg->body_buf.len > 0) {
  113. ret = msg->body_buf.begin;
  114. }
  115. if (blen) {
  116. *blen = msg->body_buf.len;
  117. }
  118. return ret;
  119. }
  120. static void
  121. rspamd_http_shname_dtor(void *p)
  122. {
  123. struct rspamd_storage_shmem *n = p;
  124. #ifdef HAVE_SANE_SHMEM
  125. shm_unlink(n->shm_name);
  126. #else
  127. unlink(n->shm_name);
  128. #endif
  129. g_free(n->shm_name);
  130. g_free(n);
  131. }
  132. struct rspamd_storage_shmem *
  133. rspamd_http_message_shmem_ref(struct rspamd_http_message *msg)
  134. {
  135. if ((msg->flags & RSPAMD_HTTP_FLAG_SHMEM) && msg->body_buf.c.shared.name) {
  136. REF_RETAIN(msg->body_buf.c.shared.name);
  137. return msg->body_buf.c.shared.name;
  138. }
  139. return NULL;
  140. }
  141. unsigned int rspamd_http_message_get_flags(struct rspamd_http_message *msg)
  142. {
  143. return msg->flags;
  144. }
  145. void rspamd_http_message_shmem_unref(struct rspamd_storage_shmem *p)
  146. {
  147. REF_RELEASE(p);
  148. }
  149. gboolean
  150. rspamd_http_message_set_body(struct rspamd_http_message *msg,
  151. const char *data, gsize len)
  152. {
  153. union _rspamd_storage_u *storage;
  154. storage = &msg->body_buf.c;
  155. rspamd_http_message_storage_cleanup(msg);
  156. if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) {
  157. storage->shared.name = g_malloc(sizeof(*storage->shared.name));
  158. REF_INIT_RETAIN(storage->shared.name, rspamd_http_shname_dtor);
  159. #ifdef HAVE_SANE_SHMEM
  160. #if defined(__DragonFly__)
  161. // DragonFly uses regular files for shm. User rspamd is not allowed to create
  162. // files in the root.
  163. storage->shared.name->shm_name = g_strdup("/tmp/rhm.XXXXXXXXXXXXXXXXXXXX");
  164. #else
  165. storage->shared.name->shm_name = g_strdup("/rhm.XXXXXXXXXXXXXXXXXXXX");
  166. #endif
  167. storage->shared.shm_fd = rspamd_shmem_mkstemp(storage->shared.name->shm_name);
  168. #else
  169. /* XXX: assume that tempdir is /tmp */
  170. storage->shared.name->shm_name = g_strdup("/tmp/rhm.XXXXXXXXXXXXXXXXXXXX");
  171. storage->shared.shm_fd = mkstemp(storage->shared.name->shm_name);
  172. #endif
  173. if (storage->shared.shm_fd == -1) {
  174. return FALSE;
  175. }
  176. if (len != 0 && len != G_MAXSIZE) {
  177. if (ftruncate(storage->shared.shm_fd, len) == -1) {
  178. return FALSE;
  179. }
  180. msg->body_buf.str = mmap(NULL, len,
  181. PROT_WRITE | PROT_READ, MAP_SHARED,
  182. storage->shared.shm_fd, 0);
  183. if (msg->body_buf.str == MAP_FAILED) {
  184. return FALSE;
  185. }
  186. msg->body_buf.begin = msg->body_buf.str;
  187. msg->body_buf.allocated_len = len;
  188. if (data != NULL) {
  189. memcpy(msg->body_buf.str, data, len);
  190. msg->body_buf.len = len;
  191. }
  192. }
  193. else {
  194. msg->body_buf.len = 0;
  195. msg->body_buf.begin = NULL;
  196. msg->body_buf.str = NULL;
  197. msg->body_buf.allocated_len = 0;
  198. }
  199. }
  200. else {
  201. if (len != 0 && len != G_MAXSIZE) {
  202. if (data == NULL) {
  203. storage->normal = rspamd_fstring_sized_new(len);
  204. msg->body_buf.len = 0;
  205. }
  206. else {
  207. storage->normal = rspamd_fstring_new_init(data, len);
  208. msg->body_buf.len = len;
  209. }
  210. }
  211. else {
  212. storage->normal = rspamd_fstring_new();
  213. }
  214. msg->body_buf.begin = storage->normal->str;
  215. msg->body_buf.str = storage->normal->str;
  216. msg->body_buf.allocated_len = storage->normal->allocated;
  217. }
  218. msg->flags |= RSPAMD_HTTP_FLAG_HAS_BODY;
  219. return TRUE;
  220. }
  221. void rspamd_http_message_set_method(struct rspamd_http_message *msg,
  222. const char *method)
  223. {
  224. int i;
  225. /* Linear search: not very efficient method */
  226. for (i = 0; i < HTTP_METHOD_MAX; i++) {
  227. if (g_ascii_strcasecmp(method, http_method_str(i)) == 0) {
  228. msg->method = i;
  229. }
  230. }
  231. }
  232. gboolean
  233. rspamd_http_message_set_body_from_fd(struct rspamd_http_message *msg,
  234. int fd)
  235. {
  236. union _rspamd_storage_u *storage;
  237. struct stat st;
  238. rspamd_http_message_storage_cleanup(msg);
  239. storage = &msg->body_buf.c;
  240. msg->flags |= RSPAMD_HTTP_FLAG_SHMEM | RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE;
  241. storage->shared.shm_fd = dup(fd);
  242. msg->body_buf.str = MAP_FAILED;
  243. if (storage->shared.shm_fd == -1) {
  244. return FALSE;
  245. }
  246. if (fstat(storage->shared.shm_fd, &st) == -1) {
  247. return FALSE;
  248. }
  249. msg->body_buf.str = mmap(NULL, st.st_size,
  250. PROT_READ, MAP_SHARED,
  251. storage->shared.shm_fd, 0);
  252. if (msg->body_buf.str == MAP_FAILED) {
  253. return FALSE;
  254. }
  255. msg->body_buf.begin = msg->body_buf.str;
  256. msg->body_buf.len = st.st_size;
  257. msg->body_buf.allocated_len = st.st_size;
  258. return TRUE;
  259. }
  260. gboolean
  261. rspamd_http_message_set_body_from_fstring_steal(struct rspamd_http_message *msg,
  262. rspamd_fstring_t *fstr)
  263. {
  264. union _rspamd_storage_u *storage;
  265. rspamd_http_message_storage_cleanup(msg);
  266. storage = &msg->body_buf.c;
  267. msg->flags &= ~(RSPAMD_HTTP_FLAG_SHMEM | RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE);
  268. storage->normal = fstr;
  269. msg->body_buf.str = fstr->str;
  270. msg->body_buf.begin = msg->body_buf.str;
  271. msg->body_buf.len = fstr->len;
  272. msg->body_buf.allocated_len = fstr->allocated;
  273. return TRUE;
  274. }
  275. gboolean
  276. rspamd_http_message_set_body_from_fstring_copy(struct rspamd_http_message *msg,
  277. const rspamd_fstring_t *fstr)
  278. {
  279. union _rspamd_storage_u *storage;
  280. rspamd_http_message_storage_cleanup(msg);
  281. storage = &msg->body_buf.c;
  282. msg->flags &= ~(RSPAMD_HTTP_FLAG_SHMEM | RSPAMD_HTTP_FLAG_SHMEM_IMMUTABLE);
  283. storage->normal = rspamd_fstring_new_init(fstr->str, fstr->len);
  284. msg->body_buf.str = storage->normal->str;
  285. msg->body_buf.begin = msg->body_buf.str;
  286. msg->body_buf.len = storage->normal->len;
  287. msg->body_buf.allocated_len = storage->normal->allocated;
  288. return TRUE;
  289. }
  290. gboolean
  291. rspamd_http_message_grow_body(struct rspamd_http_message *msg, gsize len)
  292. {
  293. struct stat st;
  294. union _rspamd_storage_u *storage;
  295. gsize newlen;
  296. storage = &msg->body_buf.c;
  297. if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) {
  298. if (storage->shared.shm_fd == -1) {
  299. return FALSE;
  300. }
  301. if (fstat(storage->shared.shm_fd, &st) == -1) {
  302. return FALSE;
  303. }
  304. /* Check if we need to grow */
  305. if ((gsize) st.st_size < msg->body_buf.len + len) {
  306. /* Need to grow */
  307. newlen = rspamd_fstring_suggest_size(msg->body_buf.len, st.st_size,
  308. len);
  309. /* Unmap as we need another size of segment */
  310. if (msg->body_buf.str != MAP_FAILED) {
  311. munmap(msg->body_buf.str, st.st_size);
  312. }
  313. if (ftruncate(storage->shared.shm_fd, newlen) == -1) {
  314. return FALSE;
  315. }
  316. msg->body_buf.str = mmap(NULL, newlen,
  317. PROT_WRITE | PROT_READ, MAP_SHARED,
  318. storage->shared.shm_fd, 0);
  319. if (msg->body_buf.str == MAP_FAILED) {
  320. return FALSE;
  321. }
  322. msg->body_buf.begin = msg->body_buf.str;
  323. msg->body_buf.allocated_len = newlen;
  324. }
  325. }
  326. else {
  327. storage->normal = rspamd_fstring_grow(storage->normal, len);
  328. /* Append might cause realloc */
  329. msg->body_buf.begin = storage->normal->str;
  330. msg->body_buf.len = storage->normal->len;
  331. msg->body_buf.str = storage->normal->str;
  332. msg->body_buf.allocated_len = storage->normal->allocated;
  333. }
  334. return TRUE;
  335. }
  336. gboolean
  337. rspamd_http_message_append_body(struct rspamd_http_message *msg,
  338. const char *data, gsize len)
  339. {
  340. union _rspamd_storage_u *storage;
  341. storage = &msg->body_buf.c;
  342. if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) {
  343. if (!rspamd_http_message_grow_body(msg, len)) {
  344. return FALSE;
  345. }
  346. memcpy(msg->body_buf.str + msg->body_buf.len, data, len);
  347. msg->body_buf.len += len;
  348. }
  349. else {
  350. storage->normal = rspamd_fstring_append(storage->normal, data, len);
  351. /* Append might cause realloc */
  352. msg->body_buf.begin = storage->normal->str;
  353. msg->body_buf.len = storage->normal->len;
  354. msg->body_buf.str = storage->normal->str;
  355. msg->body_buf.allocated_len = storage->normal->allocated;
  356. }
  357. return TRUE;
  358. }
  359. void rspamd_http_message_storage_cleanup(struct rspamd_http_message *msg)
  360. {
  361. union _rspamd_storage_u *storage;
  362. struct stat st;
  363. if (msg->flags & RSPAMD_HTTP_FLAG_SHMEM) {
  364. storage = &msg->body_buf.c;
  365. if (storage->shared.shm_fd > 0) {
  366. g_assert(fstat(storage->shared.shm_fd, &st) != -1);
  367. if (msg->body_buf.str != MAP_FAILED) {
  368. munmap(msg->body_buf.str, st.st_size);
  369. }
  370. close(storage->shared.shm_fd);
  371. }
  372. if (storage->shared.name != NULL) {
  373. REF_RELEASE(storage->shared.name);
  374. }
  375. storage->shared.shm_fd = -1;
  376. msg->body_buf.str = MAP_FAILED;
  377. }
  378. else {
  379. if (msg->body_buf.c.normal) {
  380. rspamd_fstring_free(msg->body_buf.c.normal);
  381. }
  382. msg->body_buf.c.normal = NULL;
  383. }
  384. msg->body_buf.len = 0;
  385. }
  386. void rspamd_http_message_free(struct rspamd_http_message *msg)
  387. {
  388. struct rspamd_http_header *hdr, *hcur, *hcurtmp;
  389. kh_foreach_value (msg->headers, hdr, {
  390. DL_FOREACH_SAFE (hdr, hcur, hcurtmp) {
  391. rspamd_fstring_free (hcur->combined);
  392. g_free (hcur);
  393. }
  394. });
  395. kh_destroy(rspamd_http_headers_hash, msg->headers);
  396. rspamd_http_message_storage_cleanup(msg);
  397. if (msg->url != NULL) {
  398. rspamd_fstring_free(msg->url);
  399. }
  400. if (msg->status != NULL) {
  401. rspamd_fstring_free(msg->status);
  402. }
  403. if (msg->host != NULL) {
  404. g_string_free(msg->host, TRUE);
  405. }
  406. if (msg->peer_key != NULL) {
  407. rspamd_pubkey_unref(msg->peer_key);
  408. }
  409. g_free(msg);
  410. }
  411. void rspamd_http_message_set_peer_key(struct rspamd_http_message *msg,
  412. struct rspamd_cryptobox_pubkey *pk)
  413. {
  414. if (msg->peer_key != NULL) {
  415. rspamd_pubkey_unref(msg->peer_key);
  416. }
  417. if (pk) {
  418. msg->peer_key = rspamd_pubkey_ref(pk);
  419. }
  420. else {
  421. msg->peer_key = NULL;
  422. }
  423. }
  424. void rspamd_http_message_add_header_len(struct rspamd_http_message *msg,
  425. const char *name,
  426. const char *value,
  427. gsize len)
  428. {
  429. struct rspamd_http_header *hdr, *found;
  430. unsigned int nlen, vlen;
  431. khiter_t k;
  432. int r;
  433. if (msg != NULL && name != NULL && value != NULL) {
  434. hdr = g_malloc0(sizeof(struct rspamd_http_header));
  435. nlen = strlen(name);
  436. vlen = len;
  437. if (g_ascii_strcasecmp(name, "host") == 0) {
  438. msg->flags |= RSPAMD_HTTP_FLAG_HAS_HOST_HEADER;
  439. }
  440. hdr->combined = rspamd_fstring_sized_new(nlen + vlen + 4);
  441. rspamd_printf_fstring(&hdr->combined, "%s: %*s\r\n", name, (int) vlen,
  442. value);
  443. hdr->name.begin = hdr->combined->str;
  444. hdr->name.len = nlen;
  445. hdr->value.begin = hdr->combined->str + nlen + 2;
  446. hdr->value.len = vlen;
  447. k = kh_put(rspamd_http_headers_hash, msg->headers, &hdr->name,
  448. &r);
  449. if (r != 0) {
  450. kh_value(msg->headers, k) = hdr;
  451. found = NULL;
  452. }
  453. else {
  454. found = kh_value(msg->headers, k);
  455. }
  456. DL_APPEND(found, hdr);
  457. }
  458. }
  459. void rspamd_http_message_add_header(struct rspamd_http_message *msg,
  460. const char *name,
  461. const char *value)
  462. {
  463. if (value) {
  464. rspamd_http_message_add_header_len(msg, name, value, strlen(value));
  465. }
  466. }
  467. void rspamd_http_message_add_header_fstr(struct rspamd_http_message *msg,
  468. const char *name,
  469. rspamd_fstring_t *value)
  470. {
  471. struct rspamd_http_header *hdr, *found = NULL;
  472. unsigned int nlen, vlen;
  473. khiter_t k;
  474. int r;
  475. if (msg != NULL && name != NULL && value != NULL) {
  476. hdr = g_malloc0(sizeof(struct rspamd_http_header));
  477. nlen = strlen(name);
  478. vlen = value->len;
  479. hdr->combined = rspamd_fstring_sized_new(nlen + vlen + 4);
  480. rspamd_printf_fstring(&hdr->combined, "%s: %V\r\n", name, value);
  481. hdr->name.begin = hdr->combined->str;
  482. hdr->name.len = nlen;
  483. hdr->value.begin = hdr->combined->str + nlen + 2;
  484. hdr->value.len = vlen;
  485. k = kh_put(rspamd_http_headers_hash, msg->headers, &hdr->name,
  486. &r);
  487. if (r != 0) {
  488. kh_value(msg->headers, k) = hdr;
  489. found = NULL;
  490. }
  491. else {
  492. found = kh_value(msg->headers, k);
  493. }
  494. DL_APPEND(found, hdr);
  495. }
  496. }
  497. const rspamd_ftok_t *
  498. rspamd_http_message_find_header(struct rspamd_http_message *msg,
  499. const char *name)
  500. {
  501. const rspamd_ftok_t *res = NULL;
  502. rspamd_ftok_t srch;
  503. unsigned int slen = strlen(name);
  504. khiter_t k;
  505. if (msg != NULL) {
  506. srch.begin = name;
  507. srch.len = slen;
  508. k = kh_get(rspamd_http_headers_hash, msg->headers, &srch);
  509. if (k != kh_end(msg->headers)) {
  510. res = &(kh_value(msg->headers, k)->value);
  511. }
  512. }
  513. return res;
  514. }
  515. GPtrArray *
  516. rspamd_http_message_find_header_multiple(
  517. struct rspamd_http_message *msg,
  518. const char *name)
  519. {
  520. GPtrArray *res = NULL;
  521. struct rspamd_http_header *hdr, *cur;
  522. rspamd_ftok_t srch;
  523. khiter_t k;
  524. unsigned int cnt = 0;
  525. unsigned int slen = strlen(name);
  526. if (msg != NULL) {
  527. srch.begin = name;
  528. srch.len = slen;
  529. k = kh_get(rspamd_http_headers_hash, msg->headers, &srch);
  530. if (k != kh_end(msg->headers)) {
  531. hdr = kh_value(msg->headers, k);
  532. LL_COUNT(hdr, cur, cnt);
  533. res = g_ptr_array_sized_new(cnt);
  534. LL_FOREACH(hdr, cur)
  535. {
  536. g_ptr_array_add(res, &cur->value);
  537. }
  538. }
  539. }
  540. return res;
  541. }
  542. gboolean
  543. rspamd_http_message_remove_header(struct rspamd_http_message *msg,
  544. const char *name)
  545. {
  546. struct rspamd_http_header *hdr, *hcur, *hcurtmp;
  547. gboolean res = FALSE;
  548. unsigned int slen = strlen(name);
  549. rspamd_ftok_t srch;
  550. khiter_t k;
  551. if (msg != NULL) {
  552. srch.begin = name;
  553. srch.len = slen;
  554. k = kh_get(rspamd_http_headers_hash, msg->headers, &srch);
  555. if (k != kh_end(msg->headers)) {
  556. hdr = kh_value(msg->headers, k);
  557. kh_del(rspamd_http_headers_hash, msg->headers, k);
  558. res = TRUE;
  559. DL_FOREACH_SAFE(hdr, hcur, hcurtmp)
  560. {
  561. rspamd_fstring_free(hcur->combined);
  562. g_free(hcur);
  563. }
  564. }
  565. }
  566. return res;
  567. }
  568. const char *
  569. rspamd_http_message_get_http_host(struct rspamd_http_message *msg,
  570. gsize *hostlen)
  571. {
  572. if (msg->flags & RSPAMD_HTTP_FLAG_HAS_HOST_HEADER) {
  573. rspamd_ftok_t srch;
  574. RSPAMD_FTOK_ASSIGN(&srch, "Host");
  575. khiter_t k = kh_get(rspamd_http_headers_hash, msg->headers, &srch);
  576. if (k != kh_end(msg->headers)) {
  577. *hostlen = (kh_value(msg->headers, k)->value).len;
  578. return (kh_value(msg->headers, k)->value).begin;
  579. }
  580. else if (msg->host) {
  581. *hostlen = msg->host->len;
  582. return msg->host->str;
  583. }
  584. }
  585. else {
  586. if (msg->host) {
  587. *hostlen = msg->host->len;
  588. return msg->host->str;
  589. }
  590. }
  591. return NULL;
  592. }
  593. bool rspamd_http_message_is_standard_port(struct rspamd_http_message *msg)
  594. {
  595. if (msg->flags & RSPAMD_HTTP_FLAG_WANT_SSL) {
  596. return msg->port == 443;
  597. }
  598. return msg->port == 80;
  599. }
  600. const char *rspamd_http_message_get_url(struct rspamd_http_message *msg, gsize *len)
  601. {
  602. if (msg->url) {
  603. *len = msg->url->len;
  604. return msg->url->str;
  605. }
  606. return NULL;
  607. }