Skip to content
This repository has been archived by the owner on Feb 7, 2021. It is now read-only.

Commit

Permalink
Merge pull request #130 from brandonwamboldt/cors-support-3.0
Browse files Browse the repository at this point in the history
[3.0] Send CORS headers to allow cross site requests
  • Loading branch information
daghf committed Mar 16, 2015
2 parents 85b15eb + 8d64e84 commit b5fa039
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ struct http_content_type {
const char *content_type;
};

char *http_get_header(struct MHD_Connection *connection, const char *header);
void http_add_header(struct http_response *resp, const char *key, const char *value);
void http_set_content_type(struct http_response *resp, const char *filepath);
void http_free_resp(struct http_response *resp);
Expand Down
14 changes: 13 additions & 1 deletion src/modules/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ void http_free_resp(struct http_response *resp)

struct http_response *http_mkresp(struct MHD_Connection *conn, int status, const char *body)
{
_cleanup_free_ char *origin;
struct http_response *resp = malloc(sizeof(struct http_response));
resp->status = status;
resp->connection = conn;
Expand All @@ -164,6 +165,13 @@ struct http_response *http_mkresp(struct MHD_Connection *conn, int status, const
else
resp->ndata = strlen(resp->data);
resp->headers = NULL;
http_add_header(resp, "Access-Control-Allow-Headers", "Authorization, Origin");
http_add_header(resp, "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
origin = http_get_header(conn, "Origin");
if (origin)
http_add_header(resp, "Access-Control-Allow-Origin", origin);
else
http_add_header(resp, "Access-Control-Allow-Origin", "*");
return resp;
}

Expand Down Expand Up @@ -257,7 +265,7 @@ static int get_key (void *cls, enum MHD_ValueKind kind, const char *key, const c
return MHD_YES;
}

static char *http_get_header(struct MHD_Connection *connection, const char *header)
char *http_get_header(struct MHD_Connection *connection, const char *header)
{
struct header_finder_t finder;
finder.header = header;
Expand Down Expand Up @@ -342,6 +350,10 @@ static int answer_to_connection (void *cls, struct MHD_Connection *connection,

log_request(connection, http, method, url);

if (0 == strcmp (method, "OPTIONS")) {
return send_response_ok(connection, "");
}

if (0 == strcmp (method, "GET") || !strcmp(method, "HEAD") || !strcmp(method,"DELETE")) {
ret = check_auth(connection, core, con_info);
if (ret == 1)
Expand Down

0 comments on commit b5fa039

Please sign in to comment.