-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #16: Use DelegatingClient to decipher encrypted videos
A few fixes have been made as well to use ConfigureAwait(false) in async code. This prevents hanging on encrypted videos in UI apps.
- Loading branch information
Showing
7 changed files
with
146 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace VideoLibrary | ||
{ | ||
public class DelegatingClient : IDisposable | ||
{ | ||
private bool disposed = false; | ||
private readonly HttpClient client; | ||
|
||
public DelegatingClient() | ||
{ | ||
this.client = MakeClient(); | ||
} | ||
|
||
#region IDisposable | ||
|
||
~DelegatingClient() | ||
{ | ||
Dispose(false); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposed) | ||
return; | ||
|
||
disposed = true; | ||
|
||
if (disposing) | ||
{ | ||
if (client != null) | ||
client.Dispose(); | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
#region MakeClient/MakeHandler | ||
|
||
private HttpClient MakeClient() => | ||
MakeClient(MakeHandler()); | ||
|
||
protected virtual HttpMessageHandler MakeHandler() | ||
{ | ||
var handler = new HttpClientHandler(); | ||
|
||
if (handler.SupportsAutomaticDecompression) | ||
{ | ||
handler.AutomaticDecompression = | ||
DecompressionMethods.GZip | | ||
DecompressionMethods.Deflate; | ||
} | ||
|
||
return handler; | ||
} | ||
|
||
protected virtual HttpClient MakeClient(HttpMessageHandler handler) | ||
{ | ||
return new HttpClient(handler); | ||
} | ||
|
||
#endregion | ||
|
||
#region Synchronous wrappers | ||
|
||
public HttpResponseMessage Get(string uri) => | ||
GetAsync(uri).GetAwaiter().GetResult(); | ||
|
||
public byte[] GetByteArray(string uri) => | ||
GetByteArrayAsync(uri).GetAwaiter().GetResult(); | ||
|
||
public Stream GetStream(string uri) => | ||
GetStreamAsync(uri).GetAwaiter().GetResult(); | ||
|
||
public string GetString(string uri) => | ||
GetStringAsync(uri).GetAwaiter().GetResult(); | ||
|
||
#endregion | ||
|
||
#region HttpClient wrappers | ||
|
||
// TODO: Support other kinds of HTTP requests, | ||
// such as PUT, POST, DELETE, etc. | ||
|
||
public Task<HttpResponseMessage> GetAsync(string uri) => | ||
client.GetAsync(uri); | ||
|
||
public Task<byte[]> GetByteArrayAsync(string uri) => | ||
client.GetByteArrayAsync(uri); | ||
|
||
public Task<Stream> GetStreamAsync(string uri) => | ||
client.GetStreamAsync(uri); | ||
|
||
public Task<string> GetStringAsync(string uri) => | ||
client.GetStringAsync(uri); | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters