Add Access-Control-Allow-Origin: * everywhere

This commit is contained in:
Kamil Trzcinski 2022-10-24 00:22:18 +02:00
parent cb7795ad2e
commit caafd0da44
4 changed files with 22 additions and 6 deletions

View File

@ -70,6 +70,15 @@ void camera_http_option(http_worker_t *worker, FILE *stream)
}
}
void http_cors_options(http_worker_t *worker, FILE *stream)
{
fprintf(stream, "HTTP/1.1 204 No Data\r\n");
fprintf(stream, "Access-Control-Allow-Origin: *\r\n");
fprintf(stream, "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n");
fprintf(stream, "Access-Control-Allow-Headers: Content-Type\r\n");
fprintf(stream, "\r\n");
}
http_method_t http_methods[] = {
{ "GET", "/snapshot", http_snapshot },
{ "GET", "/snapshot.jpg", http_snapshot },
@ -85,5 +94,6 @@ http_method_t http_methods[] = {
{ "POST", "/webrtc", http_webrtc_offer },
{ "GET", "/option", camera_http_option },
{ "GET", "/", http_content, "text/html", html_index_html, 0, &html_index_html_len },
{ "OPTIONS", "*/", http_cors_options },
{ }
};

View File

@ -94,7 +94,6 @@ static void http_process(http_worker_t *worker, FILE *stream)
worker->user_agent[0] = 0;
worker->content_length = -1;
// request_uri
worker->request_method = worker->client_method;
@ -149,6 +148,9 @@ static void http_process(http_worker_t *worker, FILE *stream)
continue;
if (!strstr(worker->request_params, params+1))
continue;
} else if (method->uri[0] == '*') {
if (strstr(worker->request_uri, method->uri + 1) != worker->request_uri)
continue;
} else {
if (strcmp(worker->request_uri, method->uri))
continue;
@ -237,6 +239,7 @@ int http_server(http_server_options_t *options, http_method_t *methods)
worker->listen_fd = listen_fd;
worker->methods = methods;
worker->client_fd = -1;
worker->options = *options;
pthread_create(&worker->thread, NULL, (void *(*)(void*))http_worker, worker);
}

View File

@ -25,11 +25,17 @@ typedef struct http_method_s {
unsigned *content_lengthp;
} http_method_t;
typedef struct http_server_options_s {
unsigned port;
unsigned maxcons;
} http_server_options_t;
typedef struct http_worker_s {
char *name;
int listen_fd;
http_method_t *methods;
pthread_t thread;
http_server_options_t options;
int client_fd;
int content_length;
@ -46,11 +52,6 @@ typedef struct http_worker_s {
http_method_t *current_method;
} http_worker_t;
typedef struct http_server_options_s {
unsigned port;
unsigned maxcons;
} http_server_options_t;
int http_server(http_server_options_t *options, http_method_t *methods);
void http_content(http_worker_t *worker, FILE *stream);
void http_write_response(FILE *stream, const char *status, const char *content_type, const char *body, unsigned content_length);

View File

@ -18,6 +18,8 @@ void http_write_response(
fprintf(stream, "Content-Type: %s\r\n", content_type ? content_type : "text/plain");
if (content_length > 0)
fprintf(stream, "Content-Length: %d\r\n", content_length);
if (!status || strstr(status, "200 OK") == status)
fprintf(stream, "Access-Control-Allow-Origin: *\r\n");
fprintf(stream, "\r\n");
if (body) {
fwrite(body, 1, content_length, stream);