Add Access-Control-Allow-Origin: *
everywhere
This commit is contained in:
parent
cb7795ad2e
commit
caafd0da44
@ -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[] = {
|
http_method_t http_methods[] = {
|
||||||
{ "GET", "/snapshot", http_snapshot },
|
{ "GET", "/snapshot", http_snapshot },
|
||||||
{ "GET", "/snapshot.jpg", http_snapshot },
|
{ "GET", "/snapshot.jpg", http_snapshot },
|
||||||
@ -85,5 +94,6 @@ http_method_t http_methods[] = {
|
|||||||
{ "POST", "/webrtc", http_webrtc_offer },
|
{ "POST", "/webrtc", http_webrtc_offer },
|
||||||
{ "GET", "/option", camera_http_option },
|
{ "GET", "/option", camera_http_option },
|
||||||
{ "GET", "/", http_content, "text/html", html_index_html, 0, &html_index_html_len },
|
{ "GET", "/", http_content, "text/html", html_index_html, 0, &html_index_html_len },
|
||||||
|
{ "OPTIONS", "*/", http_cors_options },
|
||||||
{ }
|
{ }
|
||||||
};
|
};
|
||||||
|
@ -94,7 +94,6 @@ static void http_process(http_worker_t *worker, FILE *stream)
|
|||||||
worker->user_agent[0] = 0;
|
worker->user_agent[0] = 0;
|
||||||
worker->content_length = -1;
|
worker->content_length = -1;
|
||||||
|
|
||||||
|
|
||||||
// request_uri
|
// request_uri
|
||||||
worker->request_method = worker->client_method;
|
worker->request_method = worker->client_method;
|
||||||
|
|
||||||
@ -149,6 +148,9 @@ static void http_process(http_worker_t *worker, FILE *stream)
|
|||||||
continue;
|
continue;
|
||||||
if (!strstr(worker->request_params, params+1))
|
if (!strstr(worker->request_params, params+1))
|
||||||
continue;
|
continue;
|
||||||
|
} else if (method->uri[0] == '*') {
|
||||||
|
if (strstr(worker->request_uri, method->uri + 1) != worker->request_uri)
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
if (strcmp(worker->request_uri, method->uri))
|
if (strcmp(worker->request_uri, method->uri))
|
||||||
continue;
|
continue;
|
||||||
@ -237,6 +239,7 @@ int http_server(http_server_options_t *options, http_method_t *methods)
|
|||||||
worker->listen_fd = listen_fd;
|
worker->listen_fd = listen_fd;
|
||||||
worker->methods = methods;
|
worker->methods = methods;
|
||||||
worker->client_fd = -1;
|
worker->client_fd = -1;
|
||||||
|
worker->options = *options;
|
||||||
pthread_create(&worker->thread, NULL, (void *(*)(void*))http_worker, worker);
|
pthread_create(&worker->thread, NULL, (void *(*)(void*))http_worker, worker);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,11 +25,17 @@ typedef struct http_method_s {
|
|||||||
unsigned *content_lengthp;
|
unsigned *content_lengthp;
|
||||||
} http_method_t;
|
} http_method_t;
|
||||||
|
|
||||||
|
typedef struct http_server_options_s {
|
||||||
|
unsigned port;
|
||||||
|
unsigned maxcons;
|
||||||
|
} http_server_options_t;
|
||||||
|
|
||||||
typedef struct http_worker_s {
|
typedef struct http_worker_s {
|
||||||
char *name;
|
char *name;
|
||||||
int listen_fd;
|
int listen_fd;
|
||||||
http_method_t *methods;
|
http_method_t *methods;
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
|
http_server_options_t options;
|
||||||
|
|
||||||
int client_fd;
|
int client_fd;
|
||||||
int content_length;
|
int content_length;
|
||||||
@ -46,11 +52,6 @@ typedef struct http_worker_s {
|
|||||||
http_method_t *current_method;
|
http_method_t *current_method;
|
||||||
} http_worker_t;
|
} 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);
|
int http_server(http_server_options_t *options, http_method_t *methods);
|
||||||
void http_content(http_worker_t *worker, FILE *stream);
|
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);
|
void http_write_response(FILE *stream, const char *status, const char *content_type, const char *body, unsigned content_length);
|
||||||
|
@ -18,6 +18,8 @@ void http_write_response(
|
|||||||
fprintf(stream, "Content-Type: %s\r\n", content_type ? content_type : "text/plain");
|
fprintf(stream, "Content-Type: %s\r\n", content_type ? content_type : "text/plain");
|
||||||
if (content_length > 0)
|
if (content_length > 0)
|
||||||
fprintf(stream, "Content-Length: %d\r\n", content_length);
|
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");
|
fprintf(stream, "\r\n");
|
||||||
if (body) {
|
if (body) {
|
||||||
fwrite(body, 1, content_length, stream);
|
fwrite(body, 1, content_length, stream);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user