FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NetCoreServer/examples/HttpServer/Program.cs at master · stratdev3/NetCoreServer · GitHub
stratdev3
NetCoreServer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NetCoreServer
/
examples
/
HttpServer
/
Program.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
205 lines (175 loc) · 7.15 KB
Breadcrumbs
NetCoreServer
/
examples
/
HttpServer
/
Program.cs
Copy path
File metadata and controls
205 lines (175 loc) · 7.15 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
using
System
;
using
System
.
Collections
.
Concurrent
;
using
System
.
Net
;
using
System
.
Net
.
Sockets
;
using
System
.
Text
;
using
NetCoreServer
;
namespace
HttpServer
{
class
CommonCache
{
public
static
CommonCache
GetInstance
(
)
{
if
(
_instance
==
null
)
_instance
=
new
CommonCache
(
)
;
return
_instance
;
}
public
string
GetAllCache
(
)
{
var
result
=
new
StringBuilder
(
)
;
result
.
Append
(
"[
\n
"
)
;
foreach
(
var
item
in
_cache
)
{
result
.
Append
(
" {
\n
"
)
;
result
.
AppendFormat
(
$
"
\"
key
\"
:
\"
{
item
.
Key
}
\"
,
\n
"
)
;
result
.
AppendFormat
(
$
"
\"
value
\"
:
\"
{
item
.
Value
}
\"
,
\n
"
)
;
result
.
Append
(
" },
\n
"
)
;
}
result
.
Append
(
"]
\n
"
)
;
return
result
.
ToString
(
)
;
}
public
bool
GetCacheValue
(
string
key
,
out
string
value
)
{
return
_cache
.
TryGetValue
(
key
,
out
value
)
;
}
public
void
PutCacheValue
(
string
key
,
string
value
)
{
_cache
[
key
]
=
value
;
}
public
bool
DeleteCacheValue
(
string
key
,
out
string
value
)
{
return
_cache
.
TryRemove
(
key
,
out
value
)
;
}
private
readonly
ConcurrentDictionary
<
string
,
string
>
_cache
=
new
ConcurrentDictionary
<
string
,
string
>
(
)
;
private
static
CommonCache
_instance
;
}
class
HttpCacheSession
:
HttpSession
{
public
HttpCacheSession
(
NetCoreServer
.
HttpServer
server
)
:
base
(
server
)
{
}
protected
override
void
OnReceivedRequest
(
HttpRequest
request
)
{
// Show HTTP request content
Console
.
WriteLine
(
request
)
;
// Process HTTP request methods
if
(
request
.
Method
==
"HEAD"
)
SendResponseAsync
(
Response
.
MakeHeadResponse
(
)
)
;
else
if
(
request
.
Method
==
"GET"
)
{
string
key
=
request
.
Url
;
// Decode the key value
key
=
Uri
.
UnescapeDataString
(
key
)
;
key
=
key
.
Replace
(
"/api/cache"
,
""
,
StringComparison
.
InvariantCultureIgnoreCase
)
;
key
=
key
.
Replace
(
"?key="
,
""
,
StringComparison
.
InvariantCultureIgnoreCase
)
;
if
(
string
.
IsNullOrEmpty
(
key
)
)
{
// Response with all cache values
SendResponseAsync
(
Response
.
MakeGetResponse
(
CommonCache
.
GetInstance
(
)
.
GetAllCache
(
)
,
"application/json; charset=UTF-8"
)
)
;
}
// Get the cache value by the given key
else
if
(
CommonCache
.
GetInstance
(
)
.
GetCacheValue
(
key
,
out
var
value
)
)
{
// Response with the cache value
SendResponseAsync
(
Response
.
MakeGetResponse
(
value
)
)
;
}
else
SendResponseAsync
(
Response
.
MakeErrorResponse
(
404
,
"Required cache value was not found for the key: "
+
key
)
)
;
}
else
if
(
(
request
.
Method
==
"POST"
)
||
(
request
.
Method
==
"PUT"
)
)
{
string
key
=
request
.
Url
;
string
value
=
request
.
Body
;
// Decode the key value
key
=
Uri
.
UnescapeDataString
(
key
)
;
key
=
key
.
Replace
(
"/api/cache"
,
""
,
StringComparison
.
InvariantCultureIgnoreCase
)
;
key
=
key
.
Replace
(
"?key="
,
""
,
StringComparison
.
InvariantCultureIgnoreCase
)
;
// Put the cache value
CommonCache
.
GetInstance
(
)
.
PutCacheValue
(
key
,
value
)
;
// Response with the cache value
SendResponseAsync
(
Response
.
MakeOkResponse
(
)
)
;
}
else
if
(
request
.
Method
==
"DELETE"
)
{
string
key
=
request
.
Url
;
// Decode the key value
key
=
Uri
.
UnescapeDataString
(
key
)
;
key
=
key
.
Replace
(
"/api/cache"
,
""
,
StringComparison
.
InvariantCultureIgnoreCase
)
;
key
=
key
.
Replace
(
"?key="
,
""
,
StringComparison
.
InvariantCultureIgnoreCase
)
;
// Delete the cache value
if
(
CommonCache
.
GetInstance
(
)
.
DeleteCacheValue
(
key
,
out
var
value
)
)
{
// Response with the cache value
SendResponseAsync
(
Response
.
MakeGetResponse
(
value
)
)
;
}
else
SendResponseAsync
(
Response
.
MakeErrorResponse
(
404
,
"Deleted cache value was not found for the key: "
+
key
)
)
;
}
else
if
(
request
.
Method
==
"OPTIONS"
)
SendResponseAsync
(
Response
.
MakeOptionsResponse
(
)
)
;
else
if
(
request
.
Method
==
"TRACE"
)
SendResponseAsync
(
Response
.
MakeTraceResponse
(
request
)
)
;
else
SendResponseAsync
(
Response
.
MakeErrorResponse
(
"Unsupported HTTP method: "
+
request
.
Method
)
)
;
}
protected
override
void
OnReceivedRequestError
(
HttpRequest
request
,
string
error
)
{
Console
.
WriteLine
(
$
"Request error:
{
error
}
"
)
;
}
protected
override
void
OnError
(
SocketError
error
)
{
Console
.
WriteLine
(
$
"HTTP session caught an error:
{
error
}
"
)
;
}
}
class
HttpCacheServer
:
NetCoreServer
.
HttpServer
{
public
HttpCacheServer
(
IPAddress
address
,
int
port
)
:
base
(
address
,
port
)
{
}
protected
override
TcpSession
CreateSession
(
)
{
return
new
HttpCacheSession
(
this
)
;
}
protected
override
void
OnError
(
SocketError
error
)
{
Console
.
WriteLine
(
$
"HTTP session caught an error:
{
error
}
"
)
;
}
}
class
Program
{
static
void
Main
(
string
[
]
args
)
{
// HTTP server port
int
port
=
8080
;
if
(
args
.
Length
>
0
)
port
=
int
.
Parse
(
args
[
0
]
)
;
// HTTP server content path
string
www
=
"../../../../../www/api"
;
if
(
args
.
Length
>
1
)
www
=
args
[
1
]
;
Console
.
WriteLine
(
$
"HTTP server port:
{
port
}
"
)
;
Console
.
WriteLine
(
$
"HTTP server static content path:
{
www
}
"
)
;
Console
.
WriteLine
(
$
"HTTP server website: http://localhost:
{
port
}
/api/index.html"
)
;
Console
.
WriteLine
(
)
;
// Create a new HTTP server
var
server
=
new
HttpCacheServer
(
IPAddress
.
Any
,
port
)
;
server
.
AddStaticContent
(
www
,
"/api"
)
;
// Start the server
Console
.
Write
(
"Server starting..."
)
;
server
.
Start
(
)
;
Console
.
WriteLine
(
"Done!"
)
;
Console
.
WriteLine
(
"Press Enter to stop the server or '!' to restart the server..."
)
;
// Perform text input
for
(
;
;
)
{
string
line
=
Console
.
ReadLine
(
)
;
if
(
string
.
IsNullOrEmpty
(
line
)
)
break
;
// Restart the server
if
(
line
==
"!"
)
{
Console
.
Write
(
"Server restarting..."
)
;
server
.
Restart
(
)
;
Console
.
WriteLine
(
"Done!"
)
;
}
}
// Stop the server
Console
.
Write
(
"Server stopping..."
)
;
server
.
Stop
(
)
;
Console
.
WriteLine
(
"Done!"
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL