forked from BabylonJS/UrlLib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrlRequest_Win32.cpp
More file actions
122 lines (115 loc) · 4.62 KB
/
Copy pathUrlRequest_Win32.cpp
File metadata and controls
122 lines (115 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "UrlRequest_Windows_Shared.h"
#include <filesystem>
#include <fstream>
#include <sstream>
namespace UrlLib
{
using namespace winrt::Windows;
namespace
{
winrt::hstring GetInstalledLocation()
{
WCHAR modulePath[4096];
DWORD result{::GetModuleFileNameW(nullptr, modulePath, ARRAYSIZE(modulePath))};
winrt::check_bool(result != 0 && result != std::size(modulePath));
winrt::check_hresult(PathCchRemoveFileSpec(modulePath, ARRAYSIZE(modulePath)));
return modulePath;
}
}
arcana::task<void, std::exception_ptr> UrlRequest::Impl::SendAsync()
{
try
{
if (m_uri.SchemeName() == L"app" || m_uri.SchemeName() == L"file")
{
auto path = GetLocalPath(m_uri);
if (m_uri.SchemeName() == L"app")
{
path = std::wstring(GetInstalledLocation()) + L'\\' + path;
}
return LoadFileAsync(path);
}
else
{
return LoadHttpAsync();
}
}
catch (const winrt::hresult_error& error)
{
// Retain the default status code of 0 to indicate a client-side (transport)
// error, but record the WinRT failure via SetError -- consistent with the
// curl/NSURL backends -- so it is diagnosable through
// ErrorString()/ErrorSymbol()/ErrorCode() instead of being silently discarded.
std::ostringstream symbol;
symbol << "0x" << std::hex << std::uppercase << static_cast<uint32_t>(error.code());
SetError("winrt", symbol.str(), static_cast<int32_t>(error.code()), winrt::to_string(error.message()));
return arcana::task_from_result<std::exception_ptr>();
}
}
gsl::span<const std::byte> UrlRequest::Impl::ResponseBuffer() const
{
if (!m_fileResponseBuffer.empty())
{
return {m_fileResponseBuffer.data(), m_fileResponseBuffer.size()};
}
else if (m_responseBuffer)
{
std::byte* bytes;
auto bufferByteAccess = m_responseBuffer.as<::Windows::Storage::Streams::IBufferByteAccess>();
winrt::check_hresult(bufferByteAccess->Buffer(reinterpret_cast<byte**>(&bytes)));
return {bytes, gsl::narrow_cast<std::size_t>(m_responseBuffer.Length())};
}
return {};
}
arcana::task<void, std::exception_ptr> UrlRequest::Impl::LoadFileAsync(const std::wstring& path)
{
switch (m_responseType)
{
case UrlResponseType::String:
{
return arcana::make_task(arcana::threadpool_scheduler, m_cancellationSource, [this, path] {
try
{
std::ifstream file{path};
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
std::ostringstream ss;
ss << file.rdbuf();
m_responseString = ss.str();
m_statusCode = UrlStatusCode::Ok;
}
catch (std::ios_base::failure)
{
std::ostringstream msg;
msg << "Failed to load file '" << winrt::to_string(path) << "'";
throw std::runtime_error{msg.str()};
}
});
}
case UrlResponseType::Buffer:
{
return arcana::make_task(arcana::threadpool_scheduler, m_cancellationSource, [this, path] {
try
{
std::ifstream file{path, std::ios::binary | std::ios::ate};
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
m_fileResponseBuffer.resize(file.tellg());
file.seekg(0, std::ios::beg);
file.read(reinterpret_cast<char*>(m_fileResponseBuffer.data()), m_fileResponseBuffer.size());
m_statusCode = UrlStatusCode::Ok;
}
catch (std::ios::failure)
{
std::ostringstream msg;
msg << "Failed to load file '" << winrt::to_string(path) << "'";
throw std::runtime_error{msg.str()};
}
});
}
default:
{
throw std::runtime_error{"Invalid response type"};
}
}
}
}
#include "UrlRequest_Shared.h"