You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
0 commit comments