From 132c7af71804361d938e26527f13bd0eb2211be2 Mon Sep 17 00:00:00 2001 From: Ivan Shynkarenka Date: Wed, 30 Dec 2020 05:58:39 +0300 Subject: [PATCH] C#7: Migrate from Tuple to ValueTuple --- .appveyor.yml | 2 +- source/NetCoreServer/FileCache.cs | 12 +++++------ source/NetCoreServer/HttpRequest.cs | 26 +++++++++++------------ source/NetCoreServer/HttpResponse.cs | 12 +++++------ source/NetCoreServer/NetCoreServer.csproj | 2 +- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 41525e8..aadd4e7 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -1,5 +1,5 @@ # Specify version format -version: "5.0.4.{build}" +version: "5.0.5.{build}" # Image to use image: Visual Studio 2019 diff --git a/source/NetCoreServer/FileCache.cs b/source/NetCoreServer/FileCache.cs index 08e052d..29bc009 100644 --- a/source/NetCoreServer/FileCache.cs +++ b/source/NetCoreServer/FileCache.cs @@ -88,15 +88,15 @@ public class FileCache /// /// Key to find /// 'true' and cache value if the cache value was found, 'false' if the given key was not found - public Tuple Find(string key) + public (bool, byte[]) Find(string key) { lock (_lock) { // Try to find the given key if (!_entriesByKey.TryGetValue(key, out var cacheValue)) - return new Tuple(false, new byte[0]); + return (false, new byte[0]); - return new Tuple(true, cacheValue.Value); + return (true, cacheValue.Value); } } @@ -106,7 +106,7 @@ public Tuple Find(string key) /// Key to find /// Cache timeout value /// 'true' and cache value if the cache value was found, 'false' if the given key was not found - public Tuple Find(string key, out DateTime timeout) + public (bool, byte[]) Find(string key, out DateTime timeout) { lock (_lock) { @@ -114,11 +114,11 @@ public Tuple Find(string key, out DateTime timeout) if (!_entriesByKey.TryGetValue(key, out var cacheValue)) { timeout = new DateTime(0); - return new Tuple(false, new byte[0]); + return (false, new byte[0]); } timeout = cacheValue.Timestamp + cacheValue.Timespan; - return new Tuple(true, cacheValue.Value); + return (true, cacheValue.Value); } } diff --git a/source/NetCoreServer/HttpRequest.cs b/source/NetCoreServer/HttpRequest.cs index ffac2d8..d74057e 100644 --- a/source/NetCoreServer/HttpRequest.cs +++ b/source/NetCoreServer/HttpRequest.cs @@ -57,11 +57,11 @@ public HttpRequest(string method, string url, string protocol = "HTTP/1.1") /// /// Get the HTTP request header by index /// - public Tuple Header(int i) + public (string, string) Header(int i) { Debug.Assert((i < _headers.Count), "Index out of bounds!"); if (i >= _headers.Count) - return new Tuple("", ""); + return ("", ""); return _headers[i]; } @@ -72,11 +72,11 @@ public Tuple Header(int i) /// /// Get the HTTP request cookie by index /// - public Tuple Cookie(int i) + public (string, string) Cookie(int i) { Debug.Assert((i < _cookies.Count), "Index out of bounds!"); if (i >= _cookies.Count) - return new Tuple("", ""); + return ("", ""); return _cookies[i]; } @@ -192,7 +192,7 @@ public HttpRequest SetHeader(string key, string value) _cache.Append("\r\n"); // Add the header to the corresponding collection - _headers.Add(new Tuple(key, value)); + _headers.Add((key, value)); return this; } @@ -217,9 +217,9 @@ public HttpRequest SetCookie(string name, string value) _cache.Append("\r\n"); // Add the header to the corresponding collection - _headers.Add(new Tuple(key, cookie)); + _headers.Add((key, cookie)); // Add the cookie to the corresponding collection - _cookies.Add(new Tuple(name, value)); + _cookies.Add((name, value)); return this; } @@ -237,7 +237,7 @@ public HttpRequest AddCookie(string name, string value) _cache.Append(value); // Add the cookie to the corresponding collection - _cookies.Add(new Tuple(name, value)); + _cookies.Add((name, value)); return this; } @@ -493,9 +493,9 @@ public HttpRequest MakeTraceRequest(string url) // HTTP request protocol private string _protocol; // HTTP request headers - private List> _headers = new List>(); + private List<(string, string)> _headers = new List<(string, string)>(); // HTTP request cookies - private List> _cookies = new List>(); + private List<(string, string)> _cookies = new List<(string, string)>(); // HTTP request body private int _bodyIndex; private int _bodySize; @@ -641,7 +641,7 @@ internal bool ReceiveHeader(byte[] buffer, int offset, int size) // Add a new header string headerName = _cache.ExtractString(headerNameIndex, headerNameSize); string headerValue = _cache.ExtractString(headerValueIndex, headerValueSize); - _headers.Add(new Tuple(headerName, headerValue)); + _headers.Add((headerName, headerValue)); // Try to find the body content length if (string.Compare(headerName, "Content-Length", StringComparison.OrdinalIgnoreCase) == 0) @@ -725,7 +725,7 @@ internal bool ReceiveHeader(byte[] buffer, int offset, int size) if ((nameSize > 0) && (cookieSize > 0)) { // Add the cookie to the corresponding collection - _cookies.Add(new Tuple(_cache.ExtractString(nameIndex, nameSize), _cache.ExtractString(cookieIndex, cookieSize))); + _cookies.Add((_cache.ExtractString(nameIndex, nameSize), _cache.ExtractString(cookieIndex, cookieSize))); // Resset the current cookie values nameIndex = j; @@ -763,7 +763,7 @@ internal bool ReceiveHeader(byte[] buffer, int offset, int size) if ((nameSize > 0) && (cookieSize > 0)) { // Add the cookie to the corresponding collection - _cookies.Add(new Tuple(_cache.ExtractString(nameIndex, nameSize), _cache.ExtractString(cookieIndex, cookieSize))); + _cookies.Add((_cache.ExtractString(nameIndex, nameSize), _cache.ExtractString(cookieIndex, cookieSize))); } } } diff --git a/source/NetCoreServer/HttpResponse.cs b/source/NetCoreServer/HttpResponse.cs index aa3a25a..2ad1fc5 100644 --- a/source/NetCoreServer/HttpResponse.cs +++ b/source/NetCoreServer/HttpResponse.cs @@ -67,11 +67,11 @@ public HttpResponse(int status, string statusPhrase, string protocol) /// /// Get the HTTP response header by index /// - public Tuple Header(int i) + public (string, string) Header(int i) { Debug.Assert((i < _headers.Count), "Index out of bounds!"); if (i >= _headers.Count) - return new Tuple("", ""); + return ("", ""); return _headers[i]; } @@ -403,7 +403,7 @@ public HttpResponse SetHeader(string key, string value) _cache.Append("\r\n"); // Add the header to the corresponding collection - _headers.Add(new Tuple(key, value)); + _headers.Add((key, value)); return this; } @@ -457,7 +457,7 @@ public HttpResponse SetCookie(string name, string value, int maxAge = 86400, str _cache.Append("\r\n"); // Add the header to the corresponding collection - _headers.Add(new Tuple(key, cookie)); + _headers.Add((key, cookie)); return this; } @@ -688,7 +688,7 @@ public HttpResponse MakeTraceResponse(Buffer request) // HTTP response protocol private string _protocol; // HTTP response headers - private List> _headers = new List>(); + private List<(string, string)> _headers = new List<(string, string)>(); // HTTP response body private int _bodyIndex; private int _bodySize; @@ -842,7 +842,7 @@ internal bool ReceiveHeader(byte[] buffer, int offset, int size) // Add a new header string headerName = _cache.ExtractString(headerNameIndex, headerNameSize); string headerValue = _cache.ExtractString(headerValueIndex, headerValueSize); - _headers.Add(new Tuple(headerName, headerValue)); + _headers.Add((headerName, headerValue)); // Try to find the body content length if (string.Compare(headerName, "Content-Length", StringComparison.OrdinalIgnoreCase) == 0) diff --git a/source/NetCoreServer/NetCoreServer.csproj b/source/NetCoreServer/NetCoreServer.csproj index d092c7d..deb66aa 100644 --- a/source/NetCoreServer/NetCoreServer.csproj +++ b/source/NetCoreServer/NetCoreServer.csproj @@ -2,7 +2,7 @@ net5.0 - 5.0.4.0 + 5.0.5.0 Ivan Shynkarenka Copyright (c) 2019-2021 Ivan Shynkarenka https://github.com/chronoxor/NetCoreServer