Skip to content

Commit 36708e9

Browse files
authored
Rewrite TcpServer.cs
Short changes: Move long code into separate methods, and use refs to modify variable by this "private void methods". Add parsing headers, select encoding ("iso-8859-1") for response on binary request, and encoding UTF-8 for response on text request. Set is_binary_request when "Content-Type: application/octet-stream" contains in headers. Rename contentLength to requestLength, because headers.Length contains there, too, so this is not Content-Length. Full changes: \nanodb.exe-source\Server\TcpServer.cs Lines 248 : remove 3 lines with test Console.WriteLine Lines 249 : remove 1 line with test Console.WriteLine Lines 250 : remove 1 line with test Console.WriteLine Lines 257 : remove 3 lines with test Console.WriteLine Lines 259 : remove 1 line with test Console.WriteLine Lines 268 : remove 3 lines with test Console.WriteLine Lines 291 : remove 1 line with test Console.WriteLine Lines 307 : remove 1 line with test code and change comment. Lines 341 : remove 1 lines with test code Lines 382 : remove 1 lines with test code Lines 374 : remove 1 lines with test code Lines 426 : remove 7 lines with test code Lines 434 : remove 17 lines with test code Lines 444 : remove 2 lines with test code Lines 448 : move 2 lines with comment, before stream.BeginWrite Lines 457 : add new-line after the end of try-section. Lines 459 : remove 11 lines with test Console.WriteLine Lines 467 - 468 : set two variables as false, when data sucessfully writted. Lines 472 - 483 : Add private void TryToApplyTcpDelay - to set tcp delay, if need. Lines 485 - 507 : Add private void AddReadedBytesFromBlock - to read bytes from NetworkStream stream, block-by-block Lines 509 - 566 : Add private void TryToExtractHeaderFromRequest - to try to extract headers, if this already readed. Lines 568 - 599 : Add private void TryUpdaterequestLength - to try update "requestLength", when Content-Length not specified in headers. Lines 601 - 652 : Add private void TryToLoadLargeRequestWithPostsInJSON - to load the large POST-request with JSON-file, before client disconnected, and try to upload this then. Lines 654 - 700 : Add private void TryToUploadIntoDataBaseThePostsFromLoadedRequestWithJSON to upload the loaded JSON, using PostDb Upload_Posts. Lines 702 - 731 : Add private void WriteBytesOfResponse - to write the different responces (binary or text) Lines 733 - 877 : Rewrite private void HandleClient Rename "buffer" to "block", and "block" to "BlockAsString" (inside AddReadedBytesFromBlock) because this is a block of bytes, that readed block-by-block. Rename "contentLength" to "requestLength", because headers.Length added for this value. Use added subprograms, with "ref", there, to make code shorter and more readable. Extract headers from NetworkStream stream, in the reading process. Extract Content-Length if exists, and set requestLength value, or update this value everytime, when new block readed. Set is_binary_request=true and change enc to "latin1" (iso-8859-1) for binary request, when "Content-Type: application/octet-stream" contains in headers. Use binary response, and encode bytes as "iso-8859-1"-text, using dynamic redefined value of "enc"-variable. Decode headers as ASCII-compatible ISO-8859-1 (latin1) encoding, and change encoding by to UTF8 (by decode from raw-bytes again, in second part of TryToExtractHeaderFromRequest), and do this if request is not binary, but text. Also set raw as null, and do not add bytes there, then. Else, read bytes up to end in "raw", and do not add "blocks" into the string "readData", and set it as "null". As result of this all: binary-request contains bytes, and readData = null. Response encoded with "iso-8859-1" encoding. text-request contains utf-8 text in readData, and raw = null. Response encoded with "UTF-8" encoding. \Changes.txt - Update this file "Changes.txt" That is all changes. TL;DR "iso-8859-1", this is ASCII-compatible, and moreover reversive encoding (thic contains all characters from range \u0000-\u00FF), that can encode all 256-bytes into 256-chars, and decode this back. With CSharp, and with JavaScript. So, because of this, the binary data can be transferred between client and server, as "iso-8859-1"-strings, in HTTP-requests (because the methods, in DbApiHandler.cs, accepting strings only) or/and in responses. (Tests) C#: byte[] AllBytes = new byte[256]; for(var i = 0; i<AllBytes.Length; i++){AllBytes[i] = (byte)i;} //Get AllBytes System.Text.Encoding enc; //Define this //Test UTF8 enc = System.Text.Encoding.UTF8; Console.WriteLine("AllBytes equals DecodedBytes? "+AllBytes.SequenceEqual(enc.GetBytes(enc.GetString(AllBytes)))); //False //Test ASCII enc = System.Text.Encoding.ASCII; Console.WriteLine("AllBytes equals DecodedBytes? "+AllBytes.SequenceEqual(enc.GetBytes(enc.GetString(AllBytes)))); //False //Test Latin1 (iso-8859-1) enc = System.Text.Encoding.GetEncoding("iso-8859-1"); Console.WriteLine("AllBytes equals DecodedBytes? "+AllBytes.SequenceEqual(enc.GetBytes(enc.GetString(AllBytes)))); //True JavaScript: var AllBytes = new Uint8Array(256); for(var i = 0; i<AllBytes.length; i++){AllBytes[i] = i;} //AllBytes var s = ''; for(var i = 0; i<AllBytes.length; i++){s += String.fromCharCode(AllBytes[i]);} //encode var DecodedBytes = new Uint8Array(s.length); for(var i=0; i<s.length; i++){DecodedBytes[i]=s.charCodeAt(i);}//decode console.log("AllBytes equals DecodedBytes? "+(AllBytes.toString() === DecodedBytes.toString())); //true //console.log(new TextEncoder("utf-8").encode(new TextDecoder("utf-8").decode(AllBytes)).toString() === AllBytes.toString()); //utf8 - false; So iso is reversive, and can be used to encode binary-requests and response, as strings, without growing the size of this strings. 1 char = 1 byte, all possible values allowed.
1 parent 8520fe3 commit 36708e9

