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; public byte[] Content;
/// <summary> /// <summary>
/// Converts the contents to a UTF8 string. /// Converts the contents to a UTF-8 string.
/// </summary> /// </summary>
public string AsText => Encoding.UTF8.GetString(Content); public string AsText => Encoding.UTF8.GetString(Content);

View file

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

View file

@ -80,14 +80,6 @@ public class HttpResponse
StringBuilder sb = new StringBuilder(); 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("HTTP/1.1 ");
sb.Append(StatusCode); sb.Append(StatusCode);
sb.Append(' '); sb.Append(' ');
@ -105,7 +97,13 @@ public class HttpResponse
Fields.ContentType = null; 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"); sb.Append("\r\n");
await stream.Write(Encoding.ASCII.GetBytes(sb.ToString())); await stream.Write(Encoding.ASCII.GetBytes(sb.ToString()));