-
Hello, During my documentation review, I discovered the option of using a DelegatingHandler. Since I only need a proxy server for specific routes, this seemed practical. I created a ProxyHandler (DelegatingHandler) that sends requests via a new HTTP client with a designated proxy server. This appeared to be the only entry point for setting a proxy server on the HTTP-Client. However, during testing, I encountered an exception:
To address this issue, I consulted ChatGPT, which provided a solution. However, I'm not entirely satisfied with it and still have some doubts. ChatGPT suggested cloning the request beforehand to resolve the problem. I'm unsure if this is the best way to define a proxy server. I would like to know if such an approach is viable and if there are potential vulnerabilities or risks that I should be aware of. Sourcode:/// <summary>
/// Used to utilize a configured proxy server when sending a request.
/// </summary>
public class ProxyHandler : DelegatingHandler
{
/// <summary>
/// Sends the request using the proxy server configured in appsettings.json.
/// </summary>
/// <param name="request">The HTTP request message being processed (Request)</param>
/// <param name="cancellationToken">Used to cancel an asynchronous operation</param>
/// <returns>HTTP response message</returns>
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
string proxyUrl = "http://127.0.0.1:8080";
string proxyUser = "";
string proxyPassword = "";
var httpClientHandler = new HttpClientHandler()
{
Proxy = new WebProxy()
{
Address = new Uri(proxyUrl),
BypassProxyOnLocal = false,
UseDefaultCredentials = false,
Credentials = string.IsNullOrEmpty(proxyUser) ? null : new NetworkCredential(proxyUser, proxyPassword)
}
};
//! Only for test purposes!
// Bypass certificate validation (Accept any server certificate)
httpClientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
//TODO: Adding getting HTTP-Client using factory...
using (var httpClient = new HttpClient(httpClientHandler))
{
// Clone the request to avoid the "already sent" issue.
var clonedRequest = await CloneHttpRequestMessage(request);
return await httpClient.SendAsync(clonedRequest, cancellationToken);
}
}
private async Task<HttpRequestMessage> CloneHttpRequestMessage(HttpRequestMessage request)
{
// Create a new HttpRequestMessage object with the same method and URI as the original message object.
var clonedRequest = new HttpRequestMessage(request.Method, request.RequestUri);
// Copy the header from the original message object to the cloned object.
foreach (var header in request.Headers)
{
clonedRequest.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
if (request.Content != null)
{
// Create a new StreamContent object and copy the content from the original message object.
clonedRequest.Content = new StreamContent(await request.Content.ReadAsStreamAsync());
// Copy the headers of the content (Content Headers) to the cloned Content object.
if (request.Content.Headers != null)
{
foreach (var header in request.Content.Headers)
{
clonedRequest.Content.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
}
}
return clonedRequest;
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Hi Wilko!
Well... This is very common error being processed and caught by HttpExceptionToErrorMapper | Line 34 private async Task<HttpRequestMessage> CloneHttpRequestMessage(HttpRequestMessage request)
{ } I don't understand why do you create this extra request? |
Beta Was this translation helpful? Give feedback.
-
Do you really consult ChatGPT in development? Come on! |
Beta Was this translation helpful? Give feedback.
-
You use ChatGPT for consultancy and now you are asking Ocelot development team, and worrying about vulnerabilities etc.? I have a couple of direct questions for you
Taking all these facts into account, I would say that you don't follow Ocelot's development process. @ggnaegi @RaynaldM @ks1990cn |
Beta Was this translation helpful? Give feedback.
You use ChatGPT for consultancy and now you are asking Ocelot development team, and worrying about vulnerabilities etc.?
Are you seriously doing this?
I have a couple of direct questions for you