-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.cs
executable file
·236 lines (214 loc) · 8.2 KB
/
Utils.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
// This software code is made available "AS IS" without warranties of any
// kind. You may copy, display, modify and redistribute the software
// code either by itself or as incorporated into your code; provided that
// you do not remove any proprietary notices. Your use of this software
// code is at your own risk and you waive any claim against Amazon
// Digital Services, Inc. or its affiliates with respect to your use of
// this software code. (c) 2006 Amazon Digital Services, Inc. or its
// affiliates.
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;
namespace com.CastRoller.AmazonS3DA
{
class Utils
{
public static readonly string METADATA_PREFIX = "x-amz-meta-";
public static readonly string AMAZON_HEADER_PREFIX = "x-amz-";
public static readonly string ALTERNATIVE_DATE_HEADER = "x-amz-date";
private static string host = "s3.amazonaws.com";
public static string Host {
get {
return host;
}
set {
host = value;
}
}
private static int securePort = 443;
public static int SecurePort {
get {
return securePort;
}
set {
securePort = value;
}
}
private static int insecurePort = 80;
public static int InsecurePort {
get {
return insecurePort;
}
set {
insecurePort = value;
}
}
public static string makeCanonicalString( string resource, WebRequest request )
{
SortedList headers = new SortedList();
foreach ( string key in request.Headers )
{
headers.Add( key, request.Headers[ key ] );
}
if (headers["Content-Type"] == null)
{
headers.Add("Content-Type", request.ContentType);
}
return makeCanonicalString(request.Method, resource, headers, null);
}
public static string makeCanonicalString( string verb, string resource,
SortedList headers, string expires )
{
StringBuilder buf = new StringBuilder();
buf.Append( verb );
buf.Append( "\n" );
SortedList interestingHeaders = new SortedList();
if (headers != null)
{
foreach (string key in headers.Keys)
{
string lk = key.ToLower();
if (lk.Equals("content-type") ||
lk.Equals("content-md5") ||
lk.Equals("date") ||
lk.StartsWith(AMAZON_HEADER_PREFIX))
{
interestingHeaders.Add(lk, headers[key]);
}
}
}
if ( interestingHeaders[ ALTERNATIVE_DATE_HEADER ] != null )
{
interestingHeaders.Add( "date", "" );
}
// if the expires is non-null, use that for the date field. this
// trumps the x-amz-date behavior.
if ( expires != null )
{
interestingHeaders.Add( "date", expires );
}
// these headers require that we still put a new line after them,
// even if they don't exist.
{
string [] newlineHeaders = { "content-type", "content-md5" };
foreach ( string header in newlineHeaders )
{
if ( interestingHeaders.IndexOfKey( header ) == -1 )
{
interestingHeaders.Add( header, "" );
}
}
}
// Finally, add all the interesting headers (i.e.: all that startwith x-amz- ;-))
foreach ( string key in interestingHeaders.Keys )
{
if ( key.StartsWith( AMAZON_HEADER_PREFIX ) )
{
buf.Append( key ).Append( ":" ).Append( ( interestingHeaders[ key ] as string ).Trim() );
}
else
{
buf.Append( interestingHeaders[ key ] );
}
buf.Append( "\n" );
}
// Do not include the query string parameters
int queryIndex = resource.IndexOf( '?' );
if ( queryIndex == -1 )
{
buf.Append( "/" + resource );
}
else
{
buf.Append( "/" + resource.Substring( 0, queryIndex ) );
}
Regex aclQueryStringRegEx = new Regex( ".*[&?]acl($|=|&).*" );
Regex torrentQueryStringRegEx = new Regex( ".*[&?]torrent($|=|&).*" );
Regex loggingQueryStringRegEx = new Regex(".*[&?]logging($|=|&).*");
if (aclQueryStringRegEx.IsMatch(resource))
{
buf.Append( "?acl" );
}
else if ( torrentQueryStringRegEx.IsMatch( resource ) )
{
buf.Append( "?torrent" );
}
else if (loggingQueryStringRegEx.IsMatch(resource))
{
buf.Append("?logging");
}
return buf.ToString();
}
public static string encode( string awsSecretAccessKey, string canonicalString, bool urlEncode )
{
Encoding ae = new UTF8Encoding();
HMACSHA1 signature = new HMACSHA1( ae.GetBytes( awsSecretAccessKey ) );
string b64 = Convert.ToBase64String(
signature.ComputeHash( ae.GetBytes(
canonicalString.ToCharArray() ) )
);
if ( urlEncode )
{
return HttpUtility.UrlEncode(b64);
}
else
{
return b64;
}
}
public static byte[] slurpInputStream(Stream stream)
{
using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = new byte[32 * 1024];
while (true)
{
int nRead = stream.Read(buffer, 0, buffer.Length);
if (nRead <= 0)
{
return ms.ToArray();
}
ms.Write(buffer, 0, nRead);
}
}
}
public static string slurpInputStreamAsString(Stream stream)
{
ASCIIEncoding encoding = new ASCIIEncoding();
return encoding.GetString( slurpInputStream( stream ) );
}
public static string getXmlChildText(XmlNode data)
{
StringBuilder builder = new StringBuilder();
foreach (XmlNode node in data.ChildNodes)
{
if (node.NodeType == XmlNodeType.Text ||
node.NodeType == XmlNodeType.CDATA)
{
builder.Append(node.Value);
}
}
return builder.ToString();
}
public static DateTime parseDate(string dateStr)
{
return DateTime.Parse(dateStr);
}
public static string getHttpDate()
{
// Setting the Culture will ensure we get a proper HTTP Date.
string date = System.DateTime.UtcNow.ToString( "ddd, dd MMM yyyy HH:mm:ss ", System.Globalization.CultureInfo.InvariantCulture ) + "GMT";
return date;
}
public static long currentTimeMillis()
{
return (long)( DateTime.UtcNow - new DateTime( 1970, 1, 1 ) ).TotalMilliseconds;
}
}
}