FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Update HTTP/HTTPS server examples and Swagger bundle · dotNetTreasury/NetCoreServer@ead0922 · GitHub

Repository navigation

Commit ead0922

Browse files
committed
Update HTTP/HTTPS server examples and Swagger bundle
1 parent 756d28b commit ead0922

19 files changed

Lines changed: 495 additions & 136 deletions

‎.appveyor.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Specify version format
2-
version: "3.0.15.{build}"
2+
version: "3.0.16.{build}"
33

44
# Image to use
55
image: Visual Studio 2019

‎README.md‎

Lines changed: 112 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,7 @@ using System;
10321032
using System.Collections.Concurrent;
10331033
using System.Net;
10341034
using System.Net.Sockets;
1035+
using System.Text;
10351036
using NetCoreServer;
10361037

10371038
namespace HttpServer
@@ -1045,17 +1046,32 @@ namespace HttpServer
10451046
return _instance;
10461047
}
10471048

1048-
public bool GetCache(string key, out string value)
1049+
public string GetAllCache()
1050+
{
1051+
var result = new StringBuilder();
1052+
result.Append("[\n");
1053+
foreach (var item in _cache)
1054+
{
1055+
result.Append(" {\n");
1056+
result.AppendFormat($" \"key\": \"{item.Key}\",\n");
1057+
result.AppendFormat($" \"value\": \"{item.Value}\",\n");
1058+
result.Append(" },\n");
1059+
}
1060+
result.Append("]\n");
1061+
return result.ToString();
1062+
}
1063+
1064+
public bool GetCacheValue(string key, out string value)
10491065
{
10501066
return _cache.TryGetValue(key, out value);
10511067
}
10521068

1053-
public void SetCache(string key, string value)
1069+
public void PutCacheValue(string key, string value)
10541070
{
10551071
_cache[key] = value;
10561072
}
10571073

1058-
public bool DeleteCache(string key, out string value)
1074+
public bool DeleteCacheValue(string key, out string value)
10591075
{
10601076
return _cache.TryRemove(key, out value);
10611077
}
@@ -1078,34 +1094,60 @@ namespace HttpServer
10781094
SendResponseAsync(Response.MakeHeadResponse());
10791095
else if (request.Method == "GET")
10801096
{
1081-
// Get the cache value
1082-
string cache;
1083-
if (CommonCache.GetInstance().GetCache(request.Url, out cache))
1097+
string key = request.Url;
1098+
1099+
// Decode the key value
1100+
key = Uri.UnescapeDataString(key);
1101+
key = key.Replace("/api/cache", "", StringComparison.InvariantCultureIgnoreCase);
1102+
key = key.Replace("?key=", "", StringComparison.InvariantCultureIgnoreCase);
1103+
1104+
if (string.IsNullOrEmpty(key))
1105+
{
1106+
// Response with all cache values
1107+
SendResponseAsync(Response.MakeGetResponse(CommonCache.GetInstance().GetAllCache(), "application/json; charset=UTF-8"));
1108+
}
1109+
// Get the cache value by the given key
1110+
else if (CommonCache.GetInstance().GetCacheValue(key, out var value))
10841111
{
10851112
// Response with the cache value
1086-
SendResponseAsync(Response.MakeGetResponse(cache));
1113+
SendResponseAsync(Response.MakeGetResponse(value));
10871114
}
10881115
else
1089-
SendResponseAsync(Response.MakeErrorResponse("Required cache value was not found for the key: " + request.Url));
1116+
SendResponseAsync(Response.MakeErrorResponse("Required cache value was not found for the key: " + key, 404));
10901117
}
10911118
else if ((request.Method == "POST") || (request.Method == "PUT"))
10921119
{
1093-
// Set the cache value
1094-
CommonCache.GetInstance().SetCache(request.Url, request.Body);
1120+
string key = request.Url;
1121+
string value = request.Body;
1122+
1123+
// Decode the key value
1124+
key = Uri.UnescapeDataString(key);
1125+
key = key.Replace("/api/cache", "", StringComparison.InvariantCultureIgnoreCase);
1126+
key = key.Replace("?key=", "", StringComparison.InvariantCultureIgnoreCase);
1127+
1128+
// Put the cache value
1129+
CommonCache.GetInstance().PutCacheValue(key, value);
1130+
10951131
// Response with the cache value
10961132
SendResponseAsync(Response.MakeOkResponse());
10971133
}
10981134
else if (request.Method == "DELETE")
10991135
{
1136+
string key = request.Url;
1137+
1138+
// Decode the key value
1139+
key = Uri.UnescapeDataString(key);
1140+
key = key.Replace("/api/cache", "", StringComparison.InvariantCultureIgnoreCase);
1141+
key = key.Replace("?key=", "", StringComparison.InvariantCultureIgnoreCase);
1142+
11001143
// Delete the cache value
1101-
string cache;
1102-
if (CommonCache.GetInstance().DeleteCache(request.Url, out cache))
1144+
if (CommonCache.GetInstance().DeleteCacheValue(key, out var value))
11031145
{
11041146
// Response with the cache value
1105-
SendResponseAsync(Response.MakeGetResponse(cache));
1147+
SendResponseAsync(Response.MakeGetResponse(value));
11061148
}
11071149
else
1108-
SendResponseAsync(Response.MakeErrorResponse("Deleted cache value was not found for the key: " + request.Url));
1150+
SendResponseAsync(Response.MakeErrorResponse("Deleted cache value was not found for the key: " + key, 404));
11091151
}
11101152
else if (request.Method == "OPTIONS")
11111153
SendResponseAsync(Response.MakeOptionsResponse());
@@ -1329,6 +1371,7 @@ using System.Net;
13291371
using System.Net.Sockets;
13301372
using System.Security.Authentication;
13311373
using System.Security.Cryptography.X509Certificates;
1374+
using System.Text;
13321375
using NetCoreServer;
13331376

