-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathHttpServerUtility.cs
More file actions
139 lines (111 loc) · 5.97 KB
/
Copy pathHttpServerUtility.cs
File metadata and controls
139 lines (111 loc) · 5.97 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Web.Hosting;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.SystemWebAdapters;
using Microsoft.AspNetCore.SystemWebAdapters.Features;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.DependencyInjection;
namespace System.Web;
[Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = Constants.ApiFromAspNet)]
public class HttpServerUtility
{
private readonly HttpContextCore _context;
internal HttpServerUtility(HttpContextCore context)
{
_context = context;
}
public string MachineName => Environment.MachineName;
public string MapPath(string? path)
=> _context.RequestServices.GetRequiredService<IMapPathUtility>().MapPath(_context.Request.Path, path);
public Exception? GetLastError() => _context.AsSystemWeb().Error;
public void ClearError() => _context.AsSystemWeb().ClearError();
/// <summary>
/// This method is similar to <see cref="WebEncoders.Base64UrlDecode(string)"/> but handles the trailing character that <see cref="UrlTokenEncode(byte[])"/>
/// appends to the string.
/// </summary>
/// <param name="input">Value to decode</param>
/// <returns>Decoded value.</returns>
/// <exception cref="ArgumentNullException"></exception>
public static byte[]? UrlTokenDecode(string input)
{
ArgumentNullException.ThrowIfNull(input);
if (input.Length == 0)
{
return Array.Empty<byte>();
}
// The number of padding chars is expected to be the final character of the string
if (!IsDigit(input[^1]))
{
return null;
}
return WebEncoders.Base64UrlDecode(input, 0, input.Length - 1);
static bool IsDigit(char c) => (uint)(c - '0') <= (uint)('9' - '0');
}
/// <summary>
/// This method is similar to <see cref="WebEncoders.Base64UrlEncode(byte[])"/> but the resulting string includes an extra character
/// that is the count of how many padding characters where removed from the Base64 encoded value.
/// </summary>
/// <param name="input">Value to encode</param>
/// <returns>URL encoded value.</returns>
/// <exception cref="ArgumentNullException"></exception>
[Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1055:URI-like return values should not be strings", Justification = Constants.ApiFromAspNet)]
public static string UrlTokenEncode(byte[] input)
{
ArgumentNullException.ThrowIfNull(input);
if (input.Length == 0)
{
return string.Empty;
}
var encoded = WebEncoders.Base64UrlEncode(input);
var padding = (encoded.Length % 4) switch
{
0 => 0,
2 => 2,
3 => 1,
_ => throw new FormatException("Invalid input"),
};
return $"{encoded}{padding}";
}
[Obsolete(Constants.Execute.Message, DiagnosticId = Constants.Execute.DiagnosticId)]
[EditorBrowsable(EditorBrowsableState.Never)]
public void Execute(string path) => Execute(path, null, preserveForm: true);
[Obsolete(Constants.Execute.Message, DiagnosticId = Constants.Execute.DiagnosticId)]
[EditorBrowsable(EditorBrowsableState.Never)]
public void Execute(string path, TextWriter writer) => Execute(path, writer, preserveForm: true);
[Obsolete(Constants.Execute.Message, DiagnosticId = Constants.Execute.DiagnosticId)]
[EditorBrowsable(EditorBrowsableState.Never)]
public void Execute(string path, bool preserveForm) => Execute(path, null, preserveForm);
[Obsolete(Constants.Execute.Message, DiagnosticId = Constants.Execute.DiagnosticId)]
[EditorBrowsable(EditorBrowsableState.Never)]
public void Execute(string path, TextWriter? writer, bool preserveForm) => _context.Features.GetRequiredFeature<ITransferRequestFeature>().Execute(path, writer, preserveForm);
[Obsolete(Constants.Transfer.Message, DiagnosticId = Constants.Transfer.DiagnosticId)]
[EditorBrowsable(EditorBrowsableState.Never)]
public void Transfer(string path) => Transfer(path, preserveForm: true);
[Obsolete(Constants.Transfer.Message, DiagnosticId = Constants.Transfer.DiagnosticId)]
[EditorBrowsable(EditorBrowsableState.Never)]
public void Transfer(string path, bool preserveForm)
{
_context.Features.GetRequiredFeature<ITransferRequestFeature>().Transfer(path, preserveForm);
_context.Response.AsSystemWeb().End();
}
[Obsolete(Constants.TransferRequest.Message, DiagnosticId = Constants.TransferRequest.DiagnosticId)]
[EditorBrowsable(EditorBrowsableState.Never)]
public void TransferRequest(string path)
=> TransferRequest(path, false, null, null, preserveUser: true);
[Obsolete(Constants.TransferRequest.Message, DiagnosticId = Constants.TransferRequest.DiagnosticId)]
[EditorBrowsable(EditorBrowsableState.Never)]
public void TransferRequest(string path, bool preserveForm)
=> TransferRequest(path, preserveForm, null, null, preserveUser: true);
[Obsolete(Constants.TransferRequest.Message, DiagnosticId = Constants.TransferRequest.DiagnosticId)]
[EditorBrowsable(EditorBrowsableState.Never)]
public void TransferRequest(string path, bool preserveForm, string? method, NameValueCollection? headers)
=> TransferRequest(path, preserveForm, method, headers, preserveUser: true);
[Obsolete(Constants.TransferRequest.Message, DiagnosticId = Constants.TransferRequest.DiagnosticId)]
[EditorBrowsable(EditorBrowsableState.Never)]
public void TransferRequest(string path, bool preserveForm, string? method, NameValueCollection? headers, bool preserveUser)
=> _context.Features.GetRequiredFeature<ITransferRequestFeature>().TransferRequest(path, preserveForm, method, headers, preserveUser);
}