1 /*-
2  * Copyright 2016 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 SRC_LIBUTIL_HTTP_PRIVATE_H_
17 #define SRC_LIBUTIL_HTTP_PRIVATE_H_
18 
19 #include "http_connection.h"
20 #include "http_parser.h"
21 #include "str_util.h"
22 #include "keypair.h"
23 #include "keypairs_cache.h"
24 #include "ref.h"
25 #include "upstream.h"
26 #include "khash.h"
27 
28 #ifdef  __cplusplus
29 extern "C" {
30 #endif
31 
32 /**
33  * HTTP header structure
34  */
35 struct rspamd_http_header {
36 	rspamd_fstring_t *combined;
37 	rspamd_ftok_t name;
38 	rspamd_ftok_t value;
39 	struct rspamd_http_header *prev, *next;
40 };
41 
42 KHASH_INIT (rspamd_http_headers_hash, rspamd_ftok_t *,
43 		struct rspamd_http_header *, 1,
44 		rspamd_ftok_icase_hash, rspamd_ftok_icase_equal);
45 
46 /**
47  * HTTP message structure, used for requests and replies
48  */
49 struct rspamd_http_message {
50 	rspamd_fstring_t *url;
51 	GString *host;
52 	rspamd_fstring_t *status;
53 	khash_t (rspamd_http_headers_hash) *headers;
54 
55 	struct _rspamd_body_buf_s {
56 		/* Data start */
57 		const gchar *begin;
58 		/* Data len */
59 		gsize len;
60 		/* Allocated len */
61 		gsize allocated_len;
62 		/* Data buffer (used to write data inside) */
63 		gchar *str;
64 
65 		/* Internal storage */
66 		union _rspamd_storage_u {
67 			rspamd_fstring_t *normal;
68 			struct _rspamd_storage_shared_s {
69 				struct rspamd_storage_shmem *name;
70 				gint shm_fd;
71 			} shared;
72 		} c;
73 	} body_buf;
74 
75 	struct rspamd_cryptobox_pubkey *peer_key;
76 	time_t date;
77 	time_t last_modified;
78 	unsigned port;
79 	int type;
80 	gint code;
81 	enum http_method method;
82 	gint flags;
83 	ref_entry_t ref;
84 };
85 
86 struct rspamd_keepalive_hash_key {
87 	rspamd_inet_addr_t *addr;
88 	gchar *host;
89 	GQueue conns;
90 };
91 
92 gint32 rspamd_keep_alive_key_hash (struct rspamd_keepalive_hash_key *k);
93 
94 bool rspamd_keep_alive_key_equal (struct rspamd_keepalive_hash_key *k1,
95 								  struct rspamd_keepalive_hash_key *k2);
96 
97 KHASH_INIT (rspamd_keep_alive_hash, struct rspamd_keepalive_hash_key *,
98 		char, 0, rspamd_keep_alive_key_hash, rspamd_keep_alive_key_equal);
99 
100 struct rspamd_http_context {
101 	struct rspamd_http_context_cfg config;
102 	struct rspamd_keypair_cache *client_kp_cache;
103 	struct rspamd_cryptobox_keypair *client_kp;
104 	struct rspamd_keypair_cache *server_kp_cache;
105 	struct upstream_ctx *ups_ctx;
106 	struct upstream_list *http_proxies;
107 	gpointer ssl_ctx;
108 	gpointer ssl_ctx_noverify;
109 	struct ev_loop *event_loop;
110 	ev_timer client_rotate_ev;
111 	khash_t (rspamd_keep_alive_hash) *keep_alive_hash;
112 };
113 
114 #define HTTP_ERROR http_error_quark ()
115 
116 GQuark http_error_quark (void);
117 
118 void rspamd_http_message_storage_cleanup (struct rspamd_http_message *msg);
119 
120 gboolean rspamd_http_message_grow_body (struct rspamd_http_message *msg,
121 										gsize len);
122 
123 #ifdef  __cplusplus
124 }
125 #endif
126 
127 #endif /* SRC_LIBUTIL_HTTP_PRIVATE_H_ */
128