|
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microsoft.AspNetCore.Mvc; |
| 5 | +using Microsoft.Azure.WebJobs; |
| 6 | +using Microsoft.Azure.WebJobs.Extensions.Http; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | +using System.Web; |
| 9 | + |
| 10 | +namespace SampleFunctions |
| 11 | +{ |
| 12 | + public static class GetDataFromUrl |
| 13 | + { |
| 14 | + private static readonly string[] allowed = { "https://samples.azuremaps.com/", |
| 15 | + "http://localhost"}; |
| 16 | + |
| 17 | + private static readonly HttpClient httpClient = new(); |
| 18 | + |
| 19 | + [FunctionName("GetDataFromUrl")] |
| 20 | + public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req) |
| 21 | + { |
| 22 | + string referer = req.Headers["Referer"]; |
| 23 | + if (string.IsNullOrEmpty(referer)) |
| 24 | + return new UnauthorizedResult(); |
| 25 | + |
| 26 | + string result = Array.Find(allowed, site => referer.StartsWith(site, StringComparison.OrdinalIgnoreCase)); |
| 27 | + if (string.IsNullOrEmpty(result)) |
| 28 | + return new UnauthorizedResult(); |
| 29 | + |
| 30 | + string url = req.Query["url"]; |
| 31 | + if (string.IsNullOrEmpty(url)) |
| 32 | + return new BadRequestObjectResult("Please pass a valid URL on the query string."); |
| 33 | + |
| 34 | + using var response = await httpClient.GetAsync(HttpUtility.UrlDecode(url)); |
| 35 | + |
| 36 | + if (!response.IsSuccessStatusCode) |
| 37 | + return new BadRequestObjectResult("An error occurred. It was not possible to get the data of this URL."); |
| 38 | + |
| 39 | + var content = await response.Content.ReadAsStringAsync(); |
| 40 | + |
| 41 | + return new OkObjectResult(content); |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments