Compare commits

...

2 commits

Author SHA1 Message Date
uwaa
8932c05860 http: add referrer 2024-12-15 08:02:08 +00:00
uwaa
a23a7b8516 http: nitpicks 2024-12-15 08:01:50 +00:00
3 changed files with 19 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

@ -25,6 +25,8 @@ public record HttpFields
public string? Location { get; set; }
public string? Referrer { get; set; }
public int? ContentLength { get; set; }
public MIMEType? ContentType { get; set; }
@ -32,7 +34,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.
@ -86,6 +88,11 @@ public record HttpFields
Location = value;
return;
case "referer":
case "referrer":
Referrer = value;
return;
case "content-length":
{
if (value == null)
@ -107,14 +114,9 @@ public record HttpFields
default:
if (value == null)
{
Misc?.Remove(key);
}
else
{
Misc ??= new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Misc[key] = value;
}
return;
}
}
@ -158,6 +160,9 @@ public record HttpFields
if (Location != null)
callback("Location", Location);
if (Referrer != null)
callback("Referer", Referrer);
if (WebSocketKey != null)
callback("Sec-WebSocket-Key", WebSocketKey);

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()));