Skip to content

Commit

Permalink
Adds support for intercepting OPTIONS requests. Closes #411
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz authored and garrytrinder committed Dec 12, 2023
1 parent d4bffd0 commit 1ccb1fc
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions dev-proxy-plugins/Behavior/RetryAfterPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ private Task OnRequest(object? sender, ProxyRequestArgs e)
{
if (e.ResponseState.HasBeenSet ||
_urlsToWatch is null ||
e.Session.HttpClient.Request.Method.ToUpper() == "OPTIONS" ||
!e.ShouldExecute(_urlsToWatch))
{
return Task.CompletedTask;
Expand Down
4 changes: 3 additions & 1 deletion dev-proxy-plugins/Guidance/CachingGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public override void Register(IPluginEvents pluginEvents,

private Task BeforeRequest(object? sender, ProxyRequestArgs e)
{
if (_urlsToWatch is null || !e.HasRequestUrlMatch(_urlsToWatch))
if (_urlsToWatch is null ||
!e.HasRequestUrlMatch(_urlsToWatch) ||
e.Session.HttpClient.Request.Method.ToUpper() == "OPTIONS")
{
return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ private Task AfterResponse(object? sender, ProxyResponseArgs e)
Request request = e.Session.HttpClient.Request;
if (_urlsToWatch is not null &&
e.HasRequestUrlMatch(_urlsToWatch) &&
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
ProxyUtils.IsGraphBetaRequest(request))
_logger?.LogRequest(BuildBetaSupportMessage(request), MessageType.Warning, new LoggingContext(e.Session));
return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public override void Register(IPluginEvents pluginEvents,
private Task BeforeRequest(object? sender, ProxyRequestArgs e)
{
Request request = e.Session.HttpClient.Request;
if (_urlsToWatch is not null && e.HasRequestUrlMatch(_urlsToWatch) && WarnNoClientRequestId(request))
if (_urlsToWatch is not null &&
e.HasRequestUrlMatch(_urlsToWatch) &&
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
WarnNoClientRequestId(request))
{
_logger?.LogRequest(BuildAddClientRequestIdMessage(request), MessageType.Warning, new LoggingContext(e.Session));

Expand Down
9 changes: 5 additions & 4 deletions dev-proxy-plugins/Guidance/GraphSdkGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ private Task OnAfterResponse(object? sender, ProxyResponseArgs e)
{
Request request = e.Session.HttpClient.Request;
// only show the message if there is an error.
if (e.Session.HttpClient.Response.StatusCode >= 400
&& _urlsToWatch is not null
&& e.HasRequestUrlMatch(_urlsToWatch)
&& WarnNoSdk(request)) {
if (e.Session.HttpClient.Response.StatusCode >= 400 &&
_urlsToWatch is not null &&
e.HasRequestUrlMatch(_urlsToWatch) &&
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
WarnNoSdk(request)) {
_logger?.LogRequest(MessageUtils.BuildUseSdkForErrorsMessage(request), MessageType.Tip, new LoggingContext(e.Session));
}

Expand Down
5 changes: 4 additions & 1 deletion dev-proxy-plugins/Guidance/GraphSelectGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public override void Register(IPluginEvents pluginEvents,
private Task AfterResponse(object? sender, ProxyResponseArgs e)
{
Request request = e.Session.HttpClient.Request;
if (_urlsToWatch is not null && e.HasRequestUrlMatch(_urlsToWatch) && WarnNoSelect(request))
if (_urlsToWatch is not null &&
e.HasRequestUrlMatch(_urlsToWatch) &&
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
WarnNoSelect(request))
_logger?.LogRequest(BuildUseSelectMessage(request), MessageType.Warning, new LoggingContext(e.Session));

return Task.CompletedTask;
Expand Down
5 changes: 4 additions & 1 deletion dev-proxy-plugins/Guidance/ODSPSearchGuidancePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public override void Register(IPluginEvents pluginEvents,
private Task BeforeRequest(object sender, ProxyRequestArgs e)
{
Request request = e.Session.HttpClient.Request;
if (_urlsToWatch is not null && e.HasRequestUrlMatch(_urlsToWatch) && WarnDeprecatedSearch(request))
if (_urlsToWatch is not null &&
e.HasRequestUrlMatch(_urlsToWatch) &&
e.Session.HttpClient.Request.Method.ToUpper() != "OPTIONS" &&
WarnDeprecatedSearch(request))
_logger?.LogRequest(BuildUseGraphSearchMessage(), MessageType.Warning, new LoggingContext(e.Session));

return Task.CompletedTask;
Expand Down
14 changes: 6 additions & 8 deletions dev-proxy/ProxyEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,7 @@ private bool IsProxiedProcess(TunnelConnectSessionEventArgs e) {
}

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

// Modify response
async Task OnBeforeResponse(object sender, SessionEventArgs e) {
var method = e.HttpClient.Request.Method.ToUpper();
// read response headers
if (method is not "OPTIONS" && IsProxiedHost(e.HttpClient.Request.RequestUri.Host)) {
if (IsProxiedHost(e.HttpClient.Request.RequestUri.Host)) {
// necessary to make the response body available to plugins
e.HttpClient.Response.KeepBody = true;
await e.GetResponseBody();
if (e.HttpClient.Response.HasBody) {
await e.GetResponseBody();
}

await _pluginEvents.RaiseProxyBeforeResponse(new ProxyResponseArgs(e, _throttledRequests, new ResponseState()));
}
}
async Task OnAfterResponse(object sender, SessionEventArgs e) {
var method = e.HttpClient.Request.Method.ToUpper();
// read response headers
if (method is not "OPTIONS" && IsProxiedHost(e.HttpClient.Request.RequestUri.Host)) {
if (IsProxiedHost(e.HttpClient.Request.RequestUri.Host)) {
_logger.LogRequest(new[] { $"{e.HttpClient.Request.Method} {e.HttpClient.Request.Url}" }, MessageType.InterceptedResponse, new LoggingContext(e));
await _pluginEvents.RaiseProxyAfterResponse(new ProxyResponseArgs(e, _throttledRequests, new ResponseState()));
}
Expand Down

0 comments on commit 1ccb1fc

Please sign in to comment.