httpd: add CORS header in http reply
@@ -102,11 +102,16 @@ static int html_print_raw(struct re_printf *pf, const struct pl *prm)
static void http_req_handler(struct http_conn *conn,
const struct http_msg *msg, void *arg)
{
+ struct mbuf *mb;
int err;
char *buf = NULL;
struct pl nprm;
(void)arg;
+ mb = mbuf_alloc(8192);
+ if (!mb)
+ return;
+
err = re_sdprintf(&buf, "%H", uri_header_unescape, &msg->prm);
if (err)
goto error;
@@ -115,24 +120,44 @@ static void http_req_handler(struct http_conn *conn,
if (0 == pl_strcasecmp(&msg->path, "/")) {
- http_creply(conn, 200, "OK",
- "text/html;charset=UTF-8",
- "%H", html_print_cmd, &nprm);
+ err = mbuf_printf(mb, "%H", html_print_cmd, &nprm);
+ if (!err) {
+ http_reply(conn, 200, "OK",
+ "Content-Type: text/html;charset=UTF-8\r\n"
+ "Content-Length: %zu\r\n"
+ "Access-Control-Allow-Origin: *\r\n"
+ "\r\n"
+ "%b",
+ mb->end,
+ mb->buf, mb->end);
+ }
+
}
else if (0 == pl_strcasecmp(&msg->path, "/raw/")) {
- http_creply(conn, 200, "OK",
- "text/plain;charset=UTF-8",
- "%H", html_print_raw, &nprm);
+ err = mbuf_printf(mb, "%H", html_print_raw, &nprm);
+ if (!err) {
+ http_reply(conn, 200, "OK",
+ "Content-Type: text/plain;charset=UTF-8\r\n"
+ "Content-Length: %zu\r\n"
+ "Access-Control-Allow-Origin: *\r\n"
+ "\r\n"
+ "%b",
+ mb->end,
+ mb->buf, mb->end);
+ }
+
}
else {
goto error;
}
+ mem_deref(mb);
mem_deref(buf);
return;
error:
+ mem_deref(mb);
mem_deref(buf);
http_ereply(conn, 404, "Not Found");
}
