Skip to content

Commit

Permalink
🐛 防止 添加 cookie 报错
Browse files Browse the repository at this point in the history
  • Loading branch information
Mossimos committed Jun 29, 2024
1 parent f10df31 commit df27068
Showing 1 changed file with 18 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net;
using MediaTypeHeaderValue = System.Net.Http.Headers.MediaTypeHeaderValue;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -140,10 +141,6 @@ async Task HandleHttpRequestAsync(HttpContext context)
return null;
}

StringValues cookie = context.Request.Headers["cookie-steamtool"];
if (StringValues.IsNullOrEmpty(cookie))
cookie = context.Request.Headers["Cookie"];
StringValues referer = context.Request.Headers["Referer-steamtool"];
context.Response.Headers.AccessControlAllowOrigin = context.Request.Headers.Origin.Count == 0 ? "*" : context.Request.Headers.Origin;
context.Response.Headers.AccessControlAllowHeaders = "*";
context.Response.Headers.AccessControlAllowMethods = "*";
Expand All @@ -167,35 +164,28 @@ async Task SendAsync()
};
foreach (var item in context.Request.Headers)
{

if (item.Key.ToLower().EndsWith("-steamtool"))
var headers = item.Key.ToLower();
if (headers.EndsWith("-steamtool"))
{

if (headers.Equals("cookie", StringComparison.OrdinalIgnoreCase))
{
CookieHttpClient.CookieContainer.Add(new Cookie
{
CommentUri = requestUri,
Domain = requestUri.Host,
Value = item.Value
});
}
if (headers.Equals("referer", StringComparison.OrdinalIgnoreCase))
{
var refererUri = new Uri(item.Value.ToString());
req.Headers.Referrer = refererUri;
}
req.Headers.TryAddWithoutValidation(item.Key.TrimEnd("-steamtool"), (IEnumerable<string?>)item.Value);
}
}
req.Headers.UserAgent.ParseAdd(context.Request.Headers.UserAgent);
if (!StringValues.IsNullOrEmpty(cookie))
{
CookieHttpClient.CookieContainer.Add(new Cookie
{
CommentUri = requestUri,
Domain = requestUri.Host,
Value = cookie,
});
// req.Headers.Add("Cookie", cookie);
}
if (!StringValues.IsNullOrEmpty(referer))
{
try
{
var refererUri = new Uri(referer.ToString());
req.Headers.Referrer = refererUri;
}
catch
{

}
}
if (hasReqContent)
{
req.Content!.Headers.ContentType = MediaTypeHeaderValue.Parse(context.Request.ContentType.ThrowIsNull());
Expand Down

0 comments on commit df27068

Please sign in to comment.