static void
rspamc_output_headers (FILE *out, struct rspamd_http_message *msg)
{
- struct rspamd_http_header *h, *htmp;
+ struct rspamd_http_header *h;
- HASH_ITER (hh, msg->headers, h, htmp) {
+ kh_foreach_value (msg->headers, h, {
rspamd_fprintf (out, "%T: %T\n", &h->name, &h->value);
- }
+ });
rspamd_fprintf (out, "\n");
}
{
rspamd_ftok_t *hn_tok, *hv_tok, srch;
gboolean has_ip = FALSE, seen_settings_header = FALSE;
- struct rspamd_http_header *header, *h, *htmp;
+ struct rspamd_http_header *header, *h;
gchar *ntok;
- HASH_ITER (hh, msg->headers, header, htmp) {
+ kh_foreach_value (msg->headers, header, {
DL_FOREACH (header, h) {
ntok = rspamd_mempool_ftokdup (task->task_pool, &h->name);
hn_tok = rspamd_mempool_alloc (task->task_pool, sizeof (*hn_tok));
rspamd_task_add_request_header (task, hn_tok, hv_tok);
}
- }
+ }); /* End of kh_foreach_value */
if (seen_settings_header && task->settings_elt) {
msg_warn_task ("ignore settings id %s as settings header is also presented",
struct rspamd_http_connection_private *priv)
{
struct rspamd_http_header *hdr;
+ khiter_t k;
+ gint r;
priv->header->combined = rspamd_fstring_append (priv->header->combined,
"\r\n", 2);
priv->header->name.len + 2;
priv->header->name.begin = priv->header->combined->str;
- HASH_FIND (hh, priv->msg->headers, priv->header->name.begin,
- priv->header->name.len, hdr);
+ k = kh_put (rspamd_http_headers_hash, priv->msg->headers, &priv->header->name,
+ &r);
- if (hdr == NULL) {
- HASH_ADD_KEYPTR (hh, priv->msg->headers, priv->header->name.begin,
- priv->header->name.len, priv->header);
+ if (r != 0) {
+ kh_value (priv->msg->headers, k) = priv->header;
+ hdr = NULL;
+ }
+ else {
+ hdr = kh_value (priv->msg->headers, k);
}
DL_APPEND (hdr, priv->header);
const guchar *nm;
gsize dec_len;
struct rspamd_http_message *msg = priv->msg;
- struct rspamd_http_header *hdr, *hdrtmp, *hcur, *hcurtmp;
+ struct rspamd_http_header *hdr, *hcur, *hcurtmp;
struct http_parser decrypted_parser;
struct http_parser_settings decrypted_cb;
enum rspamd_cryptobox_mode mode;
}
/* Cleanup message */
- HASH_ITER (hh, msg->headers, hdr, hdrtmp) {
- HASH_DELETE (hh, msg->headers, hdr);
-
+ kh_foreach_value (msg->headers, hdr, {
DL_FOREACH_SAFE (hdr, hcur, hcurtmp) {
rspamd_fstring_free (hcur->combined);
g_free (hcur);
}
- }
+ });
- msg->headers = NULL;
+ kh_destroy (rspamd_http_headers_hash, msg->headers);
+ msg->headers = kh_init (rspamd_http_headers_hash);
if (msg->url != NULL) {
msg->url = rspamd_fstring_assign (msg->url, "", 0);
rspamd_http_connection_copy_msg (struct rspamd_http_message *msg, GError **err)
{
struct rspamd_http_message *new_msg;
- struct rspamd_http_header *hdr, *nhdr, *nhdrs, *thdr, *hcur;
+ struct rspamd_http_header *hdr, *nhdr, *nhdrs, *hcur;
const gchar *old_body;
gsize old_len;
struct stat st;
new_msg->date = msg->date;
new_msg->last_modified = msg->last_modified;
- HASH_ITER (hh, msg->headers, hdr, thdr) {
+ kh_foreach_value (msg->headers, hdr, {
nhdrs = NULL;
DL_FOREACH (hdr, hcur) {
nhdr->combined = rspamd_fstring_new_init (hcur->combined->str,
hcur->combined->len);
nhdr->name.begin = nhdr->combined->str +
- (hcur->name.begin - hcur->combined->str);
+ (hcur->name.begin - hcur->combined->str);
nhdr->name.len = hcur->name.len;
nhdr->value.begin = nhdr->combined->str +
- (hcur->value.begin - hcur->combined->str);
+ (hcur->value.begin - hcur->combined->str);
nhdr->value.len = hcur->value.len;
DL_APPEND (nhdrs, nhdr);
}
- HASH_ADD_KEYPTR (hh, new_msg->headers, nhdrs->name.begin,
- nhdrs->name.len, nhdrs);
- }
+ gint r;
+ khiter_t k = kh_put (rspamd_http_headers_hash, new_msg->headers,
+ &nhdrs->name,&r);
+
+ if (r != 0) {
+ kh_value (new_msg->headers, k) = nhdrs;
+ }
+ else {
+ DL_CONCAT (kh_value (new_msg->headers, k), nhdrs);
+ }
+ });
return new_msg;
}
const guchar *nm;
gint i, cnt;
guint outlen;
- struct rspamd_http_header *hdr, *htmp, *hcur;
+ struct rspamd_http_header *hdr, *hcur;
enum rspamd_cryptobox_mode mode;
mode = rspamd_keypair_alg (priv->local_key);
}
- HASH_ITER (hh, msg->headers, hdr, htmp) {
+ kh_foreach_value (msg->headers, hdr, {
DL_FOREACH (hdr, hcur) {
segments[i].data = hcur->combined->str;
segments[i++].len = hcur->combined->len;
}
- }
+ });
/* crlfp should point now at the second crlf */
segments[i].data = crlfp;
gboolean allow_shared)
{
struct rspamd_http_connection_private *priv = conn->priv;
- struct rspamd_http_header *hdr, *htmp, *hcur;
+ struct rspamd_http_header *hdr, *hcur;
gchar repbuf[512], *pbody;
gint i, hdrcount, meth_len = 0, preludelen = 0;
gsize bodylen, enclen = 0;
}
if (priv->ctx->config.user_agent && conn->type == RSPAMD_HTTP_CLIENT) {
- struct rspamd_http_header *found = NULL;
+ rspamd_ftok_t srch;
+ khiter_t k;
+ gint r;
+
+ RSPAMD_FTOK_ASSIGN (&srch, "User-Agent");
- HASH_FIND (hh, msg->headers, "User-Agent",
- sizeof ("User-Agent") - 1, found);
+ k = kh_put (rspamd_http_headers_hash, msg->headers, &srch,&r);
- if (!found) {
- rspamd_http_message_add_header (msg, "User-Agent",
+ if (r != 0) {
+ hdr = g_malloc0 (sizeof (struct rspamd_http_header));
+ guint vlen = strlen (priv->ctx->config.user_agent);
+ hdr->combined = rspamd_fstring_sized_new (srch.len + vlen + 4);
+ rspamd_printf_fstring (&hdr->combined, "%T: %*s\r\n", &srch, vlen,
priv->ctx->config.user_agent);
+ hdr->name.begin = hdr->combined->str;
+ hdr->name.len = srch.len;
+ hdr->value.begin = hdr->combined->str + srch.len + 2;
+ hdr->value.len = vlen;
+ hdr->prev = hdr; /* for utlists */
+
+ kh_value (msg->headers, k) = hdr;
+ /* as we searched using static buffer */
+ kh_key (msg->headers, k) = &hdr->name;
}
}
hdrcount = 0;
if (msg->method < HTTP_SYMBOLS) {
- HASH_ITER (hh, msg->headers, hdr, htmp) {
+ kh_foreach_value (msg->headers, hdr, {
DL_FOREACH (hdr, hcur) {
/* <name: value\r\n> */
priv->wr_total += hcur->combined->len;
priv->outlen ++;
hdrcount ++;
}
- }
+ });
}
/* Allocate iov */
else {
i = 1;
if (msg->method < HTTP_SYMBOLS) {
- HASH_ITER (hh, msg->headers, hdr, htmp) {
+ kh_foreach_value (msg->headers, hdr, {
DL_FOREACH (hdr, hcur) {
priv->out[i].iov_base = hcur->combined->str;
priv->out[i++].iov_len = hcur->combined->len;
}
- }
+ });
priv->out[i].iov_base = "\r\n";
priv->out[i++].iov_len = 2;
new->port = 80;
new->type = type;
new->method = HTTP_INVALID;
+ new->headers = kh_init (rspamd_http_headers_hash);
REF_INIT_RETAIN (new, rspamd_http_message_free);
void
rspamd_http_message_free (struct rspamd_http_message *msg)
{
- struct rspamd_http_header *hdr, *htmp, *hcur, *hcurtmp;
-
-
- HASH_ITER (hh, msg->headers, hdr, htmp) {
- HASH_DEL (msg->headers, hdr);
+ struct rspamd_http_header *hdr, *hcur, *hcurtmp;
+ kh_foreach_value (msg->headers, hdr, {
DL_FOREACH_SAFE (hdr, hcur, hcurtmp) {
rspamd_fstring_free (hcur->combined);
g_free (hcur);
}
- }
+ });
+ kh_destroy (rspamd_http_headers_hash, msg->headers);
rspamd_http_message_storage_cleanup (msg);
if (msg->url != NULL) {
const gchar *value,
gsize len)
{
- struct rspamd_http_header *hdr, *found = NULL;
+ struct rspamd_http_header *hdr, *found;
guint nlen, vlen;
+ khiter_t k;
+ gint r;
if (msg != NULL && name != NULL && value != NULL) {
hdr = g_malloc0 (sizeof (struct rspamd_http_header));
hdr->value.begin = hdr->combined->str + nlen + 2;
hdr->value.len = vlen;
- HASH_FIND (hh, msg->headers, hdr->name.begin,
- hdr->name.len, found);
+ k = kh_put (rspamd_http_headers_hash, msg->headers, &hdr->name,
+ &r);
- if (found == NULL) {
- HASH_ADD_KEYPTR (hh, msg->headers, hdr->name.begin,
- hdr->name.len, hdr);
+ if (r != 0) {
+ kh_value (msg->headers, k) = hdr;
+ found = NULL;
+ }
+ else {
+ found = kh_value (msg->headers, k);
}
DL_APPEND (found, hdr);
{
struct rspamd_http_header *hdr, *found = NULL;
guint nlen, vlen;
+ khiter_t k;
+ gint r;
if (msg != NULL && name != NULL && value != NULL) {
hdr = g_malloc0 (sizeof (struct rspamd_http_header));
hdr->value.begin = hdr->combined->str + nlen + 2;
hdr->value.len = vlen;
- HASH_FIND (hh, msg->headers, hdr->name.begin,
- hdr->name.len, found);
+ k = kh_put (rspamd_http_headers_hash, msg->headers, &hdr->name,
+ &r);
- if (found == NULL) {
- HASH_ADD_KEYPTR (hh, msg->headers, hdr->name.begin,
- hdr->name.len, hdr);
+ if (r != 0) {
+ kh_value (msg->headers, k) = hdr;
+ found = NULL;
+ }
+ else {
+ found = kh_value (msg->headers, k);
}
DL_APPEND (found, hdr);
rspamd_http_message_find_header (struct rspamd_http_message *msg,
const gchar *name)
{
- struct rspamd_http_header *hdr;
const rspamd_ftok_t *res = NULL;
+ rspamd_ftok_t srch;
guint slen = strlen (name);
+ khiter_t k;
if (msg != NULL) {
- HASH_FIND (hh, msg->headers, name, slen, hdr);
+ srch.begin = name;
+ srch.len = slen;
+
+ k = kh_get (rspamd_http_headers_hash, msg->headers, &srch);
- if (hdr) {
- res = &hdr->value;
+ if (k != kh_end (msg->headers)) {
+ res = &(kh_value (msg->headers, k)->value);
}
}
{
GPtrArray *res = NULL;
struct rspamd_http_header *hdr, *cur;
+ rspamd_ftok_t srch;
+ khiter_t k;
+ guint cnt = 0;
guint slen = strlen (name);
if (msg != NULL) {
- HASH_FIND (hh, msg->headers, name, slen, hdr);
+ srch.begin = name;
+ srch.len = slen;
- if (hdr) {
- res = g_ptr_array_sized_new (4);
+ k = kh_get (rspamd_http_headers_hash, msg->headers, &srch);
+
+ if (k != kh_end (msg->headers)) {
+ hdr = kh_value (msg->headers, k);
+
+ LL_COUNT (hdr, cur, cnt);
+ res = g_ptr_array_sized_new (cnt);
LL_FOREACH (hdr, cur) {
g_ptr_array_add (res, &cur->value);
struct rspamd_http_header *hdr, *hcur, *hcurtmp;
gboolean res = FALSE;
guint slen = strlen (name);
+ rspamd_ftok_t srch;
+ khiter_t k;
if (msg != NULL) {
- HASH_FIND (hh, msg->headers, name, slen, hdr);
+ srch.begin = name;
+ srch.len = slen;
+
+ k = kh_get (rspamd_http_headers_hash, msg->headers, &srch);
- if (hdr) {
- HASH_DEL (msg->headers, hdr);
+ if (k != kh_end (msg->headers)) {
+ hdr = kh_value (msg->headers, k);
+ kh_del (rspamd_http_headers_hash, msg->headers, k);
res = TRUE;
DL_FOREACH_SAFE (hdr, hcur, hcurtmp) {
#include "ref.h"
#include "upstream.h"
#include "khash.h"
-#define HASH_CASELESS
-#include "uthash_strcase.h"
#ifdef __cplusplus
extern "C" {
rspamd_fstring_t *combined;
rspamd_ftok_t name;
rspamd_ftok_t value;
- UT_hash_handle hh;
struct rspamd_http_header *prev, *next;
};
+KHASH_INIT (rspamd_http_headers_hash, rspamd_ftok_t *,
+ struct rspamd_http_header *, 1,
+ rspamd_ftok_icase_hash, rspamd_ftok_icase_equal);
+
/**
* HTTP message structure, used for requests and replies
*/
rspamd_fstring_t *url;
GString *host;
rspamd_fstring_t *status;
- struct rspamd_http_header *headers;
+ khash_t (rspamd_http_headers_hash) *headers;
struct _rspamd_body_buf_s {
/* Data start */
struct rspamd_http_message *msg)
{
struct lua_http_cbdata *cbd = (struct lua_http_cbdata *)conn->ud;
- struct rspamd_http_header *h, *htmp;
+ struct rspamd_http_header *h;
const gchar *body;
gsize body_len;
/* Headers */
lua_newtable (L);
- HASH_ITER (hh, msg->headers, h, htmp) {
+ kh_foreach_value (msg->headers, h, {
/*
* Lowercase header name, as Lua cannot search in caseless matter
*/
lua_pushlstring (L, h->name.begin, h->name.len);
lua_pushlstring (L, h->value.begin, h->value.len);
lua_settable (L, -3);
- }
+ });
if (cbd->item) {
/* Replace watcher to deal with nested calls */
lua_State *L = cbd->thread->lua_state;
const gchar *body;
gsize body_len;
- struct rspamd_http_header *h, *htmp;
+ struct rspamd_http_header *h;
if (err) {
lua_pushstring (L, err);
lua_pushliteral (L, "headers");
lua_newtable (L);
- HASH_ITER (hh, msg->headers, h, htmp) {
+ kh_foreach_value (msg->headers, h, {
/*
* Lowercase header name, as Lua cannot search in caseless matter
*/
lua_pushlstring (L, h->name.begin, h->name.len);
lua_pushlstring (L, h->value.begin, h->value.len);
lua_settable (L, -3);
- }
+ });
lua_settable (L, -3);
}