telize

High performance JSON IP and GeoIP REST API (IP Geolocation)
Log | Files | Refs | README | LICENSE

commit 1183f8ac20523411ab12c3bb4e02dbc96ab1946b
parent e80e818f01e6cfbe793400c5f96061cc20d8f240
Author: Frederic Cambus <fred@statdns.com>
Date:   Fri, 12 Oct 2018 17:58:32 +0200

Set the response headers earlier so error messages have correct headers too

Diffstat:
Msrc/jsonip.c | 9++++-----
Msrc/location.c | 10+++++-----
2 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/src/jsonip.c b/src/jsonip.c @@ -35,6 +35,10 @@ jsonip(struct http_request *req) addr = kore_malloc(INET6_ADDRSTRLEN); + http_response_header(req, "Access-Control-Allow-Origin", "*"); + http_response_header(req, "Cache-Control", "no-cache"); + http_response_header(req, "Content-Type", "application/json; charset=utf-8"); + if (req->owner->addrtype == AF_INET) { inet_ntop(req->owner->addrtype, &(req->owner->addr.ipv4.sin_addr), addr, INET6_ADDRSTRLEN); } else { @@ -58,11 +62,6 @@ jsonip(struct http_request *req) kore_buf_append(&json, is_callback ? "});\n" : "}\n", is_callback ? 4 : 2); answer = kore_buf_stringify(&json, NULL); - // CORS - http_response_header(req, "Access-Control-Allow-Origin", "*"); - http_response_header(req, "Cache-Control", "no-cache"); - - http_response_header(req, "content-type", "application/json; charset=utf-8"); http_response(req, 200, answer, strlen(answer)); kore_buf_free(&json); diff --git a/src/location.c b/src/location.c @@ -74,6 +74,11 @@ location(struct http_request *req) addr = kore_malloc(INET6_ADDRSTRLEN); + /* Set response headers */ + http_response_header(req, "Access-Control-Allow-Origin", "*"); + http_response_header(req, "Cache-Control", "no-cache"); + http_response_header(req, "Content-Type", "application/json; charset=utf-8"); + /* IP address of the client originating the request */ if (req->owner->addrtype == AF_INET) inet_ntop(req->owner->addrtype, @@ -198,11 +203,6 @@ location(struct http_request *req) kore_buf_append(&json, is_callback ? "});\n" : "}\n", is_callback ? 4 : 2); answer = kore_buf_stringify(&json, NULL); - // CORS - http_response_header(req, "Access-Control-Allow-Origin", "*"); - http_response_header(req, "Cache-Control", "no-cache"); - - http_response_header(req, "content-type", "application/json; charset=utf-8"); http_response(req, 200, answer, strlen(answer)); cleanup: