forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebRequestUtils.cs
More file actions
612 lines (533 loc) · 23.6 KB
/
Copy pathWebRequestUtils.cs
File metadata and controls
612 lines (533 loc) · 23.6 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine.Scripting;
using UnityEngine.Internal;
using UnityEngine.Bindings;
namespace UnityEngine
{
// Helper class to generate form data to post to web servers using the [[WWW]] class.
public class WWWForm
{
private List<byte[]> formData; // <byte[]>
private List<string> fieldNames; // <string>
private List<string> fileNames; // <string>
private List<string> types; // <string>
private byte[] boundary;
private bool containsFiles = false;
internal static System.Text.Encoding DefaultEncoding
{
get
{
return System.Text.Encoding.ASCII;
}
}
// Creates an empty WWWForm object.
public WWWForm()
{
formData = new List<byte[]>();
fieldNames = new List<string>();
fileNames = new List<string>();
types = new List<string>();
// Generate a random boundary
boundary = new byte[40];
for (int i = 0; i < 40; i++)
{
int randomChar = Random.Range(48, 110);
if (randomChar > 57) // skip unprintable chars between 57 and 64 (inclusive)
randomChar += 7;
if (randomChar > 90) // and 91 and 96 (inclusive)
randomChar += 6;
boundary[i] = (byte)randomChar;
}
}
// Add a simple field to the form.
public void AddField(string fieldName, string value)
{
AddField(fieldName, value, Encoding.UTF8);
}
// Add a simple field to the form.
public void AddField(string fieldName, string value, Encoding e)
{
fieldNames.Add(fieldName);
fileNames.Add(null);
formData.Add(e.GetBytes(value));
types.Add("text/plain; charset=\"" + e.WebName + "\"");
}
// Adds a simple field to the form.
public void AddField(string fieldName, int i)
{
AddField(fieldName, i.ToString());
}
// Add binary data to the form.
[ExcludeFromDocs]
public void AddBinaryData(string fieldName, byte[] contents)
{
AddBinaryData(fieldName, contents, null, null);
}
// Add binary data to the form.
[ExcludeFromDocs]
public void AddBinaryData(string fieldName, byte[] contents, string fileName)
{
AddBinaryData(fieldName, contents, fileName, null);
}
// Add binary data to the form.
public void AddBinaryData(string fieldName, byte[] contents, [DefaultValue("null")] string fileName, [DefaultValue("null")] string mimeType)
{
containsFiles = true;
// We handle png files automatically as we suspect people will be uploading png files a lot due to the new
// screen shot feature. If we want to add support for detecting other file types, we will need to do it in a more extensible way.
bool isPng = contents.Length > 8 && contents[0] == 0x89 && contents[1] == 0x50 && contents[2] == 0x4e &&
contents[3] == 0x47
&& contents[4] == 0x0d && contents[5] == 0x0a && contents[6] == 0x1a && contents[7] == 0x0a;
if (fileName == null)
{
fileName = fieldName + (isPng ? ".png" : ".dat");
}
if (mimeType == null)
{
if (isPng)
mimeType = "image/png";
else
mimeType = "application/octet-stream";
}
fieldNames.Add(fieldName);
fileNames.Add(fileName);
formData.Add(contents);
types.Add(mimeType);
}
// (RO) Returns the correct request headers for posting the form using the [[WWW]] class.
public Dictionary<string, string> headers
{
get
{
Dictionary<string, string> retval = new Dictionary<string, string>();
if (containsFiles)
retval["Content-Type"] = "multipart/form-data; boundary=\"" +
System.Text.Encoding.UTF8.GetString(boundary, 0, boundary.Length) + "\"";
else
retval["Content-Type"] = "application/x-www-form-urlencoded";
return retval;
}
}
// (RO) The raw data to pass as the POST request body when sending the form.
public byte[] data
{
get
{
if (containsFiles)
{
byte[] dDash = DefaultEncoding.GetBytes("--");
byte[] crlf = DefaultEncoding.GetBytes("\r\n");
byte[] contentTypeHeader = DefaultEncoding.GetBytes("Content-Type: ");
byte[] dispositionHeader = DefaultEncoding.GetBytes("Content-disposition: form-data; name=\"");
byte[] endQuote = DefaultEncoding.GetBytes("\"");
byte[] fileNameField = DefaultEncoding.GetBytes("; filename=\"");
using (MemoryStream memStream = new MemoryStream(1024))
{
for (int i = 0; i < formData.Count; i++)
{
memStream.Write(crlf, 0, (int)crlf.Length);
memStream.Write(dDash, 0, (int)dDash.Length);
memStream.Write(boundary, 0, (int)boundary.Length);
memStream.Write(crlf, 0, (int)crlf.Length);
memStream.Write(contentTypeHeader, 0, (int)contentTypeHeader.Length);
byte[] type = System.Text.Encoding.UTF8.GetBytes((string)types[i]);
memStream.Write(type, 0, (int)type.Length);
memStream.Write(crlf, 0, (int)crlf.Length);
memStream.Write(dispositionHeader, 0, (int)dispositionHeader.Length);
string headerName = System.Text.Encoding.UTF8.HeaderName;
// Headers must be 7 bit clean, so encode as per rfc1522 using quoted-printable if needed.
string encodedFieldName = (string)fieldNames[i];
if (!WWWTranscoder.SevenBitClean(encodedFieldName, System.Text.Encoding.UTF8) ||
encodedFieldName.IndexOf("=?") > -1)
{
encodedFieldName = "=?" + headerName + "?Q?" +
WWWTranscoder.QPEncode(encodedFieldName, System.Text.Encoding.UTF8) + "?=";
}
byte[] name = System.Text.Encoding.UTF8.GetBytes(encodedFieldName);
memStream.Write(name, 0, (int)name.Length);
memStream.Write(endQuote, 0, (int)endQuote.Length);
if (fileNames[i] != null)
{
// Headers must be 7 bit clean, so encode as per rfc1522 using quoted-printable if needed.
string encodedFileName = (string)fileNames[i];
if (!WWWTranscoder.SevenBitClean(encodedFileName, System.Text.Encoding.UTF8) ||
encodedFileName.IndexOf("=?") > -1)
{
encodedFileName = "=?" + headerName + "?Q?" +
WWWTranscoder.QPEncode(encodedFileName, System.Text.Encoding.UTF8) + "?=";
}
byte[] fileName = System.Text.Encoding.UTF8.GetBytes(encodedFileName);
memStream.Write(fileNameField, 0, (int)fileNameField.Length);
memStream.Write(fileName, 0, (int)fileName.Length);
memStream.Write(endQuote, 0, (int)endQuote.Length);
}
memStream.Write(crlf, 0, (int)crlf.Length);
memStream.Write(crlf, 0, (int)crlf.Length);
byte[] formBytes = (byte[])formData[i];
memStream.Write(formBytes, 0, (int)formBytes.Length);
}
memStream.Write(crlf, 0, (int)crlf.Length);
memStream.Write(dDash, 0, (int)dDash.Length);
memStream.Write(boundary, 0, (int)boundary.Length);
memStream.Write(dDash, 0, (int)dDash.Length);
memStream.Write(crlf, 0, (int)crlf.Length);
return memStream.ToArray();
}
}
else
{
byte[] ampersand = DefaultEncoding.GetBytes("&");
byte[] equal = DefaultEncoding.GetBytes("=");
using (MemoryStream memStream = new MemoryStream(1024))
{
for (int i = 0; i < formData.Count; i++)
{
byte[] name = WWWTranscoder.DataEncode(System.Text.Encoding.UTF8.GetBytes((string)fieldNames[i]));
byte[] formBytes = (byte[])formData[i];
byte[] value = WWWTranscoder.DataEncode(formBytes);
if (i > 0) memStream.Write(ampersand, 0, (int)ampersand.Length);
memStream.Write(name, 0, (int)name.Length);
memStream.Write(equal, 0, (int)equal.Length);
memStream.Write(value, 0, (int)value.Length);
}
return memStream.ToArray();
}
}
}
}
}
[VisibleToOtherModules("UnityEngine.UnityWebRequestWWWModule")]
internal class WWWTranscoder
{
private static byte[] ucHexChars = WWWForm.DefaultEncoding.GetBytes("0123456789ABCDEF");
private static byte[] lcHexChars = WWWForm.DefaultEncoding.GetBytes("0123456789abcdef");
private static byte urlEscapeChar = (byte)'%';
private static byte[] urlSpace = new byte[] { (byte)'+' };
private static byte[] dataSpace = WWWForm.DefaultEncoding.GetBytes("%20");
private static byte[] urlForbidden = WWWForm.DefaultEncoding.GetBytes("@&;:<>=?\"'/\\!#%+$,{}|^[]`");
private static byte qpEscapeChar = (byte)'=';
private static byte[] qpSpace = new byte[] { (byte)'_' };
private static byte[] qpForbidden = WWWForm.DefaultEncoding.GetBytes("&;=?\"'%+_");
private static byte Hex2Byte(byte[] b, int offset)
{
byte result = (byte)0;
for (int i = offset; i < offset + 2; i++)
{
result *= 16;
int d = b[i];
if (d >= 48 && d <= 57) // 0 - 9
d -= 48;
else if (d >= 65 && d <= 75) // A -F
d -= 55;
else if (d >= 97 && d <= 102) // a - f
d -= 87;
if (d > 15)
{
return 63; // ?
}
result += (byte)d;
}
return result;
}
private static byte[] Byte2Hex(byte b, byte[] hexChars)
{
byte[] dest = new byte[2];
dest[0] = hexChars[b >> 4];
dest[1] = hexChars[b & 0xf];
return dest;
}
public static string URLEncode(string toEncode)
{
return URLEncode(toEncode, Encoding.UTF8);
}
public static string URLEncode(string toEncode, Encoding e)
{
byte[] data = Encode(e.GetBytes(toEncode), urlEscapeChar, urlSpace, urlForbidden, false);
return WWWForm.DefaultEncoding.GetString(data, 0, data.Length);
}
public static byte[] URLEncode(byte[] toEncode)
{
return Encode(toEncode, urlEscapeChar, urlSpace, urlForbidden, false);
}
public static string DataEncode(string toEncode)
{
return DataEncode(toEncode, Encoding.UTF8);
}
public static string DataEncode(string toEncode, Encoding e)
{
byte[] data = Encode(e.GetBytes(toEncode), urlEscapeChar, dataSpace, urlForbidden, false);
return WWWForm.DefaultEncoding.GetString(data, 0, data.Length);
}
public static byte[] DataEncode(byte[] toEncode)
{
return Encode(toEncode, urlEscapeChar, dataSpace, urlForbidden, false);
}
public static string QPEncode(string toEncode)
{
return QPEncode(toEncode, Encoding.UTF8);
}
public static string QPEncode(string toEncode, Encoding e)
{
byte[] data = Encode(e.GetBytes(toEncode), qpEscapeChar, qpSpace, qpForbidden, true);
return WWWForm.DefaultEncoding.GetString(data, 0, data.Length);
}
public static byte[] QPEncode(byte[] toEncode)
{
return Encode(toEncode, qpEscapeChar, qpSpace, qpForbidden, true);
}
public static byte[] Encode(byte[] input, byte escapeChar, byte[] space, byte[] forbidden, bool uppercase)
{
using (MemoryStream memStream = new MemoryStream(input.Length * 2))
{
// encode
for (int i = 0; i < input.Length; i++)
{
if (input[i] == 32)
{
memStream.Write(space, 0, space.Length);
}
else if (input[i] < 32 || input[i] > 126 || ByteArrayContains(forbidden, input[i]))
{
memStream.WriteByte(escapeChar);
memStream.Write(Byte2Hex(input[i], uppercase ? ucHexChars : lcHexChars), 0, 2);
}
else
{
memStream.WriteByte(input[i]);
}
}
return memStream.ToArray();
}
}
private static bool ByteArrayContains(byte[] array, byte b)
{
var arrayLength = array.Length;
for (int i = 0; i < arrayLength; i++)
{
if (array[i] == b)
return true;
}
return false;
}
public static string URLDecode(string toEncode)
{
return URLDecode(toEncode, Encoding.UTF8);
}
public static string URLDecode(string toEncode, Encoding e)
{
byte[] data = Decode(WWWForm.DefaultEncoding.GetBytes(toEncode), urlEscapeChar, urlSpace);
return e.GetString(data, 0, data.Length);
}
public static byte[] URLDecode(byte[] toEncode)
{
return Decode(toEncode, urlEscapeChar, urlSpace);
}
public static string DataDecode(string toDecode)
{
return DataDecode(toDecode, Encoding.UTF8);
}
public static string DataDecode(string toDecode, Encoding e)
{
byte[] data = Decode(WWWForm.DefaultEncoding.GetBytes(toDecode), urlEscapeChar, dataSpace);
return e.GetString(data, 0, data.Length);
}
public static byte[] DataDecode(byte[] toDecode)
{
return Decode(toDecode, urlEscapeChar, dataSpace);
}
public static string QPDecode(string toEncode)
{
return QPDecode(toEncode, Encoding.UTF8);
}
public static string QPDecode(string toEncode, Encoding e)
{
byte[] data = Decode(WWWForm.DefaultEncoding.GetBytes(toEncode), qpEscapeChar, qpSpace);
return e.GetString(data, 0, data.Length);
}
public static byte[] QPDecode(byte[] toEncode)
{
return Decode(toEncode, qpEscapeChar, qpSpace);
}
private static bool ByteSubArrayEquals(byte[] array, int index, byte[] comperand)
{
if (array.Length - index < comperand.Length)
return false;
for (int i = 0; i < comperand.Length; ++i)
if (array[index + i] != comperand[i])
return false;
return true;
}
public static byte[] Decode(byte[] input, byte escapeChar, byte[] space)
{
using (MemoryStream memStream = new MemoryStream(input.Length))
{
// decode
for (int i = 0; i < input.Length; i++)
{
if (ByteSubArrayEquals(input, i, space))
{
i += space.Length - 1;
memStream.WriteByte((byte)32);
}
else if (input[i] == escapeChar && i + 2 < input.Length)
{
i++;
memStream.WriteByte(Hex2Byte(input, i++));
}
else
{
memStream.WriteByte(input[i]);
}
}
return memStream.ToArray();
}
}
public static bool SevenBitClean(string s)
{
return SevenBitClean(s, Encoding.UTF8);
}
public static bool SevenBitClean(string s, Encoding e)
{
return SevenBitClean(e.GetBytes(s));
}
public static bool SevenBitClean(byte[] input)
{
for (int i = 0; i < input.Length; i++)
{
if (input[i] < 32 || input[i] > 126)
return false;
}
return true;
}
}
}
namespace UnityEngineInternal
{
static class WebRequestUtils
{
private static Regex domainRegex = new Regex("^\\s*\\w+(?:\\.\\w+)+(\\/.*)?$");
[RequiredByNativeCode]
internal static string RedirectTo(string baseUri, string redirectUri)
{
Uri redirectURI;
// On UNIX systems URI starting with / is misidentified as absolute path and is considered absolute
// while it is actually a relative URI. Enforce that.
if (redirectUri[0] == '/')
redirectURI = new Uri(redirectUri, UriKind.Relative);
else
redirectURI = new Uri(redirectUri, UriKind.RelativeOrAbsolute);
if (redirectURI.IsAbsoluteUri)
return redirectURI.AbsoluteUri;
var baseURI = new Uri(baseUri, UriKind.Absolute);
var finalUri = new Uri(baseURI, redirectURI);
return finalUri.AbsoluteUri;
}
internal static string MakeInitialurl(string targetUrl, string localUrl)
{
if (string.IsNullOrEmpty(targetUrl))
return "";
bool prependProtocol = false;
var localUri = new System.Uri(localUrl);
Uri targetUri = null;
if (targetUrl[0] == '/')
{
// Prepend scheme and (if needed) host
targetUri = new Uri(localUri, targetUrl);
prependProtocol = true;
}
if (targetUri == null && domainRegex.IsMatch(targetUrl))
{
targetUrl = localUri.Scheme + "://" + targetUrl;
prependProtocol = true;
}
FormatException ex = null;
try
{
// If URL starts with dot, it is relative and this would throw, skip to combining
if (targetUri == null && targetUrl[0] != '.')
targetUri = new System.Uri(targetUrl);
}
catch (FormatException e1)
{
// Technically, this should be UriFormatException but MSDN says WSA/PCL doesn't support
// UriFormatException, and recommends FormatException instead
// See: https://msdn.microsoft.com/en-us/library/system.uriformatexception%28v=vs.110%29.aspx
ex = e1;
}
if (targetUri == null)
try
{
targetUri = new System.Uri(localUri, targetUrl);
prependProtocol = true;
}
catch (FormatException)
{
throw ex;
}
return MakeUriString(targetUri, targetUrl, prependProtocol);
}
internal static string MakeUriString(Uri targetUri, string targetUrl, bool prependProtocol)
{
// for file://protocol pass in unescaped string so we can pass it to VFS
if (targetUri.IsFile)
{
if (!targetUri.IsLoopback)
return targetUri.OriginalString;
string path = targetUri.AbsolutePath;
if (path.Contains("%"))
path = URLDecode(path);
if (path.Length > 0 && path[0] != '/')
path = '/' + path;
return "file://" + path;
}
// Special handling for URIs like jar:file (Android), blob:http (WebGL and similar
// Uri.AbsoluteUri class in those cases results in jar:file/path, which is incorrect because of only one slash
// Uri.Scheme also returns scheme part before the colon (jar, blob)
// so if we didn't prepend the scheme and scheme has colon it it, construct the URI from it's parts
var scheme = targetUri.Scheme;
if (!prependProtocol && (targetUrl.Length >= scheme.Length + 2) && targetUrl[scheme.Length + 1] != '/')
{
StringBuilder sb = new StringBuilder(scheme, targetUrl.Length);
sb.Append(':');
// for these spec URIs path also has the part of URI to right of colon
// jar:file URIs should be treated like file URIs (unescaped and stripped of query&fragment)
if (scheme == "jar")
{
string path = targetUri.AbsolutePath;
if (path.Contains("%"))
path = URLDecode(path);
// common error when using Uri class and converting to string: URI will be jar:file:/path instead of jar:file:///path
if (path.StartsWith("file:/") && path.Length > 6 && path[6] != '/')
{
sb.Append("file://");
sb.Append(path.Substring(5));
}
else
sb.Append(path);
return sb.ToString();
}
sb.Append(targetUri.PathAndQuery);
sb.Append(targetUri.Fragment);
return sb.ToString();
}
// if URL contains '%', assume it is properly escaped, otherwise '%2f' gets unescaped as '/' (which may not be correct)
// otherwise escape it, i.e. replaces spaces by '%20'
if (targetUrl.Contains("%"))
return targetUri.OriginalString;
return targetUri.AbsoluteUri;
}
static string URLDecode(string encoded)
{
var urlBytes = Encoding.UTF8.GetBytes(encoded);
var decodedBytes = UnityEngine.WWWTranscoder.URLDecode(urlBytes);
return Encoding.UTF8.GetString(decodedBytes);
}
}
}