private async Task <byte []> GetResponseData() { if (_data != null) { return(_data); } _data = (byte [])await _status.ArrayBuffer(); _status.Dispose(); _status = null; return(_data); }
private async Task <byte []> GetResponseData() { if (_data != null) { return(_data); } using (ArrayBuffer dataBuffer = (ArrayBuffer)await _status.ArrayBuffer()) { using (Uint8Array dataBinView = new Uint8Array(dataBuffer)) { _data = dataBinView.ToArray(); _status.Dispose(); _status = null; } } return(_data); }
protected override void Dispose(bool disposing) { _status?.Dispose(); base.Dispose(disposing); }
private async Task doFetch(TaskCompletionSource <HttpResponseMessage> tcs, HttpRequestMessage request) { try { var requestObject = (JSObject)json.Invoke("parse", "{}"); requestObject.SetObjectProperty("method", request.Method.Method); requestObject.SetObjectProperty("credentials", GetDefaultCredentialsString()); requestObject.SetObjectProperty("cache", GetCacheModeString()); requestObject.SetObjectProperty("mode", GetRequestModeString()); // We need to check for body content if (request.Content != null) { if (request.Content is StringContent) { requestObject.SetObjectProperty("body", await request.Content.ReadAsStringAsync()); } else { requestObject.SetObjectProperty("body", await request.Content.ReadAsByteArrayAsync()); } } // Process headers // Cors has it's own restrictions on headers. // https://developer.mozilla.org/en-US/docs/Web/API/Headers var requestHeaders = GetHeadersAsStringArray(request); if (requestHeaders != null && requestHeaders.Length > 0) { using (var jsHeaders = (JSObject)global.Invoke("__mono_wasm_headers_hook__")) { for (int i = 0; i < requestHeaders.Length; i++) { //Console.WriteLine($"append: {requestHeaders[i][0]} / {requestHeaders[i][1]}"); jsHeaders.Invoke("append", requestHeaders[i][0], requestHeaders[i][1]); } requestObject.SetObjectProperty("headers", jsHeaders); } } var args = (JSObject)json.Invoke("parse", "[]"); args.Invoke("push", request.RequestUri.ToString()); args.Invoke("push", requestObject); requestObject.Dispose(); var response = (Task <object>)fetch.Invoke("apply", window, args); args.Dispose(); var t = await response; var status = new WasmFetchResponse((JSObject)t); //Console.WriteLine($"bodyUsed: {status.IsBodyUsed}"); //Console.WriteLine($"ok: {status.IsOK}"); //Console.WriteLine($"redirected: {status.IsRedirected}"); //Console.WriteLine($"status: {status.Status}"); //Console.WriteLine($"statusText: {status.StatusText}"); //Console.WriteLine($"type: {status.ResponseType}"); //Console.WriteLine($"url: {status.Url}"); byte[] buffer = null; if (status.IsOK) { buffer = (byte[])await status.ArrayBuffer(); } HttpResponseMessage httpresponse = new HttpResponseMessage((HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), status.Status.ToString())); if (buffer != null) { httpresponse.Content = new ByteArrayContent(buffer); } buffer = null; // Fill the response headers // CORS will only allow access to certain headers. // If a request is made for a resource on another origin which returns the CORs headers, then the type is cors. // cors and basic responses are almost identical except that a cors response restricts the headers you can view to // `Cache-Control`, `Content-Language`, `Content-Type`, `Expires`, `Last-Modified`, and `Pragma`. // 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. respHeaders.Invoke("forEach", 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(); } )); } tcs.SetResult(httpresponse); httpresponse = null; status.Dispose(); } catch (Exception exception) { tcs.SetException(exception); } }