From 5e8f8cc4b102877c1332fb8f3e2f3ae14644c26f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Mon, 28 Mar 2016 01:49:27 +0200 Subject: [PATCH] working OPTIONS detection, but that's it for now --- libesphttpd/core/httpd.c | 16 ++++++++++++++++ libesphttpd/include/httpd.h | 1 + 2 files changed, 17 insertions(+) diff --git a/libesphttpd/core/httpd.c b/libesphttpd/core/httpd.c index ac17fe9..9b9f0af 100644 --- a/libesphttpd/core/httpd.c +++ b/libesphttpd/core/httpd.c @@ -243,6 +243,10 @@ void ICACHE_FLASH_ATTR httpdStartResponse(HttpdConnData *conn, int code) { code, (conn->priv->flags&HFL_CHUNKED)?"Transfer-Encoding: chunked":"Connection: close"); httpdSend(conn, buff, l); + + // CORS headers + httpdSend(conn, "Access-Control-Allow-Origin: *\r\n", -1); + httpdSend(conn, "Access-Control-Allow-Methods: GET,POST,OPTIONS\r\n", -1); } //Send a http header. @@ -570,6 +574,9 @@ static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) { } else if (strncmp(h, "POST ", 5)==0) { conn->requestType = HTTPD_METHOD_POST; firstLine=1; + } else if (strncmp(h, "OPTIONS ", 8)==0) { + conn->requestType = HTTPD_METHOD_OPTIONS; + firstLine=1; } if (firstLine) { @@ -632,6 +639,15 @@ static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) { dbg("boundary = %s", conn->post->multipartBoundary); } } + } else if (strncmp(h, "Access-Control-Request-Headers: ", 32)==0) { + // CORS crap that needs to be repeated in the response + + info("CORS preflight request."); + +// int len = strlen(h); +// info("h = %s, len %d", h, len); + + //strcpy(conn->priv->corsReqHdrs, h+32);//MAX_CORS_HDR_LEN } } diff --git a/libesphttpd/include/httpd.h b/libesphttpd/include/httpd.h index 05b43eb..8c563f2 100644 --- a/libesphttpd/include/httpd.h +++ b/libesphttpd/include/httpd.h @@ -12,6 +12,7 @@ #define HTTPD_METHOD_GET 1 #define HTTPD_METHOD_POST 2 +#define HTTPD_METHOD_OPTIONS 3 typedef struct HttpdPriv HttpdPriv; typedef struct HttpdConnData HttpdConnData;