13341377
namespace HttpsServer
@@ -1342,17 +1385,32 @@ namespace HttpsServer
13421385
return _instance;
13431386
}
13441387

1345-
public bool GetCache(string key, out string value)
1388+
public string GetAllCache()
1389+
{
1390+
var result = new StringBuilder();
1391+
result.Append("[\n");
1392+
foreach (var item in _cache)
1393+
{
1394+
result.Append(" {\n");
1395+
result.AppendFormat($" \"key\": \"{item.Key}\",\n");
1396+
result.AppendFormat($" \"value\": \"{item.Value}\",\n");
1397+
result.Append(" },\n");
1398+
}
1399+
result.Append("]\n");
1400+
return result.ToString();
1401+
}
1402+
1403+
public bool GetCacheValue(string key, out string value)
13461404
{
13471405
return _cache.TryGetValue(key, out value);
13481406
}
13491407

1350-
public void SetCache(string key, string value)
1408+
public void PutCacheValue(string key, string value)
13511409
{
13521410
_cache[key] = value;
13531411
}
13541412

1355-
public bool DeleteCache(string key, out string value)
1413+
public bool DeleteCacheValue(string key, out string value)
13561414
{
13571415
return _cache.TryRemove(key, out value);
13581416
}
@@ -1375,34 +1433,60 @@ namespace HttpsServer
13751433
SendResponseAsync(Response.MakeHeadResponse());
13761434
else if (request.Method == "GET")
13771435
{
1378-
// Get the cache value
1379-
string cache;
1380-
if (CommonCache.GetInstance().GetCache(request.Url, out cache))
1436+
string key = request.Url;
1437+
1438+
// Decode the key value
1439+
key = Uri.UnescapeDataString(key);
1440+
key = key.Replace("/api/cache", "", StringComparison.InvariantCultureIgnoreCase);
1441+
key = key.Replace("?key=", "", StringComparison.InvariantCultureIgnoreCase);
1442+
1443+
if (string.IsNullOrEmpty(key))
1444+
{
1445+
// Response with all cache values
1446+
SendResponseAsync(Response.MakeGetResponse(CommonCache.GetInstance().GetAllCache(), "application/json; charset=UTF-8"));
1447+
}
1448+
// Get the cache value by the given key
1449+
else if (CommonCache.GetInstance().GetCacheValue(key, out var value))
13811450
{
13821451
// Response with the cache value
1383-
SendResponseAsync(Response.MakeGetResponse(cache));
1452+
SendResponseAsync(Response.MakeGetResponse(value));
13841453
}
13851454
else
1386-
SendResponseAsync(Response.MakeErrorResponse("Required cache value was not found for the key: " + request.Url));
1455+
SendResponseAsync(Response.MakeErrorResponse("Required cache value was not found for the key: " + key, 404));
13871456
}
13881457
else if ((request.Method == "POST") || (request.Method == "PUT"))
13891458
{
1390-
// Set the cache value
1391-
CommonCache.GetInstance().SetCache(request.Url, request.Body);
1459+
string key = request.Url;
1460+
string value = request.Body;
1461+
1462+
// Decode the key value
1463+
key = Uri.UnescapeDataString(key);
1464+
key = key.Replace("/api/cache", "", StringComparison.InvariantCultureIgnoreCase);
1465+
key = key.Replace("?key=", "", StringComparison.InvariantCultureIgnoreCase);
1466+
1467+
// Put the cache value
1468+
CommonCache.GetInstance().PutCacheValue(key, value);
1469+
13921470
// Response with the cache value
13931471
SendResponseAsync(Response.MakeOkResponse());
13941472
}
13951473
else if (request.Method == "DELETE")
13961474
{
1475+
string key = request.Url;
1476+
1477+
// Decode the key value
1478+
key = Uri.UnescapeDataString(key);
1479+
key = key.Replace("/api/cache", "", StringComparison.InvariantCultureIgnoreCase);
1480+
key = key.Replace("?key=", "", StringComparison.InvariantCultureIgnoreCase);
1481+
13971482
// Delete the cache value
1398-
string cache;
1399-
if (CommonCache.GetInstance().DeleteCache(request.Url, out cache))
1483+
if (CommonCache.GetInstance().DeleteCacheValue(key, out var value))
14001484
{
14011485
// Response with the cache value
1402-
SendResponseAsync(Response.MakeGetResponse(cache));
1486+
SendResponseAsync(Response.MakeGetResponse(value));
14031487
}
14041488
else
1405-
SendResponseAsync(Response.MakeErrorResponse("Deleted cache value was not found for the key: " + request.Url));
1489+
SendResponseAsync(Response.MakeErrorResponse("Deleted cache value was not found for the key: " + key, 404));
14061490
}
14071491
else if (request.Method == "OPTIONS")
14081492
SendResponseAsync(Response.MakeOptionsResponse());

