Skip to content

Commit

Permalink
opti: 统一 Header 为小写
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Apr 20, 2024
1 parent 333d8d8 commit 28721ec
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions CSharp-OpenBMCLAPI/Modules/HttpServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ public static async Task<FileAccessInfo> DownloadHash(HttpContext context, Clust
{
try
{
if (context.Request.Header.ContainsKey("Range"))
if (context.Request.Header.ContainsKey("range"))
{
(long from, long to) = ToRangeByte(context.Request.Header["Range"].Split("=").Last().Split("-"));
long length = (to - from);
context.Response.Header["Content-Length"] = length.ToString();
context.Response.Header["content-length"] = length.ToString();
using Stream file = cluster.storage.ReadFileStream(Utils.HashToFileName(hash));
context.Response.Header["Content-Range"] = $"{from}-{to}/{file.Length}";
context.Response.Header["content-range"] = $"{from}-{to}/{file.Length}";
file.Seek(from, SeekOrigin.Begin);
file.CopyTo(context.Response.Stream);
context.Response.StatusCode = 206;
Expand All @@ -112,7 +112,7 @@ public static async Task<FileAccessInfo> DownloadHash(HttpContext context, Clust
else
{
context.Response.StatusCode = 403;
context.Response.Header.Remove("Content-Length");
context.Response.Header.Remove("content-length");
await context.Response.WriteAsync($"Access to \"{context.Request.Path}\" has been blocked due to your request timeout or invalidity.");
}
LogAccess(context);
Expand All @@ -135,7 +135,7 @@ private static (long from, long to) ToRangeByte(string[]? rangeHeader)

public static async Task Api(HttpContext context, string query, Cluster cluster)
{
context.Response.Header.Set("Content-Type", "application/json");
context.Response.Header.Set("content-type", "application/json");
switch (query)
{
case "qps":
Expand Down
2 changes: 1 addition & 1 deletion CSharp-OpenBMCLAPI/Modules/WebServer/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static Header FromBytes(byte[][] headers)
{
continue;
}
tables.TryAdd(WebUtils.Decode(h[0]), WebUtils.Decode(h[1]));
tables.TryAdd(WebUtils.Decode(h[0]).ToLower(), WebUtils.Decode(h[1]).ToLower());
}
return new Header(tables);
}
Expand Down
2 changes: 1 addition & 1 deletion CSharp-OpenBMCLAPI/Modules/WebServer/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Request(Client client, byte[] data)
(Method, Path, Protocol) = (WebUtils.Decode(temp[0]), WebUtils.Decode(temp[1]), WebUtils.Decode(temp[2]));
Array.Copy(requestHeader, 1, requestHeader, 0, requestHeader.Length - 1);
Header = Header.FromBytes(requestHeader[1..]);
BodyLength = int.Parse(Header.Get("Content-Length", 0) + "");
BodyLength = int.Parse(Header.Get("content-length", 0) + "");
}

public async Task SkipContent()
Expand Down
4 changes: 2 additions & 2 deletions CSharp-OpenBMCLAPI/Modules/WebServer/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class Response

public async Task Call(Client client, Request request, bool needCopy = true)
{
if (!Header.ContainsKey("Content-Length")) Header.Set("Content-Length", Stream.Length);
Header.Set("Server", "CSharp-SaltWood");
if (!Header.ContainsKey("content-length")) Header.Set("content-length", Stream.Length);
Header.Set("server", "CSharp-SaltWood");
string responseHeader = $"HTTP/1.1 {StatusCode} {GetStatusMsg(StatusCode)}\r\n{Header}\r\n";
await client.Write(responseHeader.Encode());
Stream.Position = 0;
Expand Down

0 comments on commit 28721ec

Please sign in to comment.