Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
http server: implement OPTIONS command for CORS, fixes #3166
  • Loading branch information
perexg committed Oct 16, 2015
1 parent 8fa6cd5 commit 3b851fb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/http.c
Expand Up @@ -240,15 +240,15 @@ 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");
}
}

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);
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions src/http.h
Expand Up @@ -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,
Expand Down

0 comments on commit 3b851fb

Please sign in to comment.