Skip to content

Commit c798ddd

Browse files
XMLHttpRequest headers (#1144)
* XMLHttpRequest headers * typo * unused parameter * PR feedback changes part 1 * var assign * test w/o mocha/chai update * removed unnecessary download
1 parent ad46410 commit c798ddd

File tree

11 files changed

+98
-39
lines changed

11 files changed

+98
-39
lines changed

.github/workflows/nightly.yml

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,12 @@ jobs:
1414
uses: actions/checkout@master
1515
with:
1616
submodules: 'recursive'
17-
- name: Download babylon.max.js
18-
run: curl.exe -o Apps/node_modules/babylonjs/babylon.max.js --create-dirs https://preview.babylonjs.com/babylon.max.js
19-
- name: Download babylon.max.js.map
20-
run: curl.exe -o Apps/node_modules/babylonjs/babylon.max.js.map --create-dirs https://preview.babylonjs.com/babylon.max.js.map
21-
- name: Download babylonjs.materials.js
22-
run: curl.exe -o Apps/node_modules/babylonjs-materials/babylonjs.materials.js --create-dirs https://preview.babylonjs.com/materialsLibrary/babylonjs.materials.js
23-
- name: Download babylonjs.materials.js.map
24-
run: curl.exe -o Apps/node_modules/babylonjs-materials/babylonjs.materials.js.map --create-dirs https://preview.babylonjs.com/materialsLibrary/babylonjs.materials.js.map
25-
- name: Download babylon.loaders.js
26-
run: curl.exe -o Apps/node_modules/babylonjs-loaders/babylonjs.loaders.js --create-dirs https://preview.babylonjs.com/loaders/babylonjs.loaders.js
27-
- name: Download babylon.loaders.js.map
28-
run: curl.exe -o Apps/node_modules/babylonjs-loaders/babylonjs.loaders.js.map --create-dirs https://preview.babylonjs.com/loaders/babylonjs.loaders.js.map
29-
- name: Download babylon.gui.js
30-
run: curl.exe -o Apps/node_modules/babylonjs-gui/babylon.gui.js --create-dirs https://preview.babylonjs.com/gui/babylon.gui.js
31-
- name: Download babylon.gui.js.map
32-
run: curl.exe -o Apps/node_modules/babylonjs-gui/babylon.gui.js.map --create-dirs https://preview.babylonjs.com/gui/babylon.gui.js.map
33-
- name: Download chai.js
34-
run: curl.exe -o Apps/node_modules/chai/chai.js --create-dirs https://unpkg.com/chai/chai.js
35-
- name: Download mocha.js
36-
run: curl.exe -o Apps/node_modules/mocha/mocha.js --create-dirs https://unpkg.com/mocha/mocha.js
17+
- name: NPM Install
18+
run: npm install
19+
working-directory: ./Apps
20+
- name: NPM download nightly
21+
run: npm run getNightly
22+
working-directory: ./Apps
3723
- name: View Apps\node_modules content
3824
run: Get-ChildItem -Path .\Apps\node_modules -Recurse
3925
- name: Make Solution

Apps/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "BabylonNative",
33
"version": "0.0.1",
44
"private": true,
5+
"scripts": {
6+
"getNightly": "node scripts/getNightly.js"
7+
},
58
"dependencies": {
69
"babylonjs": "^5.17.0",
710
"babylonjs-gui": "^5.17.0",

Apps/scripts/getNightly.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var https = require('https');
2+
var fs = require('fs');
3+
4+
function download(filename, url) {
5+
var file = fs.createWriteStream(filename);
6+
var request = https.get(url, function(response) {
7+
response.pipe(file);
8+
});
9+
}
10+
11+
console.log('Downloading babylon.js nightly');
12+
download('node_modules/babylonjs/babylon.max.js', 'https://preview.babylonjs.com/babylon.max.js');
13+
download('node_modules/babylonjs/babylon.max.js.map', 'https://preview.babylonjs.com/babylon.max.js.map');
14+
download('node_modules/babylonjs-materials/babylonjs.materials.js', 'https://preview.babylonjs.com/materialsLibrary/babylonjs.materials.js');
15+
download('node_modules/babylonjs-materials/babylonjs.materials.js.map', 'https://preview.babylonjs.com/materialsLibrary/babylonjs.materials.js.map');
16+
download('node_modules/babylonjs-loaders/babylonjs.loaders.js', 'https://preview.babylonjs.com/loaders/babylonjs.loaders.js');
17+
download('node_modules/babylonjs-loaders/babylonjs.loaders.js.map', 'https://preview.babylonjs.com/loaders/babylonjs.loaders.js.map');
18+
download('node_modules/babylonjs-gui/babylon.gui.js', 'https://preview.babylonjs.com/gui/babylon.gui.js');
19+
download('node_modules/babylonjs-gui/babylon.gui.js.map', 'https://preview.babylonjs.com/gui/babylon.gui.js.map');

Dependencies/UrlLib/Include/UrlLib/UrlLib.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace UrlLib
3939

4040
void Abort();
4141

42-
void Open(UrlMethod method, std::string url);
42+
void Open(UrlMethod method, const std::string& url);
4343

4444
UrlResponseType ResponseType() const;
4545

@@ -53,6 +53,8 @@ namespace UrlLib
5353

5454
std::string_view ResponseString() const;
5555

56+
std::optional<std::string> GetResponseHeader(const std::string& headerName) const;
57+
5658
gsl::span<const std::byte> ResponseBuffer() const;
5759

5860
private:

Dependencies/UrlLib/Source/Android/UrlRequest.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace UrlLib
4242
m_cancellationSource.cancel();
4343
}
4444

45-
void Open(UrlMethod method, std::string url)
45+
void Open(UrlMethod method, const std::string& url)
4646
{
4747
m_method = method;
4848
Uri uri{Uri::Parse(url.data())};
@@ -188,6 +188,12 @@ namespace UrlLib
188188
return m_responseBuffer;
189189
}
190190

191+
std::optional<std::string> GetResponseHeader(const std::string& /*headerName*/) const
192+
{
193+
// todo: implementation
194+
return {};
195+
}
196+
191197
private:
192198
arcana::cancellation_source m_cancellationSource{};
193199
UrlResponseType m_responseType{UrlResponseType::String};

Dependencies/UrlLib/Source/Apple/UrlRequest.mm

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void Abort()
2323
m_cancellationSource.cancel();
2424
}
2525

