summaryrefslogtreecommitdiffstats
path: root/src/lua/lua_http.c
blob: 9df019af4d7827b12ba02249d1b1c18c4b036c54 (plain)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/* Copyright (c) 2010, Vsevolod Stakhov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *       * Redistributions of source code must retain the above copyright
 *         notice, this list of conditions and the following disclaimer.
 *       * Redistributions in binary form must reproduce the above copyright
 *         notice, this list of conditions and the following disclaimer in the
 *         documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "lua_common.h"
#include "buffer.h"
#include "dns.h"

#define MAX_HEADERS_SIZE 8192

LUA_FUNCTION_DEF (http, make_post_request);
LUA_FUNCTION_DEF (http, make_get_request);

static const struct luaL_reg    httplib_m[] = {
	LUA_INTERFACE_DEF (http, make_post_request),
	LUA_INTERFACE_DEF (http, make_get_request),
	{"__tostring", lua_class_tostring},
	{NULL, NULL}
};

struct lua_http_header {
	gchar *name;
	gchar *value;
};

struct lua_http_ud {
	struct worker_task *task;
	gint parser_state;
	struct rspamd_async_session *s;
	memory_pool_t *pool;
	struct rspamd_dns_resolver *resolver;
	struct event_base *ev_base;
	lua_State *L;
	const gchar *callback;
	gint cbref;
	gchar *req_buf;
	gint req_len;
	gint port;
	gint timeout;
	gint code;
	gint fd;
	rspamd_io_dispatcher_t *io_dispatcher;
	gint rep_len;
	GList *headers;
};

static void
lua_http_fin (void *arg)
{
	struct lua_http_ud             *ud = arg;

	if (ud->callback == NULL) {
		/* Unref callback */
		luaL_unref (ud->L, LUA_REGISTRYINDEX, ud->cbref);
	}
	rspamd_remove_dispatcher (ud->io_dispatcher);
	close (ud->fd);
}

static void
lua_http_push_error (gint code, struct lua_http_ud *ud)
{
	struct worker_task            **ptask;
	gint							num;

	/* Push error */
	if (ud->callback) {
		lua_getglobal (ud->L, ud->callback);
		ptask = lua_newuserdata (ud->L, sizeof (struct worker_task *));
		lua_setclass (ud->L, "rspamd{task}", -1);
		*ptask = ud->task;
		num = 4;
	}
	else {
		lua_rawgeti (ud->L, LUA_REGISTRYINDEX, ud->cbref);
		num = 3;
	}

	/* Code */
	lua_pushnumber (ud->L, code);
	/* Headers */
	lua_pushnil (ud->L);
	/* Reply */
	lua_pushnil (ud->L);
	if (lua_pcall (ud->L, num, 0, 0) != 0) {
		msg_info ("call to %s failed: %s", ud->callback ? ud->callback : "local function", lua_tostring (ud->L, -1));
	}

	if (ud->headers != NULL) {
		g_list_free (ud->headers);
		ud->headers = NULL;
	}

	ud->parser_state = 3;
	remove_normal_event (ud->s, lua_http_fin, ud);

}

static void
lua_http_push_reply (f_str_t *in, struct lua_http_ud *ud)
{
	GList                          *cur;
	struct lua_http_header         *header;
	struct worker_task            **ptask;
	gint							num;

	if (ud->callback) {
		/* Push error */
		lua_getglobal (ud->L, ud->callback);
		ptask = lua_newuserdata (ud->L, sizeof (struct worker_task *));
		lua_setclass (ud->L, "rspamd{task}", -1);

		*ptask = ud->task;
		num = 4;
	}
	else {
		lua_rawgeti (ud->L, LUA_REGISTRYINDEX, ud->cbref);
		num = 3;
	}
	/* Code */
	lua_pushnumber (ud->L, ud->code);
	/* Headers */
	lua_newtable (ud->L);
	cur = ud->headers;

	while (cur) {
		header = cur->data;
		lua_pushstring (ud->L, header->name);
		lua_pushstring (ud->L, header->value);
		lua_settable (ud->L, -3);
		cur = g_list_next (cur);
	}
	/* Reply */
	lua_pushlstring (ud->L, in->begin, in->len);

	if (lua_pcall (ud->L, num, 0, 0) != 0) {
		msg_info ("call to %s failed: %s", ud->callback ? ud->callback : "local function", lua_tostring (ud->L, -1));
	}

	if (ud->headers != NULL) {
		g_list_free (ud->headers);
		ud->headers = NULL;
	}

	remove_normal_event (ud->s, lua_http_fin, ud);

}

