Ejemplo n.º 1
0
 void AddHeaders(HttpContentHeaders headers)
 {
     headers.Add("X-LC-ID", client.AppId);
     headers.Add("X-LC-KEY", client.AppKey);
     headers.Add("X-LC-PLAY-USER-ID", client.UserId);
     headers.ContentType = new MediaTypeHeaderValue(APPLICATION_JSON);
 }
Ejemplo n.º 2
0
 public static void SetHeaderValue(this HttpContentHeaders Headers)
 {
     Headers.Add("sobeyhive-http-system", "INGESTSERVER");
     Headers.Add("sobeyhive-http-site", "S1");
     Headers.Add("sobeyhive-http-tool", "INGESTSERVER");
     Headers.Add("sobeyhive-http-secret", RSAHelper.RSAstr());
     Headers.Add("current-user-code", "admin");
 }
Ejemplo n.º 3
0
 public override void SetDefaultContentHeaders(Type type,
                                               HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
 {
     base.SetDefaultContentHeaders(type, headers, mediaType);
     headers.Add("X-ModelType",
                 type == typeof(IEnumerable <Product>)
             ? "IEnumerable<Product>" : "Product");
     headers.Add("X-MediaType", mediaType.MediaType);
 }
Ejemplo n.º 4
0
        private void AddUploadHeaders(HttpContentHeaders headers, FileInfo file, string name, string md5Checksum)
        {
            headers.Add("Content-MD5", md5Checksum);
            headers.Add("Content-Length", file.Length.ToString());
            headers.ContentType = MediaTypeHeaderValue.Parse(GetMimeType(file.Extension));

            headers.Add("X-Smug-FileName", name);
            headers.Add("X-Smug-Title", name);
            headers.Add("X-Smug-Caption", name);
        }
Ejemplo n.º 5
0
 private void ReplaceHeaders(HttpContentHeaders content, IEnumerable <KeyValuePair <string, string[]> > headersToAdd)
 {
     foreach (var header in headersToAdd)
     {
         content.Remove(header.Key);
         if (header.Value.Count() > 1)
         {
             content.Add(header.Key, header.Value);
         }
         else
         {
             content.Add(header.Key, header.Value.First());
         }
     }
 }
Ejemplo n.º 6
0
 private static void CopyHeaders(HttpContentHeaders fromHeaders, HttpContentHeaders toHeaders)
 {
     foreach (KeyValuePair <string, IEnumerable <string> > header in fromHeaders)
     {
         toHeaders.Add(header.Key, header.Value);
     }
 }
Ejemplo n.º 7
0
        public void ContentLength_AddInvalidValueUsingUnusualCasing_ParserRetrievedUsingCaseInsensitiveComparison()
        {
            _headers = new HttpContentHeaders(() => { return(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"); });
        }
Ejemplo n.º 8
0
        public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
        {
            base.SetDefaultContentHeaders(type, headers, mediaType);
            var random = new Random();

            headers.Add("Content-Disposition", "attachment; filename=" + $"{random.Next(1, 10)}.csv");
        }
 /// <summary>
 /// Sets content SHA1 header.
 /// </summary>
 /// <param name="headers">The http content request header.</param>
 /// <param name="value">The header value.</param>
 public static void ContentSha1(this HttpContentHeaders headers, string value)
 {
     if (!string.IsNullOrWhiteSpace(value))
     {
         headers.Add("x-bz-content-sha1", value);
     }
 }
Ejemplo n.º 10
0
 public void CopyTo(HttpContentHeaders httpHeaders)
 {
     foreach (var(key, value) in this.Where(x => !x.Value.IsNull))
     {
         httpHeaders.Add(key, value.ToString());
     }
 }
 /// <summary>
 /// Sets content file name header.
 /// </summary>
 /// <param name="headers">The http content request header.</param>
 /// <param name="value">The header value.</param>
 public static void SetContentFileName(this HttpContentHeaders headers, string value)
 {
     if (!string.IsNullOrWhiteSpace(value))
     {
         headers.Add("x-bz-file-name", value.ToUrlEncode());
     }
 }
 /// <summary>
 /// Sets content dispositon header.
 /// </summary>
 /// <param name="headers">The http content request header.</param>
 /// <param name="value">The header value.</param>
 public static void SetContentDisposition(this HttpContentHeaders headers, ContentDispositionHeaderValue value)
 {
     if (value != null)
     {
         headers.Add("b2ContentDisposition", value.ToString());
     }
 }
Ejemplo n.º 13
0
 public static void AddHeaders(this HttpContentHeaders headers, HttpContentHeaders sourceHeaders)
 {
     foreach (KeyValuePair <string, IEnumerable <string> > header in sourceHeaders)
     {
         headers.Add(header.Key, header.Value);
     }
 }
Ejemplo n.º 14
0
        protected void PopulateHeaders(HttpContentHeaders contentHeaders, HttpHeaders generalHeaders)
        {
            string hdrKey;

            foreach (var hdr in this.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.
            }
        }
 public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
 {
     base.SetDefaultContentHeaders(type, headers, mediaType);
     if (!string.IsNullOrEmpty(Filename))
     {
         headers.Add("Content-Disposition", string.Format("attachment; filename={0}", Filename));
     }
 }
Ejemplo n.º 16
0
 public static void Set(this HttpContentHeaders headers, string name, string value)
 {
     if (headers.Contains(name))
     {
         headers.Remove(name);
     }
     headers.Add(name, value);
 }
 private static void AddOrUpdateHeader(this HttpContentHeaders headers, string headerKey, string headerValue)
 {
     lock (headers)
     {
         headers.Remove(headerKey);
         headers.Add(headerKey, headerValue);
     }
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Replaces the headers.
 /// </summary>
 /// <param name="currentHeaders">The current headers.</param>
 /// <param name="oldHeaders">The old headers.</param>
 private void ReplaceHeaders(HttpContentHeaders currentHeaders, HttpContentHeaders oldHeaders)
 {
     currentHeaders.Clear();
     foreach (var item in oldHeaders)
     {
         currentHeaders.Add(item.Key, item.Value);
     }
 }
Ejemplo n.º 19
0
 public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
 {
     headers.Add("content-disposition", (new ContentDispositionHeaderValue("attachment")
     {
         FileName = "myfile.ics"
     }).ToString());
     base.SetDefaultContentHeaders(type, headers, mediaType);
 }
Ejemplo n.º 20
0
        public static void CopyTo(this HttpContentHeaders fromHeaders, HttpContentHeaders toHeaders)
        {
            Fx.Assert(fromHeaders != null, "fromHeaders cannot be null.");
            Fx.Assert(toHeaders != null, "toHeaders cannot be null.");

            foreach (KeyValuePair <string, IEnumerable <string> > header in fromHeaders)
            {
                toHeaders.Add(header.Key, header.Value);
            }
        }
        public void CopyTo_CopiesContentHeaders()
        {
            // Arrange
            HttpContentHeaders source = FormattingUtilities.CreateEmptyContentHeaders();

            source.ContentType     = MediaTypeHeaderValue.Parse("application/json; charset=utf8; parameter=value");
            source.ContentLength   = 1234;
            source.ContentLocation = new Uri("http://some.host");
            source.Add("test-name1", "test-value1");
            source.Add("test-name2", "test-value2");

            HttpContentHeaders destination = FormattingUtilities.CreateEmptyContentHeaders();

            // Act
            source.CopyTo(destination);

            // Assert
            Assert.Equal(source.ToString(), destination.ToString());
        }
 private static void CopyHeaders(Windows.Web.Http.Headers.HttpContentHeaderCollection input, HttpContentHeaders output)
 {
     foreach (var header in input)
     {
         if (!string.Equals(header.Key, "Expires", StringComparison.OrdinalIgnoreCase) || header.Value != "-1")
         {
             output.Add(header.Key, header.Value);
         }
     }
 }
Ejemplo n.º 23
0
        /// <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);
                }
            }
        }
Ejemplo n.º 24
0
            private static void CopyContentHeaders(HttpContentHeaders fromHeaders, HttpContentHeaders toHeaders)
            {
                if (fromHeaders == null || toHeaders == null)
                {
                    return;
                }

                foreach (KeyValuePair <string, IEnumerable <string> > kvPair in fromHeaders)
                {
                    // Apparently you cannot use the multi-value Add() method to add single values.
                    int valueCount = kvPair.Value.Count();
                    if (valueCount == 1)
                    {
                        toHeaders.Add(kvPair.Key, kvPair.Value.First());
                    }
                    else if (valueCount > 1)
                    {
                        toHeaders.Add(kvPair.Key, kvPair.Value);
                    }
                }
            }
        private static void SetContentHeader(this HttpContentHeaders headers, PSDataStreams invokedStatus)
        {
            if (invokedStatus == null || headers == null)
            {
                return;
            }

            if (invokedStatus.Warning != null && invokedStatus.Warning.Count > 0)
            {
                headers.Add("Ps-Warning", invokedStatus.Warning.Select(w => w.ToString().EscapeNewLine()));
            }

            if (invokedStatus.Verbose != null && invokedStatus.Verbose.Count > 0)
            {
                headers.Add("Ps-Verbose", invokedStatus.Verbose.Select(v => v.ToString().EscapeNewLine()));
            }

            if (invokedStatus.Debug != null && invokedStatus.Debug.Count > 0)
            {
                headers.Add("Ps-Debug", invokedStatus.Debug.Select(d => d.ToString().EscapeNewLine()));
            }
        }
        public static void Set(this HttpContentHeaders headers, string name, IEnumerable <string> values)
        {
            if (headers == null)
            {
                throw new ArgumentNullException(nameof(headers));
            }

            if (headers.Contains(name))
            {
                headers.Remove(name);
            }

            headers.Add(name, values);
        }
 internal static void CopyTo(this HttpContentHeaders source, HttpContentHeaders destination)
 {
     foreach (var header in source)
     {
         if (header.Key == "Expires")
         {
             if (header.Value.Count() > 0 && !DateTime.TryParse(header.Value.FirstOrDefault(), out DateTime result))
             {
                 continue;
             }
         }
         destination.Add(header.Key, header.Value);
     }
 }
Ejemplo n.º 28
0
        /// <summary>
        /// Add a name value pair to the headers. If the name already exists then the
        /// value will be updated.
        /// </summary>
        /// <param name="source">Headers collection to perform the operation on.</param>
        /// <param name="name">The header to add or update in the collection.</param>
        /// <param name="value">The content of the header.</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception>
        public static void AddOrUpdate(this HttpContentHeaders source, string name, string value)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (source.Contains(name))
            {
                source.Remove(name);
            }

            source.Add(name, value);
        }
        /// <summary>
        /// Extension method to easily fill HttpContentHeaders with the Request object
        /// </summary>
        /// <param name="headers"></param>
        /// <param name="request"></param>
        internal static HttpContentHeaders Fill(this HttpContentHeaders headers, Request request)
        {
            if (request.Body != null)
            {
                headers.Add(Defaults.ContentType, Defaults.ApplicationJson);

                if (request.CanCompress)
                {
                    headers.ContentEncoding.Add(Defaults.GzipEncoding);
                }
            }

            return(headers);
        }
Ejemplo n.º 30
0
 public static void CopyHeadersTo(this IHeaderDictionary headerDictionary, HttpHeaders httpHeaders,
                                  HttpContentHeaders contentHeaders)
 {
     foreach (var requestHeader in headerDictionary)
     {
         if (HeadersHelper.IsContentHeader(requestHeader.Key))
         {
             contentHeaders.Add(requestHeader.Key, (IEnumerable <string>)requestHeader.Value);
         }
         else
         {
             httpHeaders.Add(requestHeader.Key, (IEnumerable <string>)requestHeader.Value);
         }
     }
 }