/// <summary> /// Converts the SDK's IRequest to the CRT's HttpRequest Object /// </summary> /// <param name="request">SDK request</param> /// <returns>CRT request</returns> public static Aws.Crt.Http.HttpRequest ConvertToCrtRequest(IRequest request) { // Remove any query params that CRT will set if (request.ParameterCollection != null && request.ParameterCollection.Count > 0) { foreach (var queryParam in request.ParameterCollection.GetSortedParametersList()) { if (CrtForbiddenQueryParams.Contains(queryParam.Key)) { request.ParameterCollection.Remove(queryParam.Key); } } } var crtRequest = new Aws.Crt.Http.HttpRequest { // Using OriginalString here because ComposeUrl -> ResolveResourcePath -> // JoinResourcePathSegments -> UrlEncode will escape some sequeneces (e.g. Ä -> %C3%84) // but initializing that as a Uri will convert it back to Ä Uri = AmazonServiceClient.ComposeUrl(request, false).OriginalString, Method = request.HttpMethod }; if (request.ContentStream != null) { if (request.ContentStream.CanSeek) { crtRequest.BodyStream = request.ContentStream; } else if (request.ContentStream is WrapperStream wrappedStream) { crtRequest.BodyStream = wrappedStream.GetSeekableBaseStream(); } else { throw new AWSCommonRuntimeException("Unable to pass an HTTP request with a non-seekable content stream to CRT."); } } else if (request.Content != null) { crtRequest.BodyStream = new MemoryStream(request.Content); } var headerList = new List <HttpHeader>(request.Headers.Count); foreach (var header in request.Headers) { // Skip CRT-calculated headers if (!CrtForbiddenHeaders.Contains(header.Key)) { headerList.Add(new HttpHeader(header.Key, header.Value)); } } crtRequest.Headers = headerList.ToArray(); return(crtRequest); }
/// <summary> /// Copies the headers from a CRT requst back to an existing SDK request /// </summary> /// <param name="request">SDK request</param> /// <param name="crtRequest">CRT request</param> public static void CopyHeadersFromCrtRequest(IRequest request, Aws.Crt.Http.HttpRequest crtRequest) { // Replace all of the SDK request's headers with the CRT headers (i.e. may now include signing-related headers) request.Headers.Clear(); foreach (var header in crtRequest.Headers) { request.Headers.Add(header.Name, header.Value); } }
/// <summary> /// Extracts the list of signed headers from the 'Authorization' header set by CRT /// </summary> /// <param name="crtRequest">Signed CRT HTTPRequest</param> /// <returns>semicolon-delimited list of signed headers</returns> public static string ExtractSignedHeaders(Aws.Crt.Http.HttpRequest crtRequest) { const string startOfSignedHeadersPiece = "SignedHeaders="; foreach (var header in crtRequest.Headers) { if (header.Name == HeaderKeys.AuthorizationHeader) { foreach (var piece in header.Value.Split(' ')) { if (piece.StartsWith(startOfSignedHeadersPiece)) { var signedHeaders = piece.Substring(startOfSignedHeadersPiece.Length); signedHeaders = signedHeaders.TrimEnd(','); // Remove trailing , separating SignedHeaders from Signature return(signedHeaders); } } } } return(""); }