| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using Azure.Core.Pipeline; |
| | 5 | | using Azure; |
| | 6 | | using System; |
| | 7 | | using System.Net.Http; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | | using System.Net; |
| | 11 | | using Azure.Core; |
| | 12 | |
|
| | 13 | | namespace Azure.Identity |
| | 14 | | { |
| | 15 | | internal static class HttpExtensions |
| | 16 | | { |
| | 17 | | public static async Task<Request> ToPipelineRequestAsync(this HttpRequestMessage request, HttpPipeline pipeline) |
| | 18 | | { |
| 216 | 19 | | Request pipelineRequest = pipeline.CreateRequest(); |
| | 20 | |
|
| 216 | 21 | | pipelineRequest.Method = RequestMethod.Parse(request.Method.Method); |
| | 22 | |
|
| 216 | 23 | | pipelineRequest.Uri.Reset(request.RequestUri); |
| | 24 | |
|
| 216 | 25 | | pipelineRequest.Content = await request.Content.ToPipelineRequestContentAsync().ConfigureAwait(false); |
| | 26 | |
|
| 4480 | 27 | | foreach (System.Collections.Generic.KeyValuePair<string, System.Collections.Generic.IEnumerable<string>> hea |
| | 28 | | { |
| 8096 | 29 | | foreach (var value in header.Value) |
| | 30 | | { |
| 2024 | 31 | | pipelineRequest.Headers.Add(header.Key, value); |
| | 32 | | } |
| | 33 | | } |
| | 34 | |
|
| 216 | 35 | | return pipelineRequest; |
| 216 | 36 | | } |
| | 37 | |
|
| | 38 | | private static void AddHeader(HttpResponseMessage request, HttpHeader header) |
| | 39 | | { |
| 0 | 40 | | if (request.Headers.TryAddWithoutValidation(header.Name, header.Value)) |
| | 41 | | { |
| 0 | 42 | | return; |
| | 43 | | } |
| | 44 | |
|
| 0 | 45 | | if (!request.Content.Headers.TryAddWithoutValidation(header.Name, header.Value)) |
| | 46 | | { |
| 0 | 47 | | throw new InvalidOperationException("Unable to add header to request or content"); |
| | 48 | | } |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | public static HttpResponseMessage ToHttpResponseMessage(this Response response) |
| | 52 | | { |
| 208 | 53 | | HttpResponseMessage responseMessage = new HttpResponseMessage |
| 208 | 54 | | { |
| 208 | 55 | | StatusCode = (HttpStatusCode)response.Status, |
| 208 | 56 | |
|
| 208 | 57 | | Content = new StreamContent(response.ContentStream) |
| 208 | 58 | | }; |
| | 59 | |
|
| 4748 | 60 | | foreach (HttpHeader header in response.Headers) |
| | 61 | | { |
| 2166 | 62 | | if (!responseMessage.Headers.TryAddWithoutValidation(header.Name, header.Value)) |
| | 63 | | { |
| 452 | 64 | | if (!responseMessage.Content.Headers.TryAddWithoutValidation(header.Name, header.Value)) |
| | 65 | | { |
| 0 | 66 | | throw new InvalidOperationException("Unable to add header to request or content"); |
| | 67 | | } |
| | 68 | | } |
| | 69 | | } |
| | 70 | |
|
| 208 | 71 | | return responseMessage; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | public static async Task<RequestContent> ToPipelineRequestContentAsync(this HttpContent content) |
| | 75 | | { |
| 216 | 76 | | if (content != null) |
| | 77 | | { |
| 160 | 78 | | return RequestContent.Create(await content.ReadAsStreamAsync().ConfigureAwait(false)); |
| | 79 | | } |
| | 80 | |
|
| 56 | 81 | | return null; |
| 216 | 82 | | } |
| | 83 | |
|
| | 84 | | } |
| | 85 | | } |