From 81fcd20078718f4a3d4faae6ac8c6a3d5b776885 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Thu, 23 Feb 2023 10:10:17 -0800 Subject: [PATCH 01/23] point android extensions to correct branch --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e50fa9d..2282e4d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,8 +25,8 @@ endif() if(ANDROID AND NOT TARGET AndroidExtensions) FetchContent_Declare( AndroidExtensions - GIT_REPOSITORY https://github.com/BabylonJS/AndroidExtensions.git - GIT_TAG efaa68b2882d882470e7d1374b0d52d80db3ca35) + GIT_REPOSITORY https://github.com/lmaskati/AndroidExtensions.git + GIT_TAG origin/lmaskati/test) message(STATUS "Fetching AndroidExtensions") FetchContent_MakeAvailable(AndroidExtensions) From ae8312f4565e1bb5847daa4554e0ed656765ed03 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Thu, 23 Feb 2023 10:49:27 -0800 Subject: [PATCH 02/23] test print --- Source/UrlRequest_Android.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Source/UrlRequest_Android.cpp b/Source/UrlRequest_Android.cpp index bc0f7a5..5ba648e 100644 --- a/Source/UrlRequest_Android.cpp +++ b/Source/UrlRequest_Android.cpp @@ -34,6 +34,7 @@ namespace UrlLib public: void Open(UrlMethod method, const std::string& url) { + std::cout << "helloooo" << std::endl; m_method = method; Uri uri{Uri::Parse(url.data())}; // If the URL string doesn't contain a scheme, the URI object's scheme will be null. We throw in this case From 0a85220c1d32169cd239e534e22e75a75e345c67 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Thu, 23 Feb 2023 10:56:55 -0800 Subject: [PATCH 03/23] add missing imports --- Source/UrlRequest_Android.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Source/UrlRequest_Android.cpp b/Source/UrlRequest_Android.cpp index 5ba648e..2a64900 100644 --- a/Source/UrlRequest_Android.cpp +++ b/Source/UrlRequest_Android.cpp @@ -4,6 +4,7 @@ #include #include #include +#include using namespace android::global; using namespace android::net; From 8c5d23adb41be36230367edb7acdc53df02b51d3 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Fri, 24 Feb 2023 09:25:18 -0800 Subject: [PATCH 04/23] test newly added method --- Source/UrlRequest_Android.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/UrlRequest_Android.cpp b/Source/UrlRequest_Android.cpp index 2a64900..41d8ab6 100644 --- a/Source/UrlRequest_Android.cpp +++ b/Source/UrlRequest_Android.cpp @@ -35,7 +35,6 @@ namespace UrlLib public: void Open(UrlMethod method, const std::string& url) { - std::cout << "helloooo" << std::endl; m_method = method; Uri uri{Uri::Parse(url.data())}; // If the URL string doesn't contain a scheme, the URI object's scheme will be null. We throw in this case @@ -94,7 +93,8 @@ namespace UrlLib URLConnection connection{url.OpenConnection()}; connection.Connect(); - + auto temp = connection.GetDoOutput(); + std::cout << temp << std::endl; if (connection.GetClass().IsAssignableFrom(HttpURLConnection::Class())) { m_statusCode = static_cast(((HttpURLConnection)connection).GetResponseCode()); From f5566ca2c27660268251c9957016b47ba168519e Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Thu, 2 Mar 2023 10:41:54 -0800 Subject: [PATCH 05/23] adding logic for setRequestHeaders --- Include/UrlLib/UrlLib.h | 8 ++++++++ Source/UrlRequest_Android.cpp | 10 ++++++++++ Source/UrlRequest_Base.h | 16 ++++++++++++++++ Source/UrlRequest_Shared.h | 15 +++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/Include/UrlLib/UrlLib.h b/Include/UrlLib/UrlLib.h index c10adb8..c7c28f0 100644 --- a/Include/UrlLib/UrlLib.h +++ b/Include/UrlLib/UrlLib.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace UrlLib { @@ -17,6 +18,7 @@ namespace UrlLib enum class UrlMethod { Get, + Post }; enum class UrlResponseType @@ -49,6 +51,12 @@ namespace UrlLib arcana::task SendAsync(); + void SetRequestBody(std::string requestBody); + + void SetRequestHeader(std::string name, std::string value); + + std::unordered_map GetAllResponseHeaders() const; + UrlStatusCode StatusCode() const; std::string_view ResponseUrl() const; diff --git a/Source/UrlRequest_Android.cpp b/Source/UrlRequest_Android.cpp index 41d8ab6..26aca02 100644 --- a/Source/UrlRequest_Android.cpp +++ b/Source/UrlRequest_Android.cpp @@ -92,6 +92,16 @@ namespace UrlLib URL url{m_appPathOrUrl.data()}; URLConnection connection{url.OpenConnection()}; + + // set request headers + for (auto request : m_requestHeaders) + { + std::string key = request.first; + std::string value = request.second; + connection.SetRequestProperty(key, value); + } + m_requestHeaders.clear(); + connection.Connect(); auto temp = connection.GetDoOutput(); std::cout << temp << std::endl; diff --git a/Source/UrlRequest_Base.h b/Source/UrlRequest_Base.h index 703b705..f10c01b 100644 --- a/Source/UrlRequest_Base.h +++ b/Source/UrlRequest_Base.h @@ -21,6 +21,20 @@ namespace UrlLib m_cancellationSource.cancel(); } + void SetRequestBody(std::string requestBody) { + m_requestBody = requestBody; + } + + void SetRequestHeader(std::string name, std::string value) + { + m_requestHeaders[name] = value; + } + + std::unordered_map GetAllResponseHeaders() const + { + return m_headers; + } + UrlResponseType ResponseType() const { return m_responseType; @@ -77,5 +91,7 @@ namespace UrlLib std::string m_responseUrl{}; std::string m_responseString{}; std::unordered_map m_headers; + std::string m_requestBody{}; + std::unordered_map m_requestHeaders; }; } diff --git a/Source/UrlRequest_Shared.h b/Source/UrlRequest_Shared.h index d3e9652..ff199a6 100644 --- a/Source/UrlRequest_Shared.h +++ b/Source/UrlRequest_Shared.h @@ -37,6 +37,21 @@ namespace UrlLib m_impl->ResponseType(value); } + void UrlRequest::SetRequestBody(std::string requestBody) + { + m_impl->SetRequestBody(requestBody); + } + + void UrlRequest::SetRequestHeader(std::string key, std::string value) + { + m_impl->SetRequestHeader(key, value); + } + + std::unordered_map UrlRequest::GetAllResponseHeaders() const + { + return m_impl->GetAllResponseHeaders(); + } + arcana::task UrlRequest::SendAsync() { return m_impl->SendAsync(); From be091084102591bfb17e94f8ed77cf179f4dc3c4 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Sun, 5 Mar 2023 22:46:31 -0800 Subject: [PATCH 06/23] add android post request logic --- Source/UrlRequest_Android.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Source/UrlRequest_Android.cpp b/Source/UrlRequest_Android.cpp index 26aca02..e130662 100644 --- a/Source/UrlRequest_Android.cpp +++ b/Source/UrlRequest_Android.cpp @@ -92,6 +92,21 @@ namespace UrlLib URL url{m_appPathOrUrl.data()}; URLConnection connection{url.OpenConnection()}; + + // if this a POST request + if (m_method == UrlMethod::Post) { + ((HttpURLConnection)connection).SetRequestMethod("POST"); + connection.SetDoOutput(true); + + // need to manually set the content length of the request body + size_t numBytes = m_requestBody.size(); + connection.SetRequestProperty("Content-Length", std::to_string(numBytes)); + + OutputStream outputStream{connection.GetOutputStream()}; + OutputStreamWriter writer{outputStream}; + writer.Write(m_requestBody); + writer.Close(); + } // set request headers for (auto request : m_requestHeaders) @@ -103,8 +118,6 @@ namespace UrlLib m_requestHeaders.clear(); connection.Connect(); - auto temp = connection.GetDoOutput(); - std::cout << temp << std::endl; if (connection.GetClass().IsAssignableFrom(HttpURLConnection::Class())) { m_statusCode = static_cast(((HttpURLConnection)connection).GetResponseCode()); From a5ff5246148a90c9630ff876f51dc93954c97d8f Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Sun, 5 Mar 2023 22:54:19 -0800 Subject: [PATCH 07/23] Add windows implementation for post and setRequestHeaders --- Source/UrlRequest_Windows.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Source/UrlRequest_Windows.cpp b/Source/UrlRequest_Windows.cpp index 0800294..4440a5e 100644 --- a/Source/UrlRequest_Windows.cpp +++ b/Source/UrlRequest_Windows.cpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace UrlLib { @@ -21,6 +22,8 @@ namespace UrlLib { case UrlMethod::Get: return Web::Http::HttpMethod::Get(); + case UrlMethod::Post: + return Web::Http::HttpMethod::Post(); default: throw std::runtime_error("Unsupported method"); } @@ -83,6 +86,31 @@ namespace UrlLib requestMessage.RequestUri(m_uri); requestMessage.Method(ConvertHttpMethod(m_method)); + std::string contentType = ""; + + for (auto request : m_requestHeaders) + { + // content type needs to be set separately + if (request.first == "Content-Type") { + contentType = request.second; + } + else { + requestMessage.Headers().Append(winrt::to_hstring(request.first), winrt::to_hstring(request.second)); + } + } + + m_requestHeaders.clear(); + + // check the method + if (m_method == UrlMethod::Post) { + // if post, set the content type + requestMessage.Content(Web::Http::HttpStringContent( + winrt::to_hstring(m_requestBody), + winrt::Windows::Storage::Streams::UnicodeEncoding::Utf8, + winrt::to_hstring(contentType)) + ); + } + Web::Http::HttpClient client; return arcana::create_task(client.SendRequestAsync(requestMessage)) .then(arcana::inline_scheduler, m_cancellationSource, [this](Web::Http::HttpResponseMessage responseMessage) @@ -98,6 +126,10 @@ namespace UrlLib for (auto&& iter : responseMessage.Headers()) { m_headers.insert(std::make_pair(winrt::to_string(iter.Key()), winrt::to_string(iter.Value()))); + + std::string contentTypeValue = winrt::to_string(responseMessage.Content().Headers().ContentType().ToString()); + std::string contentTypeKey = "content-type"; + m_headers.insert(std::make_pair(contentTypeKey, contentTypeValue)); } switch (m_responseType) From 9a57c320a2096ce31f2199af41ace3415d7016d8 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Sun, 5 Mar 2023 23:02:48 -0800 Subject: [PATCH 08/23] add post logic and set request headers for apple --- Source/UrlRequest_Apple.mm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Source/UrlRequest_Apple.mm b/Source/UrlRequest_Apple.mm index d351b48..3c0b511 100644 --- a/Source/UrlRequest_Apple.mm +++ b/Source/UrlRequest_Apple.mm @@ -52,6 +52,23 @@ void Open(UrlMethod method, const std::string& url) NSURLSession* session{[NSURLSession sharedSession]}; NSURLRequest* request{[NSURLRequest requestWithURL:m_url]}; + NSMutableURLRequest* mutableRequest{[request mutableCopy]}; + + // set header requests + for (auto request: m_requestHeaders) { + [mutableRequest setValue:@(request.second.data()) forHTTPHeaderField:@(request.first.data())]; + } + + if(m_method == UrlMethod::Post) { + mutableRequest.HTTPMethod = @"POST"; + // set the body + NSString *stringBody = [NSString stringWithUTF8String:m_requestBody.data()]; + NSData *requestBodyData = [stringBody dataUsingEncoding:NSUTF8StringEncoding]; + mutableRequest.HTTPBody = requestBodyData; + } + + request = [mutableRequest copy]; + __block arcana::task_completion_source taskCompletionSource{}; id completionHandler{^(NSData* data, NSURLResponse* response, NSError* error) From dee7f25987658f29d762f9eeadb955a0c52fc95e Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Sun, 5 Mar 2023 23:55:51 -0800 Subject: [PATCH 09/23] set request headers before processing POST --- Source/UrlRequest_Android.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Source/UrlRequest_Android.cpp b/Source/UrlRequest_Android.cpp index e130662..6dd8431 100644 --- a/Source/UrlRequest_Android.cpp +++ b/Source/UrlRequest_Android.cpp @@ -92,7 +92,16 @@ namespace UrlLib URL url{m_appPathOrUrl.data()}; URLConnection connection{url.OpenConnection()}; - + + // set request headers + for (auto request : m_requestHeaders) + { + std::string key = request.first; + std::string value = request.second; + connection.SetRequestProperty(key, value); + } + m_requestHeaders.clear(); + // if this a POST request if (m_method == UrlMethod::Post) { ((HttpURLConnection)connection).SetRequestMethod("POST"); @@ -108,15 +117,6 @@ namespace UrlLib writer.Close(); } - // set request headers - for (auto request : m_requestHeaders) - { - std::string key = request.first; - std::string value = request.second; - connection.SetRequestProperty(key, value); - } - m_requestHeaders.clear(); - connection.Connect(); if (connection.GetClass().IsAssignableFrom(HttpURLConnection::Class())) { From 347433fac823ee5015bbed52af63f8ffd6521e77 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Mon, 6 Mar 2023 00:03:00 -0800 Subject: [PATCH 10/23] remove iostream import --- Source/UrlRequest_Android.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/UrlRequest_Android.cpp b/Source/UrlRequest_Android.cpp index 6dd8431..2b0b88e 100644 --- a/Source/UrlRequest_Android.cpp +++ b/Source/UrlRequest_Android.cpp @@ -4,7 +4,6 @@ #include #include #include -#include using namespace android::global; using namespace android::net; From 9b6d53060d7fd3cc496a5892ea4ab8bc8e1b48f5 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Tue, 7 Mar 2023 09:53:46 -0800 Subject: [PATCH 11/23] modify CMake to point at original repo --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2282e4d..dbefd95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,8 +25,8 @@ endif() if(ANDROID AND NOT TARGET AndroidExtensions) FetchContent_Declare( AndroidExtensions - GIT_REPOSITORY https://github.com/lmaskati/AndroidExtensions.git - GIT_TAG origin/lmaskati/test) + GIT_REPOSITORY https://github.com/BabylonJS/AndroidExtensions.git + GIT_TAG 4a54e636bde25d4b9f1b02e2414ee642005ff244) message(STATUS "Fetching AndroidExtensions") FetchContent_MakeAvailable(AndroidExtensions) From e5131d07ab3630047456e81fbb5a7c9b065205d3 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati <38430340+lmaskati@users.noreply.github.com> Date: Tue, 7 Mar 2023 09:54:25 -0800 Subject: [PATCH 12/23] Update Source/UrlRequest_Android.cpp Formatting Co-authored-by: Gary Hsu --- Source/UrlRequest_Android.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/UrlRequest_Android.cpp b/Source/UrlRequest_Android.cpp index 2b0b88e..6c5e01d 100644 --- a/Source/UrlRequest_Android.cpp +++ b/Source/UrlRequest_Android.cpp @@ -102,7 +102,8 @@ namespace UrlLib m_requestHeaders.clear(); // if this a POST request - if (m_method == UrlMethod::Post) { + if (m_method == UrlMethod::Post) + { ((HttpURLConnection)connection).SetRequestMethod("POST"); connection.SetDoOutput(true); From 3bb72303ede319961b2c12425b5509945e75bed8 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati <38430340+lmaskati@users.noreply.github.com> Date: Tue, 7 Mar 2023 09:54:35 -0800 Subject: [PATCH 13/23] Update Source/UrlRequest_Apple.mm Co-authored-by: Gary Hsu --- Source/UrlRequest_Apple.mm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/UrlRequest_Apple.mm b/Source/UrlRequest_Apple.mm index 3c0b511..c8d59cf 100644 --- a/Source/UrlRequest_Apple.mm +++ b/Source/UrlRequest_Apple.mm @@ -55,7 +55,8 @@ void Open(UrlMethod method, const std::string& url) NSMutableURLRequest* mutableRequest{[request mutableCopy]}; // set header requests - for (auto request: m_requestHeaders) { + for (auto request: m_requestHeaders) + { [mutableRequest setValue:@(request.second.data()) forHTTPHeaderField:@(request.first.data())]; } From 82e863bf080b0d88ca4f3759842b7fc2ca3cc147 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati <38430340+lmaskati@users.noreply.github.com> Date: Tue, 7 Mar 2023 09:55:05 -0800 Subject: [PATCH 14/23] Formatting Co-authored-by: Gary Hsu --- Source/UrlRequest_Windows.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/UrlRequest_Windows.cpp b/Source/UrlRequest_Windows.cpp index 4440a5e..5281723 100644 --- a/Source/UrlRequest_Windows.cpp +++ b/Source/UrlRequest_Windows.cpp @@ -102,7 +102,8 @@ namespace UrlLib m_requestHeaders.clear(); // check the method - if (m_method == UrlMethod::Post) { + if (m_method == UrlMethod::Post) + { // if post, set the content type requestMessage.Content(Web::Http::HttpStringContent( winrt::to_hstring(m_requestBody), From aa2c525216b68d4d26e89d6a01eabd99a699567f Mon Sep 17 00:00:00 2001 From: Liyaan Maskati <38430340+lmaskati@users.noreply.github.com> Date: Tue, 7 Mar 2023 09:55:20 -0800 Subject: [PATCH 15/23] Formatting Co-authored-by: Gary Hsu --- Source/UrlRequest_Apple.mm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/UrlRequest_Apple.mm b/Source/UrlRequest_Apple.mm index c8d59cf..2eb9a8c 100644 --- a/Source/UrlRequest_Apple.mm +++ b/Source/UrlRequest_Apple.mm @@ -60,7 +60,8 @@ void Open(UrlMethod method, const std::string& url) [mutableRequest setValue:@(request.second.data()) forHTTPHeaderField:@(request.first.data())]; } - if(m_method == UrlMethod::Post) { + if (m_method == UrlMethod::Post) + { mutableRequest.HTTPMethod = @"POST"; // set the body NSString *stringBody = [NSString stringWithUTF8String:m_requestBody.data()]; From fa414049334e9cd94322d1724ccf98c24de7822b Mon Sep 17 00:00:00 2001 From: Liyaan Maskati <38430340+lmaskati@users.noreply.github.com> Date: Tue, 7 Mar 2023 09:55:58 -0800 Subject: [PATCH 16/23] Formatting Co-authored-by: Gary Hsu --- Source/UrlRequest_Windows.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/UrlRequest_Windows.cpp b/Source/UrlRequest_Windows.cpp index 5281723..6b5ee82 100644 --- a/Source/UrlRequest_Windows.cpp +++ b/Source/UrlRequest_Windows.cpp @@ -94,7 +94,8 @@ namespace UrlLib if (request.first == "Content-Type") { contentType = request.second; } - else { + else + { requestMessage.Headers().Append(winrt::to_hstring(request.first), winrt::to_hstring(request.second)); } } From d949839a8a8a53bb36f71b0a251261c64fc6a8bd Mon Sep 17 00:00:00 2001 From: Liyaan Maskati <38430340+lmaskati@users.noreply.github.com> Date: Tue, 7 Mar 2023 10:13:45 -0800 Subject: [PATCH 17/23] formatting Co-authored-by: Gary Hsu --- Source/UrlRequest_Apple.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/UrlRequest_Apple.mm b/Source/UrlRequest_Apple.mm index 2eb9a8c..22c8912 100644 --- a/Source/UrlRequest_Apple.mm +++ b/Source/UrlRequest_Apple.mm @@ -64,8 +64,8 @@ void Open(UrlMethod method, const std::string& url) { mutableRequest.HTTPMethod = @"POST"; // set the body - NSString *stringBody = [NSString stringWithUTF8String:m_requestBody.data()]; - NSData *requestBodyData = [stringBody dataUsingEncoding:NSUTF8StringEncoding]; + NSString* stringBody = [NSString stringWithUTF8String:m_requestBody.data()]; + NSData* requestBodyData = [stringBody dataUsingEncoding:NSUTF8StringEncoding]; mutableRequest.HTTPBody = requestBodyData; } From ca9cde7ef7456920331d5950dbd84a299a7b4b6d Mon Sep 17 00:00:00 2001 From: Liyaan Maskati <38430340+lmaskati@users.noreply.github.com> Date: Tue, 7 Mar 2023 10:17:06 -0800 Subject: [PATCH 18/23] Remove unnecessary string copies Co-authored-by: Gary Hsu --- Source/UrlRequest_Android.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/UrlRequest_Android.cpp b/Source/UrlRequest_Android.cpp index 6c5e01d..cabf3c1 100644 --- a/Source/UrlRequest_Android.cpp +++ b/Source/UrlRequest_Android.cpp @@ -95,8 +95,8 @@ namespace UrlLib // set request headers for (auto request : m_requestHeaders) { - std::string key = request.first; - std::string value = request.second; + const std::string& key = request.first; + const std::string& value = request.second; connection.SetRequestProperty(key, value); } m_requestHeaders.clear(); From a473d09391766b1d8cce6c178011df9a1fa60f23 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Tue, 7 Mar 2023 10:27:49 -0800 Subject: [PATCH 19/23] process content type response header outside for loop --- Source/UrlRequest_Windows.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/UrlRequest_Windows.cpp b/Source/UrlRequest_Windows.cpp index 4440a5e..3fcb74e 100644 --- a/Source/UrlRequest_Windows.cpp +++ b/Source/UrlRequest_Windows.cpp @@ -126,11 +126,11 @@ namespace UrlLib for (auto&& iter : responseMessage.Headers()) { m_headers.insert(std::make_pair(winrt::to_string(iter.Key()), winrt::to_string(iter.Value()))); - - std::string contentTypeValue = winrt::to_string(responseMessage.Content().Headers().ContentType().ToString()); - std::string contentTypeKey = "content-type"; - m_headers.insert(std::make_pair(contentTypeKey, contentTypeValue)); } + // process the content type response header + std::string contentTypeValue = winrt::to_string(responseMessage.Content().Headers().ContentType().ToString()); + std::string contentTypeKey = "content-type"; + m_headers.insert(std::make_pair(contentTypeKey, contentTypeValue)); switch (m_responseType) { From 424f4816ce4d3130395f4d2ec63722f6f30c92a6 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati <38430340+lmaskati@users.noreply.github.com> Date: Tue, 7 Mar 2023 10:30:06 -0800 Subject: [PATCH 20/23] Automatically sets to empty string Co-authored-by: Gary Hsu --- Source/UrlRequest_Windows.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/UrlRequest_Windows.cpp b/Source/UrlRequest_Windows.cpp index 1ca64eb..3057cc0 100644 --- a/Source/UrlRequest_Windows.cpp +++ b/Source/UrlRequest_Windows.cpp @@ -86,7 +86,7 @@ namespace UrlLib requestMessage.RequestUri(m_uri); requestMessage.Method(ConvertHttpMethod(m_method)); - std::string contentType = ""; + std::string contentType; for (auto request : m_requestHeaders) { From 826d4cbd1d119dfef4eee1c801d08c7372942869 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Tue, 7 Mar 2023 10:52:47 -0800 Subject: [PATCH 21/23] avoid copying for GetAllResponseHeaders --- Include/UrlLib/UrlLib.h | 2 +- Source/UrlRequest_Base.h | 2 +- Source/UrlRequest_Shared.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Include/UrlLib/UrlLib.h b/Include/UrlLib/UrlLib.h index c7c28f0..ba2fd00 100644 --- a/Include/UrlLib/UrlLib.h +++ b/Include/UrlLib/UrlLib.h @@ -55,7 +55,7 @@ namespace UrlLib void SetRequestHeader(std::string name, std::string value); - std::unordered_map GetAllResponseHeaders() const; + const std::unordered_map& GetAllResponseHeaders() const; UrlStatusCode StatusCode() const; diff --git a/Source/UrlRequest_Base.h b/Source/UrlRequest_Base.h index f10c01b..57a0315 100644 --- a/Source/UrlRequest_Base.h +++ b/Source/UrlRequest_Base.h @@ -30,7 +30,7 @@ namespace UrlLib m_requestHeaders[name] = value; } - std::unordered_map GetAllResponseHeaders() const + const std::unordered_map& GetAllResponseHeaders() const { return m_headers; } diff --git a/Source/UrlRequest_Shared.h b/Source/UrlRequest_Shared.h index ff199a6..de023d0 100644 --- a/Source/UrlRequest_Shared.h +++ b/Source/UrlRequest_Shared.h @@ -47,7 +47,7 @@ namespace UrlLib m_impl->SetRequestHeader(key, value); } - std::unordered_map UrlRequest::GetAllResponseHeaders() const + const std::unordered_map& UrlRequest::GetAllResponseHeaders() const { return m_impl->GetAllResponseHeaders(); } From 3571d70bf5980dbe3f631dec50a0f829dc71e08d Mon Sep 17 00:00:00 2001 From: Gary Hsu Date: Tue, 7 Mar 2023 11:32:17 -0800 Subject: [PATCH 22/23] Whitespace --- Source/UrlRequest_Windows.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/UrlRequest_Windows.cpp b/Source/UrlRequest_Windows.cpp index 3057cc0..3f185d7 100644 --- a/Source/UrlRequest_Windows.cpp +++ b/Source/UrlRequest_Windows.cpp @@ -91,7 +91,8 @@ namespace UrlLib for (auto request : m_requestHeaders) { // content type needs to be set separately - if (request.first == "Content-Type") { + if (request.first == "Content-Type") + { contentType = request.second; } else From 8bd39325b0a1d4d034aa73fd043db93feab66dc4 Mon Sep 17 00:00:00 2001 From: Liyaan Maskati Date: Tue, 7 Mar 2023 13:15:11 -0800 Subject: [PATCH 23/23] group all header related functions together --- Include/UrlLib/UrlLib.h | 4 ++-- Source/UrlRequest_Base.h | 22 +++++++++++----------- Source/UrlRequest_Shared.h | 10 +++++----- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Include/UrlLib/UrlLib.h b/Include/UrlLib/UrlLib.h index ba2fd00..b87a3e1 100644 --- a/Include/UrlLib/UrlLib.h +++ b/Include/UrlLib/UrlLib.h @@ -55,6 +55,8 @@ namespace UrlLib void SetRequestHeader(std::string name, std::string value); + std::optional GetResponseHeader(const std::string& headerName) const; + const std::unordered_map& GetAllResponseHeaders() const; UrlStatusCode StatusCode() const; @@ -65,8 +67,6 @@ namespace UrlLib gsl::span ResponseBuffer() const; - std::optional GetResponseHeader(const std::string& headerName) const; - private: class Impl; class ImplBase; diff --git a/Source/UrlRequest_Base.h b/Source/UrlRequest_Base.h index 57a0315..c8a32a0 100644 --- a/Source/UrlRequest_Base.h +++ b/Source/UrlRequest_Base.h @@ -35,6 +35,17 @@ namespace UrlLib return m_headers; } + std::optional GetResponseHeader(const std::string& headerName) const + { + const auto it = m_headers.find(ToLower(headerName.data())); + if (it == m_headers.end()) + { + return {}; + } + + return it->second; + } + UrlResponseType ResponseType() const { return m_responseType; @@ -60,17 +71,6 @@ namespace UrlLib return m_responseString; } - std::optional GetResponseHeader(const std::string& headerName) const - { - const auto it = m_headers.find(ToLower(headerName.data())); - if (it == m_headers.end()) - { - return {}; - } - - return it->second; - } - protected: static std::string ToLower(const char* str) { diff --git a/Source/UrlRequest_Shared.h b/Source/UrlRequest_Shared.h index de023d0..0b8ff53 100644 --- a/Source/UrlRequest_Shared.h +++ b/Source/UrlRequest_Shared.h @@ -52,6 +52,11 @@ namespace UrlLib return m_impl->GetAllResponseHeaders(); } + std::optional UrlRequest::GetResponseHeader(const std::string& headerName) const + { + return m_impl->GetResponseHeader(headerName); + } + arcana::task UrlRequest::SendAsync() { return m_impl->SendAsync(); @@ -76,9 +81,4 @@ namespace UrlLib { return m_impl->ResponseBuffer(); } - - std::optional UrlRequest::GetResponseHeader(const std::string& headerName) const - { - return m_impl->GetResponseHeader(headerName); - } }