public void ContentLength_AddInvalidValueUsingUnusualCasing_ParserRetrievedUsingCaseInsensitiveComparison() { _headers = new HttpContentHeaders(new ComputeLengthHttpContent(() => 15)); // Use uppercase header name to make sure the parser gets retrieved using case-insensitive comparison. Assert.Throws<FormatException>(() => { _headers.Add("CoNtEnT-LeNgTh", "this is invalid"); }); }
private static void CopyHeaders(HttpContentHeaders fromHeaders, HttpContentHeaders toHeaders) { foreach (KeyValuePair<string, IEnumerable<string>> header in fromHeaders) { toHeaders.Add(header.Key, header.Value); } }
/// <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); } } }
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) { base.SetDefaultContentHeaders(type, headers, mediaType); string name = HttpContext.Current.Request.Url.Segments[HttpContext.Current.Request.Url.Segments.Length-1]; headers.Add("Content-Disposition", "attachment; filename=" + name + ".csv"); }
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) { base.SetDefaultContentHeaders(type, headers, mediaType); headers.Add("Content-Disposition", "attachment; filename=" + Filename); }
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) { base.SetDefaultContentHeaders(type, headers, mediaType); headers.Add("Content-Disposition", string.Format("attachment; filename={0}", CsvFileName)); }
/// <summary>Sets the default content headers.</summary> /// <param name="type">The <see cref="Type"/>.</param> /// <param name="headers">The <see cref="HttpContentHeaders"/>.</param> /// <param name="mediaType">The <see cref="MediaTypeHeaderValue"/>.</param> public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) { headers.Add("Content-Disposition", "attachment; filename=\"" + type.Name + ".csv\""); base.SetDefaultContentHeaders(type, headers, mediaType); }
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) { headers.Add(RestfulServiceConstants.RqModelTypeHeaderKey, EncryptionManager.Encrypt(type.AssemblyQualifiedName)); base.SetDefaultContentHeaders(type, headers, mediaType); }
public static void PopulateHeaders(this HttpPacket packet, HttpContentHeaders contentHeaders, HttpHeaders generalHeaders) { if (packet == null) throw new ArgumentNullException("packet"); string hdrKey; foreach (var hdr in packet.Headers) { if (hdr.Key == null) continue; hdrKey = hdr.Key.Trim().ToUpperInvariant(); if (hdrKey == "CONTENT-LENGTH") continue; //Content Length is automaitically calculated if (Array.IndexOf<String>(contentOnlyHeaders, hdrKey) >= 0) { //TODO: Confirm if HttpResponseMessage/HttpRequestMessage will break headers into "," commas whereas in actuality header in Packet is an entire header contentHeaders.Add(hdr.Key.Trim(), hdr.Value); } else { generalHeaders.Add(hdr.Key.Trim(), hdr.Value); } //TODO: Check if a string can be parsed properly into the typed header //Test adding multiple headers of the same name will do. // Look up the Add overload that takes an ienumerable<string> to figure out its purpose. } }
/// <summary> /// Populates contentheaders and generalheaders with headers from the <see cref="HttpPacket"/>> /// </summary> /// <param name="packet"></param> /// <param name="contentHeaders"></param> /// <param name="generalHeaders"></param> public static void PopulateHeaders(this HttpPacket packet, HttpContentHeaders contentHeaders, HttpHeaders generalHeaders) { if (packet == null) throw new ArgumentNullException("packet"); bool dateHeaderProcessed = false; string hdrKey; foreach (var hdr in packet.Headers) { if (hdr.Key == null) continue; hdrKey = hdr.Key.Trim().ToUpperInvariant(); if (hdrKey == "CONTENT-LENGTH") { continue; //Content Length is automatically calculated by System.Net.Http.ByteArrayContent } else if (hdrKey == "DATE") { if (dateHeaderProcessed) continue; //Already Processed dateHeaderProcessed = true; //Date Header in wrong format causes exception in System.Net.Http.HttpResponseMessage/HttpRequestMessage //TODO: Confirm that this exception still occurs in the newer Nuget version of System.Net.Http //Check if the date string is in RFC 1123 format var val = (hdr.Value == null || !hdr.Value.Any()) ? null : hdr.Value.First().Trim(); if(val != null && Common.Shared.IsValidHttpDate(val)) { generalHeaders.Add("Date", val); } continue; } if (Array.IndexOf<String>(contentOnlyHeaders, hdrKey) >= 0) { contentHeaders.Add(hdr.Key.Trim(), hdr.Value); } else { generalHeaders.Add(hdr.Key.Trim(), hdr.Value); } } }