-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathHttpRedirectionResource.cs
More file actions
223 lines (201 loc) · 8.47 KB
/
HttpRedirectionResource.cs
File metadata and controls
223 lines (201 loc) · 8.47 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System.Threading.Tasks;
namespace Waher.Networking.HTTP
{
/// <summary>
/// An HTTP redirection resource. Incoming requests are redirected to another location.
/// </summary>
public class HttpRedirectionResource : HttpSynchronousResource, IHttpGetMethod, IHttpGetRangesMethod,
IHttpPostMethod, IHttpPostRangesMethod, IHttpPutMethod, IHttpPutRangesMethod, IHttpOptionsMethod,
IHttpDeleteMethod, IHttpPatchMethod, IHttpPatchRangesMethod, IHttpTraceMethod
{
private readonly string location;
private readonly bool includeSubPaths;
private readonly bool permanent;
/// <summary>
/// An HTTP Reverse proxy resource. Incoming requests are reverted to a another web server for processing. Responses
/// are returned asynchronously as they are received.
/// </summary>
/// <param name="ResourceName">Name of resource.</param>
/// <param name="Location">New location</param>
/// <param name="IncludeSubPaths">If subpaths are to be redirected as well.</param>
/// <param name="Permanent">If redirection is permanent.</param>
public HttpRedirectionResource(string ResourceName, string Location, bool IncludeSubPaths, bool Permanent)
: base(ResourceName)
{
this.location = Location;
this.includeSubPaths = IncludeSubPaths;
this.permanent = Permanent;
}
/// <summary>
/// If the resource uses user sessions.
/// </summary>
public override bool UserSessions => false;
/// <summary>
/// If the resource handles sub-paths.
/// </summary>
public override bool HandlesSubPaths => this.includeSubPaths;
/// <summary>
/// If the GET method is allowed.
/// </summary>
public bool AllowsGET => true;
/// <summary>
/// If the POST method is allowed.
/// </summary>
public bool AllowsPOST => true;
/// <summary>
/// If the PUT method is allowed.
/// </summary>
public bool AllowsPUT => true;
/// <summary>
/// If the DELETE method is allowed.
/// </summary>
public bool AllowsDELETE => true;
/// <summary>
/// If the PATCH method is allowed.
/// </summary>
public bool AllowsPATCH => true;
/// <summary>
/// If the TRACE method is allowed.
/// </summary>
public bool AllowsTRACE => true;
/// <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 Task GET(HttpRequest Request, HttpResponse Response)
{
return Task.CompletedTask; // Never called. Request processed by custom Execute method below.
}
/// <summary>
/// Executes the ranged GET method on the resource.
/// </summary>
/// <param name="Request">HTTP Request</param>
/// <param name="Response">HTTP Response</param>
/// <param name="FirstInterval">First byte range interval.</param>
/// <exception cref="HttpException">If an error occurred when processing the method.</exception>
public Task GET(HttpRequest Request, HttpResponse Response, ByteRangeInterval FirstInterval)
{
return Task.CompletedTask; // Never called. Request processed by custom Execute method below.
}
/// <summary>
/// Executes the POST 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 Task POST(HttpRequest Request, HttpResponse Response)
{
return Task.CompletedTask; // Never called. Request processed by custom Execute method below.
}
/// <summary>
/// Executes the ranged POST method on the resource.
/// </summary>
/// <param name="Request">HTTP Request</param>
/// <param name="Response">HTTP Response</param>
/// <param name="Interval">Content byte range.</param>
/// <exception cref="HttpException">If an error occurred when processing the method.</exception>
public Task POST(HttpRequest Request, HttpResponse Response, ContentByteRangeInterval Interval)
{
return Task.CompletedTask; // Never called. Request processed by custom Execute method below.
}
/// <summary>
/// Executes the PUT 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 Task PUT(HttpRequest Request, HttpResponse Response)
{
return Task.CompletedTask; // Never called. Request processed by custom Execute method below.
}
/// <summary>
/// Executes the ranged PUT method on the resource.
/// </summary>
/// <param name="Request">HTTP Request</param>
/// <param name="Response">HTTP Response</param>
/// <param name="Interval">Content byte range.</param>
/// <exception cref="HttpException">If an error occurred when processing the method.</exception>
public Task PUT(HttpRequest Request, HttpResponse Response, ContentByteRangeInterval Interval)
{
return Task.CompletedTask; // Never called. Request processed by custom Execute method below.
}
/// <summary>
/// Executes the OPTIONS 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 override Task OPTIONS(HttpRequest Request, HttpResponse Response)
{
return Task.CompletedTask; // Never called. Request processed by custom Execute method below.
}
/// <summary>
/// Executes the DELETE 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 Task DELETE(HttpRequest Request, HttpResponse Response)
{
return Task.CompletedTask; // Never called. Request processed by custom Execute method below.
}
/// <summary>
/// Executes the PATCH 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 Task PATCH(HttpRequest Request, HttpResponse Response)
{
return Task.CompletedTask; // Never called. Request processed by custom Execute method below.
}
/// <summary>
/// Executes the ranged PATCH method on the resource.
/// </summary>
/// <param name="Request">HTTP Request</param>
/// <param name="Response">HTTP Response</param>
/// <param name="Interval">Content byte range.</param>
/// <exception cref="HttpException">If an error occurred when processing the method.</exception>
public Task PATCH(HttpRequest Request, HttpResponse Response, ContentByteRangeInterval Interval)
{
return Task.CompletedTask; // Never called. Request processed by custom Execute method below.
}
/// <summary>
/// Executes the TRACE 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 Task TRACE(HttpRequest Request, HttpResponse Response)
{
return Task.CompletedTask; // Never called. Request processed by custom Execute method below.
}
/// <summary>
/// Executes a method on the resource. The default behaviour is to call the corresponding execution methods defined in the specialized
/// interfaces <see cref="IHttpGetMethod"/>, <see cref="IHttpPostMethod"/>, <see cref="IHttpPutMethod"/> and <see cref="IHttpDeleteMethod"/>
/// if they are defined for the resource.
/// </summary>
/// <param name="Server">HTTP Server</param>
/// <param name="Request">HTTP Request</param>
/// <param name="Response">HTTP Response</param>
public override async Task Execute(HttpServer Server, HttpRequest Request, HttpResponse Response)
{
if (this.includeSubPaths)
{
if (this.permanent)
await Response.SendResponse(new PermanentRedirectException(this.location + Request.SubPath));
else
await Response.SendResponse(new TemporaryRedirectException(this.location + Request.SubPath));
}
else
{
if (this.permanent)
await Response.SendResponse(new PermanentRedirectException(this.location));
else
await Response.SendResponse(new TemporaryRedirectException(this.location));
}
}
}
}