Пример #1
0
        public static Tuple<long, long, long> ExtractContentRange(HttpResponse response)
        {
            // example Content-Range: bytes 595883-1191764/1191765

            if (response.Headers.ContainsKey("Content-Range"))
            {
                var line = String.Format("{0}:{1}", "Content-Range", response.Headers["Content-Range"]);

                var result = ExtractContentRange(line);
                return result;
            }

            return null;
        }
Пример #2
0
        public static HttpResponse Parse(byte[] headerBytes)
        {
            if (headerBytes == null)
            {
                return null;
            }

            if (headerBytes.Length == 0)
            {
                return null;
            }

            string headerStr = Encoding.Default.GetString(headerBytes);

            string[] headersArray = headerStr.Split(new string[]{"\r\n"},StringSplitOptions.None);
            List<string> headers=new List<string>();

            foreach (string header in headersArray)
            {
                if (!string.IsNullOrEmpty(header))
                {
                    headers.Add(header);
                }
            }

            Dictionary<string,string> headersDic=new Dictionary<string, string>();

            for (int i = 1; i < headers.Count; i++)
            {
                string[] h = headers[i].Split(new string[] {":"}, StringSplitOptions.None);
                string key = h[0].Trim();
                string value = h[1].Trim();

                try
                {
                    // in case of duplicate key for example

                    headersDic.Add(key, value);
                }
                catch (ArgumentException ex)
                {
                }
            }

            string[] status = headers[0].Split(new string[] {" "}, StringSplitOptions.None);
            string httpVersion = status[0];
            string statusCode = status[1];
            string statusMessage = status[2];

            HttpResponse httpResponse=new HttpResponse();
            httpResponse.HttpVersion = httpVersion;
            httpResponse.Headers = headersDic;
            httpResponse.StatusCode = statusCode;
            httpResponse.StatusMessage = statusMessage;

            return httpResponse;
        }