diff options
author | Alfred E. Heggestad <aeh@db.org> | 2014-02-09 11:50:07 +0100 |
---|---|---|
committer | Alfred E. Heggestad <aeh@db.org> | 2014-02-09 11:50:07 +0100 |
commit | 98bf08bdcf2edd9d397f32650a8bfe62186fbecf (patch) | |
tree | ebc6ec71f44bff8c42e4eefced61948623df02fc /modules/httpd/httpd.c | |
parent | e6ad5cf4401b860ba402d4b7b3c7c254bc87a019 (diff) |
baresip 0.4.10
Diffstat (limited to 'modules/httpd/httpd.c')
-rw-r--r-- | modules/httpd/httpd.c | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/modules/httpd/httpd.c b/modules/httpd/httpd.c new file mode 100644 index 0000000..12ef9dd --- /dev/null +++ b/modules/httpd/httpd.c @@ -0,0 +1,103 @@ +/** + * @file httpd.c Webserver UI module + * + * Copyright (C) 2010 Creytiv.com + */ +#include <re.h> +#include <baresip.h> + + +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 http_msg *req) +{ + struct pl params; + + if (!pf || !req) + return EINVAL; + + if (pl_isset(&req->prm)) { + params.p = req->prm.p + 1; + params.l = req->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, ¶ms); +} + + +static void http_req_handler(struct http_conn *conn, + const struct http_msg *msg, void *arg) +{ + (void)arg; + + if (0 == pl_strcasecmp(&msg->path, "/")) { + + http_creply(conn, 200, "OK", + "text/html;charset=UTF-8", + "%H", html_print_cmd, msg); + } + else { + http_ereply(conn, 404, "Not Found"); + } +} + + +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; + + info("httpd: listening on %J\n", &laddr); + + return 0; +} + + +static int module_close(void) +{ + httpsock = mem_deref(httpsock); + + return 0; +} + + +EXPORT_SYM const struct mod_export DECL_EXPORTS(httpd) = { + "httpd", + "application", + module_init, + module_close, +}; |