]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
http server: implement OPTIONS command for CORS, fixes #3166
authorJaroslav Kysela <perex@perex.cz>
Fri, 16 Oct 2015 16:07:51 +0000 (18:07 +0200)
committerJaroslav Kysela <perex@perex.cz>
Fri, 16 Oct 2015 16:07:51 +0000 (18:07 +0200)
src/http.c
src/http.h

index 211a63dceadc97fd63903eb22703b476f64c9419..9743ccf02e387e2cdda0aa11c8cd8608efba7908 100644 (file)
@@ -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:
index 6633856e6ab590a12bc9c993f82e1dbaa795e9ec..e4ec13b49f208b93345aeb8e6b695f88c9657f11 100644 (file)
@@ -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,