FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NetCoreServer/source/NetCoreServer/HttpSession.cs at master · EthanAnro/NetCoreServer · GitHub
EthanAnro
NetCoreServer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NetCoreServer
/
source
/
NetCoreServer
/
HttpSession.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
236 lines (212 loc) · 9.61 KB
Breadcrumbs
NetCoreServer
/
source
/
NetCoreServer
/
HttpSession.cs
Copy path
File metadata and controls
236 lines (212 loc) · 9.61 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using
System
;
namespace
NetCoreServer
{
/// <summary>
/// HTTP session is used to receive/send HTTP requests/responses from the connected HTTP client.
/// </summary>
/// <remarks>Thread-safe.</remarks>
public
class
HttpSession
:
TcpSession
{
public
HttpSession
(
HttpServer
server
)
:
base
(
server
)
{
Cache
=
server
.
Cache
;
Request
=
new
HttpRequest
(
)
;
Response
=
new
HttpResponse
(
)
;
}
/// <summary>
/// Get the static content cache
/// </summary>
public
FileCache
Cache
{
get
;
}
/// <summary>
/// Get the HTTP request
/// </summary>
protected
HttpRequest
Request
{
get
;
}
/// <summary>
/// Get the HTTP response
/// </summary>
public
HttpResponse
Response
{
get
;
}
#region Send response / Send response body
/// <summary>
/// Send the current HTTP response (synchronous)
/// </summary>
/// <returns>Size of sent data</returns>
public
long
SendResponse
(
)
=>
SendResponse
(
Response
)
;
/// <summary>
/// Send the HTTP response (synchronous)
/// </summary>
/// <param name="response">HTTP response</param>
/// <returns>Size of sent data</returns>
public
long
SendResponse
(
HttpResponse
response
)
=>
Send
(
response
.
Cache
.
Data
,
response
.
Cache
.
Offset
,
response
.
Cache
.
Size
)
;
/// <summary>
/// Send the HTTP response body (synchronous)
/// </summary>
/// <param name="body">HTTP response body</param>
/// <returns>Size of sent data</returns>
public
long
SendResponseBody
(
string
body
)
=>
Send
(
body
)
;
/// <summary>
/// Send the HTTP response body (synchronous)
/// </summary>
/// <param name="body">HTTP response body as a span of characters</param>
/// <returns>Size of sent data</returns>
public
long
SendResponseBody
(
ReadOnlySpan
<
char
>
body
)
=>
Send
(
body
)
;
/// <summary>
/// Send the HTTP response body (synchronous)
/// </summary>
/// <param name="buffer">HTTP response body buffer</param>
/// <returns>Size of sent data</returns>
public
long
SendResponseBody
(
byte
[
]
buffer
)
=>
Send
(
buffer
)
;
/// <summary>
/// Send the HTTP response body (synchronous)
/// </summary>
/// <param name="buffer">HTTP response body buffer</param>
/// <param name="offset">HTTP response body buffer offset</param>
/// <param name="size">HTTP response body size</param>
/// <returns>Size of sent data</returns>
public
long
SendResponseBody
(
byte
[
]
buffer
,
long
offset
,
long
size
)
=>
Send
(
buffer
,
offset
,
size
)
;
/// <summary>
/// Send the HTTP response body (synchronous)
/// </summary>
/// <param name="buffer">HTTP response body buffer as a span of bytes</param>
/// <returns>Size of sent data</returns>
public
long
SendResponseBody
(
ReadOnlySpan
<
byte
>
buffer
)
=>
Send
(
buffer
)
;
/// <summary>
/// Send the current HTTP response (asynchronous)
/// </summary>
/// <returns>'true' if the current HTTP response was successfully sent, 'false' if the session is not connected</returns>
public
bool
SendResponseAsync
(
)
=>
SendResponseAsync
(
Response
)
;
/// <summary>
/// Send the HTTP response (asynchronous)
/// </summary>
/// <param name="response">HTTP response</param>
/// <returns>'true' if the current HTTP response was successfully sent, 'false' if the session is not connected</returns>
public
bool
SendResponseAsync
(
HttpResponse
response
)
=>
SendAsync
(
response
.
Cache
.
Data
,
response
.
Cache
.
Offset
,
response
.
Cache
.
Size
)
;
/// <summary>
/// Send the HTTP response body (asynchronous)
/// </summary>
/// <param name="body">HTTP response body</param>
/// <returns>'true' if the HTTP response body was successfully sent, 'false' if the session is not connected</returns>
public
bool
SendResponseBodyAsync
(
string
body
)
=>
SendAsync
(
body
)
;
/// <summary>
/// Send the HTTP response body (asynchronous)
/// </summary>
/// <param name="body">HTTP response body as a span of characters</param>
/// <returns>'true' if the HTTP response body was successfully sent, 'false' if the session is not connected</returns>
public
bool
SendResponseBodyAsync
(
ReadOnlySpan
<
char
>
body
)
=>
SendAsync
(
body
)
;
/// <summary>
/// Send the HTTP response body (asynchronous)
/// </summary>
/// <param name="buffer">HTTP response body buffer</param>
/// <returns>'true' if the HTTP response body was successfully sent, 'false' if the session is not connected</returns>
public
bool
SendResponseBodyAsync
(
byte
[
]
buffer
)
=>
SendAsync
(
buffer
)
;
/// <summary>
/// Send the HTTP response body (asynchronous)
/// </summary>
/// <param name="buffer">HTTP response body buffer</param>
/// <param name="offset">HTTP response body buffer offset</param>
/// <param name="size">HTTP response body size</param>
/// <returns>'true' if the HTTP response body was successfully sent, 'false' if the session is not connected</returns>
public
bool
SendResponseBodyAsync
(
byte
[
]
buffer
,
long
offset
,
long
size
)
=>
SendAsync
(
buffer
,
offset
,
size
)
;
/// <summary>
/// Send the HTTP response body (asynchronous)
/// </summary>
/// <param name="buffer">HTTP response body buffer as a span of bytes</param>
/// <returns>'true' if the HTTP response body was successfully sent, 'false' if the session is not connected</returns>
public
bool
SendResponseBodyAsync
(
ReadOnlySpan
<
byte
>
buffer
)
=>
SendAsync
(
buffer
)
;
#endregion
#region Session handlers
protected
override
void
OnReceived
(
byte
[
]
buffer
,
long
offset
,
long
size
)
{
// Receive HTTP request header
if
(
Request
.
IsPendingHeader
(
)
)
{
if
(
Request
.
ReceiveHeader
(
buffer
,
(
int
)
offset
,
(
int
)
size
)
)
OnReceivedRequestHeader
(
Request
)
;
size
=
0
;
}
// Check for HTTP request error
if
(
Request
.
IsErrorSet
)
{
OnReceivedRequestError
(
Request
,
"Invalid HTTP request!"
)
;
Request
.
Clear
(
)
;
Disconnect
(
)
;
return
;
}
// Receive HTTP request body
if
(
Request
.
ReceiveBody
(
buffer
,
(
int
)
offset
,
(
int
)
size
)
)
{
OnReceivedRequestInternal
(
Request
)
;
Request
.
Clear
(
)
;
return
;
}
// Check for HTTP request error
if
(
Request
.
IsErrorSet
)
{
OnReceivedRequestError
(
Request
,
"Invalid HTTP request!"
)
;
Request
.
Clear
(
)
;
Disconnect
(
)
;
return
;
}
}
protected
override
void
OnDisconnected
(
)
{
// Receive HTTP request body
if
(
Request
.
IsPendingBody
(
)
)
{
OnReceivedRequestInternal
(
Request
)
;
Request
.
Clear
(
)
;
return
;
}
}
/// <summary>
/// Handle HTTP request header received notification
/// </summary>
/// <remarks>Notification is called when HTTP request header was received from the client.</remarks>
/// <param name="request">HTTP request</param>
protected
virtual
void
OnReceivedRequestHeader
(
HttpRequest
request
)
{
}
/// <summary>
/// Handle HTTP request received notification
/// </summary>
/// <remarks>Notification is called when HTTP request was received from the client.</remarks>
/// <param name="request">HTTP request</param>
protected
virtual
void
OnReceivedRequest
(
HttpRequest
request
)
{
}
/// <summary>
/// Handle HTTP cached request received notification
/// </summary>
/// <remarks>
/// Notification is called when HTTP request was received
/// from the client and the corresponding cached content
/// was found.
///
/// Default behavior is just send cached response content
/// to the client.
/// </remarks>
/// <param name="request">HTTP request</param>
/// <param name="content">Cached response content</param>
protected
virtual
void
OnReceivedCachedRequest
(
HttpRequest
request
,
byte
[
]
content
)
{
SendAsync
(
content
)
;
}
/// <summary>
/// Handle HTTP request error notification
/// </summary>
/// <remarks>Notification is called when HTTP request error was received from the client.</remarks>
/// <param name="request">HTTP request</param>
/// <param name="error">HTTP request error</param>
protected
virtual
void
OnReceivedRequestError
(
HttpRequest
request
,
string
error
)
{
}
#endregion
private
void
OnReceivedRequestInternal
(
HttpRequest
request
)
{
// Try to get the cached response
if
(
request
.
Method
==
"GET"
)
{
var
index
=
request
.
Url
.
IndexOf
(
'?'
)
;
var
response
=
Cache
.
Find
(
(
index
<
0
)
?
request
.
Url
:
request
.
Url
.
Substring
(
0
,
index
)
)
;
if
(
response
.
Item1
)
{
// Process the request with the cached response
OnReceivedCachedRequest
(
request
,
response
.
Item2
)
;
return
;
}
}
// Process the request
OnReceivedRequest
(
request
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL