summaryrefslogtreecommitdiff
path: root/modules/httpd/httpd.c
blob: 8348a43e4827d6dbb6879558f1f17590c187ee89 (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
/**
 * @file httpd.c Webserver UI module
 *
 * Copyright (C) 2010 - 2015 Creytiv.com
 */
#include <re.h>
#include <baresip.h>


/**
 * @defgroup httpd httpd
 *
 * HTTP Server module for the User-Interface
 *
 * Open your favourite web browser and point it to http://127.0.0.1:8000/
 *
 * Example URLs:
 \verbatim
  http://127.0.0.1:8000?h                  -- Print the Help menu
  http://127.0.0.1:8000?d1234@target.com   -- Make an outgoing call
 \endverbatim
 */


static struct http_sock *httpsock;


static int html_print_head(struct re_printf *pf, void *unused)
{
	(void)unused;

	return re_hprintf(pf,
			  "<html>\n"
			  "<head>\n"
			  "<title>Baresip v" BARESIP_VERSION "</title>\n"
			  "</head>\n");
}


static int html_print_cmd(struct re_printf *pf, const struct pl *prm)
{
	struct pl params;

	if (!pf || !prm)
		return EINVAL;

	if (pl_isset(prm)) {
		params.p = prm->p + 1;
		params.l = prm->l - 1;
	}
	else {
		params.p = "h";
		params.l = 1;
	}

	return re_hprintf(pf,
			  "%H"
			  "<body>\n"
			  "<pre>\n"
			  "%H"
			  "</pre>\n"
			  "</body>\n"
			  "</html>\n",
			  html_print_head, NULL,
			  ui_input_pl, &params);
}


static int html_print_raw(struct re_printf *pf, const struct pl *prm)
{
	struct pl params;

	if (!pf || !prm)
		return EINVAL;

	if (pl_isset(prm)) {
		params.p = prm->p + 1;
		params.l = prm->l - 1;
	}
	else {
		params.p = "h";
		params.l = 1;
	}

	return re_hprintf(pf,
			  "%H",
			  ui_input_pl, &params);
}

static void http_req_handler(struct http_conn *conn,
			     const struct http_msg *msg, void *arg)
{
	int err;
	char *buf;
	struct pl nprm;
	(void)arg;

	err = re_sdprintf(&buf, "%H", uri_header_unescape, &msg->prm);
	if (err)
		goto error;

	pl_set_str(&nprm, buf);

	if (0 == pl_strcasecmp(&msg->path, "/")) {

		http_creply(conn, 200, "OK",
			    "text/html;charset=UTF-8",
			    "%H", html_print_cmd, &nprm);
	}
	else if (0 == pl_strcasecmp(&msg->path, "/raw/")) {

		http_creply(conn, 200, "OK",
			    "text/plain;charset=UTF-8",
			    "%H", html_print_raw, &nprm);
	}
	else {
		goto error;
	}
	mem_deref(buf);

	return;

	error:
		mem_deref(buf);
		http_ereply(conn, 404, "Not Found");

}


static int output_handler(const char *str)
{
	(void)str;

	/* TODO: print 'str' to all active HTTP connections */

	return 0;
}


static struct ui ui_http = {
	.name = "http",
	.outputh = output_handler
};


static int module_init(void)
{
	struct sa laddr;
	int err;

	if (conf_get_sa(conf_cur(), "http_listen", &laddr)) {
		sa_set_str(&laddr, "0.0.0.0", 8000);
	}

	err = http_listen(&httpsock, &laddr, http_req_handler, NULL);
	if (err)
		return err;

	ui_register(&ui_http);

	info("httpd: listening on %J\n", &laddr);

	return 0;
}


static int module_close(void)
{
	ui_unregister(&ui_http);

	httpsock = mem_deref(httpsock);

	return 0;
}


EXPORT_SYM const struct mod_export DECL_EXPORTS(httpd) = {
	"httpd",
	"application",
	module_init,
	module_close,
};