26-
void Open(UrlMethod method, std::string url)
26+
void Open(UrlMethod method, const std::string& url)
2727
{
2828
m_method = method;
2929
NSString* urlString = [NSString stringWithUTF8String:url.data()];
@@ -142,6 +142,12 @@ UrlStatusCode StatusCode() const
142142
return {};
143143
}
144144

145+
std::optional<std::string> GetResponseHeader(const std::string& /*headerName*/) const
146+
{
147+
// todo: implementation
148+
return {};
149+
}
150+
145151
private:
146152
arcana::cancellation_source m_cancellationSource{};
147153
UrlResponseType m_responseType{UrlResponseType::String};

Dependencies/UrlLib/Source/Shared/UrlRequest.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ namespace UrlLib
2222
m_impl->Abort();
2323
}
2424

25-
void UrlRequest::Open(UrlMethod method, std::string url)
25+
void UrlRequest::Open(UrlMethod method, const std::string& url)
2626
{
27-
m_impl->Open(method, std::move(url));
27+
m_impl->Open(method, url);
2828
}
2929

3030
UrlResponseType UrlRequest::ResponseType() const
@@ -57,6 +57,11 @@ namespace UrlLib
5757
return m_impl->ResponseString();
5858
}
5959

60+
std::optional<std::string> UrlRequest::GetResponseHeader(const std::string& headerName) const
61+
{
62+
return m_impl->GetResponseHeader(headerName);
63+
}
64+
6065
gsl::span<const std::byte> UrlRequest::ResponseBuffer() const
6166
{
6267
return m_impl->ResponseBuffer();

Dependencies/UrlLib/Source/Unix/UrlRequest.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace UrlLib
2121
m_cancellationSource.cancel();
2222
}
2323

