protected virtual void CreateAuthorizationHeader(HttpClient client, HttpContentHeaders headers) { string contentToSign = ""; contentToSign += _option.Value.RQLMethod; contentToSign += new Uri(client.BaseAddress, _option.Value.RQLEndPoint).ToString(); contentToSign += headers.GetValues("Content-MD5").Single(); contentToSign += headers.GetValues("Content-Type").Single(); contentToSign += client.DefaultRequestHeaders.GetValues("Date").Single(); Console.WriteLine("Signing request " + contentToSign); byte[] hashBytes = RQLRequestSignator.Sign(_option.Value.AuthenticationToken, contentToSign); string hashDigest = RQLRequestSignator.HexDigest(hashBytes); var authorizationParameter = String.Concat("runner-", _option.Value.AuthenticationUserId, ":", hashDigest); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Cubicweb", authorizationParameter); }
private static Dictionary <string, string> GetHeaders(HttpContentHeaders headers) { var output = new Dictionary <string, string>(); foreach (var item in headers.ToList()) { output.Add(item.Key, headers.GetValues(item.Key).First().ToString()); } return(output); }
private static void AssertActualHeaders(HttpContentHeaders headers, SoapVersion soapVersion, string action) { if (headers.Contains("ActionHeader")) { soapVersion.Should().Be(SoapVersion.Soap11); headers.GetValues("ActionHeader").Single().Should().Be(action); } else if (headers.ContentType.Parameters.Any(e => e.Name == "ActionParameter")) { var actionParam = headers.ContentType.Parameters.Single(e => e.Name == "ActionParameter"); actionParam.Value.Should().Be($"\"{action}\""); soapVersion.Should().Be(SoapVersion.Soap12); } }
public void ContentLength_SetCustomValue_DelegateNotInvoked() { _headers = new HttpContentHeaders(() => { throw new ShouldNotBeInvokedException(); }); _headers.ContentLength = 27; Assert.Equal((long)27, _headers.ContentLength); Assert.Equal((long)27, _headers.GetParsedValues(HttpKnownHeaderNames.ContentLength)); // After explicitly setting the content length, set it to null. _headers.ContentLength = null; Assert.Equal(null, _headers.ContentLength); Assert.False(_headers.Contains(HttpKnownHeaderNames.ContentLength)); // Make sure the header gets serialized correctly _headers.ContentLength = 12345; Assert.Equal("12345", _headers.GetValues("Content-Length").First()); }
public void ContentLength_SetCustomValue_TryComputeLengthNotInvoked() { _headers = new HttpContentHeaders(new ComputeLengthHttpContent(() => { throw new ShouldNotBeInvokedException(); })); _headers.ContentLength = 27; Assert.Equal((long)27, _headers.ContentLength); Assert.Equal((long)27, _headers.GetSingleParsedValue(KnownHeaders.ContentLength.Descriptor)); // After explicitly setting the content length, set it to null. _headers.ContentLength = null; Assert.Null(_headers.ContentLength); Assert.False(_headers.Contains(KnownHeaders.ContentLength.Name)); // Make sure the header gets serialized correctly _headers.ContentLength = 12345; Assert.Equal("12345", _headers.GetValues("Content-Length").First()); }
/// <summary> /// Initializes a new instance of the <see cref="ImageByteArray" /> class. /// </summary> /// <param name="imageData">The image data byte array.</param> /// <param name="headers">The response headers.</param> public ImageByteArray(byte[] imageData, HttpContentHeaders headers) { Bytes = imageData; if (headers == null) { return; } // Attempt to set the ImageTime property. if (headers.Contains("Image-Time")) { try { var imageTimeHeaderValues = headers.GetValues("Image-Time"); if (imageTimeHeaderValues != null) { var imageTimeHeaderValue = imageTimeHeaderValues.FirstOrDefault(); if (!string.IsNullOrWhiteSpace(imageTimeHeaderValue)) { DateTime parsedTime; if (DateTime.TryParse(imageTimeHeaderValue, out parsedTime)) { ImageTime = parsedTime.ToUniversalTime(); } else { MainForm.Instance.WriteToLog("Failed to parse Image-Time header field."); } } } } catch (Exception) { MainForm.Instance.WriteToLog("Failed to set the image time."); } } if (headers.ContentType != null) { ContentType = headers.ContentType.MediaType; } }
/// <summary>Copies all headers from the source to the target.</summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> /// <param name="handleContentLength">true to handle content length.</param> /// <param name="handleContentEncoding">true to handle content encoding.</param> /// <param name="handleChangedValues">true to handle changed values.</param> public static void CopyTo( this HttpContentHeaders source, HttpContentHeaders target, bool handleContentLength = true, bool handleContentEncoding = true, bool handleChangedValues = false) { // Remove headers we are going to rewrite and headers with null values foreach (var header in source) { try { if (!handleContentLength && header.Key.Equals("Content-Length", StringComparison.OrdinalIgnoreCase)) { return; } if (!handleContentEncoding && header.Key.Equals("Content-Encoding", StringComparison.OrdinalIgnoreCase)) { return; } if (!handleChangedValues) { // If values have changed, dont update it if (target.Contains(header.Key)) { if (target.GetValues(header.Key).Any(targetValue => header.Value.Any(originalValue => originalValue != targetValue))) { return; } } } target.Add(header.Key, header.Value); } catch (Exception ex) { Debug.WriteLine(ex); } } }
/// <summary>Copies all headers from the source to the target.</summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> /// <param name="handleContentLength">true to handle content length.</param> /// <param name="handleChangedValues">true to handle changed values.</param> public static void CopyTo( this HttpContentHeaders source, HttpContentHeaders target, bool handleContentLength = true, bool handleChangedValues = false) { // Copy all other headers unless they have been modified and we want to preserve that foreach (var header in source) { try { if (!handleContentLength && header.Key.Equals("Content-Length", StringComparison.OrdinalIgnoreCase)) { continue; } if (!handleChangedValues) { // If values have changed, dont update it if (target.Contains(header.Key)) { if (target.GetValues(header.Key).Any(targetValue => header.Value.Any(originalValue => originalValue != targetValue))) { continue; } } } target.Add(header.Key, header.Value); } catch (Exception ex) { Debug.WriteLine(ex); } } }
public void ContentType_UseAddMethodWithInvalidValue_InvalidValueRecognized() { _headers.TryAddWithoutValidation("Content-Type", "text/plain; charset=utf-8; custom=value, other/type"); Assert.Null(_headers.ContentType); Assert.Equal(1, _headers.GetValues("Content-Type").Count()); Assert.Equal("text/plain; charset=utf-8; custom=value, other/type", _headers.GetValues("Content-Type").First()); _headers.Clear(); _headers.TryAddWithoutValidation("Content-Type", ",text/plain"); // leading separator Assert.Null(_headers.ContentType); Assert.Equal(1, _headers.GetValues("Content-Type").Count()); Assert.Equal(",text/plain", _headers.GetValues("Content-Type").First()); _headers.Clear(); _headers.TryAddWithoutValidation("Content-Type", "text/plain,"); // trailing separator Assert.Null(_headers.ContentType); Assert.Equal(1, _headers.GetValues("Content-Type").Count()); Assert.Equal("text/plain,", _headers.GetValues("Content-Type").First()); }
public static string Get(this HttpContentHeaders headers, string name) { return(headers.Contains(name) ? headers.GetValues(name).FirstOrDefault() : null); }