Skip to content

Commit e3a684e

Browse files
committed
proxy
1 parent 3c68988 commit e3a684e

1 file changed

Lines changed: 31 additions & 44 deletions

File tree

‎Services/SampleFunctions/GetDataFromUrl.cs‎

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,53 @@ namespace SampleFunctions;
66

77
public class GetDataFromUrl()
88
{
9-
private static readonly string[] AllowedDomains = [
10-
"https://samples.azuremaps.com/",
11-
"http://localhost:58035/" // For local testing
12-
];
13-
149
private static readonly HttpClient _HttpClient = new();
1510

1611
[Function("GetDataFromUrl")]
1712
public static async Task<HttpResponseData> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req)
1813
{
19-
// Check if the referer header is present and if the domain is allowed
20-
if (req.Headers.TryGetValues("Referer", out var referer) && AllowedDomains.Any(domain => referer.ToString().StartsWith(domain)))
21-
{
22-
var badRequest = req.CreateResponse(HttpStatusCode.BadRequest);
14+
var badRequest = req.CreateResponse(HttpStatusCode.BadRequest);
2315

24-
// Get the URL from the query string
25-
var url = Uri.UnescapeDataString(req.Query["url"]);
26-
if (string.IsNullOrEmpty(url))
27-
{
28-
badRequest.WriteString("Please pass a valid URL address in the query string.");
29-
return badRequest;
30-
}
16+
// Get the URL from the query string
17+
var url = Uri.UnescapeDataString(req.Query["url"]);
18+
if (string.IsNullOrEmpty(url))
19+
{
20+
badRequest.WriteString("Please pass a valid URL address in the query string.");
21+
return badRequest;
22+
}
3123

32-
// Download the content from the URL
33-
var result = await _HttpClient.GetAsync(url);
24+
// Download the content from the URL
25+
var result = await _HttpClient.GetAsync(url);
3426

35-
if (!result.IsSuccessStatusCode)
36-
{
37-
badRequest.WriteString("The URL you specified was unable to download.");
38-
return badRequest;
39-
}
27+
if (!result.IsSuccessStatusCode)
28+
{
29+
badRequest.WriteString("The URL you specified was unable to download.");
30+
return badRequest;
31+
}
4032

41-
// Create a new response
42-
var response = req.CreateResponse(HttpStatusCode.OK);
33+
// Create a new response
34+
var response = req.CreateResponse(HttpStatusCode.OK);
4335

44-
// Copy the headers from the result to the response, except for the ones we want to skip
45-
foreach (var header in result.Headers)
36+
// Copy the headers from the result to the response, except for the ones we want to skip
37+
foreach (var header in result.Headers)
38+
{
39+
if (!IsHeaderToSkip(header.Key))
4640
{
47-
if (!IsHeaderToSkip(header.Key))
48-
{
49-
response.Headers.Add(header.Key, header.Value.First());
50-
}
41+
response.Headers.Add(header.Key, header.Value.First());
5142
}
43+
}
5244

53-
// Set the 'Content-Type' header in the response
54-
response.Headers.Add("Content-Type", result.Content.Headers.ContentType.MediaType);
55-
56-
// Add the CORS header to the response
57-
response.Headers.Add("Access-Control-Allow-Origin", "*");
45+
// Set the 'Content-Type' header in the response
46+
response.Headers.Add("Content-Type", result.Content.Headers.ContentType.MediaType);
5847

59-
// Copy the content from the result to the response
60-
var content = await result.Content.ReadAsByteArrayAsync();
61-
await response.Body.WriteAsync(content);
48+
// Add the CORS header to the response
49+
response.Headers.Add("Access-Control-Allow-Origin", "*");
6250

63-
return response;
64-
}
51+
// Copy the content from the result to the response
52+
var content = await result.Content.ReadAsByteArrayAsync();
53+
await response.Body.WriteAsync(content);
6554

66-
// Return access denied if the referer domain is not allowed
67-
var forbidden = req.CreateResponse(HttpStatusCode.Forbidden);
68-
return forbidden;
55+
return response;
6956
}
7057

7158
private static bool IsHeaderToSkip(string headerName)

0 commit comments

Comments
 (0)