From 75590c6d965ce142014cd6829e98be2279f55c61 Mon Sep 17 00:00:00 2001 From: Rick Winter Date: Thu, 6 Aug 2020 15:38:53 -0700 Subject: [PATCH] Add requestId policy implementation (#239) * Add requestId policy implementation Includes uuid class --- sdk/core/azure-core/inc/http/policy.hpp | 8 ++- sdk/core/azure-core/inc/uuid.hpp | 82 +++++++++++++++++++++++++ sdk/core/azure-core/test/uuid.cpp | 15 +++++ 3 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 sdk/core/azure-core/inc/uuid.hpp create mode 100644 sdk/core/azure-core/test/uuid.cpp diff --git a/sdk/core/azure-core/inc/http/policy.hpp b/sdk/core/azure-core/inc/http/policy.hpp index d05821be5..10d1280b9 100644 --- a/sdk/core/azure-core/inc/http/policy.hpp +++ b/sdk/core/azure-core/inc/http/policy.hpp @@ -8,6 +8,7 @@ #include "http.hpp" #include "logging/logging.hpp" #include "transport.hpp" +#include "uuid.hpp" #include #include @@ -106,6 +107,9 @@ namespace Azure { namespace Core { namespace Http { }; class RequestIdPolicy : public HttpPolicy { + private: + constexpr static const char* RequestIdHeader = "x-ms-client-request-id"; + public: explicit RequestIdPolicy() {} @@ -119,7 +123,9 @@ namespace Azure { namespace Core { namespace Http { Request& request, NextHttpPolicy nextHttpPolicy) const override { - // Do real work here + auto uuid = UUID::CreateUUID().GetUUIDString(); + + request.AddHeader(RequestIdHeader, uuid); return nextHttpPolicy.Send(ctx, request); } }; diff --git a/sdk/core/azure-core/inc/uuid.hpp b/sdk/core/azure-core/inc/uuid.hpp new file mode 100644 index 000000000..7988814d3 --- /dev/null +++ b/sdk/core/azure-core/inc/uuid.hpp @@ -0,0 +1,82 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: MIT + +#pragma once + +#include // for placement new +#include +#include +#include // for swap and move + +namespace Azure { namespace Core { + + class UUID { + + private: + static const int UUIDSize = 16; + + uint8_t m_uuid[UUIDSize]; + // The UUID reserved variants. + static constexpr uint8_t ReservedNCS = 0x80; + static constexpr uint8_t ReservedRFC4122 = 0x40; + static constexpr uint8_t ReservedMicrosoft = 0x20; + static constexpr uint8_t ReservedFuture = 0x00; + + private: + UUID(uint8_t const uuid[UUIDSize]) + { + memcpy(m_uuid, uuid, UUIDSize); + } + + public: + + std::string GetUUIDString() + { + // Guid is 36 characters + // Add one byte for the \0 + char s[37]; + + std::snprintf( + s, + sizeof(s), + "%2.2x%2.2x%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x", + m_uuid[0], + m_uuid[1], + m_uuid[2], + m_uuid[3], + m_uuid[4], + m_uuid[5], + m_uuid[6], + m_uuid[7], + m_uuid[8], + m_uuid[9], + m_uuid[10], + m_uuid[11], + m_uuid[12], + m_uuid[13], + m_uuid[14], + m_uuid[15]); + + return std::string(s); + } + + static UUID CreateUUID() { + std::random_device rd; + std::mt19937 gen(rd()); + + uint8_t uuid[UUIDSize] = {}; + + for (int i = 0; i < UUIDSize; i += 4) + *reinterpret_cast(uuid + i) = gen(); + + // SetVariant to ReservedRFC4122 + uuid[8] = (uuid[8] | ReservedRFC4122) & 0x7F; + + constexpr uint8_t version = 4; + + uuid[6] = (uuid[6] & 0xF) | (version << 4); + + return UUID(uuid); + } + }; +}} // namespace Azure::Core diff --git a/sdk/core/azure-core/test/uuid.cpp b/sdk/core/azure-core/test/uuid.cpp new file mode 100644 index 000000000..6d2e8fbe7 --- /dev/null +++ b/sdk/core/azure-core/test/uuid.cpp @@ -0,0 +1,15 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: MIT + +#include "gtest/gtest.h" +#include +#include +#include + +using namespace Azure::Core; + +TEST(UUID, Basic) +{ + auto uuid = UUID::CreateUUID(); + EXPECT_TRUE(uuid.GetUUIDString().length() == 36); +}