| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | # Specify version format | |
| 2 | - version: "3.0.15.{build}" | ||
| 2 | + version: "3.0.16.{build}" | ||
| 3 | 3 | ||
| 4 | 4 | # Image to use | |
| 5 | 5 | image: Visual Studio 2019 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1032,6 +1032,7 @@ using System; | |||
| 1032 | 1032 | using System.Collections.Concurrent; | |
| 1033 | 1033 | using System.Net; | |
| 1034 | 1034 | using System.Net.Sockets; | |
| 1035 | + using System.Text; | ||
| 1035 | 1036 | using NetCoreServer; | |
| 1036 | 1037 | ||
| 1037 | 1038 | namespace HttpServer | |
@@ -1045,17 +1046,32 @@ namespace HttpServer | |||
| 1045 | 1046 | return _instance; | |
| 1046 | 1047 | } | |
| 1047 | 1048 | ||
| 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) | ||
| 1049 | 1065 | { | |
| 1050 | 1066 | return _cache.TryGetValue(key, out value); | |
| 1051 | 1067 | } | |
| 1052 | 1068 | ||
| 1053 | - public void SetCache(string key, string value) | ||
| 1069 | + public void PutCacheValue(string key, string value) | ||
| 1054 | 1070 | { | |
| 1055 | 1071 | _cache[key] = value; | |
| 1056 | 1072 | } | |
| 1057 | 1073 | ||
| 1058 | - public bool DeleteCache(string key, out string value) | ||
| 1074 | + public bool DeleteCacheValue(string key, out string value) | ||
| 1059 | 1075 | { | |
| 1060 | 1076 | return _cache.TryRemove(key, out value); | |
| 1061 | 1077 | } | |
@@ -1078,34 +1094,60 @@ namespace HttpServer | |||
| 1078 | 1094 | SendResponseAsync(Response.MakeHeadResponse()); | |
| 1079 | 1095 | else if (request.Method == "GET") | |
| 1080 | 1096 | { | |
| 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)) | ||
| 1084 | 1111 | { | |
| 1085 | 1112 | // Response with the cache value | |
| 1086 | - SendResponseAsync(Response.MakeGetResponse(cache)); | ||
| 1113 | + SendResponseAsync(Response.MakeGetResponse(value)); | ||
| 1087 | 1114 | } | |
| 1088 | 1115 | 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)); | ||
| 1090 | 1117 | } | |
| 1091 | 1118 | else if ((request.Method == "POST") || (request.Method == "PUT")) | |
| 1092 | 1119 | { | |
| 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 | + | ||
| 1095 | 1131 | // Response with the cache value | |
| 1096 | 1132 | SendResponseAsync(Response.MakeOkResponse()); | |
| 1097 | 1133 | } | |
| 1098 | 1134 | else if (request.Method == "DELETE") | |
| 1099 | 1135 | { | |
| 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 | + | ||
| 1100 | 1143 | // 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)) | ||
| 1103 | 1145 | { | |
| 1104 | 1146 | // Response with the cache value | |
| 1105 | - SendResponseAsync(Response.MakeGetResponse(cache)); | ||
| 1147 | + SendResponseAsync(Response.MakeGetResponse(value)); | ||
| 1106 | 1148 | } | |
| 1107 | 1149 | 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)); | ||
| 1109 | 1151 | } | |
| 1110 | 1152 | else if (request.Method == "OPTIONS") | |
| 1111 | 1153 | SendResponseAsync(Response.MakeOptionsResponse()); | |
@@ -1329,6 +1371,7 @@ using System.Net; | |||
| 1329 | 1371 | using System.Net.Sockets; | |
| 1330 | 1372 | using System.Security.Authentication; | |
| 1331 | 1373 | using System.Security.Cryptography.X509Certificates; | |
| 1374 | + using System.Text; | ||
| 1332 | 1375 | using NetCoreServer; | |
| 1333 | 1376 | ||
| 1334 | 1377 | namespace HttpsServer | |
@@ -1342,17 +1385,32 @@ namespace HttpsServer | |||
| 1342 | 1385 | return _instance; | |
| 1343 | 1386 | } | |
| 1344 | 1387 | ||
| 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) | ||
| 1346 | 1404 | { | |
| 1347 | 1405 | return _cache.TryGetValue(key, out value); | |
| 1348 | 1406 | } | |
| 1349 | 1407 | ||
| 1350 | - public void SetCache(string key, string value) | ||
| 1408 | + public void PutCacheValue(string key, string value) | ||
| 1351 | 1409 | { | |
| 1352 | 1410 | _cache[key] = value; | |
| 1353 | 1411 | } | |
| 1354 | 1412 | ||
| 1355 | - public bool DeleteCache(string key, out string value) | ||
| 1413 | + public bool DeleteCacheValue(string key, out string value) | ||
| 1356 | 1414 | { | |
| 1357 | 1415 | return _cache.TryRemove(key, out value); | |
| 1358 | 1416 | } | |
@@ -1375,34 +1433,60 @@ namespace HttpsServer | |||
| 1375 | 1433 | SendResponseAsync(Response.MakeHeadResponse()); | |
| 1376 | 1434 | else if (request.Method == "GET") | |
| 1377 | 1435 | { | |
| 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)) | ||
| 1381 | 1450 | { | |
| 1382 | 1451 | // Response with the cache value | |
| 1383 | - SendResponseAsync(Response.MakeGetResponse(cache)); | ||
| 1452 | + SendResponseAsync(Response.MakeGetResponse(value)); | ||
| 1384 | 1453 | } | |
| 1385 | 1454 | 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)); | ||
| 1387 | 1456 | } | |
| 1388 | 1457 | else if ((request.Method == "POST") || (request.Method == "PUT")) | |
| 1389 | 1458 | { | |
| 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 | + | ||
| 1392 | 1470 | // Response with the cache value | |
| 1393 | 1471 | SendResponseAsync(Response.MakeOkResponse()); | |
| 1394 | 1472 | } | |
| 1395 | 1473 | else if (request.Method == "DELETE") | |
| 1396 | 1474 | { | |
| 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 | + | ||
| 1397 | 1482 | // 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)) | ||
| 1400 | 1484 | { | |
| 1401 | 1485 | // Response with the cache value | |
| 1402 | - SendResponseAsync(Response.MakeGetResponse(cache)); | ||
| 1486 | + SendResponseAsync(Response.MakeGetResponse(value)); | ||
| 1403 | 1487 | } | |
| 1404 | 1488 | 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)); | ||
| 1406 | 1490 | } | |
| 1407 | 1491 | else if (request.Method == "OPTIONS") | |
| 1408 | 1492 | SendResponseAsync(Response.MakeOptionsResponse()); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | using System.Collections.Concurrent; | |
| 3 | 3 | using System.Net; | |
| 4 | 4 | using System.Net.Sockets; | |
| 5 | + using System.Text; | ||
| 5 | 6 | using NetCoreServer; | |
| 6 | 7 | ||
| 7 | 8 | namespace HttpServer | |
@@ -15,17 +16,32 @@ public static CommonCache GetInstance() | |||
| 15 | 16 | return _instance; | |
| 16 | 17 | } | |
| 17 | 18 | ||
| 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) | ||
| 19 | 35 | { | |
| 20 | 36 | return _cache.TryGetValue(key, out value); | |
| 21 | 37 | } | |
| 22 | 38 | ||
| 23 | - public void SetCache(string key, string value) | ||
| 39 | + public void PutCacheValue(string key, string value) | ||
| 24 | 40 | { | |
| 25 | 41 | _cache[key] = value; | |
| 26 | 42 | } | |
| 27 | 43 | ||
| 28 | - public bool DeleteCache(string key, out string value) | ||
| 44 | + public bool DeleteCacheValue(string key, out string value) | ||
| 29 | 45 | { | |
| 30 | 46 | return _cache.TryRemove(key, out value); | |
| 31 | 47 | } | |
@@ -48,34 +64,60 @@ protected override void OnReceivedRequest(HttpRequest request) | |||
| 48 | 64 | SendResponseAsync(Response.MakeHeadResponse()); | |
| 49 | 65 | else if (request.Method == "GET") | |
| 50 | 66 | { | |
| 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)) | ||
| 54 | 81 | { | |
| 55 | 82 | // Response with the cache value | |
| 56 | - SendResponseAsync(Response.MakeGetResponse(cache)); | ||
| 83 | + SendResponseAsync(Response.MakeGetResponse(value)); | ||
| 57 | 84 | } | |
| 58 | 85 | 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)); | ||
| 60 | 87 | } | |
| 61 | 88 | else if ((request.Method == "POST") || (request.Method == "PUT")) | |
| 62 | 89 | { | |
| 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 | + | ||
| 65 | 101 | // Response with the cache value | |
| 66 | 102 | SendResponseAsync(Response.MakeOkResponse()); | |
| 67 | 103 | } | |
| 68 | 104 | else if (request.Method == "DELETE") | |
| 69 | 105 | { | |
| 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 | + | ||
| 70 | 113 | // 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)) | ||
| 73 | 115 | { | |
| 74 | 116 | // Response with the cache value | |
| 75 | - SendResponseAsync(Response.MakeGetResponse(cache)); | ||
| 117 | + SendResponseAsync(Response.MakeGetResponse(value)); | ||
| 76 | 118 | } | |
| 77 | 119 | 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)); | ||
| 79 | 121 | } | |
| 80 | 122 | else if (request.Method == "OPTIONS") | |
| 81 | 123 | SendResponseAsync(Response.MakeOptionsResponse()); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments