diff options
-rw-r--r-- | pikul.c | 19 | ||||
-rw-r--r-- | private.h | 2 |
2 files changed, 19 insertions, 2 deletions
@@ -46,7 +46,16 @@ static size_t handle(char *contents, size_t size, size_t nmemb, struct shipping fprintf(stderr, "%s\n", contents); #endif json_tokener *tokener = shipping->tokener; - json_object *response = json_tokener_parse_ex(tokener, contents, realsize); + json_object *response; + if (shipping->mode == PLACES) { + shipping->response_data = realloc(shipping->response_data, + shipping->response_size + realsize + 1); + memcpy(&(shipping->response_data[shipping->response_size]), contents, realsize); + shipping->response_size += realsize; + shipping->response_data[shipping->response_size] = '\0'; + response = json_tokener_parse_ex(tokener, shipping->response_data, shipping->response_size); + } else + response = json_tokener_parse_ex(tokener, contents, realsize); enum json_tokener_error error = json_tokener_get_error(tokener); if (!response) { if (error == json_tokener_continue) @@ -74,7 +83,7 @@ static size_t handle(char *contents, size_t size, size_t nmemb, struct shipping const char **place_attrs = (const char **)shipping->data; enum { PLACE_CODE, DISTRICT, CITY, PROVINCE }; for (size_t i = 0; i < places_length; i++) { - places[i] = malloc(sizeof(struct pikul_place)); + places[i] = calloc(1, sizeof(struct pikul_place)); struct pikul_place *place = places[i]; json_object *object = json_object_array_get_idx(place_array, i); struct json_object_iterator iterator = json_object_iter_begin(object); @@ -202,7 +211,10 @@ struct pikul_place **pikul_origins(enum pikul_company company) curl_easy_setopt(shipping->handle, CURLOPT_URL, shipping->url); curl_easy_setopt(shipping->handle, CURLOPT_HTTPGET, 1L); shipping->mode = PLACES; + shipping->response_size = 0; + shipping->response_data = malloc(1); curl_easy_perform(shipping->handle); + free(shipping->response_data); free(shipping->url); return (struct pikul_place **)shipping->data; } @@ -220,7 +232,10 @@ struct pikul_place **pikul_destinations(enum pikul_company company) curl_easy_setopt(shipping->handle, CURLOPT_URL, shipping->url); curl_easy_setopt(shipping->handle, CURLOPT_HTTPGET, 1L); shipping->mode = PLACES; + shipping->response_size = 0; + shipping->response_data = malloc(1); curl_easy_perform(shipping->handle); + free(shipping->response_data); free(shipping->url); return (struct pikul_place **)shipping->data; } @@ -9,6 +9,8 @@ extern struct shipping { const char **status_trail; char *url; char *post; + size_t response_size; + char *response_data; json_tokener *tokener; enum { PLACES, |