From e67bba84a8da15b7a9a701bd13f844edbae6e8b8 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Thu, 27 Aug 2020 11:48:34 +0000 Subject: [PATCH 01/12] draft --- api/include/opentelemetry/http/http_client.h | 62 ++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 api/include/opentelemetry/http/http_client.h diff --git a/api/include/opentelemetry/http/http_client.h b/api/include/opentelemetry/http/http_client.h new file mode 100644 index 0000000000..44cff31339 --- /dev/null +++ b/api/include/opentelemetry/http/http_client.h @@ -0,0 +1,62 @@ +#pragma once +OPENTELEMETRY_BEGIN_NAMESPACE +namespace http +{ + +enum class HttpMethod: uint8_t { + HTTP_GET, + HTTP_POST, + HTTP_DELETE, + HTTP_PUT, + HTTP_HEAD, + HTTP_PATCH +}; + +class IHttpClient +{ +public: + virtual IHttpRequest* CreateRequest() = 0; + virtual void SendRequest (IHttpRequest* request, IHttpResponse* response) = 0; + virtual void SendRequestAsync(IHttpRequest* request, IHttpResponseCallback* callback) = 0; + virtual void CancelRequestAsync(nostd::string_view const& id) = 0; + virtual void CancelAllRequests(); +}; + +class IHttpRequest +{ +public: + virtual nostd::string_view& GetId() = 0; + virtual void SetMethod(nostd::string_view const& method) = 0; + virtual void SetUrl(nostd::string_view const& url) = 0; + virtual HttpHeaders& GetHeaders() = 0; + virtual void SetBody(uint8_t* body, int len) = 0; + virtual void GetBody(uint8_t* body, int* len) = 0; +}; + +class HttpHeaders +{ +public: + virtual void set(nostd::string_view const& name, nostd::string_view const& value) = 0; + virtual void add(nostd::string_view const& name, nostd::string_view const& value) = 0; + virtual nostd::string_view const& get(nostd::string_view const& name) = 0; + virtual bool has(nostd::string_view const& name) = 0; +}; + +class IHttpResponse +{ +public: + virtual nostd::string_view& GetId() = 0; + virtual HttpResult GetResult() = 0; + virtual unsigned GetStatusCode() = 0; + virtual HttpHeaders& GetHeaders() = 0; + virtual std::vector& GetBody() = 0; +}; + +class IHttpResponseCallback +{ +public: + virtual void OnHttpResponse(IHttpResponse* response) = 0; +}; +} // namespace nostd +OPENTELEMETRY_END_NAMESPACE +OPENTELEMETRY_END_NAMESPACE From 9bf7c419dfcc4ee81bf36a7d3b2e8bb4c7a56497 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Thu, 27 Aug 2020 14:35:06 +0000 Subject: [PATCH 02/12] remove vector --- api/include/opentelemetry/http/http_client.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/include/opentelemetry/http/http_client.h b/api/include/opentelemetry/http/http_client.h index 44cff31339..4fb394e3d7 100644 --- a/api/include/opentelemetry/http/http_client.h +++ b/api/include/opentelemetry/http/http_client.h @@ -49,7 +49,7 @@ class IHttpResponse virtual HttpResult GetResult() = 0; virtual unsigned GetStatusCode() = 0; virtual HttpHeaders& GetHeaders() = 0; - virtual std::vector& GetBody() = 0; + virtual void GetBody(uint8_t* body, int* len) = 0; }; class IHttpResponseCallback From f6bd12a7a57f930db7ffab6097d62be04eb640fe Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Thu, 27 Aug 2020 18:26:31 +0000 Subject: [PATCH 03/12] implement --- api/include/opentelemetry/http/http_client.h | 8 +- .../opentelemetry/sdk/http/http_client.h | 147 ++++++++++++++++++ 2 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 sdk/include/opentelemetry/sdk/http/http_client.h diff --git a/api/include/opentelemetry/http/http_client.h b/api/include/opentelemetry/http/http_client.h index 4fb394e3d7..82b7da6c1b 100644 --- a/api/include/opentelemetry/http/http_client.h +++ b/api/include/opentelemetry/http/http_client.h @@ -12,6 +12,13 @@ enum class HttpMethod: uint8_t { HTTP_PATCH }; +enum class HttpResult: uint8_t { + HTTP_OK, + HTTP_ABORTED, + HTTP_LOCALFAILURE, + HTTP_REMOTEFAILURE +}; + class IHttpClient { public: @@ -59,4 +66,3 @@ class IHttpResponseCallback }; } // namespace nostd OPENTELEMETRY_END_NAMESPACE -OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/http/http_client.h b/sdk/include/opentelemetry/sdk/http/http_client.h new file mode 100644 index 0000000000..e47b9b4d07 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/http/http_client.h @@ -0,0 +1,147 @@ +#pragma once +#include "opentelemetry/http/http_client.h" + +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace http +{ +namespace http_api = opentelemetry::http; + +class HttpHeaders: http_api::HttpHeaders, std::multimap +{ + +public: + virtual void set(nostd::string_view const& name, nostd::string_view const& value) override + { + auto range = equal_range(name); + auto hint = erase(range.first, range.second); + insert(hint, std::make_pair(name, value)); + } + + virtual void add(nostd::string_view const& name, nostd::string_view const& value) override + { + insert(std::make_pair(name, value)); + } + + virtual nostd::string_view const& get(nostd::string_view const& name) const override + { + auto it = find(name); + return (it != end()) ? it->second : m_empty; + } + + virtual bool has(nostd::string_view const& name) const override + { + auto it = find(name); + return (it != end()); + } +private: + nostd::string_view m_empty; +}; + +class SimpleHttpRequest:: public http_api::IHttpRequest +{ +public: + SimpleHttpRequest(std::string const& id): _id(id), method_("GET") + { + } + + virtual nostd::string_view& GetId() const override + { + return id_; + } + + virtual void SetMethod(nostd::string_view const& method) override + { + method_ = method; + } + + virtual void SetUrl(std::string const& url) override + { + url_ = url; + } + + virtual HttpHeaders& GetHeaders() override + { + return headers_; + } + + virtual void SetBody(const uint8_t* const body, int len) override + { + for (int i = 0; i < len; i++) + { + body_.push_back(body[i]); + } + } + + virtual void GetBody(uint8_t* body, int* len) override + { + int i = 0; + for (auto &e: body_) + { + body_[i++] = e; + } + return body_; + } + +private: + + std::vector body_; + std::string method_; + std::string id_; + std::string url_; + +}; + +class SimpleHttpResponse: public IHttpResponse +{ +public: + SimpleHttpResponse(nostd::string_view const& id): + id_(id), m + result_(HTTP_LOCALFAULURE), + statusCode_(0) + { + } + + virtual HttpResult GetResult() override + { + return result_; + } + + virtual unsigned GetStatusCode() override + { + return statusCode_; + } + + virtual HttpHeaders& GetHeaders() override + { + return headers_; + } + + virtual void GetBody(uint8_t* body, int& len) override + { + int i = 0; + for (auto &e: body_) + { + body_[i++] = e; + } + return body_; + } +private: + + nostd::string_view id_; + unsigned statusCode_; + HttpHeaders headers_; + std::vector body_; +}; +} //http +} //sdk +OPENTELEMETRY_END_NAMESPACE + + + + + + From c58d316c8afed28370df5a2f5ef6daa3746003cd Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 31 Aug 2020 11:32:24 +0000 Subject: [PATCH 04/12] fix implementation --- api/include/opentelemetry/http/http_client.h | 154 +++++++++++++----- .../opentelemetry/sdk/http/http_client.h | 74 +++++---- 2 files changed, 157 insertions(+), 71 deletions(-) diff --git a/api/include/opentelemetry/http/http_client.h b/api/include/opentelemetry/http/http_client.h index 82b7da6c1b..d658178936 100644 --- a/api/include/opentelemetry/http/http_client.h +++ b/api/include/opentelemetry/http/http_client.h @@ -1,68 +1,148 @@ #pragma once + +#include "opentelemetry/nostd/string_view.h" + OPENTELEMETRY_BEGIN_NAMESPACE namespace http { -enum class HttpMethod: uint8_t { - HTTP_GET, - HTTP_POST, - HTTP_DELETE, - HTTP_PUT, - HTTP_HEAD, - HTTP_PATCH -}; -enum class HttpResult: uint8_t { - HTTP_OK, - HTTP_ABORTED, - HTTP_LOCALFAILURE, - HTTP_REMOTEFAILURE +enum class HttpResult: uint8_t +{ + // Response has been received successfully from target server. + HttpResult_OK = 0, + + // Request has been aborted by the caller. The server might or + // might not have already received or processed the request. + HttpResult_Aborted = 1, + + // Local conditions have prevented the request from being sent + // (invalid request parameters, out of memory, internal error etc.) + HttpResult_LocalFailure = 2, + + // Network conditions somewhere between the local machine and + // the target server have caused the request to fail + // (connection failed, connection dropped abruptly etc.). + HttpResult_NetworkFailure = 3 + }; -class IHttpClient +// The HttpHeaders class contains a set of HTTP headers. +class HttpHeaders { public: - virtual IHttpRequest* CreateRequest() = 0; - virtual void SendRequest (IHttpRequest* request, IHttpResponse* response) = 0; - virtual void SendRequestAsync(IHttpRequest* request, IHttpResponseCallback* callback) = 0; - virtual void CancelRequestAsync(nostd::string_view const& id) = 0; - virtual void CancelAllRequests(); + // Inserts a name/value pair, and removes elements with the same name. + virtual void set(nostd::string_view const& name, nostd::string_view const& value) = 0; + + // Inserts a name/value pair + virtual void add(nostd::string_view const& name, nostd::string_view const& value) = 0; + + // Gets a string value given a name + virtual nostd::string_view const& get(nostd::string_view const& name) const = 0; + + // Tests whether the headers contains the specified name. + virtual bool has(nostd::string_view const& name) const = 0; }; -class IHttpRequest +// The HttpRequest class represents a Request object. +// Individual HTTP client implementations can implement the request object in +// the most efficient way. Either fill the request first, and then issue +// the underlying request, or create the real request immediately, and then forward +// the methods and set the individual parameters directly one by one. +class HttpRequest { public: - virtual nostd::string_view& GetId() = 0; + // Gets the request ID. + virtual const nostd::string_view& GetId() const = 0; + + // Sets the request Method virtual void SetMethod(nostd::string_view const& method) = 0; + + // Gets the request URI virtual void SetUrl(nostd::string_view const& url) = 0; - virtual HttpHeaders& GetHeaders() = 0; - virtual void SetBody(uint8_t* body, int len) = 0; - virtual void GetBody(uint8_t* body, int* len) = 0; + + // Gets the request Headers + virtual HttpHeaders* GetHeaders() const = 0; + + // Sets the request body + virtual void SetBody(const uint8_t* const body, const size_t len) = 0; + + // Gets the request body + virtual void GetBody(uint8_t* body, size_t& len) = 0; }; -class HttpHeaders +// The HttpResponse class represents a Response object. +// Individual HTTP client implementations can implement the response object +// in the most efficient way. Either copy all of the underlying data to a new +// structure, and provide it to the callback; or keep the real +// response data around, forward the methods, and retrieve the individual +// values directly one by one. +class HttpResponse { public: - virtual void set(nostd::string_view const& name, nostd::string_view const& value) = 0; - virtual void add(nostd::string_view const& name, nostd::string_view const& value) = 0; - virtual nostd::string_view const& get(nostd::string_view const& name) = 0; - virtual bool has(nostd::string_view const& name) = 0; + // Gets the response ID. + virtual const nostd::string_view& GetId() = 0; + + // Get the response result code. + virtual HttpResult GetResult() = 0; + + // Gets the response status code. + virtual unsigned GetStatusCode() = 0; + + // Gets the response headers. + virtual HttpHeaders* GetHeaders() = 0; + + // Gets the response body. + virtual void GetBody(uint8_t* body, size_t& len) = 0; }; -class IHttpResponse +// The HttpResponseCallback class receives HTTP client responses +class HttpResponseCallback { public: - virtual nostd::string_view& GetId() = 0; - virtual HttpResult GetResult() = 0; - virtual unsigned GetStatusCode() = 0; - virtual HttpHeaders& GetHeaders() = 0; - virtual void GetBody(uint8_t* body, int* len) = 0; + // Called when an HTTP request completes. + // The passed response object contains details about the exact way the + // request finished (HTTP status code, headers, content, error codes + // etc.). The ownership of the response object is transferred to the + // callback object. It can store it for later if necessary. Finally, it + // must be deleted using its virtual destructor. + virtual void OnHttpResponse(HttpResponse* response) = 0; }; -class IHttpResponseCallback +// The HttpClient class is the interface for HTTP client implementations. +class HttpClient { public: - virtual void OnHttpResponse(IHttpResponse* response) = 0; + + // Creates an empty HTTP request object. + // The created request object has only its ID prepopulated. Other fields + // must be set by the caller. The request object can then be sent + // using SendRequestAsync(). If you are not going to use the request object + // then you can delete it safely using its virtual destructor. + virtual HttpRequest* CreateRequest() = 0; + + // Begins an HTTP request. + // The method takes ownership of the passed request, and can destroy it before + // returning to the caller. Do not access the request object in any + // way after this invocation, and do not delete it. + // The callback object is always called, even if the request is + // cancelled, or if an error occurs immediately during sending. In the + // latter case, the OnHttpResponse() callback is called before this + // method returns. You must keep the callback object alive until its + // OnHttpResponse() callback is called. + virtual void SendRequestAsync(HttpRequest* request, HttpResponseCallback* callback) = 0; + + // Cancels an HTTP request. + // The caller must provide a string ID returned earlier by request->GetId(). + // The request is cancelled asynchronously. The caller must still + // wait for the relevant OnHttpResponse() callback (it can just come + // earlier with some "aborted" error status). + virtual void CancelRequestAsync(nostd::string_view const& id) = 0; + + virtual void CancelAllRequests() + { + } }; + } // namespace nostd OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/http/http_client.h b/sdk/include/opentelemetry/sdk/http/http_client.h index e47b9b4d07..d2b6218432 100644 --- a/sdk/include/opentelemetry/sdk/http/http_client.h +++ b/sdk/include/opentelemetry/sdk/http/http_client.h @@ -1,8 +1,10 @@ #pragma once #include "opentelemetry/http/http_client.h" - #include "opentelemetry/version.h" +#include +#include + OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk { @@ -10,13 +12,14 @@ namespace http { namespace http_api = opentelemetry::http; +// HttpHeaders implementation class HttpHeaders: http_api::HttpHeaders, std::multimap { public: virtual void set(nostd::string_view const& name, nostd::string_view const& value) override { - auto range = equal_range(name); + auto range = equal_range(std::string(name.data())); auto hint = erase(range.first, range.second); insert(hint, std::make_pair(name, value)); } @@ -28,47 +31,55 @@ class HttpHeaders: http_api::HttpHeaders, std::multimapsecond : m_empty; } virtual bool has(nostd::string_view const& name) const override { - auto it = find(name); + auto it = find(std::string(name.data())); return (it != end()); } + private: - nostd::string_view m_empty; + + nostd::string_view m_empty{}; }; -class SimpleHttpRequest:: public http_api::IHttpRequest +class SimpleHttpRequest: public http_api::HttpRequest { public: - SimpleHttpRequest(std::string const& id): _id(id), method_("GET") + + SimpleHttpRequest(nostd::string_view const& id): id_(id), method_("GET") { } - - virtual nostd::string_view& GetId() const override + + // Gets the HTTP request ID. + virtual const nostd::string_view& GetId() const override { return id_; } + // Sets the request method virtual void SetMethod(nostd::string_view const& method) override { method_ = method; } - virtual void SetUrl(std::string const& url) override + // Sets the HTTP request URI. + virtual void SetUrl(nostd::string_view const& url) override { url_ = url; } - virtual HttpHeaders& GetHeaders() override + // Gets the HTTP request headers. + virtual http_api::HttpHeaders* GetHeaders() const override { return headers_; } - virtual void SetBody(const uint8_t* const body, int len) override + // Sets the request body. + virtual void SetBody(const uint8_t* const body, const size_t len) override { for (int i = 0; i < len; i++) { @@ -76,36 +87,37 @@ class SimpleHttpRequest:: public http_api::IHttpRequest } } - virtual void GetBody(uint8_t* body, int* len) override + virtual void GetBody(uint8_t* body, size_t& len) override { - int i = 0; + len = 0; for (auto &e: body_) { - body_[i++] = e; + body[len++] = e; } - return body_; + len = (len > 0 ) ? len - 1 : len ; } private: - + http_api::HttpHeaders* headers_; std::vector body_; - std::string method_; - std::string id_; - std::string url_; + nostd::string_view method_; + nostd::string_view id_; + nostd::string_view url_; + }; -class SimpleHttpResponse: public IHttpResponse +class SimpleHttpResponse: public http_api::HttpResponse { public: SimpleHttpResponse(nostd::string_view const& id): - id_(id), m - result_(HTTP_LOCALFAULURE), + id_(id), + result_(http_api::HttpResult::HttpResult_LocalFailure), statusCode_(0) { } - virtual HttpResult GetResult() override + virtual http_api::HttpResult GetResult() override { return result_; } @@ -115,33 +127,27 @@ class SimpleHttpResponse: public IHttpResponse return statusCode_; } - virtual HttpHeaders& GetHeaders() override + virtual http_api::HttpHeaders* GetHeaders() override { return headers_; } - virtual void GetBody(uint8_t* body, int& len) override + virtual void GetBody(uint8_t* body, size_t& len) override { int i = 0; for (auto &e: body_) { body_[i++] = e; } - return body_; } private: nostd::string_view id_; unsigned statusCode_; - HttpHeaders headers_; + http_api::HttpHeaders* headers_; + http_api::HttpResult result_; std::vector body_; }; } //http } //sdk OPENTELEMETRY_END_NAMESPACE - - - - - - From d7fa6c965217bffd4f81453874e82e783a87e81a Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 31 Aug 2020 11:36:24 +0000 Subject: [PATCH 05/12] fix indent --- sdk/include/opentelemetry/sdk/http/http_client.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/http/http_client.h b/sdk/include/opentelemetry/sdk/http/http_client.h index d2b6218432..ebaecbb955 100644 --- a/sdk/include/opentelemetry/sdk/http/http_client.h +++ b/sdk/include/opentelemetry/sdk/http/http_client.h @@ -99,10 +99,10 @@ class SimpleHttpRequest: public http_api::HttpRequest private: http_api::HttpHeaders* headers_; - std::vector body_; - nostd::string_view method_; - nostd::string_view id_; - nostd::string_view url_; + std::vector body_; + nostd::string_view method_; + nostd::string_view id_; + nostd::string_view url_; }; From 041191eca1a2b11f4442f06138b0e4a4fe419ca2 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 31 Aug 2020 11:42:05 +0000 Subject: [PATCH 06/12] fix indent --- api/include/opentelemetry/http/http_client.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/api/include/opentelemetry/http/http_client.h b/api/include/opentelemetry/http/http_client.h index d658178936..757c6dd3c5 100644 --- a/api/include/opentelemetry/http/http_client.h +++ b/api/include/opentelemetry/http/http_client.h @@ -100,12 +100,12 @@ class HttpResponse class HttpResponseCallback { public: - // Called when an HTTP request completes. - // The passed response object contains details about the exact way the - // request finished (HTTP status code, headers, content, error codes - // etc.). The ownership of the response object is transferred to the + // Called when an HTTP request completes. + // The passed response object contains details about the exact way the + // request finished (HTTP status code, headers, content, error codes + // etc.). The ownership of the response object is transferred to the // callback object. It can store it for later if necessary. Finally, it - // must be deleted using its virtual destructor. + // must be deleted using its virtual destructor. virtual void OnHttpResponse(HttpResponse* response) = 0; }; From 71eaa9922613a9ed3e2b4e859a227dd0f24b5c37 Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Mon, 31 Aug 2020 11:46:43 +0000 Subject: [PATCH 07/12] spacing --- sdk/include/opentelemetry/sdk/http/http_client.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/http/http_client.h b/sdk/include/opentelemetry/sdk/http/http_client.h index ebaecbb955..ee4347578c 100644 --- a/sdk/include/opentelemetry/sdk/http/http_client.h +++ b/sdk/include/opentelemetry/sdk/http/http_client.h @@ -42,12 +42,12 @@ class HttpHeaders: http_api::HttpHeaders, std::multimap Date: Mon, 31 Aug 2020 15:58:59 +0000 Subject: [PATCH 08/12] MS license --- api/include/opentelemetry/http/http_client.h | 2 ++ sdk/include/opentelemetry/sdk/http/http_client.h | 2 ++ 2 files changed, 4 insertions(+) diff --git a/api/include/opentelemetry/http/http_client.h b/api/include/opentelemetry/http/http_client.h index 757c6dd3c5..bb60a5e8fa 100644 --- a/api/include/opentelemetry/http/http_client.h +++ b/api/include/opentelemetry/http/http_client.h @@ -1,3 +1,5 @@ +// Copyright (c) Microsoft. All rights reserved + #pragma once #include "opentelemetry/nostd/string_view.h" diff --git a/sdk/include/opentelemetry/sdk/http/http_client.h b/sdk/include/opentelemetry/sdk/http/http_client.h index ee4347578c..7903e2663a 100644 --- a/sdk/include/opentelemetry/sdk/http/http_client.h +++ b/sdk/include/opentelemetry/sdk/http/http_client.h @@ -1,3 +1,5 @@ +// Copyright (c) Microsoft. All rights reserved. + #pragma once #include "opentelemetry/http/http_client.h" #include "opentelemetry/version.h" From 0f4592bd9a39eb9f0d1bdef15594f62c918548dd Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 1 Sep 2020 06:28:22 +0000 Subject: [PATCH 09/12] review comments --- api/include/opentelemetry/http/http_client.h | 19 +++++++++++++++++-- .../opentelemetry/sdk/http/http_client.h | 14 +++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/api/include/opentelemetry/http/http_client.h b/api/include/opentelemetry/http/http_client.h index bb60a5e8fa..680f167a2d 100644 --- a/api/include/opentelemetry/http/http_client.h +++ b/api/include/opentelemetry/http/http_client.h @@ -1,4 +1,16 @@ -// Copyright (c) Microsoft. All rights reserved +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. #pragma once @@ -40,9 +52,12 @@ class HttpHeaders virtual void add(nostd::string_view const& name, nostd::string_view const& value) = 0; // Gets a string value given a name + // Returns: + // If there are multiple headers with same name, it returns any one of the value (implementation defined) + // Empty string if there is no header with speicfied name.. virtual nostd::string_view const& get(nostd::string_view const& name) const = 0; - // Tests whether the headers contains the specified name. + // Tests whether the headers contain the specified name. virtual bool has(nostd::string_view const& name) const = 0; }; diff --git a/sdk/include/opentelemetry/sdk/http/http_client.h b/sdk/include/opentelemetry/sdk/http/http_client.h index 7903e2663a..282c373729 100644 --- a/sdk/include/opentelemetry/sdk/http/http_client.h +++ b/sdk/include/opentelemetry/sdk/http/http_client.h @@ -1,4 +1,16 @@ -// Copyright (c) Microsoft. All rights reserved. +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. #pragma once #include "opentelemetry/http/http_client.h" From a993179998941e1acb4c565b271de6f924e4854b Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 1 Sep 2020 06:38:15 +0000 Subject: [PATCH 10/12] fix format --- api/include/opentelemetry/http/http_client.h | 164 ++++++++-------- .../opentelemetry/sdk/http/http_client.h | 175 ++++++++---------- 2 files changed, 153 insertions(+), 186 deletions(-) diff --git a/api/include/opentelemetry/http/http_client.h b/api/include/opentelemetry/http/http_client.h index 680f167a2d..6a27cca475 100644 --- a/api/include/opentelemetry/http/http_client.h +++ b/api/include/opentelemetry/http/http_client.h @@ -20,24 +20,23 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace http { - -enum class HttpResult: uint8_t +enum class HttpResult : uint8_t { - // Response has been received successfully from target server. - HttpResult_OK = 0, + // Response has been received successfully from target server. + HttpResult_OK = 0, - // Request has been aborted by the caller. The server might or - // might not have already received or processed the request. - HttpResult_Aborted = 1, + // Request has been aborted by the caller. The server might or + // might not have already received or processed the request. + HttpResult_Aborted = 1, - // Local conditions have prevented the request from being sent - // (invalid request parameters, out of memory, internal error etc.) - HttpResult_LocalFailure = 2, + // Local conditions have prevented the request from being sent + // (invalid request parameters, out of memory, internal error etc.) + HttpResult_LocalFailure = 2, - // Network conditions somewhere between the local machine and - // the target server have caused the request to fail - // (connection failed, connection dropped abruptly etc.). - HttpResult_NetworkFailure = 3 + // Network conditions somewhere between the local machine and + // the target server have caused the request to fail + // (connection failed, connection dropped abruptly etc.). + HttpResult_NetworkFailure = 3 }; @@ -45,20 +44,20 @@ enum class HttpResult: uint8_t class HttpHeaders { public: - // Inserts a name/value pair, and removes elements with the same name. - virtual void set(nostd::string_view const& name, nostd::string_view const& value) = 0; + // Inserts a name/value pair, and removes elements with the same name. + virtual void set(nostd::string_view const &name, nostd::string_view const &value) = 0; - // Inserts a name/value pair - virtual void add(nostd::string_view const& name, nostd::string_view const& value) = 0; + // Inserts a name/value pair + virtual void add(nostd::string_view const &name, nostd::string_view const &value) = 0; - // Gets a string value given a name - // Returns: - // If there are multiple headers with same name, it returns any one of the value (implementation defined) - // Empty string if there is no header with speicfied name.. - virtual nostd::string_view const& get(nostd::string_view const& name) const = 0; + // Gets a string value given a name + // Returns: + // If there are multiple headers with same name, it returns any one of the value + // (implementation defined) Empty string if there is no header with speicfied name.. + virtual nostd::string_view const &get(nostd::string_view const &name) const = 0; - // Tests whether the headers contain the specified name. - virtual bool has(nostd::string_view const& name) const = 0; + // Tests whether the headers contain the specified name. + virtual bool has(nostd::string_view const &name) const = 0; }; // The HttpRequest class represents a Request object. @@ -69,23 +68,23 @@ class HttpHeaders class HttpRequest { public: - // Gets the request ID. - virtual const nostd::string_view& GetId() const = 0; + // Gets the request ID. + virtual const nostd::string_view &GetId() const = 0; - // Sets the request Method - virtual void SetMethod(nostd::string_view const& method) = 0; + // Sets the request Method + virtual void SetMethod(nostd::string_view const &method) = 0; - // Gets the request URI - virtual void SetUrl(nostd::string_view const& url) = 0; + // Gets the request URI + virtual void SetUrl(nostd::string_view const &url) = 0; - // Gets the request Headers - virtual HttpHeaders* GetHeaders() const = 0; + // Gets the request Headers + virtual HttpHeaders *GetHeaders() const = 0; - // Sets the request body - virtual void SetBody(const uint8_t* const body, const size_t len) = 0; + // Sets the request body + virtual void SetBody(const uint8_t *const body, const size_t len) = 0; - // Gets the request body - virtual void GetBody(uint8_t* body, size_t& len) = 0; + // Gets the request body + virtual void GetBody(uint8_t *body, size_t &len) = 0; }; // The HttpResponse class represents a Response object. @@ -97,69 +96,66 @@ class HttpRequest class HttpResponse { public: - // Gets the response ID. - virtual const nostd::string_view& GetId() = 0; + // Gets the response ID. + virtual const nostd::string_view &GetId() = 0; - // Get the response result code. - virtual HttpResult GetResult() = 0; + // Get the response result code. + virtual HttpResult GetResult() = 0; - // Gets the response status code. - virtual unsigned GetStatusCode() = 0; + // Gets the response status code. + virtual unsigned GetStatusCode() = 0; - // Gets the response headers. - virtual HttpHeaders* GetHeaders() = 0; + // Gets the response headers. + virtual HttpHeaders *GetHeaders() = 0; - // Gets the response body. - virtual void GetBody(uint8_t* body, size_t& len) = 0; + // Gets the response body. + virtual void GetBody(uint8_t *body, size_t &len) = 0; }; // The HttpResponseCallback class receives HTTP client responses class HttpResponseCallback { public: - // Called when an HTTP request completes. - // The passed response object contains details about the exact way the - // request finished (HTTP status code, headers, content, error codes - // etc.). The ownership of the response object is transferred to the - // callback object. It can store it for later if necessary. Finally, it - // must be deleted using its virtual destructor. - virtual void OnHttpResponse(HttpResponse* response) = 0; + // Called when an HTTP request completes. + // The passed response object contains details about the exact way the + // request finished (HTTP status code, headers, content, error codes + // etc.). The ownership of the response object is transferred to the + // callback object. It can store it for later if necessary. Finally, it + // must be deleted using its virtual destructor. + virtual void OnHttpResponse(HttpResponse *response) = 0; }; // The HttpClient class is the interface for HTTP client implementations. class HttpClient { public: - - // Creates an empty HTTP request object. - // The created request object has only its ID prepopulated. Other fields - // must be set by the caller. The request object can then be sent - // using SendRequestAsync(). If you are not going to use the request object - // then you can delete it safely using its virtual destructor. - virtual HttpRequest* CreateRequest() = 0; - - // Begins an HTTP request. - // The method takes ownership of the passed request, and can destroy it before - // returning to the caller. Do not access the request object in any - // way after this invocation, and do not delete it. - // The callback object is always called, even if the request is - // cancelled, or if an error occurs immediately during sending. In the - // latter case, the OnHttpResponse() callback is called before this - // method returns. You must keep the callback object alive until its - // OnHttpResponse() callback is called. - virtual void SendRequestAsync(HttpRequest* request, HttpResponseCallback* callback) = 0; - - // Cancels an HTTP request. - // The caller must provide a string ID returned earlier by request->GetId(). - // The request is cancelled asynchronously. The caller must still - // wait for the relevant OnHttpResponse() callback (it can just come - // earlier with some "aborted" error status). - virtual void CancelRequestAsync(nostd::string_view const& id) = 0; - - virtual void CancelAllRequests() - { - } + // Creates an empty HTTP request object. + // The created request object has only its ID prepopulated. Other fields + // must be set by the caller. The request object can then be sent + // using SendRequestAsync(). If you are not going to use the request object + // then you can delete it safely using its virtual destructor. + virtual HttpRequest *CreateRequest() = 0; + + // Begins an HTTP request. + // The method takes ownership of the passed request, and can destroy it before + // returning to the caller. Do not access the request object in any + // way after this invocation, and do not delete it. + // The callback object is always called, even if the request is + // cancelled, or if an error occurs immediately during sending. In the + // latter case, the OnHttpResponse() callback is called before this + // method returns. You must keep the callback object alive until its + // OnHttpResponse() callback is called. + virtual void SendRequestAsync(HttpRequest *request, HttpResponseCallback *callback) = 0; + + // Cancels an HTTP request. + // The caller must provide a string ID returned earlier by request->GetId(). + // The request is cancelled asynchronously. The caller must still + // wait for the relevant OnHttpResponse() callback (it can just come + // earlier with some "aborted" error status). + virtual void CancelRequestAsync(nostd::string_view const &id) = 0; + + virtual void CancelAllRequests() {} }; -} // namespace nostd +} // namespace http OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/include/opentelemetry/sdk/http/http_client.h b/sdk/include/opentelemetry/sdk/http/http_client.h index 282c373729..e0ef35c18e 100644 --- a/sdk/include/opentelemetry/sdk/http/http_client.h +++ b/sdk/include/opentelemetry/sdk/http/http_client.h @@ -27,142 +27,113 @@ namespace http namespace http_api = opentelemetry::http; // HttpHeaders implementation -class HttpHeaders: http_api::HttpHeaders, std::multimap +class HttpHeaders : http_api::HttpHeaders, std::multimap { public: - virtual void set(nostd::string_view const& name, nostd::string_view const& value) override - { - auto range = equal_range(std::string(name.data())); - auto hint = erase(range.first, range.second); - insert(hint, std::make_pair(name, value)); - } - - virtual void add(nostd::string_view const& name, nostd::string_view const& value) override - { - insert(std::make_pair(name, value)); - } - - virtual nostd::string_view const& get(nostd::string_view const& name) const override - { - auto it = find(std::string(name.data())); - return (it != end()) ? it->second : m_empty; - } - - virtual bool has(nostd::string_view const& name) const override - { - auto it = find(std::string(name.data())); - return (it != end()); - } + virtual void set(nostd::string_view const &name, nostd::string_view const &value) override + { + auto range = equal_range(std::string(name.data())); + auto hint = erase(range.first, range.second); + insert(hint, std::make_pair(name, value)); + } + + virtual void add(nostd::string_view const &name, nostd::string_view const &value) override + { + insert(std::make_pair(name, value)); + } + + virtual nostd::string_view const &get(nostd::string_view const &name) const override + { + auto it = find(std::string(name.data())); + return (it != end()) ? it->second : m_empty; + } + + virtual bool has(nostd::string_view const &name) const override + { + auto it = find(std::string(name.data())); + return (it != end()); + } private: - nostd::string_view m_empty{}; + nostd::string_view m_empty{}; }; -class SimpleHttpRequest: public http_api::HttpRequest +class SimpleHttpRequest : public http_api::HttpRequest { public: + SimpleHttpRequest(nostd::string_view const &id) : id_(id), method_("GET") {} - SimpleHttpRequest(nostd::string_view const& id): id_(id), method_("GET") - { - } - - // Gets the HTTP request ID. - virtual const nostd::string_view& GetId() const override - { - return id_; - } + // Gets the HTTP request ID. + virtual const nostd::string_view &GetId() const override { return id_; } - // Sets the request method - virtual void SetMethod(nostd::string_view const& method) override - { - method_ = method; - } + // Sets the request method + virtual void SetMethod(nostd::string_view const &method) override { method_ = method; } - // Sets the HTTP request URI. - virtual void SetUrl(nostd::string_view const& url) override - { - url_ = url; - } + // Sets the HTTP request URI. + virtual void SetUrl(nostd::string_view const &url) override { url_ = url; } - // Gets the HTTP request headers. - virtual http_api::HttpHeaders* GetHeaders() const override - { - return headers_; - } + // Gets the HTTP request headers. + virtual http_api::HttpHeaders *GetHeaders() const override { return headers_; } - // Sets the request body. - virtual void SetBody(const uint8_t* const body, const size_t len) override + // Sets the request body. + virtual void SetBody(const uint8_t *const body, const size_t len) override + { + for (int i = 0; i < len; i++) { - for (int i = 0; i < len; i++) - { - body_.push_back(body[i]); - } + body_.push_back(body[i]); } + } - virtual void GetBody(uint8_t* body, size_t& len) override + virtual void GetBody(uint8_t *body, size_t &len) override + { + len = 0; + for (auto &e : body_) { - len = 0; - for (auto &e: body_) - { - body[len++] = e; - } - len = (len > 0 ) ? len - 1 : len ; + body[len++] = e; } + len = (len > 0) ? len - 1 : len; + } private: - http_api::HttpHeaders* headers_; - std::vector body_; - nostd::string_view method_; - nostd::string_view id_; - nostd::string_view url_; - - + http_api::HttpHeaders *headers_; + std::vector body_; + nostd::string_view method_; + nostd::string_view id_; + nostd::string_view url_; }; -class SimpleHttpResponse: public http_api::HttpResponse +class SimpleHttpResponse : public http_api::HttpResponse { public: - SimpleHttpResponse(nostd::string_view const& id): - id_(id), - result_(http_api::HttpResult::HttpResult_LocalFailure), - statusCode_(0) - { - } + SimpleHttpResponse(nostd::string_view const &id) + : id_(id), result_(http_api::HttpResult::HttpResult_LocalFailure), statusCode_(0) + {} - virtual http_api::HttpResult GetResult() override - { - return result_; - } + virtual http_api::HttpResult GetResult() override { return result_; } - virtual unsigned GetStatusCode() override - { - return statusCode_; - } + virtual unsigned GetStatusCode() override { return statusCode_; } - virtual http_api::HttpHeaders* GetHeaders() override - { - return headers_; - } + virtual http_api::HttpHeaders *GetHeaders() override { return headers_; } - virtual void GetBody(uint8_t* body, size_t& len) override + virtual void GetBody(uint8_t *body, size_t &len) override + { + int i = 0; + for (auto &e : body_) { - int i = 0; - for (auto &e: body_) - { - body_[i++] = e; - } + body_[i++] = e; } + } private: - nostd::string_view id_; - unsigned statusCode_; - http_api::HttpHeaders* headers_; - http_api::HttpResult result_; - std::vector body_; + nostd::string_view id_; + unsigned statusCode_; + http_api::HttpHeaders *headers_; + http_api::HttpResult result_; + std::vector body_; }; -} //http -} //sdk +} // namespace http +} // namespace sdk OPENTELEMETRY_END_NAMESPACE From c648f9f90742731f2bc34a2883bf2cfa9b507fdf Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 1 Sep 2020 11:34:20 +0000 Subject: [PATCH 11/12] review comments --- api/include/opentelemetry/http/http_client.h | 8 ++++---- .../opentelemetry/sdk/http/http_client.h | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/api/include/opentelemetry/http/http_client.h b/api/include/opentelemetry/http/http_client.h index 6a27cca475..f05293261c 100644 --- a/api/include/opentelemetry/http/http_client.h +++ b/api/include/opentelemetry/http/http_client.h @@ -57,7 +57,7 @@ class HttpHeaders virtual nostd::string_view const &get(nostd::string_view const &name) const = 0; // Tests whether the headers contain the specified name. - virtual bool has(nostd::string_view const &name) const = 0; + virtual bool contains(nostd::string_view const &name) const = 0; }; // The HttpRequest class represents a Request object. @@ -145,16 +145,16 @@ class HttpClient // latter case, the OnHttpResponse() callback is called before this // method returns. You must keep the callback object alive until its // OnHttpResponse() callback is called. - virtual void SendRequestAsync(HttpRequest *request, HttpResponseCallback *callback) = 0; + virtual void SendRequest(HttpRequest *request, HttpResponseCallback *callback) = 0; // Cancels an HTTP request. // The caller must provide a string ID returned earlier by request->GetId(). // The request is cancelled asynchronously. The caller must still // wait for the relevant OnHttpResponse() callback (it can just come // earlier with some "aborted" error status). - virtual void CancelRequestAsync(nostd::string_view const &id) = 0; + virtual void CancelRequest(nostd::string_view const &id) = 0; - virtual void CancelAllRequests() {} + virtual void CancelAllRequestsSync() {} }; } // namespace http diff --git a/sdk/include/opentelemetry/sdk/http/http_client.h b/sdk/include/opentelemetry/sdk/http/http_client.h index e0ef35c18e..167f2e66dc 100644 --- a/sdk/include/opentelemetry/sdk/http/http_client.h +++ b/sdk/include/opentelemetry/sdk/http/http_client.h @@ -18,6 +18,9 @@ #include #include +#include +#include +#include OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -26,10 +29,19 @@ namespace http { namespace http_api = opentelemetry::http; +static std::function CaseInsensitiveComparator + = [](const std::string& s1, const std::string& s2) -> bool { + std::string str1(s1.length(),' '); + std::string str2(s2.length(),' '); + auto lowerCase = [](char c) -> char { return tolower(c);}; + std::transform(s1.begin(), s1.end(), str1.begin(), lowerCase); + std::transform(s2.begin(), s2.end(), str2.begin(), lowerCase); + return str1 < str2; + }; + // HttpHeaders implementation -class HttpHeaders : http_api::HttpHeaders, std::multimap +class HttpHeaders : http_api::HttpHeaders, std::multimap { - public: virtual void set(nostd::string_view const &name, nostd::string_view const &value) override { @@ -49,7 +61,7 @@ class HttpHeaders : http_api::HttpHeaders, std::multimapsecond : m_empty; } - virtual bool has(nostd::string_view const &name) const override + virtual bool contains(nostd::string_view const &name) const override { auto it = find(std::string(name.data())); return (it != end()); From aa8ee7cfdaee78157bb770a04fa1a3454289d47d Mon Sep 17 00:00:00 2001 From: Lalit Kumar Bhasin Date: Tue, 1 Sep 2020 12:30:19 +0000 Subject: [PATCH 12/12] fix review comments --- api/include/opentelemetry/http/http_client.h | 4 +-- .../opentelemetry/sdk/http/http_client.h | 36 ++++++++++--------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/api/include/opentelemetry/http/http_client.h b/api/include/opentelemetry/http/http_client.h index f05293261c..de41a41c12 100644 --- a/api/include/opentelemetry/http/http_client.h +++ b/api/include/opentelemetry/http/http_client.h @@ -78,7 +78,7 @@ class HttpRequest virtual void SetUrl(nostd::string_view const &url) = 0; // Gets the request Headers - virtual HttpHeaders *GetHeaders() const = 0; + virtual HttpHeaders &GetHeaders() const = 0; // Sets the request body virtual void SetBody(const uint8_t *const body, const size_t len) = 0; @@ -106,7 +106,7 @@ class HttpResponse virtual unsigned GetStatusCode() = 0; // Gets the response headers. - virtual HttpHeaders *GetHeaders() = 0; + virtual const HttpHeaders &GetHeaders() = 0; // Gets the response body. virtual void GetBody(uint8_t *body, size_t &len) = 0; diff --git a/sdk/include/opentelemetry/sdk/http/http_client.h b/sdk/include/opentelemetry/sdk/http/http_client.h index 167f2e66dc..0364459f62 100644 --- a/sdk/include/opentelemetry/sdk/http/http_client.h +++ b/sdk/include/opentelemetry/sdk/http/http_client.h @@ -16,11 +16,12 @@ #include "opentelemetry/http/http_client.h" #include "opentelemetry/version.h" -#include -#include -#include #include +#include #include +#include +#include +#include OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk @@ -29,18 +30,19 @@ namespace http { namespace http_api = opentelemetry::http; -static std::function CaseInsensitiveComparator - = [](const std::string& s1, const std::string& s2) -> bool { - std::string str1(s1.length(),' '); - std::string str2(s2.length(),' '); - auto lowerCase = [](char c) -> char { return tolower(c);}; - std::transform(s1.begin(), s1.end(), str1.begin(), lowerCase); - std::transform(s2.begin(), s2.end(), str2.begin(), lowerCase); - return str1 < str2; - }; +static std::function CaseInsensitiveComparator = + [](const std::string &s1, const std::string &s2) -> bool { + std::string str1(s1.length(), ' '); + std::string str2(s2.length(), ' '); + auto lowerCase = [](char c) -> char { return tolower(c); }; + std::transform(s1.begin(), s1.end(), str1.begin(), lowerCase); + std::transform(s2.begin(), s2.end(), str2.begin(), lowerCase); + return str1 < str2; +}; // HttpHeaders implementation -class HttpHeaders : http_api::HttpHeaders, std::multimap +class HttpHeaders : http_api::HttpHeaders, + std::multimap { public: virtual void set(nostd::string_view const &name, nostd::string_view const &value) override @@ -87,7 +89,7 @@ class SimpleHttpRequest : public http_api::HttpRequest virtual void SetUrl(nostd::string_view const &url) override { url_ = url; } // Gets the HTTP request headers. - virtual http_api::HttpHeaders *GetHeaders() const override { return headers_; } + virtual http_api::HttpHeaders &GetHeaders() const override { return *headers_; } // Sets the request body. virtual void SetBody(const uint8_t *const body, const size_t len) override @@ -109,7 +111,7 @@ class SimpleHttpRequest : public http_api::HttpRequest } private: - http_api::HttpHeaders *headers_; + std::unique_ptr headers_; std::vector body_; nostd::string_view method_; nostd::string_view id_; @@ -128,7 +130,7 @@ class SimpleHttpResponse : public http_api::HttpResponse virtual unsigned GetStatusCode() override { return statusCode_; } - virtual http_api::HttpHeaders *GetHeaders() override { return headers_; } + virtual const http_api::HttpHeaders &GetHeaders() override { return *headers_; } virtual void GetBody(uint8_t *body, size_t &len) override { @@ -142,7 +144,7 @@ class SimpleHttpResponse : public http_api::HttpResponse private: nostd::string_view id_; unsigned statusCode_; - http_api::HttpHeaders *headers_; + std::unique_ptr headers_; http_api::HttpResult result_; std::vector body_; };