-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.cs
More file actions
105 lines (82 loc) · 3.79 KB
/
Copy pathHttpRequest.cs
File metadata and controls
105 lines (82 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System.IO.Pipelines;
using Shiny.Net.HttpServer.Http1;
namespace Shiny.Net.HttpServer;
/// <summary>
/// The inbound side of a request. Mirrors ASP.NET Core's <c>HttpRequest</c> closely enough that
/// handler code reads the same, including raw <see cref="Body"/> stream access for streaming
/// uploads and a <see cref="BodyReader"/> for pipeline-style reads.
/// </summary>
public sealed class HttpRequest
{
Stream? body;
internal HttpRequest(HttpContext context) => this.HttpContext = context;
public HttpContext HttpContext { get; }
/// <summary>The HTTP method, interned to a <see cref="HttpMethods"/> constant when known.</summary>
public string Method { get; internal set; } = HttpMethods.Get;
/// <summary>"http" or "https". Reflects the transport unless forwarded headers are honoured.</summary>
public string Scheme { get; internal set; } = "http";
public bool IsHttps => string.Equals(this.Scheme, "https", StringComparison.OrdinalIgnoreCase);
/// <summary>The Host header value, including port when present.</summary>
public string? Host { get; internal set; }
/// <summary>The request protocol, e.g. "HTTP/1.1".</summary>
public string Protocol { get; internal set; } = HttpProtocols.Http11;
/// <summary>The decoded, absolute path. Always starts with '/'.</summary>
public string Path { get; internal set; } = "/";
/// <summary>The raw query string including the leading '?', or null when absent.</summary>
public string? QueryString { get; internal set; }
/// <summary>The raw request target exactly as it appeared on the request line.</summary>
public string RawTarget { get; internal set; } = "/";
public HeaderDictionary Headers { get; } = new();
public QueryCollection Query { get; } = new();
public RequestCookieCollection Cookies { get; } = new();
/// <summary>Route parameters captured by the router. Empty when no route matched.</summary>
public RouteValueDictionary RouteValues { get; } = new();
public string? ContentType
{
get => this.Headers.ContentType;
set => this.Headers.ContentType = value;
}
/// <summary>
/// Declared body length. Null for chunked bodies (where the length is unknown up front)
/// and for bodies that are absent entirely.
/// </summary>
public long? ContentLength => this.Headers.ContentLength;
/// <summary>True when the request used chunked transfer encoding.</summary>
public bool IsChunked { get; internal set; }
/// <summary>
/// The request body as a stream. Read-only and forward-only — suitable for streaming a large
/// upload straight to disk without buffering. Never null; an empty stream when there is no body.
/// </summary>
public Stream Body
{
get => this.body ??= EmptyReadStream.Instance;
internal set => this.body = value;
}
/// <summary>
/// The body as a <see cref="PipeReader"/>, for zero-copy parsing of large payloads.
/// </summary>
public PipeReader BodyReader => this.bodyReader ??= PipeReader.Create(
this.Body,
new StreamPipeReaderOptions(leaveOpen: true)
);
PipeReader? bodyReader;
/// <summary>True when a body is present (either a positive Content-Length or chunked).</summary>
public bool HasBody => this.IsChunked || this.ContentLength > 0;
internal void Reset()
{
this.Method = HttpMethods.Get;
this.Scheme = "http";
this.Host = null;
this.Protocol = HttpProtocols.Http11;
this.Path = "/";
this.QueryString = null;
this.RawTarget = "/";
this.IsChunked = false;
this.body = null;
this.bodyReader = null;
this.Headers.Reset();
this.Query.Reset();
this.Cookies.Reset();
this.RouteValues.Reset();
}
}