This repository has been archived by the owner on Nov 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
BundleResponse.cs
127 lines (114 loc) · 4.55 KB
/
BundleResponse.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
// Copyright (c) Microsoft Corporation, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace System.Web.Optimization {
/// <summary>
/// The response data that will be sent in reply to a bundle request.
/// </summary>
public class BundleResponse {
/// <summary>
/// Initializes a new instance of the <see cref="Bundle"/> class.
/// </summary>
public BundleResponse() {
CreationDate = DateTimeOffset.UtcNow;
}
/// <summary>
/// Initializes a new instance of the <see cref="Bundle"/> class.
/// </summary>
/// <param name="content">The content of the bundle which is sent as the response body.</param>
/// <param name="files">The list of files that were used to generate the bundle.</param>
public BundleResponse(string content, IEnumerable<BundleFile> files) : this() {
Content = content;
Files = files;
Cacheability = HttpCacheability.Public; // REVIEW: What should the default be, public vs No cache?
}
private string _content;
private string _contentHash;
/// <summary>
/// The content of the bundle which is sent as the response body.
/// </summary>
public string Content {
get {
return _content;
}
set {
_content = value;
_contentHash = null;
}
}
/// <summary>
/// The response content-type header.
/// </summary>
public string ContentType { get; set; }
/// <summary>
/// The time when this response was created
/// </summary>
public DateTimeOffset CreationDate { get; private set; }
/// <summary>
/// Enables control over the cache headers that are spent in the bundle response.
/// </summary>
public HttpCacheability Cacheability { get; set; }
/// <summary>
/// The list of files that were used to generate the bundle.
/// </summary>
/// <remarks>
/// The list of files is preserved in <see cref="BundleResponse"/> so that <see cref="System.Web.Caching.CacheDependency"/> objects can be
/// setup to monitor changes to the underlying files and rebuild the bundle when any of those contents change.
/// </remarks>
public IEnumerable<BundleFile> Files {
get {
return _files;
}
set {
_files = value;
}
}
IEnumerable<BundleFile> _files;
internal static string ComputeHash(string input) {
using (SHA256 sha256 = CreateHashAlgorithm()) {
byte[] hash = sha256.ComputeHash(Encoding.Unicode.GetBytes(input));
return HttpServerUtility.UrlTokenEncode(hash);
}
}
/// <summary>
/// Returns a hashcode of the bundle contents, for purposes of generating a 'versioned' url for cache busting purposes.
/// This is not used for cryptographic purposes, just as a quick and dirty way to give browsers a different url when the bundle
/// changes
/// </summary>
/// <returns></returns>
internal string GetContentHashCode() {
if (_contentHash == null) {
if (String.IsNullOrEmpty(Content)) {
_contentHash = String.Empty;
}
else {
_contentHash = ComputeHash(Content);
}
}
return _contentHash;
}
private static readonly bool _isMonoRuntime = Type.GetType("Mono.Runtime") != null;
/// <summary>
/// Determines if we are to only allow Fips compliant algorithms.
/// </summary>
/// <remarks>
/// CryptoConfig.AllowOnlyFipsAlgorithms does not exist in Mono.
/// </remarks>
private static bool AllowOnlyFipsAlgorithms {
get {
return !_isMonoRuntime && CryptoConfig.AllowOnlyFipsAlgorithms;
}
}
// Needed to support XP3 which doesn't support SHA256Cng
private static SHA256 CreateHashAlgorithm() {
if (AllowOnlyFipsAlgorithms) {
return new SHA256CryptoServiceProvider();
}
else {
return new SHA256Managed();
}
}
}
}