File tree

2 files changed

+478
-266
lines changed

2 files changed

+478
-266
lines changed

Changes.txt

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,104 @@ BEGIN CHANGELOG:
99
__________________________________________________________________________________________________________________________________________________________________
1010
(XX.XX.XXXX) commit (latest, unknown hash) ? files changed with ? additions and ? deletions.
1111
__________________________________________________________________________________________________________________________________________________________________
12-
(22.06.2021) commit (latest, unknown hash) 8 files changed with ? additions and ? deletions.
12+
(23.06.2021) commit (latest, unknown hash) 2 files changed with ? additions and ? deletions.
13+
14+
Rewrite TcpServer.cs
15+
16+
Short changes:
17+
Move long code into separate methods, and use refs to modify variable by this "private void methods".
18+
Add parsing headers, select encoding ("iso-8859-1") for response on binary request, and encoding UTF-8 for response on text request.
19+
Set is_binary_request when "Content-Type: application/octet-stream" contains in headers.
20+
Rename contentLength to requestLength, because headers.Length contains there, too, so this is not Content-Length.
21+
22+
Full changes:
23+
\nanodb.exe-source\Server\TcpServer.cs
24+
Lines 248 : remove 3 lines with test Console.WriteLine
25+
Lines 249 : remove 1 line with test Console.WriteLine
26+
Lines 250 : remove 1 line with test Console.WriteLine
27+
Lines 257 : remove 3 lines with test Console.WriteLine
28+
Lines 259 : remove 1 line with test Console.WriteLine
29+
Lines 268 : remove 3 lines with test Console.WriteLine
30+
Lines 291 : remove 1 line with test Console.WriteLine
31+
Lines 307 : remove 1 line with test code and change comment.
32+
Lines 341 : remove 1 lines with test code
33+
Lines 382 : remove 1 lines with test code
34+
Lines 374 : remove 1 lines with test code
35+
Lines 426 : remove 7 lines with test code
36+
Lines 434 : remove 17 lines with test code
37+
Lines 444 : remove 2 lines with test code
38+
Lines 448 : move 2 lines with comment, before stream.BeginWrite
39+
Lines 457 : add new-line after the end of try-section.
40+
Lines 459 : remove 11 lines with test Console.WriteLine
41+
Lines 467 - 468 : set two variables as false, when data sucessfully writted.
42+
Lines 472 - 483 : Add private void TryToApplyTcpDelay - to set tcp delay, if need.
43+
Lines 485 - 507 : Add private void AddReadedBytesFromBlock - to read bytes from NetworkStream stream, block-by-block
44+
Lines 509 - 566 : Add private void TryToExtractHeaderFromRequest - to try to extract headers, if this already readed.
45+
Lines 568 - 599 : Add private void TryUpdaterequestLength - to try update "requestLength", when Content-Length not specified in headers.
46+
Lines 601 - 652 : Add private void TryToLoadLargeRequestWithPostsInJSON - to load the large POST-request with JSON-file,
47+
before client disconnected, and try to upload this then.
48+
Lines 654 - 700 : Add private void TryToUploadIntoDataBaseThePostsFromLoadedRequestWithJSON
49+
to upload the loaded JSON, using PostDb Upload_Posts.
50+
Lines 702 - 731 : Add private void WriteBytesOfResponse - to write the different responces (binary or text)
51+
Lines 733 - 877 : Rewrite private void HandleClient
52+
Rename "buffer" to "block", and "block" to "BlockAsString" (inside AddReadedBytesFromBlock)
53+
because this is a block of bytes, that readed block-by-block.
54+
Rename "contentLength" to "requestLength", because headers.Length added for this value.
55+
Use added subprograms, with "ref", there, to make code shorter and more readable.
56+
Extract headers from NetworkStream stream, in the reading process.
57+
Extract Content-Length if exists, and set requestLength value, or update this value everytime, when new block readed.
58+
Set is_binary_request=true and change enc to "latin1" (iso-8859-1) for binary request,
59+
when "Content-Type: application/octet-stream" contains in headers.
60+
Use binary response, and encode bytes as "iso-8859-1"-text,
61+
using dynamic redefined value of "enc"-variable.
62+
Decode headers as ASCII-compatible ISO-8859-1 (latin1) encoding,
63+
and change encoding by to UTF8 (by decode from raw-bytes again, in second part of TryToExtractHeaderFromRequest),
64+
and do this if request is not binary, but text. Also set raw as null, and do not add bytes there, then.
65+
Else, read bytes up to end in "raw", and do not add "blocks" into the string "readData", and set it as "null".
66+
As result of this all:
67+
binary-request contains bytes, and readData = null. Response encoded with "iso-8859-1" encoding.
68+
text-request contains utf-8 text in readData, and raw = null. Response encoded with "UTF-8" encoding.
69+
\Changes.txt - Update this file "Changes.txt"
70+
71+
That is all changes.
72+
73+
TL;DR
74+
"iso-8859-1", this is ASCII-compatible, and moreover reversive encoding (thic contains all characters from range \u0000-\u00FF),
75+
that can encode all 256-bytes into 256-chars, and decode this back.
76+
With CSharp, and with JavaScript.
77+
So, because of this, the binary data can be transferred between client and server,
78+
as "iso-8859-1"-strings, in HTTP-requests (because the methods, in DbApiHandler.cs, accepting strings only)
79+
or/and in responses.
80+
81+
(Tests)
82+
C#:
83+
byte[] AllBytes = new byte[256]; for(var i = 0; i<AllBytes.Length; i++){AllBytes[i] = (byte)i;} //Get AllBytes
84+
System.Text.Encoding enc; //Define this
85+
86+
//Test UTF8
87+
enc = System.Text.Encoding.UTF8;
88+
Console.WriteLine("AllBytes equals DecodedBytes? "+AllBytes.SequenceEqual(enc.GetBytes(enc.GetString(AllBytes)))); //False
89+
90+
//Test ASCII
91+
enc = System.Text.Encoding.ASCII;
92+
Console.WriteLine("AllBytes equals DecodedBytes? "+AllBytes.SequenceEqual(enc.GetBytes(enc.GetString(AllBytes)))); //False
93+
94+
//Test Latin1 (iso-8859-1)
95+
enc = System.Text.Encoding.GetEncoding("iso-8859-1");
96+
Console.WriteLine("AllBytes equals DecodedBytes? "+AllBytes.SequenceEqual(enc.GetBytes(enc.GetString(AllBytes)))); //True
97+
98+
JavaScript:
99+
var AllBytes = new Uint8Array(256); for(var i = 0; i<AllBytes.length; i++){AllBytes[i] = i;} //AllBytes
100+
var s = ''; for(var i = 0; i<AllBytes.length; i++){s += String.fromCharCode(AllBytes[i]);} //encode
101+
var DecodedBytes = new Uint8Array(s.length); for(var i=0; i<s.length; i++){DecodedBytes[i]=s.charCodeAt(i);}//decode
102+
console.log("AllBytes equals DecodedBytes? "+(AllBytes.toString() === DecodedBytes.toString())); //true
103+
104+
//console.log(new TextEncoder("utf-8").encode(new TextDecoder("utf-8").decode(AllBytes)).toString() === AllBytes.toString()); //utf8 - false;
105+
106+
So iso is reversive, and can be used to encode binary-requests and response, as strings, without growing the size of this strings.
107+
1 char = 1 byte, all possible values allowed.
108+
__________________________________________________________________________________________________________________________________________________________________
109+
(22.06.2021) commit 8520fe36ed8cfeb8bd33b6fd8c6b01d42a1f41df 8 files changed with 127 additions and 34 deletions.
13110

14111
Rewrite MimeTypes.cs
15112

0 commit comments

Comments
 (0)