-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathHttpEmbeddedResource.cs
More file actions
164 lines (142 loc) · 5.18 KB
/
HttpEmbeddedResource.cs
File metadata and controls
164 lines (142 loc) · 5.18 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
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
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Waher.Content;
using Waher.Runtime.IO;
namespace Waher.Networking.HTTP
{
/// <summary>
/// Publishes an embedded resource through HTTP GET.
/// </summary>
public class HttpEmbeddedResource : HttpResource, IHttpGetMethod
{
private const int BufferSize = 32768;
private readonly HttpAuthenticationScheme[] authenticationSchemes;
private readonly Assembly assembly;
private readonly string embeddedResourceName;
private readonly string contentType;
private string etag = null;
/// <summary>
/// Publishes an embedded resource through HTTP GET.
/// </summary>
/// <param name="ResourceName">Name of resource.</param>
/// <param name="EmbeddedResourceName">Resource name of embedded resource.</param>
/// <param name="Assembly">Assembly containing the embedded resource.</param>
/// <param name="AuthenticationSchemes">Any authentication schemes used to authenticate users before access is granted.</param>
public HttpEmbeddedResource(string ResourceName, string EmbeddedResourceName, Assembly Assembly,
params HttpAuthenticationScheme[] AuthenticationSchemes)
: this(ResourceName, EmbeddedResourceName, Assembly, InternetContent.GetContentType(Path.GetExtension(ResourceName)), AuthenticationSchemes)
{
}
/// <summary>
/// Publishes an embedded resource through HTTP GET.
/// </summary>
/// <param name="ResourceName">Name of resource.</param>
/// <param name="EmbeddedResourceName">Resource name of embedded resource.</param>
/// <param name="Assembly">Assembly containing the embedded resource.</param>
/// <param name="ContentType">Internet Content Type to use when the embedded resource is requested.</param>
/// <param name="AuthenticationSchemes">Any authentication schemes used to authenticate users before access is granted.</param>
public HttpEmbeddedResource(string ResourceName, string EmbeddedResourceName, Assembly Assembly, string ContentType,
params HttpAuthenticationScheme[] AuthenticationSchemes)
: base(ResourceName)
{
this.embeddedResourceName = EmbeddedResourceName;
this.assembly = Assembly;
this.contentType = ContentType;
this.authenticationSchemes = AuthenticationSchemes;
}
/// <summary>
/// If the resource is synchronous (i.e. returns a response in the method handler), or if it is asynchronous
/// (i.e. sends the response from another thread).
/// </summary>
public override bool Synchronous => true;
/// <summary>
/// If the resource handles sub-paths.
/// </summary>
public override bool HandlesSubPaths => false;
/// <summary>
/// If the resource uses user sessions.
/// </summary>
public override bool UserSessions => false;
/// <summary>
/// If the GET method is allowed.
/// </summary>
public bool AllowsGET => true;
/// <summary>
/// Any authentication schemes used to authenticate users before access is granted to the corresponding resource.
/// </summary>
/// <param name="Request">Current request</param>
public override HttpAuthenticationScheme[] GetAuthenticationSchemes(HttpRequest Request)
{
return this.authenticationSchemes;
}
/// <summary>
/// Executes the GET method on the resource.
/// </summary>
/// <param name="Request">HTTP Request</param>
/// <param name="Response">HTTP Response</param>
/// <exception cref="HttpException">If an error occurred when processing the method.</exception>
public async Task GET(HttpRequest Request, HttpResponse Response)
{
Stream f = this.assembly.GetManifestResourceStream(this.embeddedResourceName)
?? throw new NotFoundException("Resource not found: " + this.embeddedResourceName);
try
{
if (this.etag is null)
this.etag = this.ComputeETag(f);
if (!(Request.Header.IfNoneMatch is null) && Request.Header.IfNoneMatch.Value == this.etag)
{
await Response.SendResponse(new NotModifiedException());
return;
}
Response.SetHeader("ETag", this.etag);
long l = f.Length;
long Pos = 0;
int Size = (int)Math.Min(BufferSize, l);
byte[] Buffer = new byte[Size];
int i;
Response.ContentType = this.contentType;
Response.ContentLength = l;
if (!Response.OnlyHeader)
{
while (Pos < l)
{
i = await f.TryReadAllAsync(Buffer, 0, Size);
if (i <= 0)
throw new Exception("Unexpected end of stream.");
await Response.Write(true, Buffer, 0, i);
Pos += i;
}
}
}
finally
{
f.Dispose();
}
}
/// <summary>
/// Method called when a resource has been registered on a server.
/// </summary>
/// <param name="Server">Server</param>
public override void AddReference(HttpServer Server)
{
base.AddReference(Server);
Server.ETagSaltChanged += this.Server_ETagSaltChanged;
}
/// <summary>
/// Method called when a resource has been unregistered from a server.
/// </summary>
/// <param name="Server">Server</param>
public override bool RemoveReference(HttpServer Server)
{
Server.ETagSaltChanged -= this.Server_ETagSaltChanged;
return base.RemoveReference(Server);
}
private Task Server_ETagSaltChanged(object Sender, EventArgs e)
{
this.etag = null;
return Task.CompletedTask;
}
}
}