/*
 * Parsing utils
 */
static gboolean
lua_http_parse_first_line (struct lua_http_ud *ud, f_str_t *in)
{
	const gchar                    *p;

	/* Assume first line is like this: HTTP/1.1 200 OK */
	if (in->len < sizeof ("HTTP/1.1 OK") + 2) {
		msg_info ("bad http string: %V", in);
		return FALSE;
	}

	p = in->begin + sizeof("HTTP/1.1 ") - 1;
	ud->code = strtoul (p, NULL, 10);

	ud->parser_state = 1;
	return TRUE;
}

static gboolean
lua_http_parse_header_line (struct lua_http_ud *ud, f_str_t *in)
{
	const gchar                    *p = in->begin;
	struct lua_http_header         *new;

	while (p < in->begin + in->len) {
		if (*p == ':') {
			break;
		}
		p ++;
	}

	if (*p != ':') {
		return FALSE;
	}
	/* Copy name */
	new = memory_pool_alloc (ud->pool, sizeof (struct lua_http_header));
	new->name = memory_pool_alloc (ud->pool, p - in->begin + 1);
	rspamd_strlcpy (new->name, in->begin, p - in->begin + 1);

	p ++;
	/* Copy value */
	while (p < in->begin + in->len && g_ascii_isspace (*p)) {
		p ++;
	}
	new->value = memory_pool_alloc (ud->pool, in->begin + in->len - p + 1);
	rspamd_strlcpy (new->value, p, in->begin + in->len - p + 1);

	/* Check content-length */
	if (ud->rep_len == 0 && g_ascii_strcasecmp (new->name, "content-length") == 0) {
		ud->rep_len = strtoul (new->value, NULL, 10);
	}

	/* Insert a header to the list */
	ud->headers = g_list_prepend (ud->headers, new);

	return TRUE;
}

/* Read callback */
static gboolean
lua_http_read_cb (f_str_t * in, void *arg)
{
	struct lua_http_ud             *ud = arg;

	switch (ud->parser_state) {
	case 0:
		/* Parse first line */
		return lua_http_parse_first_line (ud, in);
	case 1:
		if (ud->code != 200) {
			lua_http_push_error (ud->code, ud);
			return FALSE;
		}
		/* Parse header */
		if (in->len == 0) {
			/* Final line */
			if (ud->rep_len == 0) {
				/* No content-length */
				msg_info ("http reply contains no content-length header");
				lua_http_push_error (450, ud);
				return FALSE;
			}
			else {
				ud->parser_state = 2;
				rspamd_set_dispatcher_policy (ud->io_dispatcher, BUFFER_CHARACTER, ud->rep_len);
			}
		}
		else {
			return lua_http_parse_header_line (ud, in);
		}
		break;
	case 2:
		/* Get reply */
		lua_http_push_reply (in, ud);
		return FALSE;
	}

	return TRUE;
}

static void
lua_http_err_cb (GError * err, void *arg)
{
	struct lua_http_ud             *ud = arg;
	msg_info ("abnormally closing connection to http server error: %s", err->message);
	g_error_free (err);

	if (ud->parser_state != 3) {
		lua_http_push_error (500, ud);
	}
	else {
		remove_normal_event (ud->s, lua_http_fin, ud);
	}
}



