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

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

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