-
Notifications
You must be signed in to change notification settings - Fork 228
Fix GZip handling for requests #4165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6f75ddc
Fix GZip handling for requests
JoshLove-msft e8f30e5
Flush GZipStream when compressing.
JoshLove-msft 1f9b7e7
Removing flush call - not needed for Test Proxy target platforms
JoshLove-msft 93452c7
repair record mode operation. we were exhausting the request body str…
scbedd fe31843
Update mock handler
JoshLove-msft 71338dd
Merge branch 'proxy-gzip' of https://github.com/JoshLove-msft/azure-s…
JoshLove-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...e.Sdk.Tools.TestProxy.Tests/Test.RecordEntries/request_response_with_gzipped_content.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| { | ||
| "Entries": [ | ||
| { | ||
| "RequestUri": "https://fakeazsdktestaccount.table.core.windows.net/Tables", | ||
| "RequestMethod": "POST", | ||
| "RequestHeaders": { | ||
| "Accept": "application/json;odata=minimalmetadata", | ||
| "Accept-Encoding": "gzip, deflate", | ||
| "Authorization": "Sanitized", | ||
| "Connection": "keep-alive", | ||
| "Content-Length": "34", | ||
| "Content-Type": "application/json", | ||
| "Content-Encoding": "gzip", | ||
| "DataServiceVersion": "3.0", | ||
| "Date": "Tue, 18 May 2021 23:27:42 GMT", | ||
| "User-Agent": "azsdk-python-data-tables/12.0.0b7 Python/3.8.6 (Windows-10-10.0.19041-SP0)", | ||
| "x-ms-client-request-id": "a4c24b7a-b830-11eb-a05e-10e7c6392c5a", | ||
| "x-ms-date": "Tue, 18 May 2021 23:27:42 GMT", | ||
| "x-ms-version": "2019-02-02" | ||
| }, | ||
| "RequestBody": "{\u0022TableName\u0022: \u0022listtable09bf2a3d\u0022}", | ||
| "StatusCode": 201, | ||
| "ResponseHeaders": { | ||
| "Cache-Control": "no-cache", | ||
| "Content-Type": "application/json", | ||
| "Content-Encoding": "gzip", | ||
| "Date": "Tue, 18 May 2021 23:27:43 GMT", | ||
| "Retry-After": "10", | ||
| "Location": "https://fakeazsdktestaccount.table.core.windows.net/Tables(\u0027listtable09bf2a3d\u0027)", | ||
| "Server": [ | ||
| "Windows-Azure-Table/1.0", | ||
| "Microsoft-HTTPAPI/2.0" | ||
| ], | ||
| "Transfer-Encoding": "chunked", | ||
| "X-Content-Type-Options": "nosniff", | ||
| "x-ms-client-request-id": "a4c24b7a-b830-11eb-a05e-10e7c6392c5a", | ||
| "x-ms-request-id": "d2270777-c002-0072-313d-4ce19f000000", | ||
| "x-ms-version": "2019-02-02" | ||
| }, | ||
| "ResponseBody": { | ||
| "odata.metadata": "https://fakeazsdktestaccount.table.core.windows.net/$metadata#Tables/@Element", | ||
| "TableName": "listtable09bf2a3d", | ||
| "connectionString": null | ||
| } | ||
| } | ||
| ], | ||
| "Variables": {} | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/GZipUtilities.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.IO.Compression; | ||
| using System.Linq; | ||
| using System.Net.Http.Headers; | ||
| using Microsoft.AspNetCore.Http; | ||
|
|
||
| namespace Azure.Sdk.Tools.TestProxy.Common | ||
| { | ||
| /// <summary> | ||
| /// Utility methods to compress and decompress content to/from GZip. | ||
| /// </summary> | ||
| public static class GZipUtilities | ||
| { | ||
| private const string Gzip = "gzip"; | ||
| private const string ContentEncoding = "Content-Encoding"; | ||
|
|
||
| public static byte[] CompressBody(byte[] incomingBody, IDictionary<string, string[]> headers) | ||
| { | ||
| if (headers.TryGetValue(ContentEncoding, out var values) && values.Contains(Gzip)) | ||
| { | ||
| return CompressBodyCore(incomingBody); | ||
| } | ||
|
|
||
| return incomingBody; | ||
| } | ||
|
|
||
| public static byte[] CompressBody(byte[] incomingBody, IHeaderDictionary headers) | ||
| { | ||
| if (headers.TryGetValue(ContentEncoding, out var values) && values.Contains(Gzip)) | ||
| { | ||
| return CompressBodyCore(incomingBody); | ||
| } | ||
|
|
||
| return incomingBody; | ||
| } | ||
|
|
||
| public static byte[] CompressBodyCore(byte[] body) | ||
| { | ||
| using var uncompressedStream = new MemoryStream(body); | ||
| using var resultStream = new MemoryStream(); | ||
| using (var compressedStream = new GZipStream(resultStream, CompressionMode.Compress)) | ||
| { | ||
| uncompressedStream.CopyTo(compressedStream); | ||
| } | ||
| return resultStream.ToArray(); | ||
| } | ||
|
|
||
| public static byte[] DecompressBody(MemoryStream incomingBody, HttpContentHeaders headers) | ||
| { | ||
| if (headers.TryGetValues(ContentEncoding, out var values) && values.Contains(Gzip)) | ||
| { | ||
| return DecompressBodyCore(incomingBody); | ||
| } | ||
|
|
||
| return incomingBody.ToArray(); | ||
| } | ||
|
|
||
| public static byte[] DecompressBody(byte[] incomingBody, IHeaderDictionary headers) | ||
| { | ||
| if (headers.TryGetValue(ContentEncoding, out var values) && values.Contains(Gzip)) | ||
| { | ||
| return DecompressBodyCore(new MemoryStream(incomingBody)); | ||
| } | ||
|
|
||
| return incomingBody; | ||
| } | ||
|
|
||
| private static byte[] DecompressBodyCore(MemoryStream stream) | ||
| { | ||
| using var uncompressedStream = new GZipStream(stream, CompressionMode.Decompress); | ||
| using var resultStream = new MemoryStream(); | ||
| uncompressedStream.CopyTo(resultStream); | ||
| return resultStream.ToArray(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.