Skip to content

Commit 24b213e

Browse files
Merge pull request #59 from ParsePlatform/richardross.filestate.secure
Added `SecureUrl` to FileState to support TLS file downloads.
2 parents e01f99e + 3b9e41d commit 24b213e

File tree

5 files changed

+85
-14
lines changed

5 files changed

+85
-14
lines changed

Parse/Internal/File/State/FileState.cs

100644100755
Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
// Copyright (c) 2015-present, Parse, LLC. All rights reserved. This source code is licensed under the BSD-style license found in the LICENSE file in the root directory of this source tree. An additional grant of patent rights can be found in the PATENTS file in the same directory.
2-
3-
using System;
4-
5-
namespace Parse.Internal {
6-
internal class FileState {
7-
public string Name { get; internal set; }
8-
public string MimeType { get; internal set; }
9-
public Uri Url { get; internal set; }
10-
}
11-
}
1+
// Copyright (c) 2015-present, Parse, LLC. All rights reserved. This source code is licensed under the BSD-style license found in the LICENSE file in the root directory of this source tree. An additional grant of patent rights can be found in the PATENTS file in the same directory.
2+
3+
using System;
4+
5+
namespace Parse.Internal {
6+
internal class FileState {
7+
private const string ParseFileSecureScheme = "https";
8+
private const string ParseFileSecureDomain = "files.parsetfss.com";
9+
10+
public string Name { get; internal set; }
11+
public string MimeType { get; internal set; }
12+
public Uri Url { get; internal set; }
13+
public Uri SecureUrl {
14+
get {
15+
Uri uri = Url;
16+
if (uri.Host == ParseFileSecureDomain) {
17+
return new UriBuilder(uri) {
18+
Scheme = ParseFileSecureScheme,
19+
Port = -1, // This makes URIBuilder assign the default port for the URL scheme.
20+
}.Uri;
21+
}
22+
return uri;
23+
}
24+
}
25+
}
26+
}

Parse/ParseFile.cs

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Parse {
2525
/// await obj.SaveAsync();
2626
/// </code>
2727
/// </example>
28-
public class ParseFile : IJsonConvertible {
28+
public class ParseFile : IJsonConvertible {
2929
private FileState state;
3030
private readonly Stream dataStream;
3131
private readonly TaskQueue taskQueue = new TaskQueue();
@@ -111,7 +111,7 @@ public string MimeType {
111111
[ParseFieldName("url")]
112112
public Uri Url {
113113
get {
114-
return state.Url;
114+
return state.SecureUrl;
115115
}
116116
}
117117

ParseTest.Unit/FileStateTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using Parse;
3+
using Parse.Internal;
4+
using System.Text;
5+
using System.Collections.Generic;
6+
using NUnit.Framework;
7+
8+
namespace ParseTest {
9+
[TestFixture]
10+
public class FileStateTests {
11+
[Test]
12+
public void TestSecureUrl() {
13+
Uri unsecureUri = new Uri("http://files.parsetfss.com/yolo.txt");
14+
Uri secureUri = new Uri("https://files.parsetfss.com/yolo.txt");
15+
Uri randomUri = new Uri("http://random.server.local/file.foo");
16+
17+
FileState state = new FileState {
18+
Name = "A",
19+
Url = unsecureUri,
20+
MimeType = null
21+
};
22+
23+
Assert.AreEqual(unsecureUri, state.Url);
24+
Assert.AreEqual(secureUri, state.SecureUrl);
25+
26+
// Make sure the proper port was given back.
27+
Assert.AreEqual(443, state.SecureUrl.Port);
28+
29+
state = new FileState {
30+
Name = "B",
31+
Url = randomUri,
32+
MimeType = null
33+
};
34+
35+
Assert.AreEqual(randomUri, state.Url);
36+
Assert.AreEqual(randomUri, state.Url);
37+
}
38+
}
39+
}

ParseTest.Unit/FileTests.cs

100644100755
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,21 @@ public Task TestFileSave() {
4949
Assert.False(file.IsDirty);
5050
});
5151
}
52+
53+
[Test]
54+
public void TestSecureUrl() {
55+
Uri unsecureUri = new Uri("http://files.parsetfss.com/yolo.txt");
56+
Uri secureUri = new Uri("https://files.parsetfss.com/yolo.txt");
57+
Uri randomUri = new Uri("http://random.server.local/file.foo");
58+
59+
ParseFile file = new ParseFile("Foo", unsecureUri);
60+
Assert.AreEqual(secureUri, file.Url);
61+
62+
file = new ParseFile("Bar", secureUri);
63+
Assert.AreEqual(secureUri, file.Url);
64+
65+
file = new ParseFile("Baz", randomUri);
66+
Assert.AreEqual(randomUri, file.Url);
67+
}
5268
}
5369
}

ParseTest.Unit/ParseTest.Unit.NetFx45.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="DecoderTests.cs" />
8585
<Compile Include="EncoderTests.cs" />
8686
<Compile Include="FileControllerTests.cs" />
87+
<Compile Include="FileStateTests.cs" />
8788
<Compile Include="FileTests.cs" />
8889
<Compile Include="ProgressTests.cs" />
8990
<Compile Include="GeoPointTests.cs" />
@@ -148,4 +149,4 @@
148149
<Target Name="AfterBuild">
149150
</Target>
150151
-->
151-
</Project>
152+
</Project>

0 commit comments

Comments
 (0)