Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add binary handlers in DbApiHandler.cs
Short changes: Add supporting of binary requests, and binary-responses. Add binary handlers in _handlers_bin dictionary, in DbApiHandler.cs This handlers assept byte[] from binary-requests, sent with "Content-Type: application/octet-stream". A text-handlers in _handlers dictionary, accept strings only. The reversive encoding "iso-8859-1" (latin1), can encode each one byte into each one char in range "\u0000-\u00FF", so this encoding can be used, to send bytes, as string on the text-handlers. But, by default, there is an utf-8 bytes, so this decoded as UTF-8 text. Add an example text-handlers (text-handler) "echo-string" accept utf-8 encoded string, and echo this as string. this can accept AllBytes as latin-1 string, but return this as utf-8 encoded string. (text-handler) "echo-binary-string" accept AllBytes, as latin1-encoded string, and echo this as binary-response (with "Content-Type: application/octet-stream") (binary-handler) "echo-bytes" accept AllBytes, as binary request (with "Content-Type: application/octet-stream") and echo this as binary-response (with "Content-Type: application/octet-stream") Add test file: \pages\test_echo_handlers.html 44 tests, with example code, how to send the data with different types, and how to decode the responses back. Full changes: \nanodb.exe-source\Server\DbApiHandler.cs Lines 28 : Add Dictionary<string, Func<string,byte[],HttpResponse>> _handlers_bin Lines 34 : Set this as new Dictionary<string, Func<string, byte[], HttpResponse>>(); Lines 96 - 99 : Att three test-handlers for full-server: "echo-string" //return the same string, utf-8 "echo-binary-string" //return bytes of binary string "echo-bytes" //return bytes of binary request Lines 170 - 173 : Add the same handlers for lite-server, but comment this, to prevent DDoS-attack, with large requests. Lines 174 - 181 : Rewrite long line with code. Lines 183 - 187 : Add private HttpResponse EchoString to echo text-string in utf-8 encoding. Lines 189 - 204 : Add private HttpResponse EchoBinaryString to echo text-string in iso-8859-1 (latin1) encoding. Lines 206 - 214 : Add private HttpResponse EchoBytes to echo bytes, that was been send with "Content-Type: application/octet-stream" Lines 216 - 232 : Add public static bool isLatin1String + commented javascript-line. Use _lockObj there. Lines 1136 : Made this _lockObj the private static, because isLatin1String is public static. Lines 1268 : Add comment for HttpResponse Handle Lines 1269-1213 : Rewrite HttpResponse Handle, and add supporting of binary-handlers: "_handlers_bin", that accept byte[], not string. \nanodb.exe-source\Server\HttpConnection.cs Lines 20 : Rename string Request to string RequestText, because this is a text of request. Lines 21 : Rename byte[] Raw to byte[] RequestBytes, because this is a bytes for binary-requests. Lines 24 : string RequestHeadersString, to save headers from TcpServer.cs Lines 25 : Add bool isBinaryRequest = false Lines 27 - 34 : Rewrite arguments one per line. Add two optional arguments: string headers = null, and bool set_binary_request = false Lines 36 : Rename Raw to RequestBytes, because this is a bytes of binary-requests. Lines 37 : Rename Request to RequestText, because this is a text of text-request. Lines 40 - 44 : Set "RequestHeadersString" and "isBinaryRequest" from optional arguments, if this was been specified. Lines 53 : Change condition from (response.IsBinary) to (response.IsBinary == true), to show this must to be true. Lines 54, 56 : Using GetResponseHeaders() instead of GetResponse(); because this is a headers. \nanodb.exe-source\Server\HttpRequest.cs Lines 26 : Add public readonly bool isBinary = false; to mark binary requests. Lines 27 : Add public readonly byte[] Content_bytes; to save bytes of binary-request. Lines 28 : Add public readonly int MaxHeadersSize = 48*1024; to read partial request with headers, from large POST-request. Lines 29 : Add public readonly int MaxBinaryContentStringSize = 4*1024*1024; to limit size of binary string, that can be writted from binary-request. Lines 31 : Add private readonly object _lockObj to lock process Lines 33 - 49 : Move the code to extract headers from "he", into separate method: public Dictionary<string, string> GetHeadersFromString(ref string he). Use lock, there. Lines 51 : Rewrite public HttpRequest(HttpConnection conn), use conn only, there. Lines 53 : Use lock, in public HttpRequest(HttpConnection conn) Lines 55 - 56 : Add two lines, with comments, about state of text-request, and binary-request. Lines 57 - 60 : Extract four values from conn, add commented default values of this, and comments. Lines 62 - 210 : Rewrite the rest code. Add supporting of binary-reqests. Write Content_bytes from bytes for binary-requests, and leave Content = null. use headers, parsed in TcpServer.cs, but do extracting the headers optionally, if this was been not specified, in conn.HeadersString Lines 212 : use method: Headers = GetHeadersFromString(ref he); //get Dictionary <string, string> Headers from "he" \nanodb.exe-source\Server\HttpServer.cs Lines 52 : OnConnectionAdded: Remove old test-code with Console.WriteLine, and use connection only, in arguments of running HttpRequest. Lines 63 : OnConnectionAdded_liet: the same change. Lines 84 : Add line to show request.Headers["Referrer"], but leave this commented. Lines 139 : Rename connection.Request to connection.RequestText, because this was been renamed in HttpConnection.cs \nanodb.exe-source\Server\HttpResponse.cs Lines 15 : Rename private readonly string _response; to private readonly string _headers; because really, this is a headers of response. Lines 18 : Add private readonly bool _isBinary; to set this flag true, when response is binary. Lines 28 - 43 : Rewrite HttpResponse for binary responses, that accept "byte[] bytes" Lines 45 - 75 : Rewrite HttpResponse for text-responses, that accept "string content" Add optional parameters, there. Now this can return bytes, if isBinaryResponse and encoding was been specified. Lines 77 - 82 : Rewrite short version of HttpResponse(string code, string content) Add there an optional parameters bool isBinaryResponse = false, System.Text.Encoding encoding = null Now this return binary responses, as bytes of text, encoded with specified encoding. Lines 84 : Rename GetResponse() to GetResponseHeaders(), because really this return headers. Lines 86 : Rename _response to _headers, because really, this is a headers of response. \nanodb.exe-source\Server\TcpServer.cs Lines 555 : Comment test Console.WriteLine. Lines 557 : Comment test Console.WriteLine. Lines 561 : Add comment Lines 824 - 827 : Add two test Console.WriteLine, but comment this. Lines 861 : Add optional argument "headers", readed in "headers"-variable, to save this in HttpConnection.HeadersString Lines 862 : Add test value of null, there, to test the case when headers not specified. HttpRequest, must to parse this from if this not exists in conn.HeadersString. Lines 864 - 865 : Add two comments about state of requests, when "is_binary_request" not specified. Lines 866 : Add optional argument "is_binary_request" that got value in TcpServer.cs Lines 867 : Add commented the test case, "true", and "false", when this value specified for any request (seems, like ok, in both cases. \pages\test_echo_handlers.html Add this file with 44 tests. This available on http://IP:PORT/pages/test_echo_handlers.html See source code and comments. Make this file readonly, to prevent modifications of the source code. This is just an example of JavaScript code, to send the different types of data on the API Handlers of DbApiHandler.cs. \Changes.txt - Update this file "Changes.txt"
- Loading branch information