summaryrefslogtreecommitdiff
path: root/pikul.c
diff options
context:
space:
mode:
authorꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-07-25 16:04:47 +0800
committerꦌꦫꦶꦏ꧀ꦦꦿꦧꦮꦑꦩꦭ꧀ <erik@darapsa.co.id>2021-07-25 16:04:47 +0800
commit6974bf5dd6a200ea746eba15d363cea6026de179 (patch)
treed210cb220ba325a2bcd77e287b11b15ebf1ab8fd /pikul.c
parent65f3618d80f4b5d9b53e861ea15e0bfddad41305 (diff)
Chunks get appended
Diffstat (limited to 'pikul.c')
-rw-r--r--pikul.c19
1 files changed, 17 insertions, 2 deletions
diff --git a/pikul.c b/pikul.c
index 93bc664..4cfe937 100644
--- a/pikul.c
+++ b/pikul.c
@@ -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;
}