1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/*-
* Copyright 2019 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef RSPAMD_HTTP_MESSAGE_H
#define RSPAMD_HTTP_MESSAGE_H
#include "config.h"
#include "keypair.h"
#include "keypairs_cache.h"
#include "fstring.h"
#include "ref.h"
#ifdef __cplusplus
extern "C" {
#endif
struct rspamd_http_connection;
enum rspamd_http_message_type {
HTTP_REQUEST = 0,
HTTP_RESPONSE
};
/**
* Extract the current message from a connection to deal with separately
* @param conn
* @return
*/
struct rspamd_http_message *rspamd_http_connection_steal_msg(
struct rspamd_http_connection *conn);
/**
* Copy the current message from a connection to deal with separately
* @param conn
* @return
*/
struct rspamd_http_message *rspamd_http_connection_copy_msg(
struct rspamd_http_message *msg, GError **err);
/**
* Create new HTTP message
* @param type request or response
* @return new http message
*/
struct rspamd_http_message *rspamd_http_new_message(enum rspamd_http_message_type type);
/**
* Increase refcount number for an HTTP message
* @param msg message to use
* @return
*/
struct rspamd_http_message *rspamd_http_message_ref(struct rspamd_http_message *msg);
/**
* Decrease number of refcounts for http message
* @param msg
*/
void rspamd_http_message_unref(struct rspamd_http_message *msg);
/**
* Sets a key for peer
* @param msg
* @param pk
*/
void rspamd_http_message_set_peer_key(struct rspamd_http_message *msg,
struct rspamd_cryptobox_pubkey *pk);
/**
* Create HTTP message from URL
* @param url
* @return new message or NULL
*/
struct rspamd_http_message *rspamd_http_message_from_url(const gchar *url);
/**
* Returns body for a message
* @param msg
* @param blen pointer where to save body length
* @return pointer to body start
*/
const gchar *rspamd_http_message_get_body(struct rspamd_http_message *msg,
gsize *blen);
/**
* Set message's body from the string
* @param msg
* @param data
* @param len
* @return TRUE if a message's body has been set
*/
gboolean rspamd_http_message_set_body(struct rspamd_http_message *msg,
const gchar *data, gsize len);
/**
* Set message's method by name
* @param msg
* @param method
*/
void rspamd_http_message_set_method(struct rspamd_http_message *msg,
const gchar *method);
/**
* Maps fd as message's body
* @param msg
* @param fd
* @return TRUE if a message's body has been set
*/
gboolean rspamd_http_message_set_body_from_fd(struct rspamd_http_message *msg,
gint fd);
/**
* Uses rspamd_fstring_t as message's body, string is consumed by this operation
* @param msg
* @param fstr
* @return TRUE if a message's body has been set
*/
gboolean rspamd_http_message_set_body_from_fstring_steal(struct rspamd_http_message *msg,
rspamd_fstring_t *fstr);
/**
* Uses rspamd_fstring_t as message's body, string is copied by this operation
* @param msg
* @param fstr
* @return TRUE if a message's body has been set
*/
gboolean rspamd_http_message_set_body_from_fstring_copy(struct rspamd_http_message *msg,
const rspamd_fstring_t *fstr);
/**
* Appends data to message's body
* @param msg
* @param data
* @param len
* @return TRUE if a message's body has been set
*/
gboolean rspamd_http_message_append_body(struct rspamd_http_message *msg,
const gchar *data, gsize len);
/**
* Append a header to http message
* @param rep
* @param name
* @param value
*/
void rspamd_http_message_add_header(struct rspamd_http_message *msg,
const gchar *name,
const gchar *value);
void rspamd_http_message_add_header_len(struct rspamd_http_message *msg,
const gchar *name,
const gchar *value,
gsize len);
void rspamd_http_message_add_header_fstr(struct rspamd_http_message *msg,
const gchar *name,
rspamd_fstring_t *value);
/**
* Search for a specified header in message
* @param msg message
* @param name name of header
*/
const rspamd_ftok_t *rspamd_http_message_find_header(
struct rspamd_http_message *msg,
const gchar *name);
/**
* Search for a header that has multiple values
* @param msg
* @param name
* @return list of rspamd_ftok_t * with values
*/
GPtrArray *rspamd_http_message_find_header_multiple(
struct rspamd_http_message *msg,
const gchar *name);
/**
* Remove specific header from a message
* @param msg
* @param name
* @return
*/
gboolean rspamd_http_message_remove_header(struct rspamd_http_message *msg,
const gchar *name);
/**
* Free HTTP message
* @param msg
*/
void rspamd_http_message_free(struct rspamd_http_message *msg);
/**
* Extract arguments from a message's URI contained inside query string decoding
* them if needed
* @param msg HTTP request message
* @return new GHashTable which maps rspamd_ftok_t* to rspamd_ftok_t*
* (table must be freed by a caller)
*/
GHashTable *rspamd_http_message_parse_query(struct rspamd_http_message *msg);
/**
* Increase refcount for shared file (if any) to prevent early memory unlinking
* @param msg
*/
struct rspamd_storage_shmem *rspamd_http_message_shmem_ref(struct rspamd_http_message *msg);
/**
* Decrease external ref for shmem segment associated with a message
* @param msg
*/
void rspamd_http_message_shmem_unref(struct rspamd_storage_shmem *p);
/**
* Returns message's flags
* @param msg
* @return
*/
guint rspamd_http_message_get_flags(struct rspamd_http_message *msg);
/**
* Returns an HTTP hostname for a message, derived from a header if it has it
* or from a url if it doesn't
* @param msg
* @param hostlen output of the host length
* @return
*/
const gchar *rspamd_http_message_get_http_host(struct rspamd_http_message *msg,
gsize *hostlen);
/**
* Returns true if a message has standard port (80 or 443 for https)
* @param msg
* @return
*/
bool rspamd_http_message_is_standard_port(struct rspamd_http_message *msg);
#ifdef __cplusplus
}
#endif
#endif
|