Skip to content

Commit

Permalink
fix parse http header
Browse files Browse the repository at this point in the history
  • Loading branch information
tqk2811 committed Jul 27, 2024
1 parent d1daf47 commit 2953bf4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
29 changes: 17 additions & 12 deletions src/TqkLibrary.Proxy/Helpers/HeaderRequestParse.cs
Original file line number Diff line number Diff line change
@@ -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)'
Expand Down Expand Up @@ -61,30 +62,34 @@ public static HeaderRequestParse ParseRequest(IEnumerable<string> 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;
}
Expand Down
22 changes: 13 additions & 9 deletions src/TqkLibrary.Proxy/Helpers/HeaderResponseParse.cs
Original file line number Diff line number Diff line change
@@ -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)'
Expand Down Expand Up @@ -42,18 +43,21 @@ private HeaderResponseParse(string line)
public static HeaderResponseParse ParseResponse(IEnumerable<string> 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;
}
Expand Down

0 comments on commit 2953bf4

Please sign in to comment.