}
int td_request_done(td_request* self, int code) {
- return 0;
+ char* effective_url = NULL;
+ td_file* file = NULL;
+ double total_time;
+ int status;
+ int r;
+
+ curl_off_t download_size = 0;
+ curl_off_t download_speed = 0;
+
+ // Log the result
+ DEBUG(self->ctx, "cURL xfer done: %d - %s\n", code, curl_easy_strerror(code));
+ if (*self->error)
+ DEBUG(self->ctx, " Error: %s\n", self->error);
+
+ // Effective URL
+ r = curl_easy_getinfo(self->handle, CURLINFO_EFFECTIVE_URL, &effective_url);
+ if (r)
+ goto ERROR;
+
+ if (effective_url)
+ DEBUG(self->ctx, " Effective URL: %s\n", effective_url);
+
+ // Response code
+ r = curl_easy_getinfo(self->handle, CURLINFO_RESPONSE_CODE, &status);
+ if (r)
+ goto ERROR;
+
+ if (status)
+ DEBUG(self->ctx, " Status Code: %ld\n", status);
+
+ // Total Time
+ r = curl_easy_getinfo(self->handle, CURLINFO_TOTAL_TIME, &total_time);
+ if (r)
+ goto ERROR;
+
+ DEBUG(self->ctx, " Total Time: %.2fms\n", total_time * 1000.0);
+
+ // Download Size
+ r = curl_easy_getinfo(self->handle, CURLINFO_SIZE_DOWNLOAD_T, &download_size);
+ if (r)
+ goto ERROR;
+
+ if (download_size)
+ DEBUG(self->ctx, " Download Size: %ld bytes\n", download_size);
+
+ // Download Speed
+ r = curl_easy_getinfo(self->handle, CURLINFO_SPEED_DOWNLOAD_T, &download_speed);
+ if (r)
+ goto ERROR;
+
+ if (download_speed)
+ DEBUG(self->ctx, " Download Speed: %ld bps\n", download_speed);
+
+ // Make the output buffer to a file object
+ r = td_file_open_buffer(&file, self->ctx, self->buffer);
+ if (r < 0)
+ goto ERROR;
+
+ // Call any callbacks
+ switch (code) {
+ // Success
+ case 0:
+ if (self->callbacks.on_success) {
+ r = self->callbacks.on_success(self->ctx,
+ file, self->callbacks.on_success_data);
+ }
+ break;
+
+ // Failure
+ default:
+ if (self->callbacks.on_failure) {
+ r = self->callbacks.on_failure(self->ctx,
+ code, file, self->callbacks.on_failure_data);
+ }
+ break;
+ }
+
+ERROR:
+ if (file)
+ td_file_unref(file);
+
+ return r;
}