This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
/
BrowserHttpMessageHandler.cs
180 lines (159 loc) · 6.72 KB
/
BrowserHttpMessageHandler.cs
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.JSInterop;
using Mono.WebAssembly.Interop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Blazor.Browser.Http
{
/// <summary>
/// A browser-compatible implementation of <see cref="HttpMessageHandler"/>
/// </summary>
public class BrowserHttpMessageHandler : HttpMessageHandler
{
/// <summary>
/// Gets or sets the default value of the 'credentials' option on outbound HTTP requests.
/// Defaults to <see cref="FetchCredentialsOption.SameOrigin"/>.
/// </summary>
public static FetchCredentialsOption DefaultCredentials { get; set; }
= FetchCredentialsOption.SameOrigin;
static object _idLock = new object();
static int _nextRequestId = 0;
static IDictionary<int, TaskCompletionSource<HttpResponseMessage>> _pendingRequests
= new Dictionary<int, TaskCompletionSource<HttpResponseMessage>>();
/// <summary>
/// The name of a well-known property that can be added to <see cref="HttpRequestMessage.Properties"/>
/// to control the arguments passed to the underlying JavaScript <code>fetch</code> API.
/// </summary>
public const string FetchArgs = "BrowserHttpMessageHandler.FetchArgs";
/// <inheritdoc />
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<HttpResponseMessage>();
cancellationToken.Register(() => tcs.TrySetCanceled());
int id;
lock (_idLock)
{
id = _nextRequestId++;
_pendingRequests.Add(id, tcs);
}
var options = new FetchOptions();
if (request.Properties.TryGetValue(FetchArgs, out var fetchArgs))
{
options.RequestInitOverrides = fetchArgs;
}
options.RequestInit = new RequestInit
{
Credentials = GetDefaultCredentialsString(),
Headers = GetHeadersAsStringArray(request),
Method = request.Method.Method
};
options.RequestUri = request.RequestUri.ToString();
if (JSRuntime.Current is MonoWebAssemblyJSRuntime mono)
{
mono.InvokeUnmarshalled<int, byte[], string, object>(
"Blazor._internal.http.sendAsync",
id,
request.Content == null ? null : await request.Content.ReadAsByteArrayAsync(),
Json.Serialize(options));
}
else
{
throw new NotImplementedException("BrowserHttpMessageHandler only supports running under Mono WebAssembly.");
}
return await tcs.Task;
}
private string[][] GetHeadersAsStringArray(HttpRequestMessage request)
=> (from header in request.Headers.Concat(request.Content?.Headers ?? Enumerable.Empty<KeyValuePair<string, IEnumerable<string>>>())
from headerValue in header.Value // There can be more than one value for each name
select new[] { header.Key, headerValue }).ToArray();
private static void ReceiveResponse(
string id,
string responseDescriptorJson,
byte[] responseBodyData,
string errorText)
{
TaskCompletionSource<HttpResponseMessage> tcs;
var idVal = int.Parse(id);
lock (_idLock)
{
tcs = _pendingRequests[idVal];
_pendingRequests.Remove(idVal);
}
if (errorText != null)
{
tcs.SetException(new HttpRequestException(errorText));
}
else
{
var responseDescriptor = Json.Deserialize<ResponseDescriptor>(responseDescriptorJson);
var responseContent = responseBodyData == null ? null : new ByteArrayContent(responseBodyData);
var responseMessage = responseDescriptor.ToResponseMessage(responseContent);
tcs.SetResult(responseMessage);
}
}
private static byte[] AllocateArray(string length)
{
return new byte[int.Parse(length)];
}
private static string GetDefaultCredentialsString()
{
// See https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials for
// standard values and meanings
switch (DefaultCredentials)
{
case FetchCredentialsOption.Omit:
return "omit";
case FetchCredentialsOption.SameOrigin:
return "same-origin";
case FetchCredentialsOption.Include:
return "include";
default:
throw new ArgumentException($"Unknown credentials option '{DefaultCredentials}'.");
}
}
// Keep these in sync with TypeScript class in Http.ts
private class FetchOptions
{
public string RequestUri { get; set; }
public RequestInit RequestInit { get; set; }
public object RequestInitOverrides { get; set; }
}
private class RequestInit
{
public string Credentials { get; set; }
public string[][] Headers { get; set; }
public string Method { get; set; }
}
private class ResponseDescriptor
{
#pragma warning disable 0649
public int StatusCode { get; set; }
public string StatusText { get; set; }
public string[][] Headers { get; set; }
#pragma warning restore 0649
public HttpResponseMessage ToResponseMessage(HttpContent content)
{
var result = new HttpResponseMessage((HttpStatusCode)StatusCode);
result.ReasonPhrase = StatusText;
result.Content = content;
var headers = result.Headers;
var contentHeaders = result.Content?.Headers;
foreach (var pair in Headers)
{
if (!headers.TryAddWithoutValidation(pair[0], pair[1]))
{
contentHeaders?.TryAddWithoutValidation(pair[0], pair[1]);
}
}
return result;
}
}
}
}