Skip to content

Commit f9bb0cc

Browse files
committed
TD-4945: CORS Policy Issue on iPhone Landing Page
1 parent 5672130 commit f9bb0cc

File tree

3 files changed

+87
-4
lines changed

3 files changed

+87
-4
lines changed

LearningHub.Nhs.WebUI/Controllers/Api/MediaManifestProxyController.cs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
/// <summary>
1515
/// Defines the <see cref="MediaManifestProxyController" />.
1616
/// </summary>
17-
[Authorize]
1817
[Route("api/[controller]")]
1918
[ApiController]
2019
public class MediaManifestProxyController : ControllerBase
@@ -37,6 +36,7 @@ public MediaManifestProxyController(ILogger<MediaManifestProxyController> logger
3736
/// <param name="playBackUrl">The playBackUrl.</param>
3837
/// <param name="token">The token.</param>
3938
/// <returns>The MediaManifestProxy string.</returns>
39+
[Authorize]
4040
public string Get(string playBackUrl, string token)
4141
{
4242
this.logger.LogDebug($"playBackUrl={playBackUrl} token={token}");
@@ -103,6 +103,80 @@ public string Get(string playBackUrl, string token)
103103
return null;
104104
}
105105

106+
/// <summary>
107+
/// The LandingPageGet.
108+
/// </summary>
109+
/// <param name="playBackUrl">The playBackUrl.</param>
110+
/// <param name="token">The token.</param>
111+
/// <returns>The MediaManifestProxy string.</returns>
112+
[HttpGet]
113+
[Route("LandingPageGet")]
114+
public string LandingPageGet(string playBackUrl, string token)
115+
{
116+
this.logger.LogDebug($"playBackUrl={playBackUrl} token={token}");
117+
var httpRequest = (HttpWebRequest)WebRequest.Create(new Uri(playBackUrl));
118+
httpRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
119+
httpRequest.Timeout = 30000;
120+
121+
var httpResponse = httpRequest.GetResponse();
122+
123+
try
124+
{
125+
this.logger.LogDebug($"Calling httpResponse.GetResponseStream(): playBackUrl={playBackUrl} ");
126+
var stream = httpResponse.GetResponseStream();
127+
if (stream != null)
128+
{
129+
using (var reader = new StreamReader(stream))
130+
{
131+
const string qualityLevelRegex = @"(|)([^""\s]+\.m3u8\(encryption=cbc\))";
132+
const string fragmentsRegex = @"(Fragments\([\w\d=-]+,[\w\d=-]+\))";
133+
const string urlRegex = @"(https?:\/\/[\da-z\.-]+\.[a-z\.]{2,6}[\/\w \.-]*\?[^,\s""]*)";
134+
135+
var baseUrl = playBackUrl.Substring(0, playBackUrl.IndexOf(".ism", System.StringComparison.OrdinalIgnoreCase)) + ".ism";
136+
this.logger.LogDebug($"baseUrl={baseUrl}");
137+
138+
var content = reader.ReadToEnd();
139+
140+
content = ReplaceUrisWithProxy(content, baseUrl);
141+
var newContent = Regex.Replace(content, urlRegex, match =>
142+
{
143+
string baseUrlWithQuery = match.Groups[1].Value; // URL including the query string
144+
145+
// Append the token correctly without modifying surrounding characters
146+
string newUrl = baseUrlWithQuery.Contains("?") ?
147+
$"{baseUrlWithQuery}&token={token}" :
148+
$"{baseUrlWithQuery}?token={token}";
149+
150+
return newUrl;
151+
});
152+
153+
this.logger.LogDebug($"newContent={newContent}");
154+
155+
var match = Regex.Match(playBackUrl, qualityLevelRegex);
156+
if (match.Success)
157+
{
158+
this.logger.LogDebug($"match.Success");
159+
var qualityLevel = match.Groups[0].Value;
160+
newContent = Regex.Replace(newContent, fragmentsRegex, m => string.Format(CultureInfo.InvariantCulture, baseUrl + "/" + qualityLevel + "/" + m.Value));
161+
this.logger.LogDebug($"Updated newContent={newContent}");
162+
}
163+
164+
return newContent;
165+
}
166+
}
167+
}
168+
catch (Exception ex)
169+
{
170+
this.logger.LogError(ex.Message);
171+
}
172+
finally
173+
{
174+
httpResponse.Close();
175+
}
176+
177+
return null;
178+
}
179+
106180
private static string ReplaceUrisWithProxy(string playlistContent, string proxyUrl)
107181
{
108182
// Split the playlist content into lines

LearningHub.Nhs.WebUI/Controllers/MediaController.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,25 @@ public MediaController(IWebHostEnvironment hostingEnvironment, ILogger<MediaCont
3939
/// <param name="playBackUrl">The playBackUrl.</param>
4040
/// <param name="token">The token.</param>
4141
/// <param name="origin">The orgin node.</param>
42+
/// <param name="isLandingPage">The isLandingPage.</param>
4243
/// <returns>The <see cref="IActionResult"/>.</returns>
4344
[Route("Media/MediaManifest")]
44-
public IActionResult MediaManifest(string playBackUrl, string token, string origin = "*")
45+
public IActionResult MediaManifest(string playBackUrl, string token, string origin = "*", bool isLandingPage = false)
4546
{
4647
try
4748
{
4849
this.Logger.LogDebug($"playBackUrl={playBackUrl} token={token}");
4950
var hostPortion = this.Request.Host;
51+
var manifestProxyUrl = string.Empty;
52+
if (isLandingPage)
53+
{
54+
manifestProxyUrl = string.Format("https://{0}/api/MediaManifestProxy/LandingPageGet", hostPortion);
55+
}
56+
else
57+
{
58+
manifestProxyUrl = string.Format("https://{0}/api/MediaManifestProxy", hostPortion);
59+
}
5060

51-
var manifestProxyUrl = string.Format("https://{0}/api/MediaManifestProxy", hostPortion);
5261
this.Logger.LogDebug($"manifestProxyUrl={manifestProxyUrl}");
5362

5463
var modifiedTopLeveLManifest = this.azureMediaService.GetTopLevelManifestForToken(manifestProxyUrl, playBackUrl, token);

LearningHub.Nhs.WebUI/Views/Home/_CmsVideo.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
109109
if (checkIfIphone()) {
110110
var token = model.videoAsset.azureMediaAsset.authenticationToken;
111-
url = '@requestURL' + "Media/MediaManifest?playBackUrl=" + url + "&token=" + token + "&origin=" + '@requestURL';
111+
url = '@requestURL' + "Media/MediaManifest?playBackUrl=" + url + "&token=" + token + "&origin=" + '@requestURL' + "&isLandingPage=" + true;
112112
}
113113
var subtitleTrack = null;
114114
if (model.videoAsset.azureMediaAsset && model.videoAsset.closedCaptionsFile) {

0 commit comments

Comments
 (0)