‎examples/HttpServer/Program.cs‎

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Concurrent;
33
using System.Net;
44
using System.Net.Sockets;
5+
using System.Text;
56
using NetCoreServer;
67

78
namespace HttpServer
@@ -15,17 +16,32 @@ public static CommonCache GetInstance()
1516
return _instance;
1617
}
1718

18-
public bool GetCache(string key, out string value)
19+
public string GetAllCache()
20+
{
21+
var result = new StringBuilder();
22+
result.Append("[\n");
23+
foreach (var item in _cache)
24+
{
25+
result.Append(" {\n");
26+
result.AppendFormat($" \"key\": \"{item.Key}\",\n");
27+
result.AppendFormat($" \"value\": \"{item.Value}\",\n");
28+
result.Append(" },\n");
29+
}
30+
result.Append("]\n");
31+
return result.ToString();
32+
}
33+
34+
public bool GetCacheValue(string key, out string value)
1935
{
2036
return _cache.TryGetValue(key, out value);
2137
}
2238

23-
public void SetCache(string key, string value)
39+
public void PutCacheValue(string key, string value)
2440
{
2541
_cache[key] = value;
2642
}
2743

28-
public bool DeleteCache(string key, out string value)
44+
public bool DeleteCacheValue(string key, out string value)
2945
{
3046
return _cache.TryRemove(key, out value);
3147
}
@@ -48,34 +64,60 @@ protected override void OnReceivedRequest(HttpRequest request)
4864
SendResponseAsync(Response.MakeHeadResponse());
4965
else if (request.Method == "GET")
5066
{
51-
// Get the cache value
52-
string cache;
53-
if (CommonCache.GetInstance().GetCache(request.Url, out cache))
67+
string key = request.Url;
68+
69+
// Decode the key value
70+
key = Uri.UnescapeDataString(key);
71+
key = key.Replace("/api/cache", "", StringComparison.InvariantCultureIgnoreCase);
72+
key = key.Replace("?key=", "", StringComparison.InvariantCultureIgnoreCase);
73+
74+
if (string.IsNullOrEmpty(key))
75+
{
76+
// Response with all cache values
77+
SendResponseAsync(Response.MakeGetResponse(CommonCache.GetInstance().GetAllCache(), "application/json; charset=UTF-8"));
78+
}
79+
// Get the cache value by the given key
80+
else if (CommonCache.GetInstance().GetCacheValue(key, out var value))
5481
{
5582
// Response with the cache value
56-
SendResponseAsync(Response.MakeGetResponse(cache));
83+
SendResponseAsync(Response.MakeGetResponse(value));
5784
}
5885
else
59-
SendResponseAsync(Response.MakeErrorResponse("Required cache value was not found for the key: " + request.Url));
86+
SendResponseAsync(Response.MakeErrorResponse("Required cache value was not found for the key: " + key, 404));
6087
}
6188
else if ((request.Method == "POST") || (request.Method == "PUT"))
6289
{
63-
// Set the cache value
64-
CommonCache.GetInstance().SetCache(request.Url, request.Body);
90+
string key = request.Url;
91+
string value = request.Body;
92+
93+
// Decode the key value
94+
key = Uri.UnescapeDataString(key);
95+
key = key.Replace("/api/cache", "", StringComparison.InvariantCultureIgnoreCase);
96+
key = key.Replace("?key=", "", StringComparison.InvariantCultureIgnoreCase);
97+
98+
// Put the cache value
99+
CommonCache.GetInstance().PutCacheValue(key, value);
100+
65101
// Response with the cache value
66102
SendResponseAsync(Response.MakeOkResponse());
67103
}
68104
else if (request.Method == "DELETE")
69105
{
106+
string key = request.Url;
107+
108+
// Decode the key value
109+
key = Uri.UnescapeDataString(key);
110+
key = key.Replace("/api/cache", "", StringComparison.InvariantCultureIgnoreCase);
111+
key = key.Replace("?key=", "", StringComparison.InvariantCultureIgnoreCase);
112+
70113
// Delete the cache value
71-
string cache;
72-
if (CommonCache.GetInstance().DeleteCache(request.Url, out cache))
114+
if (CommonCache.GetInstance().DeleteCacheValue(key, out var value))
73115
{
74116
// Response with the cache value
75-
SendResponseAsync(Response.MakeGetResponse(cache));
117+
SendResponseAsync(Response.MakeGetResponse(value));
76118
}
77119
else
78-
SendResponseAsync(Response.MakeErrorResponse("Deleted cache value was not found for the key: " + request.Url));
120+
SendResponseAsync(Response.MakeErrorResponse("Deleted cache value was not found for the key: " + key, 404));
79121
}
80122
else if (request.Method == "OPTIONS")
81123
SendResponseAsync(Response.MakeOptionsResponse());

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL