Ejemplo n.º 1
0
        internal string GetContentType()
        {
            if (ContentHeaders.TryGetValue("Content-Type", out var contentTypes))
            {
                var contentType = contentTypes.First();
                if (contentType.Contains("json"))
                {
                    return(WebContentType.Json);
                }

                if (contentType.Contains("xml"))
                {
                    return(WebContentType.Xml);
                }

                if (contentType.Contains("html"))
                {
                    return(WebContentType.Html);
                }

                if (contentType.Contains("octet"))
                {
                    return(WebContentType.Octet);
                }
            }

            return(WebContentType.Text);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Removes a specific request header from the header collection
 /// </summary>
 /// <param name="name"></param>
 public void RemoveContentHeader(string name)
 {
     if (string.IsNullOrWhiteSpace(name))
     {
         throw new ArgumentNullException(nameof(name));
     }
     if (ContentHeaders.ContainsKey(name))
     {
         ContentHeaders.Remove(name);
     }
 }
Ejemplo n.º 3
0
 public void SetContentHeader(string name, string value)
 {
     ContentHeaders ??= new();
     if (ContentHeaders.ContainsKey(name))
     {
         ContentHeaders[name] = value;
     }
     else
     {
         ContentHeaders.Add(name, value);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds a specific request header to the Request Header Collection
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void AddContentHeader(string name, string value)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentNullException($"the value of {name}");
            }

            ContentHeaders.Add(name, value);
        }
Ejemplo n.º 5
0
        public override string ToString()
        {
            string contentEncoding = string.Empty;
            string debugString     = string.Empty;

            if (ContentHeaders.ContainsKey(MIMEContentHeaders.ContentTransferEncoding) && InnerBody != null)
            {
                contentEncoding = ContentHeaders[MIMEContentHeaders.ContentTransferEncoding].Value;
            }

            byte[] readableBinaries = GetBytes();
            switch (contentEncoding)
            {
            case MIMEContentTransferEncoding.Binary:
                int payLoadLength = 0;
                payLoadLength = InnerBody.Length;
                byte[] headers = new byte[readableBinaries.Length - payLoadLength];
                Buffer.BlockCopy(readableBinaries, 0, headers, 0, headers.Length);

                if (InnerBody != null && InnerMessage == null)
                {
                    if (ContentHeaders.ContainsKey(MIMEContentHeaders.BridgingOffsets))
                    {
                        debugString = Encoding.UTF8.GetString(headers) + "\r\nMulti-Package Binary Data: {Length: " + payLoadLength + "}";
                    }
                    else
                    {
                        debugString = Encoding.UTF8.GetString(headers) + "\r\nUnknown Binary Data: {Length: " + payLoadLength + "}";
                    }
                }



                if (InnerBody != null && InnerMessage != null)
                {
                    debugString = Encoding.UTF8.GetString(headers) + "\r\n" + InnerMessage.ToString();
                }
                break;

            default:
                debugString = Encoding.UTF8.GetString(readableBinaries);
                break;
            }
            return(debugString);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Clears all internal data structures which contain headers
 /// </summary>
 public void ClearHeaders()
 {
     ContentHeaders.Clear();
     RequestHeaders.Clear();
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Clears the content headers
 /// </summary>
 public void ClearContentHeaders()
 {
     ContentHeaders.Clear();
 }
Ejemplo n.º 8
0
        public HttpRequestMessage ToHttpResponseMessage()
        {
            var result = new HttpRequestMessage()
            {
                Version       = Version,
                VersionPolicy = VersionPolicy,
                Method        = Method,
                RequestUri    = RequestUri,
            };

            var memoryStream  = new MemoryStream(Convert.FromBase64String(ContentBase64));
            var text          = Encoding.UTF8.GetString(memoryStream.ToArray());
            var streamContent = new StreamContent(memoryStream);

            result.Content = streamContent;


            var headerContentList = ContentHeaders.ToObject <List <HeaderContent> >();

            if (headerContentList != null)
            {
                foreach (var headerContent in  headerContentList)
                {
                    result.Content.Headers.Add(headerContent.Key, headerContent.Value);
                }
            }

            //if (ContentHeaders is JArray jArray)
            //{
            //    foreach (var jToken in jArray)
            //    {
            //        if (jToken is JObject jObject)
            //        {
            //            string key = null;

            //            if (jObject.First is JProperty keyProperty)
            //            {
            //                if (keyProperty.Name=="Key")
            //                {
            //                    key = keyProperty.Value.ToString();
            //                }
            //            }

            //            if (jObject.Last is JProperty valueProperty)
            //            {

            //            }
            //        }
            //    }
            //}

            headerContentList = Headers.ToObject <List <HeaderContent> >();
            if (headerContentList != null)
            {
                foreach (var headerContent in headerContentList)
                {
                    result.Headers.Add(headerContent.Key, headerContent.Value);
                }
            }


            return(result);
        }