static void
lua_http_dns_callback (struct rspamd_dns_reply *reply, gpointer arg)
{
	struct lua_http_ud             *ud = arg;
	union rspamd_reply_element     *elt;
	struct in_addr                  ina;
	struct timeval                  tv;

	if (reply->code != DNS_RC_NOERROR) {
		lua_http_push_error (450, ud);
		return;
	}

	/* Create socket to server */
	elt = reply->elements->data;
	memcpy (&ina, &elt->a.addr[0], sizeof (struct in_addr));

	ud->fd = make_universal_socket (inet_ntoa (ina), ud->port, SOCK_STREAM, TRUE, FALSE, FALSE);

	if (ud->fd == -1) {
		lua_http_push_error (450, ud);
		return;
	}

	/* Create dispatcher for HTTP protocol */
	msec_to_tv (ud->timeout, &tv);
	ud->io_dispatcher = rspamd_create_dispatcher (ud->ev_base, ud->fd, BUFFER_LINE, lua_http_read_cb, NULL,
			lua_http_err_cb, &tv, ud);
	/* Write request */
	register_async_event (ud->s, lua_http_fin, ud, g_quark_from_static_string ("lua http"));

	if (!rspamd_dispatcher_write (ud->io_dispatcher, ud->req_buf, ud->req_len, TRUE, TRUE)) {
		lua_http_push_error (450, ud);
		return;
	}
}

/**
 * Common request function
 */
static gint
lua_http_make_request_common (lua_State *L, struct worker_task *task, const gchar *callback,
		const gchar *hostname, const gchar *path, const gchar *data, gint top)
{
	gint                           r, s, datalen;
	struct lua_http_ud			  *ud;

	/* Calculate buffer size */
	datalen = (data != NULL) ? strlen (data) : 0;
	s = MAX_HEADERS_SIZE + sizeof (CRLF) * 3 + strlen (hostname) + strlen (path) + datalen
			+ sizeof ("POST HTTP/1.1");

	ud = memory_pool_alloc0 (task->task_pool, sizeof (struct lua_http_ud));
	ud->L = L;
	ud->s = task->s;
	ud->pool = task->task_pool;
	ud->ev_base = task->ev_base;
	ud->task = task;
	/* Preallocate buffer */
	ud->req_buf = memory_pool_alloc (task->task_pool, s);
	ud->callback = callback;

	/* Print request */
	r = rspamd_snprintf (ud->req_buf, s, "%s %s HTTP/1.1" CRLF
										 "Connection: close" CRLF
										 "Host: %s" CRLF,
										 (data != NULL) ? "POST" : "GET", path, hostname);
	if (datalen > 0) {
		r += rspamd_snprintf (ud->req_buf + r, s - r, "Content-Length: %d" CRLF, datalen);
	}
	/* Now assume that we have a table with headers at the top of the stack */

	if (lua_gettop (L) > top && lua_istable (L, top + 1)) {
		/* Add headers */
		lua_pushnil (L);  /* first key */
		while (lua_next (L, top + 1) != 0) {
			r += rspamd_snprintf (ud->req_buf + r, s - r, "%s: %s" CRLF, lua_tostring (L, -2), lua_tostring (L, -1));
			lua_pop(L, 1);
		}
	}
	/* Now check port and timeout */
	if (lua_gettop (L) > top + 1) {
		ud->port = lua_tonumber (L, top + 2);
	}
	else {
		ud->port = 80;
	}
	if (lua_gettop (L) > top + 2) {
		ud->timeout = lua_tonumber (L, top + 3);
	}
	else {
		/* Assume default timeout as 1000 msec */
		ud->timeout = 1000;
	}

	if (datalen > 0) {
		r += rspamd_snprintf (ud->req_buf + r, s - r, CRLF "%s", data);
	}
	else {
		r += rspamd_snprintf (ud->req_buf + r, s - r, CRLF);
	}

	ud->req_len = r;

	/* Resolve hostname */
	if (make_dns_request (task->resolver, task->s, task->task_pool, lua_http_dns_callback, ud,
			DNS_REQUEST_A, hostname)) {
		task->dns_requests ++;
	}

	return 0;
}

/**
 * Common request function (new version)
 */
