telize

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

jsonip.c (1217B)


      1 /*
      2  * Telize 3.0.0
      3  * Copyright (c) 2013-2020, Frederic Cambus
      4  * https://www.telize.com
      5  *
      6  * Created:      2013-08-15
      7  * Last Updated: 2019-03-13
      8  *
      9  * Telize is released under the BSD 2-Clause license.
     10  * See LICENSE file for details.
     11  */
     12 
     13 #include "telize.h"
     14 
     15 int
     16 request_json_ip(struct http_request *req)
     17 {
     18 	struct kore_buf		json;
     19 	char			*answer, *callback;
     20 	char			ip[INET6_ADDRSTRLEN];
     21 
     22 	if (!telize_request_ip(req, ip, sizeof(ip))) {
     23 		http_response(req, HTTP_STATUS_INTERNAL_ERROR, NULL, 0);
     24 		return (KORE_RESULT_OK);
     25 	}
     26 
     27 	callback = NULL;
     28 
     29 	http_populate_get(req);
     30 	kore_buf_init(&json, 4096);
     31 
     32 	http_response_header(req, "Access-Control-Allow-Origin", "*");
     33 	http_response_header(req, "Cache-Control", "no-cache");
     34 	http_response_header(req, "Content-Type",
     35 	    "application/json; charset=utf-8");
     36 
     37 	if (http_argument_get_string(req, "callback", &callback))
     38 		kore_buf_appendf(&json, "%s(", callback);
     39 
     40 	kore_buf_appendf(&json, "{\"ip\":\"%s\"", ip);
     41 	kore_buf_append(&json, callback != NULL ? "});\n" : "}\n",
     42 	    callback != NULL ? 4 : 2);
     43 
     44 	answer = kore_buf_stringify(&json, NULL);
     45 	http_response(req, HTTP_STATUS_OK, answer, strlen(answer));
     46 	kore_buf_cleanup(&json);
     47 
     48 	return (KORE_RESULT_OK);
     49 }