Skip to content

Commit

Permalink
Modified S5144(C#): Use HttpClient instead of old WebRequest (#4431)
Browse files Browse the repository at this point in the history
  • Loading branch information
loris-s-sonarsource authored Oct 23, 2024
1 parent fa06fa2 commit 3759f65
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions rules/S5144/csharp/how-to-fix-it/dotnet.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ using System.Web.Mvc;
public class ExampleController: Controller
{
[HttpGet]
public IActionResult ImageFetch(string location)
public async Task<IActionResult> ImageFetch(string location)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(location);
await using Stream stream =
await client.GetStreamAsync(location); // Noncompliant
var exampleImage =
await JsonSerializer.DeserializeAsync<ExampleImage>(stream);
return Ok();
return Ok(example ?? new());
}
}
----
Expand All @@ -36,7 +39,7 @@ public class ExampleController: Controller
private readonly string[] allowedDomains = { "trusted1.example.com", "trusted2.example.com" };
[HttpGet]
public IActionResult ImageFetch(string location)
public async Task<IActionResult> ImageFetch(string location)
{
Uri uri = new Uri(location);
Expand All @@ -45,9 +48,12 @@ public class ExampleController: Controller
return BadRequest();
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
await using Stream stream =
await client.GetStreamAsync(location);
var exampleImage =
await JsonSerializer.DeserializeAsync<ExampleImage>(stream);
return Ok();
return Ok(example ?? new());
}
}
----
Expand Down

0 comments on commit 3759f65

Please sign in to comment.