telize

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

commit 414335e1663856f2f35ce83b1199ae9f1925677a
parent fa7e3058e6431e212e4b83ca0f93e48b569b231a
Author: Frederic Cambus <fred@statdns.com>
Date:   Fri,  5 Oct 2018 00:01:01 +0200

Handle optional callback parameter in the 'jsonip' endpoint

Diffstat:
Mconf/telize.conf | 5+++++
Msrc/jsonip.c | 12++++++++++--
2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/conf/telize.conf b/conf/telize.conf @@ -5,6 +5,8 @@ load ./telize.so tls_dhparam dh2048.pem +validator v_callback regex ^.*$ + domain * { certfile cert/server.pem certkey cert/key.pem @@ -12,5 +14,8 @@ domain * { accesslog telize.log static /ip ip + static /jsonip jsonip + params qs:get /jsonip { + validate callback v_callback } diff --git a/src/jsonip.c b/src/jsonip.c @@ -25,9 +25,11 @@ int jsonip(struct http_request *req) { const char *visitor_ip; - char *answer, *ip, addr[INET6_ADDRSTRLEN]; + char *answer, *callback, *json, *ip, addr[INET6_ADDRSTRLEN]; json_t *output = json_object(); + http_populate_get(req); + if (req->owner->addrtype == AF_INET) { inet_ntop(req->owner->addrtype, &(req->owner->addr.ipv4.sin_addr), addr, sizeof(addr)); } else { @@ -41,7 +43,13 @@ jsonip(struct http_request *req) } json_object_set_new(output, "ip", json_string(ip)); - answer = json_dumps(output, JSON_INDENT(3)); + json = json_dumps(output, JSON_INDENT(3)); + + if (http_argument_get_string(req, "callback", &callback)) { + asprintf(&answer, "%s(%s);", callback, json); + } else { + answer = json; + } http_response_header(req, "content-type", "application/json"); http_response(req, 200, answer, strlen(answer));