static gint
lua_http_make_request_common_new (lua_State *L, struct rspamd_async_session *session, memory_pool_t *pool, struct event_base *base, gint cbref,
		const gchar *hostname, const gchar *path, const gchar *data, gint top)
{
	gint                           r, s, datalen;
	struct lua_http_ud			  *ud;
	struct in_addr				   ina;
	struct timeval                 tv;

	/* Calculate buffer size */
	datalen = (data != NULL) ? strlen (data) : 0;
	s = MAX_HEADERS_SIZE + sizeof (CRLF) * 3 + strlen (hostname) + strlen (path) + datalen
			+ sizeof ("POST HTTP/1.1");

	ud = memory_pool_alloc0 (pool, sizeof (struct lua_http_ud));
	ud->L = L;
	ud->pool = pool;
	ud->s = session;
	ud->ev_base = base;
	/* Preallocate buffer */
	ud->req_buf = memory_pool_alloc (pool, s);
	ud->callback = NULL;
	ud->cbref = cbref;

	/* Print request */
	r = rspamd_snprintf (ud->req_buf, s, "%s %s HTTP/1.1" CRLF
										 "Connection: close" CRLF,
										 (data != NULL) ? "POST" : "GET", path);
	if (datalen > 0) {
		r += rspamd_snprintf (ud->req_buf + r, s - r, "Content-Length: %d" CRLF, datalen);
	}
	/* Now assume that we have a table with headers at the top of the stack */

	if (lua_gettop (L) > top && lua_istable (L, top + 1)) {
		/* Add headers */
		lua_pushnil (L);  /* first key */
		while (lua_next (L, top + 1) != 0) {
			r += rspamd_snprintf (ud->req_buf + r, s - r, "%s: %s" CRLF, lua_tostring (L, -2), lua_tostring (L, -1));
			lua_pop (L, 1);
		}
	}
	/* Now check port and timeout */
	if (lua_gettop (L) > top + 1) {
		ud->port = lua_tonumber (L, top + 2);
	}
	else {
		ud->port = 80;
	}
	if (lua_gettop (L) > top + 2) {
		ud->timeout = lua_tonumber (L, top + 3);
	}
	else {
		/* Assume default timeout as 1000 msec */
		ud->timeout = 1000;
	}

	if (datalen > 0) {
		r += rspamd_snprintf (ud->req_buf + r, s - r, CRLF "%s", data);
	}
	else {
		r += rspamd_snprintf (ud->req_buf + r, s - r, CRLF);
	}

	ud->req_len = r;

	if (inet_aton (hostname, &ina) == 0) {
		msg_err ("%s is not valid ip address", hostname);
		luaL_unref (L, LUA_REGISTRYINDEX, cbref);
		lua_pushnil (L);
		return 1;
	}

	ud->fd = make_universal_socket (inet_ntoa (ina), ud->port, SOCK_STREAM, TRUE, FALSE, FALSE);

	if (ud->fd == -1) {
		luaL_unref (L, LUA_REGISTRYINDEX, cbref);
		lua_pushnil (L);
		return 1;
	}

	/* Create dispatcher for HTTP protocol */
	msec_to_tv (ud->timeout, &tv);
	ud->io_dispatcher = rspamd_create_dispatcher (ud->ev_base, ud->fd, BUFFER_LINE, lua_http_read_cb, NULL,
			lua_http_err_cb, &tv, ud);
	/* Write request */
	register_async_event (ud->s, lua_http_fin, ud, g_quark_from_static_string ("lua http"));

	if (!rspamd_dispatcher_write (ud->io_dispatcher, ud->req_buf, ud->req_len, TRUE, TRUE)) {
		luaL_unref (L, LUA_REGISTRYINDEX, cbref);
		lua_pushnil (L);
		return 1;
	}

	return 0;
}


/*
 * Typical usage:
 * rspamd_http.post_request(task, 'callback', 'hostname', 'path', 'data'[, headers -> { name = 'value' }])
 */
