Skip to content

Commit d97a798

Browse files
Adds support for intercepting OPTIONS requests. Closes #411 (#415)
1 parent d4bffd0 commit d97a798

8 files changed

+28
-16
lines changed

dev-proxy-plugins/Behavior/RetryAfterPlugin.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ private Task OnRequest(object? sender, ProxyRequestArgs e)
2929
{
3030
if (e.ResponseState.HasBeenSet ||
3131
_urlsToWatch is null ||
32+
e.Session.HttpClient.Request.Method.ToUpper() == "OPTIONS" ||
3233
!e.ShouldExecute(_urlsToWatch))
3334
{
3435
return Task.CompletedTask;

dev-proxy-plugins/Guidance/CachingGuidancePlugin.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public override void Register(IPluginEvents pluginEvents,
3131

3232
private Task BeforeRequest(object? sender, ProxyRequestArgs e)
3333
{
34-
if (_urlsToWatch is null || !e.HasRequestUrlMatch(_urlsToWatch))
34+
if (_urlsToWatch is null ||
35+
!e.HasRequestUrlMatch(_urlsToWatch) ||
36+
e.Session.HttpClient.Request.Method.ToUpper() == "OPTIONS")
3537
{
3638
return Task.CompletedTask;
3739
}

dev-proxy-plugins/Guidance/GraphBetaSupportGuidancePlugin.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ private Task AfterResponse(object? sender, ProxyResponseArgs e)
2424
Request request = e.Session.HttpClient.Request;
2525
if (_urlsToWatch is not null &&
2626
e.HasRequestUrlMatch(_urlsToWatch) &&
27+
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
2728
ProxyUtils.IsGraphBetaRequest(request))
2829
_logger?.LogRequest(BuildBetaSupportMessage(request), MessageType.Warning, new LoggingContext(e.Session));
2930
return Task.CompletedTask;

dev-proxy-plugins/Guidance/GraphClientRequestIdGuidancePlugin.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public override void Register(IPluginEvents pluginEvents,
2424
private Task BeforeRequest(object? sender, ProxyRequestArgs e)
2525
{
2626
Request request = e.Session.HttpClient.Request;
27-
if (_urlsToWatch is not null && e.HasRequestUrlMatch(_urlsToWatch) && WarnNoClientRequestId(request))
27+
if (_urlsToWatch is not null &&
28+
e.HasRequestUrlMatch(_urlsToWatch) &&
29+
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
30+
WarnNoClientRequestId(request))
2831
{
2932
_logger?.LogRequest(BuildAddClientRequestIdMessage(request), MessageType.Warning, new LoggingContext(e.Session));
3033

dev-proxy-plugins/Guidance/GraphSdkGuidancePlugin.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ private Task OnAfterResponse(object? sender, ProxyResponseArgs e)
2323
{
2424
Request request = e.Session.HttpClient.Request;
2525
// only show the message if there is an error.
26-
if (e.Session.HttpClient.Response.StatusCode >= 400
27-
&& _urlsToWatch is not null
28-
&& e.HasRequestUrlMatch(_urlsToWatch)
29-
&& WarnNoSdk(request)) {
26+
if (e.Session.HttpClient.Response.StatusCode >= 400 &&
27+
_urlsToWatch is not null &&
28+
e.HasRequestUrlMatch(_urlsToWatch) &&
29+
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
30+
WarnNoSdk(request)) {
3031
_logger?.LogRequest(MessageUtils.BuildUseSdkForErrorsMessage(request), MessageType.Tip, new LoggingContext(e.Session));
3132
}
3233

dev-proxy-plugins/Guidance/GraphSelectGuidancePlugin.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public override void Register(IPluginEvents pluginEvents,
2424
private Task AfterResponse(object? sender, ProxyResponseArgs e)
2525
{
2626
Request request = e.Session.HttpClient.Request;
27-
if (_urlsToWatch is not null && e.HasRequestUrlMatch(_urlsToWatch) && WarnNoSelect(request))
27+
if (_urlsToWatch is not null &&
28+
e.HasRequestUrlMatch(_urlsToWatch) &&
29+
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
30+
WarnNoSelect(request))
2831
_logger?.LogRequest(BuildUseSelectMessage(request), MessageType.Warning, new LoggingContext(e.Session));
2932

3033
return Task.CompletedTask;

dev-proxy-plugins/Guidance/ODSPSearchGuidancePlugin.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public override void Register(IPluginEvents pluginEvents,
2424
private Task BeforeRequest(object sender, ProxyRequestArgs e)
2525
{
2626
Request request = e.Session.HttpClient.Request;
27-
if (_urlsToWatch is not null && e.HasRequestUrlMatch(_urlsToWatch) && WarnDeprecatedSearch(request))
27+
if (_urlsToWatch is not null &&
28+
e.HasRequestUrlMatch(_urlsToWatch) &&
29+
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
30+
WarnDeprecatedSearch(request))
2831
_logger?.LogRequest(BuildUseGraphSearchMessage(), MessageType.Warning, new LoggingContext(e.Session));
2932

3033
return Task.CompletedTask;

dev-proxy/ProxyEngine.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,7 @@ private bool IsProxiedProcess(TunnelConnectSessionEventArgs e) {
352352
}
353353

354354
async Task OnRequest(object sender, SessionEventArgs e) {
355-
var method = e.HttpClient.Request.Method.ToUpper();
356-
// The proxy does not intercept or alter OPTIONS requests
357-
if (method is not "OPTIONS" && IsProxiedHost(e.HttpClient.Request.RequestUri.Host)) {
355+
if (IsProxiedHost(e.HttpClient.Request.RequestUri.Host)) {
358356
// we need to keep the request body for further processing
359357
// by plugins
360358
e.HttpClient.Request.KeepBody = true;
@@ -386,20 +384,20 @@ private async Task HandleRequest(SessionEventArgs e) {
386384

387385
// Modify response
388386
async Task OnBeforeResponse(object sender, SessionEventArgs e) {
389-
var method = e.HttpClient.Request.Method.ToUpper();
390387
// read response headers
391-
if (method is not "OPTIONS" && IsProxiedHost(e.HttpClient.Request.RequestUri.Host)) {
388+
if (IsProxiedHost(e.HttpClient.Request.RequestUri.Host)) {
392389
// necessary to make the response body available to plugins
393390
e.HttpClient.Response.KeepBody = true;
394-
await e.GetResponseBody();
391+
if (e.HttpClient.Response.HasBody) {
392+
await e.GetResponseBody();
393+
}
395394

396395
await _pluginEvents.RaiseProxyBeforeResponse(new ProxyResponseArgs(e, _throttledRequests, new ResponseState()));
397396
}
398397
}
399398
async Task OnAfterResponse(object sender, SessionEventArgs e) {
400-
var method = e.HttpClient.Request.Method.ToUpper();
401399
// read response headers
402-
if (method is not "OPTIONS" && IsProxiedHost(e.HttpClient.Request.RequestUri.Host)) {
400+
if (IsProxiedHost(e.HttpClient.Request.RequestUri.Host)) {
403401
_logger.LogRequest(new[] { $"{e.HttpClient.Request.Method} {e.HttpClient.Request.Url}" }, MessageType.InterceptedResponse, new LoggingContext(e));
404402
await _pluginEvents.RaiseProxyAfterResponse(new ProxyResponseArgs(e, _throttledRequests, new ResponseState()));
405403
}

0 commit comments

Comments
 (0)