24-
void Open(UrlMethod method, std::string url)
24+
void Open(UrlMethod method, const std::string& url)
2525
{
2626
m_method = method;
2727
if (m_curl)
@@ -118,6 +118,12 @@ namespace UrlLib
118118
return m_responseBuffer;
119119
}
120120

121+
std::optional<std::string> GetResponseHeader(const std::string& /*headerName*/) const
122+
{
123+
// todo: implementation
124+
return {};
125+
}
126+
121127
private:
122128
struct CurlMulti
123129
{

Dependencies/UrlLib/Source/Windows/UrlRequest.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <winrt/Windows.Storage.Streams.h>
88
#include <winrt/Windows.Web.Http.h>
99
#include <winrt/Windows.ApplicationModel.h>
10+
#include <winrt/Windows.Foundation.Collections.h>
1011

1112
namespace UrlLib
1213
{
@@ -59,7 +60,7 @@ namespace UrlLib
5960
m_cancellationSource.cancel();
6061
}
6162

62-
void Open(UrlMethod method, std::string url)
63+
void Open(UrlMethod method, const std::string& url)
6364
{
6465
m_method = method;
6566
m_uri = Foundation::Uri{winrt::to_hstring(url)};
@@ -114,6 +115,11 @@ namespace UrlLib
114115

115116
m_responseUrl = winrt::to_string(responseMessage.RequestMessage().RequestUri().RawUri());
116117

118+
for (auto&& iter : responseMessage.Headers())
119+
{
120+
m_headers.insert(std::make_pair(winrt::to_string(iter.Key()), winrt::to_string(iter.Value())));
121+
}
122+
117123
switch (m_responseType)
118124
{
119125
case UrlResponseType::String:
@@ -164,6 +170,16 @@ namespace UrlLib
164170
return m_responseString;
165171
}
166172

173+
std::optional<std::string> GetResponseHeader(const std::string& headerName) const
174+
{
175+
const auto iter = m_headers.find(headerName);
176+
if (iter != m_headers.end())
177+
{
178+
return iter->second;
179+
}
180+
return {};
181+
}
182+
167183
gsl::span<const std::byte> ResponseBuffer() const
168184
{
169185
if (m_responseBuffer)
@@ -213,6 +229,7 @@ namespace UrlLib
213229
std::string m_responseUrl{};
214230
std::string m_responseString{};
215231
Storage::Streams::IBuffer m_responseBuffer{};
232+
std::map<std::string, std::string> m_headers;
216233
};
217234
}
218235

Polyfills/XMLHttpRequest/Source/XMLHttpRequest.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ namespace Babylon::Polyfills::Internal
100100
InstanceAccessor("responseType", &XMLHttpRequest::GetResponseType, &XMLHttpRequest::SetResponseType),
101101
InstanceAccessor("responseURL", &XMLHttpRequest::GetResponseURL, nullptr),
102102
InstanceAccessor("status", &XMLHttpRequest::GetStatus, nullptr),
103+
InstanceMethod("getResponseHeader", &XMLHttpRequest::GetResponseHeader),
103104
InstanceMethod("addEventListener", &XMLHttpRequest::AddEventListener),
104105
InstanceMethod("removeEventListener", &XMLHttpRequest::RemoveEventListener),
105106
InstanceMethod("abort", &XMLHttpRequest::Abort),
@@ -128,8 +129,8 @@ namespace Babylon::Polyfills::Internal
128129

129130
Napi::Value XMLHttpRequest::GetResponse(const Napi::CallbackInfo&)
130131
{
131-
gsl::span<const std::byte> responseBuffer{m_request.ResponseBuffer()};
132-
auto arrayBuffer{Napi::ArrayBuffer::New(Env(), responseBuffer.size())};
132+
const gsl::span<const std::byte> responseBuffer{m_request.ResponseBuffer()};
133+
const auto arrayBuffer{Napi::ArrayBuffer::New(Env(), responseBuffer.size())};
133134
std::memcpy(arrayBuffer.Data(), responseBuffer.data(), arrayBuffer.ByteLength());
134135
return std::move(arrayBuffer);
135136
}
@@ -149,6 +150,13 @@ namespace Babylon::Polyfills::Internal
149150
m_request.ResponseType(ResponseType::StringToEnum(value.As<Napi::String>().Utf8Value()));
150151
}
151152

