From: Michael Tremer Date: Fri, 27 Jun 2025 08:56:10 +0000 (+0000) Subject: xfer: Pass the HTTP status code to the response callback X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=aaada345da8f3b0aeaeb0c503f3deaf04e72e8e8;p=pakfire.git xfer: Pass the HTTP status code to the response callback Signed-off-by: Michael Tremer --- diff --git a/src/cli/lib/upload_list.c b/src/cli/lib/upload_list.c index caf8bbcb..0e35fbc8 100644 --- a/src/cli/lib/upload_list.c +++ b/src/cli/lib/upload_list.c @@ -30,7 +30,7 @@ static const char* doc = "Lists all uploads"; static int list_callback(struct pakfire_xfer* xfer, - pakfire_xfer_error_code_t code, struct json_object* response, void* data) { + pakfire_xfer_error_code_t code, int status, struct json_object* response, void* data) { // XXX For now, just dump the entire JSON object return cli_dump_json(response); } diff --git a/src/pakfire/client.c b/src/pakfire/client.c index c182c38a..3ac3dda4 100644 --- a/src/pakfire/client.c +++ b/src/pakfire/client.c @@ -167,7 +167,7 @@ static int pakfire_client_ready(struct pakfire_client* self) { } static int pakfire_client_auth_response(struct pakfire_xfer* xfer, - pakfire_xfer_error_code_t code, json_object* response, void* data); + pakfire_xfer_error_code_t code, int status, json_object* response, void* data); static int pakfire_client_auth_required(struct pakfire_client* self); @@ -649,7 +649,7 @@ static int pakfire_client_auth_successful( } static int pakfire_client_auth_response(struct pakfire_xfer* xfer, - pakfire_xfer_error_code_t code, json_object* response, void* data) { + pakfire_xfer_error_code_t code, int status, json_object* response, void* data) { struct pakfire_client* client = data; switch (code) { @@ -893,7 +893,7 @@ static void pakfire_client_build_free(struct pakfire_client_build* self) { } static int pakfire_client_build_response(struct pakfire_xfer* xfer, - pakfire_xfer_error_code_t status, struct json_object* response, void* data) { + pakfire_xfer_error_code_t code, int status, struct json_object* response, void* data) { struct pakfire_client_build* build = data; const char* uuid = NULL; int r = 0; @@ -906,7 +906,7 @@ static int pakfire_client_build_response(struct pakfire_xfer* xfer, goto ERROR; // Call the callback - r = build->callback(build->client, status, uuid, build->data); + r = build->callback(build->client, code, uuid, build->data); } ERROR: @@ -1095,8 +1095,8 @@ ERROR: } static int pakfire_upload_payload_callback(struct pakfire_xfer* xfer, - pakfire_xfer_error_code_t code, struct json_object* response, void* data) { - pakfire_client_upload_status status = PAKFIRE_CLIENT_UPLOAD_SUCCESSFUL; + pakfire_xfer_error_code_t code, int status, struct json_object* response, void* data) { + pakfire_client_upload_status s = PAKFIRE_CLIENT_UPLOAD_SUCCESSFUL; struct pakfire_client_upload* upload = data; int r = 0; @@ -1106,13 +1106,13 @@ static int pakfire_upload_payload_callback(struct pakfire_xfer* xfer, break; default: - status = PAKFIRE_CLIENT_UPLOAD_ERROR; + s = PAKFIRE_CLIENT_UPLOAD_ERROR; break; } // Call the callback (if any) if (upload->callback) - r = upload->callback(upload->client, status, upload->path, upload->uuid, upload->data); + r = upload->callback(upload->client, s, upload->path, upload->uuid, upload->data); // Free the upload pakfire_client_upload_free(upload); @@ -1163,7 +1163,7 @@ ERROR: } static int pakfire_client_upload_response(struct pakfire_xfer* xfer, - pakfire_xfer_error_code_t code, struct json_object* response, void* data) { + pakfire_xfer_error_code_t code, int status, struct json_object* response, void* data) { struct pakfire_client_upload* self = data; const char* uuid = NULL; int r; diff --git a/src/pakfire/xfer.c b/src/pakfire/xfer.c index f024ad98..dc56c119 100644 --- a/src/pakfire/xfer.c +++ b/src/pakfire/xfer.c @@ -934,7 +934,7 @@ ERROR: return r; } -static int pakfire_xfer_call_response_callback(struct pakfire_xfer* self, pakfire_xfer_error_code_t code) { +static int pakfire_xfer_call_response_callback(struct pakfire_xfer* self, pakfire_xfer_error_code_t code, int status) { struct json_object* response = NULL; char* error = NULL; int r; @@ -956,7 +956,7 @@ static int pakfire_xfer_call_response_callback(struct pakfire_xfer* self, pakfir } // Run the callback - r = self->callbacks.response(self, code, response, self->callbacks.data); + r = self->callbacks.response(self, code, status, response, self->callbacks.data); ERROR: if (response) @@ -967,7 +967,7 @@ ERROR: return r; } -static int pakfire_xfer_fail(struct pakfire_xfer* xfer, int code) { +static int pakfire_xfer_fail(struct pakfire_xfer* xfer, pakfire_xfer_error_code_t code, int status) { int r; // Drop the hasher @@ -1023,7 +1023,7 @@ static int pakfire_xfer_fail(struct pakfire_xfer* xfer, int code) { // Call the response callback if (xfer->callbacks.response) { - r = pakfire_xfer_call_response_callback(xfer, code); + r = pakfire_xfer_call_response_callback(xfer, code, status); if (r < 0) return r; } @@ -1096,7 +1096,7 @@ static int pakfire_xfer_socket_recv(struct pakfire_xfer* xfer) { // We seem to have lost the connection case CURLE_GOT_NOTHING: - return pakfire_xfer_fail(xfer, PAKFIRE_XFER_TRANSPORT_ERROR); + return pakfire_xfer_fail(xfer, PAKFIRE_XFER_TRANSPORT_ERROR, 599); default: ERROR(xfer->ctx, "Could not read from WebSocket: %s\n", curl_easy_strerror(r)); @@ -1258,7 +1258,7 @@ static int pakfire_xfer_verify(struct pakfire_xfer* self) { pakfire_hashes_dump(self->ctx, &computed_hashes, LOG_ERR); // Consider the download failed - return pakfire_xfer_fail(self, PAKFIRE_XFER_DIGEST_MISMATCH); + return pakfire_xfer_fail(self, PAKFIRE_XFER_DIGEST_MISMATCH, 599); // Errors default: @@ -1267,7 +1267,7 @@ static int pakfire_xfer_verify(struct pakfire_xfer* self) { } } -static int pakfire_xfer_save(struct pakfire_xfer* xfer) { +static int pakfire_xfer_save(struct pakfire_xfer* xfer, int status) { int fd = -EBADF; int r; @@ -1277,7 +1277,7 @@ static int pakfire_xfer_save(struct pakfire_xfer* xfer) { // Call the output callback if configured if (xfer->callbacks.response) - return pakfire_xfer_call_response_callback(xfer, PAKFIRE_XFER_OK); + return pakfire_xfer_call_response_callback(xfer, PAKFIRE_XFER_OK, status); // Nothing to do if path isn't set if (!*xfer->path) @@ -1326,9 +1326,9 @@ pakfire_xfer_error_code_t pakfire_xfer_done( CURL* h = xfer->handle; int r; const char* scheme = NULL; - long response_code; long http_version; double total_time; + long status = 0; curl_off_t download_size = 0; curl_off_t download_speed = 0; @@ -1356,9 +1356,9 @@ pakfire_xfer_error_code_t pakfire_xfer_done( DEBUG(xfer->ctx, " Effective URL: %s\n", xfer->effective_url); // Response code - curl_easy_getinfo(h, CURLINFO_RESPONSE_CODE, &response_code); - if (response_code) - DEBUG(xfer->ctx, " Response code: %ld\n", response_code); + curl_easy_getinfo(h, CURLINFO_RESPONSE_CODE, &status); + if (status) + DEBUG(xfer->ctx, " Status Code: %ld\n", status); // HTTP Version curl_easy_getinfo(h, CURLINFO_HTTP_VERSION, &http_version); @@ -1411,7 +1411,7 @@ pakfire_xfer_error_code_t pakfire_xfer_done( return r; // Save the payload - r = pakfire_xfer_save(xfer); + r = pakfire_xfer_save(xfer, status); if (r < 0) return r; break; @@ -1419,7 +1419,7 @@ pakfire_xfer_error_code_t pakfire_xfer_done( case PAKFIRE_XFER_UPLOAD: // Call the output callback if configured if (xfer->callbacks.response) { - r = pakfire_xfer_call_response_callback(xfer, PAKFIRE_XFER_OK); + r = pakfire_xfer_call_response_callback(xfer, PAKFIRE_XFER_OK, status); if (r < 0) return r; } @@ -1439,7 +1439,7 @@ pakfire_xfer_error_code_t pakfire_xfer_done( code = pakfire_xfer_code(code, code); // Report that something went wrong - r = pakfire_xfer_fail(xfer, code); + r = pakfire_xfer_fail(xfer, code, status); if (r < 0) return r; } diff --git a/src/pakfire/xfer.h b/src/pakfire/xfer.h index 855b38bc..c9809390 100644 --- a/src/pakfire/xfer.h +++ b/src/pakfire/xfer.h @@ -128,7 +128,7 @@ int pakfire_xfer_set_output_buffer(struct pakfire_xfer* xfer, char** buffer, siz int pakfire_xfer_set_output_path(struct pakfire_xfer* xfer, const char* path); typedef int (*pakfire_xfer_response_callback)(struct pakfire_xfer* xfer, - pakfire_xfer_error_code_t status, json_object* response, void* data); + pakfire_xfer_error_code_t error, int code, struct json_object* response, void* data); int pakfire_xfer_set_response_callback(struct pakfire_xfer* xfer, pakfire_xfer_response_callback callback, void* data);