static gint
lua_http_make_post_request (lua_State *L)
{
	struct worker_task            *task, **ptask;
	memory_pool_t				  *pool, **ppool;
	struct rspamd_async_session	  *session, **psession;
	struct event_base			  *base, **pbase;
	const gchar                   *hostname, *path, *data, *callback;
	gint						   cbref;


	/* Check whether we have a task object */
	ptask = lua_check_class (L, 1, "rspamd{task}");
	task = ptask ? *(ptask) : NULL;

	if (!task) {
		psession = luaL_checkudata (L, 1, "rspamd{session}");
		luaL_argcheck (L, psession != NULL, 1, "'session' expected");
		session = psession ? *(psession) : NULL;
		ppool = luaL_checkudata (L, 2, "rspamd{mempool}");
		luaL_argcheck (L, ppool != NULL, 2, "'mempool' expected");
		pool = ppool ? *(ppool) : NULL;
		pbase = luaL_checkudata (L, 3, "rspamd{ev_base}");
		luaL_argcheck (L, ppool != NULL, 3, "'ev_base' expected");
		base = pbase ? *(pbase) : NULL;
	}

	/* Now extract hostname, path and data */

	if (task) {
		callback = memory_pool_strdup (task->task_pool, luaL_checkstring (L, 2));
		hostname = memory_pool_strdup (task->task_pool, luaL_checkstring (L, 3));
		path = memory_pool_strdup (task->task_pool, luaL_checkstring (L, 4));
		data = memory_pool_strdup (task->task_pool, luaL_checkstring (L, 5));

		if (callback != NULL && hostname != NULL && path != NULL && data != NULL) {
			return lua_http_make_request_common (L, task, callback, hostname, path, data, 5);
		}
		else {
			msg_info ("invalid arguments number");
		}
	}
	else {
		/* Common version */
		hostname = memory_pool_strdup (pool, luaL_checkstring (L, 4));
		path = memory_pool_strdup (pool, luaL_checkstring (L, 5));
		data = memory_pool_strdup (pool, luaL_checkstring (L, 6));
		if (session != NULL && pool != NULL && hostname != NULL && path != NULL && data != NULL && lua_isfunction (L, 7)) {
			lua_pushvalue (L, 7);
			cbref = luaL_ref (L, LUA_REGISTRYINDEX);
			return lua_http_make_request_common_new (L, session, pool, base, cbref, hostname, path, data, 7);
		}
	}

	return 0;
}

/*
 * Typical usage:
 * rspamd_http.get_request(task, 'callback', 'hostname', 'path'[, headers -> { name = 'value' }])
 */
static gint
lua_http_make_get_request (lua_State *L)
{
	struct worker_task            *task, **ptask;
	memory_pool_t				  *pool, **ppool;
	struct rspamd_async_session	  *session, **psession;
	struct event_base			  *base, **pbase;
	const gchar                   *hostname, *path, *callback;
	gint						   cbref;


	/* Check whether we have a task object */
	ptask = lua_check_class (L, 1, "rspamd{task}");
	task = ptask ? *(ptask) : NULL;

	if (!task) {
		psession = luaL_checkudata (L, 1, "rspamd{session}");
		luaL_argcheck (L, psession != NULL, 1, "'session' expected");
		session = psession ? *(psession) : NULL;
		ppool = luaL_checkudata (L, 2, "rspamd{mempool}");
		luaL_argcheck (L, ppool != NULL, 2, "'mempool' expected");
		pool = ppool ? *(ppool) : NULL;
		pbase = luaL_checkudata (L, 3, "rspamd{ev_base}");
		luaL_argcheck (L, ppool != NULL, 3, "'ev_base' expected");
		base = pbase ? *(pbase) : NULL;
	}

	/* Now extract hostname, path and data */

	if (task) {
		callback = memory_pool_strdup (task->task_pool, luaL_checkstring (L, 2));
		hostname = memory_pool_strdup (task->task_pool, luaL_checkstring (L, 3));
		path = memory_pool_strdup (task->task_pool, luaL_checkstring (L, 4));

		if (callback != NULL && hostname != NULL && path != NULL) {
			return lua_http_make_request_common (L, task, callback, hostname, path, NULL, 4);
		}
		else {
			msg_info ("invalid arguments number");
		}
	}
	else {
		/* Common version */
		hostname = memory_pool_strdup (pool, luaL_checkstring (L, 4));
		path = memory_pool_strdup (pool, luaL_checkstring (L, 5));
		if (session != NULL && pool != NULL && hostname != NULL && path != NULL && lua_isfunction (L, 6)) {
			lua_pushvalue (L, 6);
			cbref = luaL_ref (L, LUA_REGISTRYINDEX);
			return lua_http_make_request_common_new (L, session, pool, base, cbref, hostname, path, NULL, 6);
		}
	}

	return 0;
}

gint
luaopen_http (lua_State * L)
{

	luaL_register (L, "rspamd_http", httplib_m);

	return 1;
}