From 2953bf48d0e5945f408bd2063fb6f01934d23080 Mon Sep 17 00:00:00 2001 From: tqk2811 Date: Sat, 27 Jul 2024 16:18:43 +0700 Subject: [PATCH] fix parse http header --- .../Helpers/HeaderRequestParse.cs | 29 +++++++++++-------- .../Helpers/HeaderResponseParse.cs | 22 ++++++++------ 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/TqkLibrary.Proxy/Helpers/HeaderRequestParse.cs b/src/TqkLibrary.Proxy/Helpers/HeaderRequestParse.cs index 9a77dfd..6b0b438 100644 --- a/src/TqkLibrary.Proxy/Helpers/HeaderRequestParse.cs +++ b/src/TqkLibrary.Proxy/Helpers/HeaderRequestParse.cs @@ -1,4 +1,5 @@ -using System.Net.Http.Headers; +using System.Collections.Specialized; +using System.Net.Http.Headers; using System.Text.RegularExpressions; /* Unmerged change from project 'TqkLibrary.Proxy (net6.0)' @@ -61,30 +62,34 @@ public static HeaderRequestParse ParseRequest(IEnumerable lines) { HeaderRequestParse headerRequestParse = new HeaderRequestParse(lines.FirstOrDefault()!); - var dict = lines.Skip(1) + NameValueCollection nameValueCollection = new NameValueCollection(); + foreach (var line in lines + .Skip(1) .Select(x => x.Split(':')) - .Where(x => x.Length == 2) - .ToDictionary(k => k[0].Trim().ToLower(), v => v[1].Trim()); + .Where(x => x.Length == 2)) + { + nameValueCollection.Add(line[0].ToLower(), line[1]); + } - if (dict.TryGetValue("proxy-connection", out string? proxy_connection)) + if (nameValueCollection.TryGetValues("proxy-connection", out string[]? proxy_connection)) { - headerRequestParse.IsKeepAlive = "keep-alive".Equals(proxy_connection, StringComparison.OrdinalIgnoreCase); + headerRequestParse.IsKeepAlive = proxy_connection!.Any(x => x.Contains("keep-alive", StringComparison.OrdinalIgnoreCase)); } - if (dict.TryGetValue("host", out string? host)) + if (nameValueCollection.TryGetValues("host", out string[]? host)) { - headerRequestParse.Host = host; + headerRequestParse.Host = host!.First(); } - if (dict.TryGetValue("proxy-authorization", out string? Proxy_Authorization)) + if (nameValueCollection.TryGetValues("proxy-authorization", out string[]? Proxy_Authorization)) { //https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml - string[] split = Proxy_Authorization.Split(' '); + string[] split = Proxy_Authorization!.First().Split(' '); if (split.Length == 2) { headerRequestParse.ProxyAuthorization = new AuthenticationHeaderValue(split[0], split[1]); } } - if (dict.TryGetValue("content-length", out string? content_length) && - int.TryParse(content_length, out int int_cl)) + if (nameValueCollection.TryGetValues("content-length", out string[]? content_length) && + int.TryParse(content_length!.First(), out int int_cl)) { headerRequestParse.ContentLength = int_cl; } diff --git a/src/TqkLibrary.Proxy/Helpers/HeaderResponseParse.cs b/src/TqkLibrary.Proxy/Helpers/HeaderResponseParse.cs index 5aacdb1..19647ff 100644 --- a/src/TqkLibrary.Proxy/Helpers/HeaderResponseParse.cs +++ b/src/TqkLibrary.Proxy/Helpers/HeaderResponseParse.cs @@ -1,4 +1,5 @@ -using System.Net; +using System.Collections.Specialized; +using System.Net; using System.Text.RegularExpressions; /* Unmerged change from project 'TqkLibrary.Proxy (net6.0)' @@ -42,18 +43,21 @@ private HeaderResponseParse(string line) public static HeaderResponseParse ParseResponse(IEnumerable lines) { HeaderResponseParse responseStatusCode = new HeaderResponseParse(lines.FirstOrDefault()!); - - var dict = lines.Skip(1) + NameValueCollection nameValueCollection = new NameValueCollection(); + foreach (var line in lines + .Skip(1) .Select(x => x.Split(':')) - .Where(x => x.Length == 2) - .ToDictionary(k => k[0].Trim().ToLower(), v => v[1].Trim()); + .Where(x => x.Length == 2)) + { + nameValueCollection.Add(line[0].ToLower(), line[1]); + } - if (dict.TryGetValue("proxy-connection", out string? proxy_connection)) + if (nameValueCollection.TryGetValues("proxy-connection", out string[]? proxy_connection)) { - responseStatusCode.IsKeepAlive = "keep-alive".Equals(proxy_connection, StringComparison.OrdinalIgnoreCase); + responseStatusCode.IsKeepAlive = proxy_connection!.Any(x => x.Contains("keep-alive", StringComparison.OrdinalIgnoreCase)); } - if (dict.TryGetValue("content-length", out string? content_length) && - int.TryParse(content_length, out int int_cl)) + if (nameValueCollection.TryGetValues("content-length", out string[]? content_length) && + int.TryParse(content_length!.First(), out int int_cl)) { responseStatusCode.ContentLength = int_cl; }