153+
Napi::Value XMLHttpRequest::GetResponseHeader(const Napi::CallbackInfo& info)
154+
{
155+
const auto headerName = info[0].As<Napi::String>().Utf8Value();
156+
const auto header = m_request.GetResponseHeader(headerName);
157+
return header ? Napi::Value::From(Env(), header.value()) : info.Env().Null();
158+
}
159+
152160
Napi::Value XMLHttpRequest::GetResponseURL(const Napi::CallbackInfo&)
153161
{
154162
return Napi::Value::From(Env(), m_request.ResponseUrl().data());
@@ -161,8 +169,8 @@ namespace Babylon::Polyfills::Internal
161169

162170
void XMLHttpRequest::AddEventListener(const Napi::CallbackInfo& info)
163171
{
164-
std::string eventType = info[0].As<Napi::String>().Utf8Value();
165-
Napi::Function eventHandler = info[1].As<Napi::Function>();
172+
const std::string eventType = info[0].As<Napi::String>().Utf8Value();
173+
const Napi::Function eventHandler = info[1].As<Napi::Function>();
166174

167175
const auto& eventHandlerRefs = m_eventHandlerRefs[eventType];
168176
for (auto it = eventHandlerRefs.begin(); it != eventHandlerRefs.end(); ++it)
@@ -178,9 +186,9 @@ namespace Babylon::Polyfills::Internal
178186

179187
void XMLHttpRequest::RemoveEventListener(const Napi::CallbackInfo& info)
180188
{
181-
std::string eventType = info[0].As<Napi::String>().Utf8Value();
182-
Napi::Function eventHandler = info[1].As<Napi::Function>();
183-
auto itType = m_eventHandlerRefs.find(eventType);
189+
const std::string eventType = info[0].As<Napi::String>().Utf8Value();
190+
const Napi::Function eventHandler = info[1].As<Napi::Function>();
191+
const auto itType = m_eventHandlerRefs.find(eventType);
184192
if (itType != m_eventHandlerRefs.end())
185193
{
186194
auto& eventHandlerRefs = itType->second;
@@ -205,13 +213,13 @@ namespace Babylon::Polyfills::Internal
205213
try
206214
{
207215
// printfs for debugging CI, will be removed
208-
auto inputURL{info[1].As<Napi::String>()};
216+
const auto inputURL{info[1].As<Napi::String>()};
209217
// If the input URL contains any true % characters, encode them as %25
210-
auto encodedPercentURL{Napi::String::New(info.Env(), EncodePercent(inputURL.Utf8Value()))};
218+
const auto encodedPercentURL{Napi::String::New(info.Env(), EncodePercent(inputURL.Utf8Value()))};
211219
// Decode the input URL to get a completely unencoded URL
212-
auto decodedURL{info.Env().Global().Get("decodeURI").As<Napi::Function>().Call({encodedPercentURL})};
220+
const auto decodedURL{info.Env().Global().Get("decodeURI").As<Napi::Function>().Call({encodedPercentURL})};
213221
// Re-encode the URL to make sure that every illegal character is encoded
214-
auto finalURL{info.Env().Global().Get("encodeURI").As<Napi::Function>().Call({decodedURL}).As<Napi::String>()};
222+
const auto finalURL{info.Env().Global().Get("encodeURI").As<Napi::Function>().Call({decodedURL}).As<Napi::String>()};
215223
m_request.Open(MethodType::StringToEnum(info[0].As<Napi::String>().Utf8Value()), finalURL.Utf8Value());
216224
SetReadyState(ReadyState::Opened);
217225
}
@@ -257,7 +265,7 @@ namespace Babylon::Polyfills::Internal
257265

258266
void XMLHttpRequest::RaiseEvent(const char* eventType)
259267
{
260-
auto it = m_eventHandlerRefs.find(eventType);
268+
const auto it = m_eventHandlerRefs.find(eventType);
261269
if (it != m_eventHandlerRefs.end())
262270
{
263271
const auto& eventHandlerRefs = it->second;

0 commit comments

Comments
 (0)