Skip to content

Commit

Permalink
additional logging
Browse files Browse the repository at this point in the history
  • Loading branch information
scbedd committed Jan 31, 2023
1 parent fb73696 commit f633086
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ private static async Task<string> _generateLogLine(HttpResponseMessage resp)
headers = Encoding.UTF8.GetString(ms.ToArray());
}

sb.AppendLine("URI: [ " + resp.StatusCode + "]");
sb.AppendLine("Headers: [" + headers + "]");
sb.AppendLine("Response Statuscode: [ " + resp.StatusCode + "]");
sb.AppendLine("Response Headers: [" + headers + "]");

return sb.ToString();
}
Expand Down
30 changes: 19 additions & 11 deletions tools/test-proxy/Azure.Sdk.Tools.TestProxy/RecordingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public async Task StartRecordingAsync(string sessionId, HttpResponse outgoingRes

public async Task HandleRecordRequestAsync(string recordingId, HttpRequest incomingRequest, HttpResponse outgoingResponse)
{
await DebugLogger.LogRequestDetailsAsync(incomingRequest);
//await DebugLogger.LogRequestDetailsAsync(incomingRequest);

if (!RecordingSessions.TryGetValue(recordingId, out var session))
{
Expand Down Expand Up @@ -477,11 +477,14 @@ public static async Task<RecordEntry> CreateEntryAsync(HttpRequest request)
entry.RequestUri = GetRequestUri(request).AbsoluteUri;
entry.RequestMethod = new RequestMethod(request.Method);

foreach (var header in request.Headers)
lock (request.Headers)
{
if (IncludeHeader(header.Key))
foreach (var header in request.Headers)
{
entry.Request.Headers.Add(header.Key, header.Value.ToArray());
if (IncludeHeader(header.Key))
{
entry.Request.Headers.Add(header.Key, header.Value.ToArray());
}
}
}

Expand Down Expand Up @@ -956,14 +959,19 @@ public static string GetHeader(HttpRequest request, string name, bool allowNulls

public static Uri GetRequestUri(HttpRequest request)
{
// Instead of obtaining the Path of the request from request.Path, we use this
// more complicated method obtaining the raw string from the httpcontext. Unfortunately,
// The native request functions implicitly decode the Path value. EG: "aa%27bb" is decoded into 'aa'bb'.
// Using the RawTarget PREVENTS this automatic decode. We still lean on the URI constructors
// to give us some amount of safety, but note that we explicitly disable escaping in that combination.
var rawTarget = request.HttpContext.Features.Get<IHttpRequestFeature>().RawTarget;
var hostValue = GetHeader(request, "x-recording-upstream-base-uri");
string rawTarget = string.Empty;
string hostValue = string.Empty;

lock (request)
{
// Instead of obtaining the Path of the request from request.Path, we use this
// more complicated method obtaining the raw string from the httpcontext. Unfortunately,
// The native request functions implicitly decode the Path value. EG: "aa%27bb" is decoded into 'aa'bb'.
// Using the RawTarget PREVENTS this automatic decode. We still lean on the URI constructors
// to give us some amount of safety, but note that we explicitly disable escaping in that combination.
rawTarget = request.HttpContext.Features.Get<IHttpRequestFeature>().RawTarget;
hostValue = GetHeader(request, "x-recording-upstream-base-uri");
}
// it is easy to forget the x-recording-upstream-base-uri value
if (string.IsNullOrWhiteSpace(hostValue))
{
Expand Down
3 changes: 2 additions & 1 deletion tools/test-proxy/Azure.Sdk.Tools.TestProxy/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",

}

0 comments on commit f633086

Please sign in to comment.