From 826fb95e56afd7a379f9d4c874a162f95ccf15ac Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 15 Jun 2025 22:13:14 +1200 Subject: [PATCH] 'Generic Commit' --- src/update_handler.cpp | 39 ++++++++++++++++++++-- testing/test.sh | 75 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 3 deletions(-) diff --git a/src/update_handler.cpp b/src/update_handler.cpp index e3786bc..b2cbca0 100644 --- a/src/update_handler.cpp +++ b/src/update_handler.cpp @@ -1,5 +1,6 @@ #include "update_handler.hpp" #include +#include #include namespace simple_object_storage { @@ -32,9 +33,41 @@ void UpdateHandler::handle_update_object(const drogon::HttpRequestPtr& req, std: callback(resp); return; } - } else if (contentType.find("multipart/form-data") != std::string::npos || - contentType.find("application/x-www-form-urlencoded") != std::string::npos) { - // Handle form data + } else if (contentType.find("multipart/form-data") != std::string::npos) { + // Handle multipart form data + body = nlohmann::json::object(); + + // Parse the multipart form data + drogon::MultiPartParser formParser; + if (formParser.parse(req) != 0) { + resp->setStatusCode(drogon::k400BadRequest); + nlohmann::json response = {{"result", "error"}, {"error", "Failed to parse multipart data"}}; + resp->setBody(response.dump()); + resp->setContentTypeCode(drogon::CT_APPLICATION_JSON); + callback(resp); + return; + } + + auto ¶meters = formParser.getParameters(); + + // Get hash from form data + auto hash_it = parameters.find("hash"); + if (hash_it != parameters.end()) { + body["hash"] = hash_it->second; + } + + // Parse metadata if present + auto meta_it = parameters.find("metadata"); + if (meta_it != parameters.end()) { + try { + body["metadata"] = nlohmann::json::parse(meta_it->second); + } catch (const nlohmann::json::parse_error& e) { + // If parsing as JSON fails, treat it as a string + body["metadata"] = meta_it->second; + } + } + } else if (contentType.find("application/x-www-form-urlencoded") != std::string::npos) { + // Handle URL-encoded form data body = nlohmann::json::object(); const auto& form = req->getParameters(); diff --git a/testing/test.sh b/testing/test.sh index 2cbd04a..deeac9b 100755 --- a/testing/test.sh +++ b/testing/test.sh @@ -402,6 +402,81 @@ sleep 3 # Now try a request with a valid token - should be rate limited echo "Attempting request with valid token (should NOT be rate limited)" RESPONSE=$(curl -s -X PUT -H "Authorization: Bearer ${WRITE_TOKEN}" -F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" -F "metadata={\"labeltags\":[\"test:latest\"]}" "${HOSTURL}/upload") + +#------------------------------------------------------------------------------------------------ +title "8: Test update endpoint" + +# First upload a test file +UPLOAD_RESPONSE=$(curl -s -X PUT \ + -H "Authorization: Bearer ${WRITE_TOKEN}" \ + -F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" \ + -F "metadata={\"labeltags\":[\"test:update\"]}" \ + "${HOSTURL}/upload") + +HASH=$(echo "${UPLOAD_RESPONSE}" | jq -r '.hash') +if [ -z "${HASH}" ] || [ "${HASH}" = "null" ]; then + die "Failed to upload test file for update test" +fi + +# Test 8.1: Update metadata using JSON +UPDATED_METADATA='{"labeltags":["test:updated", "version:1.0"], "new_field":"test_value"}' +UPDATE_RESPONSE=$(curl -s -X PUT \ + -H "Authorization: Bearer ${WRITE_TOKEN}" \ + -H "Content-Type: application/json" \ + -d "{\"hash\":\"${HASH}\", \"metadata\":${UPDATED_METADATA}}" \ + "${HOSTURL}/update") + +echo "Update response: ${UPDATE_RESPONSE}" +if [ "$(echo "${UPDATE_RESPONSE}" | jq -r '.result')" != "success" ]; then + die "Failed to update metadata via JSON" +fi + +# Verify the update +UPDATED_METADATA_RESPONSE=$(curl -s "${HOSTURL}/meta/${HASH}") +if ! echo "${UPDATED_METADATA_RESPONSE}" | jq -e '.metadata.new_field == "test_value"' | grep -q true; then + die "Metadata was not updated correctly via JSON" +fi + +# Test 8.2: Update metadata using form data +# Update with form data using raw JSON string +UPDATE_FORM_RESPONSE=$(curl -s -X PUT \ + -H "Authorization: Bearer ${WRITE_TOKEN}" \ + -F "hash=${HASH}" \ + -F 'metadata={"labeltags":["test:form_updated"], "form_field":"form_value"}' \ + "${HOSTURL}/update") + +echo "Form update response: ${UPDATE_FORM_RESPONSE}" +if [ "$(echo "${UPDATE_FORM_RESPONSE}" | jq -r '.result')" != "success" ]; then + die "Failed to update metadata via form data" +fi + +# Verify the form update +UPDATED_FORM_METADATA_RESPONSE=$(curl -s "${HOSTURL}/meta/${HASH}") +if ! echo "${UPDATED_FORM_METADATA_RESPONSE}" | jq -e '.metadata.form_field == "form_value"' | grep -q true; then + die "Metadata was not updated correctly via form data" +fi + +# Test 8.3: Test error cases +# Missing hash +MISSING_HASH_RESPONSE=$(curl -s -X PUT \ + -H "Authorization: Bearer ${WRITE_TOKEN}" \ + -d '{"metadata":{}}' \ + "${HOSTURL}/update") +if [ "$(echo "${MISSING_HASH_RESPONSE}" | jq -r '.error')" != "Missing 'hash' or 'metadata' field in request body" ]; then + die "Expected error for missing hash, got: ${MISSING_HASH_RESPONSE}" +fi + +# Missing metadata +MISSING_METADATA_RESPONSE=$(curl -s -X PUT \ + -H "Authorization: Bearer ${WRITE_TOKEN}" \ + -d "{\"hash\":\"${HASH}\"}" \ + "${HOSTURL}/update") +if [ "$(echo "${MISSING_METADATA_RESPONSE}" | jq -r '.error')" != "Missing 'hash' or 'metadata' field in request body" ]; then + die "Expected error for missing metadata, got: ${MISSING_METADATA_RESPONSE}" +fi + +# Clean up +curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${HOSTURL}/deleteobject?hash=${HASH}" > /dev/null if echo "${RESPONSE}" | jq -r '.error' | grep -q "Too many authentication attempts"; then die "Expected no rate limit error, got: ${RESPONSE}" fi