From: Jaroslav Kysela Date: Fri, 16 Oct 2015 16:07:51 +0000 (+0200) Subject: http server: implement OPTIONS command for CORS, fixes #3166 X-Git-Tag: v4.2.1~1894 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3b851fb3bec7b445c5bb087f2693542f33fb1c3f;p=thirdparty%2Ftvheadend.git http server: implement OPTIONS command for CORS, fixes #3166 --- diff --git a/src/http.c b/src/http.c index 211a63dce..9743ccf02 100644 --- a/src/http.c +++ b/src/http.c @@ -240,7 +240,7 @@ http_send_header(http_connection_t *hc, int rc, const char *content, htsbuf_qprintf(&hdrs, "Server: HTS/tvheadend\r\n"); if (config.cors_origin && config.cors_origin[0]) { htsbuf_qprintf(&hdrs, "Access-Control-Allow-Origin: %s\r\n", config.cors_origin); - htsbuf_qprintf(&hdrs, "Access-Control-Allow-Methods: POST, GET\r\n"); + htsbuf_qprintf(&hdrs, "Access-Control-Allow-Methods: POST, GET, OPTIONS\r\n"); htsbuf_qprintf(&hdrs, "Access-Control-Allow-Headers: x-requested-with\r\n"); } } @@ -248,7 +248,7 @@ http_send_header(http_connection_t *hc, int rc, const char *content, if(maxage == 0) { if (hc->hc_version != RTSP_VERSION_1_0) htsbuf_qprintf(&hdrs, "Cache-Control: no-cache\r\n"); - } else { + } else if (maxage > 0) { time(&t); tm = gmtime_r(&t, &tm0); @@ -293,6 +293,8 @@ http_send_header(http_connection_t *hc, int rc, const char *content, if(contentlen > 0) htsbuf_qprintf(&hdrs, "Content-Length: %"PRId64"\r\n", contentlen); + else if(contentlen == INT64_MIN) + htsbuf_qprintf(&hdrs, "Content-Length: 0\r\n"); if(range) { htsbuf_qprintf(&hdrs, "Accept-Ranges: %s\r\n", "bytes"); @@ -620,6 +622,17 @@ dump_request(http_connection_t *hc) http_cmd2str(hc->hc_cmd), hc->hc_url, buf); } +/** + * HTTP GET + */ +static int +http_cmd_options(http_connection_t *hc) +{ + http_send_header(hc, HTTP_STATUS_OK, NULL, INT64_MIN, + NULL, NULL, -1, 0, NULL, NULL); + return 0; +} + /** * HTTP GET */ @@ -719,6 +732,8 @@ http_process_request(http_connection_t *hc, htsbuf_queue_t *spill) default: http_error(hc, HTTP_STATUS_BAD_REQUEST); return 0; + case HTTP_CMD_OPTIONS: + return http_cmd_options(hc); case HTTP_CMD_GET: return http_cmd_get(hc); case HTTP_CMD_HEAD: diff --git a/src/http.h b/src/http.h index 6633856e6..e4ec13b49 100644 --- a/src/http.h +++ b/src/http.h @@ -112,6 +112,8 @@ typedef enum http_cmd { RTSP_CMD_PAUSE, } http_cmd_t; +#define HTTP_CMD_OPTIONS RTSP_CMD_OPTIONS + typedef enum http_ver { HTTP_VERSION_1_0, HTTP_VERSION_1_1,