-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathManifestProxyController.cs
96 lines (85 loc) · 3.54 KB
/
ManifestProxyController.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
// -----------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation. All rights reserved.
// -----------------------------------------------------------------------------
namespace Microsoft.Azure.CloudVideo.VideoManagementService.Cms.Controllers
{
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Cache;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Http;
using CloudVideo.Common;
public class ManifestProxyController : ApiController
{
/// <summary>
/// Support for: GET /api/Manifest(playbackUrl, token)
/// </summary>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Web API request will dispose of this object when thread completes")]
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", Justification = "These are format strings, not URIs")]
public HttpResponseMessage Get(string playbackUrl, string token)
{
Check.NotNullOrWhiteSpace(playbackUrl, "playbackUrl");
Check.NotNullOrWhiteSpace(token, "token");
if (token.StartsWith("Bearer=", StringComparison.OrdinalIgnoreCase))
{
token = token.Substring("Bearer=".Length);
}
var collection = HttpUtility.ParseQueryString(token);
var authToken = collection.ToQueryString();
string armoredAuthToken = HttpUtility.UrlEncode(authToken);
var httpRequest = (HttpWebRequest)WebRequest.Create(new Uri(playbackUrl));
httpRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
httpRequest.Timeout = 30000;
var httpResponse = httpRequest.GetResponse();
var response = this.Request.CreateResponse();
try
{
var stream = httpResponse.GetResponseStream();
if (stream != null)
{
using (var reader = new StreamReader(stream))
{
const string qulityLevelRegex = @"(QualityLevels\(\d+\))";
const string fragmentsRegex = @"(Fragments\([\w\d=-]+,[\w\d=-]+\))";
const string urlRegex = @"("")(https?:\/\/[\da-z\.-]+\.[a-z\.]{2,6}[\/\w \.-]*\/?[\?&][^&=]+=[^&=#]*)("")";
var baseUrl = playbackUrl.Substring(0, playbackUrl.IndexOf(".ism", System.StringComparison.OrdinalIgnoreCase)) + ".ism";
var content = reader.ReadToEnd();
var newContent = Regex.Replace(content, urlRegex, string.Format(CultureInfo.InvariantCulture, "$1$2&token={0}$3", armoredAuthToken));
var match = Regex.Match(playbackUrl, qulityLevelRegex);
if (match.Success)
{
var qulityLevel = match.Groups[0].Value;
newContent = Regex.Replace(newContent, fragmentsRegex, m => string.Format(CultureInfo.InvariantCulture, baseUrl + "/" + qulityLevel + "/" + m.Value));
}
response.Content = new StringContent(newContent, Encoding.UTF8, "application/vnd.apple.mpegurl");
}
}
}
finally
{
httpResponse.Close();
}
return response;
}
}
public static class QueryExtensions
{
public static string ToQueryString(this NameValueCollection collection)
{
Check.NotNull(collection, "collection");
IEnumerable<string> segments = from key in collection.AllKeys
from value in collection.GetValues(key)
select string.Format(CultureInfo.InvariantCulture, "{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value));
return string.Join("&", segments);
}
}
}