http: nitpicks

This commit is contained in:
uwaa 2024-12-15 08:01:50 +00:00
parent ce8ccdae1b
commit a23a7b8516
3 changed files with 9 additions and 16 deletions

View file

@ -24,7 +24,7 @@ public struct HttpContent
public byte[] Content;
/// <summary>
/// Converts the contents to a UTF8 string.
/// Converts the contents to a UTF-8 string.
/// </summary>
public string AsText => Encoding.UTF8.GetString(Content);

View file

@ -32,7 +32,7 @@ public record HttpFields
/// <summary>
/// Extra fields to include.
/// </summary>
public Dictionary<string, string>? Misc;
public readonly Dictionary<string, string> Misc = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Sets a field. The string will be parsed for non-string fields like Accept.
@ -107,14 +107,9 @@ public record HttpFields
default:
if (value == null)
{
Misc?.Remove(key);
}
else
{
Misc ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Misc[key] = value;
}
return;
}
}

View file

@ -80,14 +80,6 @@ public class HttpResponse
StringBuilder sb = new StringBuilder();
void writeField(string name, string value)
{
sb.Append(name);
sb.Append(": ");
sb.Append(value);
sb.Append("\r\n");
}
sb.Append("HTTP/1.1 ");
sb.Append(StatusCode);
sb.Append(' ');
@ -105,7 +97,13 @@ public class HttpResponse
Fields.ContentType = null;
}
Fields.EmitAll(writeField);
Fields.EmitAll((string name, string value) =>
{
sb.Append(name);
sb.Append(": ");
sb.Append(value);
sb.Append("\r\n");
});
sb.Append("\r\n");
await stream.Write(Encoding.ASCII.GetBytes(sb.ToString()));