Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,29 +176,29 @@ private async Task doFetch (TaskCompletionSource<HttpResponseMessage> tcs, HttpR
// View more information https://developers.google.com/web/updates/2015/03/introduction-to-fetch#response_types
//
// Note: Some of the headers may not even be valid header types in .NET thus we use TryAddWithoutValidation
using (var respHeaders = status.Headers) {
// Here we invoke the forEach on the headers object
// Note: the Action takes 3 objects and not two. The other seems to be the Header object.
var foreachAction = new Action<object, object, object> ((value, name, other) => {

if (!httpresponse.Headers.TryAddWithoutValidation ((string)name, (string)value))
if (httpresponse.Content != null)
if (!httpresponse.Content.Headers.TryAddWithoutValidation ((string)name, (string)value))
Console.WriteLine ($"Warning: Can not add response header for name: {name} value: {value}");
((JSObject)other).Dispose ();
});

try {

respHeaders.Invoke ("forEach", foreachAction);
} finally {
// Do not remove the following line of code. The httpresponse is used in the lambda above when parsing the Headers.
// if a local is captured (used) by a lambda it becomes heap memory as we translate them into fields on an object.
// The foreachAction is allocated when marshalled to JavaScript. Since we do not know when JS is finished with the
// Action we need to tell the Runtime to de-allocate the object and remove the instance from JS as well.
WebAssembly.Runtime.FreeObject (foreachAction);
using (var respHeaders = (JSObject)status.Headers) {
if (respHeaders != null) {
using (var entriesIterator = (JSObject)respHeaders.Invoke ("entries")) {
JSObject nextResult = null;
try {
nextResult = (JSObject)entriesIterator.Invoke ("next");
while (!(bool)nextResult.GetObjectProperty ("done")) {
using (var resultValue = (WebAssembly.Core.Array)nextResult.GetObjectProperty ("value")) {
var name = (string)resultValue [0];
var value = (string)resultValue [1];
if (!httpresponse.Headers.TryAddWithoutValidation (name, value))
if (httpresponse.Content != null)
if (!httpresponse.Content.Headers.TryAddWithoutValidation (name, value))
Console.WriteLine ($"Warning: Can not add response header for name: {name} value: {value}");
}
nextResult?.Dispose ();
nextResult = (JSObject)entriesIterator.Invoke ("next");
}
} finally {
nextResult?.Dispose ();
}
}
}

}

tcs.SetResult (httpresponse);
Expand Down Expand Up @@ -433,7 +433,6 @@ public override void Write (byte [] buffer, int offset, int count)
throw new NotSupportedException ();
}
}

}

/// <summary>
Expand Down Expand Up @@ -535,5 +534,5 @@ public enum RequestMode {
[Export (EnumValue = ConvertEnum.ToLower)]
Navigate,
}

}
57 changes: 57 additions & 0 deletions sdks/wasm/tests/browser/IssuesTestSuite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.ComponentModel;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Net.Http;
using WebAssembly;
using WebAssembly.Core;

Expand Down Expand Up @@ -43,6 +44,61 @@ public static double Issue13428()
var k = Math.Truncate(20d); // a print window pops up
return k;
}

static WebAssembly.Core.Array fetchResponse = new WebAssembly.Core.Array();
public static async Task<object> IssueDoubleFetch ()
{
await Fetch();
await Fetch();
return fetchResponse;
}

private static async Task Fetch()
{
var client = CreateHttpClient();
var response = await client.GetStringAsync("base/publish/NowIsTheTime.txt");
fetchResponse.Push(response);
}

static WebAssembly.Core.Array fetchHeadersResponse = new WebAssembly.Core.Array();
public static async Task<object> IssueDoubleFetchHeaders ()
{

await FetchHeaders();
await FetchHeaders();
return fetchHeadersResponse;
}

private static async Task FetchHeaders()
{
var client = CreateHttpClient();
var response = await client.GetAsync("base/publish/NowIsTheTime.txt");
// Raise exception if fails
response.EnsureSuccessStatusCode();
// On success, return sign in results from the server response packet
var responseContent = await response.Content.ReadAsStringAsync();
fetchHeadersResponse.Push(response.Headers.ToString());
}

static HttpClient CreateHttpClient()
{
//Console.WriteLine("Create HttpClient");
string BaseApiUrl = string.Empty;
var window = (JSObject)WebAssembly.Runtime.GetGlobalObject("window");
using (var location = (JSObject)window.GetObjectProperty("location"))
{
BaseApiUrl = (string)location.GetObjectProperty("origin");
}
// WasmHttpMessageHandler.StreamingEnabled = true;
// var client = new System.Net.Http.HttpClient()
// {
// DefaultRequestHeaders = { { "origin", "WindowsCalculator" } }
// };

return new HttpClient() { BaseAddress = new Uri(BaseApiUrl), DefaultRequestHeaders = { { "origin", "WindowsCalculator" } } };
}


}

[Flags]
Expand Down Expand Up @@ -177,4 +233,5 @@ public enum Longs : long
[Description("64 bits")]
enum64 = unchecked((long)0x8000000000000000L)
}

}
37 changes: 37 additions & 0 deletions sdks/wasm/tests/browser/issues-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,41 @@ describe("The WebAssembly Issues Test Suite",function(){
assert.equal(doublevalue, 20, "result doesn't match 20");

}, DEFAULT_TIMEOUT);

it('IssuesTestSuite: https://github.com/mono/mono/issues/14940 should not crash when issuing two fetchs back to back.', (done) => {
//karmaHTML.issuesspec.document gives the access to the Document object of 'http-spec.html' file
var _document = karmaHTML.issuesspec.document;
_document.Module.BINDING.call_static_method("[IssuesTestSuite]TestSuite.Program:IssueDoubleFetch", []).then(
(result) =>
{
try {
assert.equal(result.length, 2, "result does not match Fetch Issue of 2.");
done()
} catch (e) {
done.fail(e);
}
},
(error) => done.fail(error)

);
}, DEFAULT_TIMEOUT);

it('IssuesTestSuite: https://github.com/mono/mono/issues/14940 should not crash when retrieving headers issuing two fetchs back to back.', (done) => {
//karmaHTML.issuesspec.document gives the access to the Document object of 'http-spec.html' file
var _document = karmaHTML.issuesspec.document;
_document.Module.BINDING.call_static_method("[IssuesTestSuite]TestSuite.Program:IssueDoubleFetchHeaders", []).then(
(result) =>
{
try {
assert.equal(result.length, 2, "result does not match number of headers 2.");
done()
} catch (e) {
done.fail(e);
}
},
(error) => done.fail(error)

);
}, DEFAULT_TIMEOUT);

});