Skip to content

Commit beb5bdb

Browse files
authored
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"
1 parent 36708e9 commit beb5bdb

File tree

8 files changed

+3090
-116
lines changed

8 files changed

+3090
-116
lines changed

Changes.txt

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,138 @@ BEGIN CHANGELOG:
99
__________________________________________________________________________________________________________________________________________________________________
1010
(XX.XX.XXXX) commit (latest, unknown hash) ? files changed with ? additions and ? deletions.
1111
__________________________________________________________________________________________________________________________________________________________________
12+
(27.06.2021) commit (latest, unknown hash) 8 files changed with ? additions and ? deletions.
13+
14+
Add binary handlers in DbApiHandler.cs
15+
16+
Short changes:
17+
Add supporting of binary requests, and binary-responses.
18+
Add binary handlers in _handlers_bin dictionary, in DbApiHandler.cs
19+
This handlers assept byte[] from binary-requests, sent with "Content-Type: application/octet-stream".
20+
A text-handlers in _handlers dictionary, accept strings only.
21+
22+
The reversive encoding "iso-8859-1" (latin1), can encode each one byte into each one char in range "\u0000-\u00FF",
23+
so this encoding can be used, to send bytes, as string on the text-handlers.
24+
But, by default, there is an utf-8 bytes, so this decoded as UTF-8 text.
25+
26+
Add an example text-handlers
27+
(text-handler) "echo-string"
28+
accept utf-8 encoded string, and echo this as string.
29+
this can accept AllBytes as latin-1 string, but return this as utf-8 encoded string.
30+
31+
(text-handler) "echo-binary-string"
32+
accept AllBytes, as latin1-encoded string,
33+
and echo this as binary-response (with "Content-Type: application/octet-stream")
34+
35+
(binary-handler) "echo-bytes"
36+
accept AllBytes, as binary request (with "Content-Type: application/octet-stream")
37+
and echo this as binary-response (with "Content-Type: application/octet-stream")
38+
39+
Add test file: \pages\test_echo_handlers.html
40+
44 tests, with example code,
41+
how to send the data with different types,
42+
and how to decode the responses back.
43+
44+
Full changes:
45+
\nanodb.exe-source\Server\DbApiHandler.cs
46+
Lines 28 : Add Dictionary<string, Func<string,byte[],HttpResponse>> _handlers_bin
47+
Lines 34 : Set this as new Dictionary<string, Func<string, byte[], HttpResponse>>();
48+
Lines 96 - 99 : Att three test-handlers for full-server:
49+
"echo-string" //return the same string, utf-8
50+
"echo-binary-string" //return bytes of binary string
51+
"echo-bytes" //return bytes of binary request
52+
Lines 170 - 173 : Add the same handlers for lite-server, but comment this, to prevent DDoS-attack, with large requests.
53+
Lines 174 - 181 : Rewrite long line with code.
54+
Lines 183 - 187 : Add private HttpResponse EchoString to echo text-string in utf-8 encoding.
55+
Lines 189 - 204 : Add private HttpResponse EchoBinaryString to echo text-string in iso-8859-1 (latin1) encoding.
56+
Lines 206 - 214 : Add private HttpResponse EchoBytes to echo bytes, that was been send with "Content-Type: application/octet-stream"
57+
Lines 216 - 232 : Add public static bool isLatin1String + commented javascript-line. Use _lockObj there.
58+
Lines 1136 : Made this _lockObj the private static, because isLatin1String is public static.
59+
Lines 1268 : Add comment for HttpResponse Handle
60+
Lines 1269-1213 : Rewrite HttpResponse Handle, and add supporting of binary-handlers: "_handlers_bin", that accept byte[], not string.
61+
62+
\nanodb.exe-source\Server\HttpConnection.cs
63+
Lines 20 : Rename string Request to string RequestText, because this is a text of request.
64+
Lines 21 : Rename byte[] Raw to byte[] RequestBytes, because this is a bytes for binary-requests.
65+
Lines 24 : string RequestHeadersString, to save headers from TcpServer.cs
66+
Lines 25 : Add bool isBinaryRequest = false
67+
Lines 27 - 34 : Rewrite arguments one per line.
68+
Add two optional arguments: string headers = null, and bool set_binary_request = false
69+
Lines 36 : Rename Raw to RequestBytes, because this is a bytes of binary-requests.
70+
Lines 37 : Rename Request to RequestText, because this is a text of text-request.
71+
Lines 40 - 44 : Set "RequestHeadersString" and "isBinaryRequest" from optional arguments, if this was been specified.
72+
Lines 53 : Change condition from (response.IsBinary) to (response.IsBinary == true), to show this must to be true.
73+
Lines 54, 56 : Using GetResponseHeaders() instead of GetResponse(); because this is a headers.
74+
75+
\nanodb.exe-source\Server\HttpRequest.cs
76+
Lines 26 : Add public readonly bool isBinary = false;
77+
to mark binary requests.
78+
Lines 27 : Add public readonly byte[] Content_bytes;
79+
to save bytes of binary-request.
80+
Lines 28 : Add public readonly int MaxHeadersSize = 48*1024;
81+
to read partial request with headers, from large POST-request.
82+
Lines 29 : Add public readonly int MaxBinaryContentStringSize = 4*1024*1024;
83+
to limit size of binary string, that can be writted from binary-request.
84+
Lines 31 : Add private readonly object _lockObj
85+
to lock process
86+
Lines 33 - 49 : Move the code to extract headers from "he", into separate method:
87+
public Dictionary<string, string> GetHeadersFromString(ref string he).
88+
Use lock, there.
89+
Lines 51 : Rewrite public HttpRequest(HttpConnection conn), use conn only, there.
90+
Lines 53 : Use lock, in public HttpRequest(HttpConnection conn)
91+
Lines 55 - 56 : Add two lines, with comments, about state of text-request, and binary-request.
92+
Lines 57 - 60 : Extract four values from conn, add commented default values of this, and comments.
93+
Lines 62 - 210 : Rewrite the rest code.
94+
Add supporting of binary-reqests.
95+
Write Content_bytes from bytes for binary-requests, and leave Content = null.
96+
use headers, parsed in TcpServer.cs, but do extracting the headers optionally,
97+
if this was been not specified, in conn.HeadersString
98+
Lines 212 : use method: Headers = GetHeadersFromString(ref he); //get Dictionary <string, string> Headers from "he"
99+
100+
\nanodb.exe-source\Server\HttpServer.cs
101+
Lines 52 : OnConnectionAdded: Remove old test-code with Console.WriteLine,
102+
and use connection only, in arguments of running HttpRequest.
103+
Lines 63 : OnConnectionAdded_liet: the same change.
104+
Lines 84 : Add line to show request.Headers["Referrer"],
105+
but leave this commented.
106+
Lines 139 : Rename connection.Request to connection.RequestText, because this was been renamed in HttpConnection.cs
107+
108+
\nanodb.exe-source\Server\HttpResponse.cs
109+
Lines 15 : Rename private readonly string _response; to private readonly string _headers;
110+
because really, this is a headers of response.
111+
Lines 18 : Add private readonly bool _isBinary;
112+
to set this flag true, when response is binary.
113+
Lines 28 - 43 : Rewrite HttpResponse for binary responses, that accept "byte[] bytes"
114+
Lines 45 - 75 : Rewrite HttpResponse for text-responses, that accept "string content"
115+
Add optional parameters, there.
116+
Now this can return bytes, if isBinaryResponse and encoding was been specified.
117+
Lines 77 - 82 : Rewrite short version of HttpResponse(string code, string content)
118+
Add there an optional parameters bool isBinaryResponse = false, System.Text.Encoding encoding = null
119+
Now this return binary responses, as bytes of text, encoded with specified encoding.
120+
Lines 84 : Rename GetResponse() to GetResponseHeaders(), because really this return headers.
121+
Lines 86 : Rename _response to _headers, because really, this is a headers of response.
122+
123+
\nanodb.exe-source\Server\TcpServer.cs
124+
Lines 555 : Comment test Console.WriteLine.
125+
Lines 557 : Comment test Console.WriteLine.
126+
Lines 561 : Add comment
127+
Lines 824 - 827 : Add two test Console.WriteLine, but comment this.
128+
Lines 861 : Add optional argument "headers", readed in "headers"-variable, to save this in HttpConnection.HeadersString
129+
Lines 862 : Add test value of null, there, to test the case when headers not specified.
130+
HttpRequest, must to parse this from if this not exists in conn.HeadersString.
131+
Lines 864 - 865 : Add two comments about state of requests, when "is_binary_request" not specified.
132+
Lines 866 : Add optional argument "is_binary_request" that got value in TcpServer.cs
133+
Lines 867 : Add commented the test case, "true", and "false", when this value specified for any request (seems, like ok, in both cases.
134+
135+
\pages\test_echo_handlers.html
136+
Add this file with 44 tests.
137+
This available on http://IP:PORT/pages/test_echo_handlers.html
138+
See source code and comments.
139+
Make this file readonly, to prevent modifications of the source code.
140+
This is just an example of JavaScript code, to send the different types of data on the API Handlers of DbApiHandler.cs.
141+
142+
\Changes.txt - Update this file "Changes.txt"
143+
__________________________________________________________________________________________________________________________________________________________________
12144
(23.06.2021) commit (latest, unknown hash) 2 files changed with ? additions and ? deletions.
13145

14146
Rewrite TcpServer.cs

nanodb.exe-source/Server/DbApiHandler.cs

Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ class DbApiHandler : IRequestHandler
2525
{
2626
private PostDb _db;
2727
private Dictionary<string, Func<string,string,HttpResponse>> _handlers;
28+
private Dictionary<string, Func<string,byte[],HttpResponse>> _handlers_bin;
2829
private bool is_lite = false; //True, when this is request on lite-server, or false, if this is request on full-server.
2930
public DbApiHandler(PostDb db, bool lite = false, bool SetAllowReput = false, bool SetByPassValidation = false)
3031
{
3132
_db = db;
3233
_handlers = new Dictionary<string, Func<string, string, HttpResponse>>();
34+
_handlers_bin = new Dictionary<string, Func<string, byte[], HttpResponse>>();
3335
is_lite = lite; allowReput = SetAllowReput; bypassValidation = SetByPassValidation; //set this once, for current Instance.
3436
if(lite == false){ //all actions allowed for full server
3537
// filling handlers dictionary with actions that will be called with (request address, request content) args:
@@ -91,6 +93,10 @@ public DbApiHandler(PostDb db, bool lite = false, bool SetAllowReput = false, bo
9193
_handlers["delete_reports"] = DeleteReportsForPostHash;
9294
_handlers["undelete_post"] = UndeletePost; //undelete post, after this was been "deleted_once" on full-server: http://127.0.0.1:7346/api/undelete_post/{POST_HASH}
9395
_handlers["random_captcha"] = RandomCaptcha; //GET random captcha or solve this (POST)
96+
97+
_handlers ["echo-string"] = EchoString; //return the same string, utf-8
98+
_handlers ["echo-binary-string"] = EchoBinaryString; //return bytes of binary string
99+
_handlers_bin ["echo-bytes"] = EchoBytes; //return bytes back
94100
}
95101
else if(lite == true){ //not all actions allowed for lite-server to run this by another anonymous users.
96102

@@ -161,9 +167,70 @@ public DbApiHandler(PostDb db, bool lite = false, bool SetAllowReput = false, bo
161167
// _handlers["png-collect-avail"] = (a,b)=>new HttpResponse(_collectAvail ? StatusCode.Ok : StatusCode.NotFound, _collectAvail ? "Finished." : "Collect...");
162168
_handlers["png-create-avail"] = (a,b)=>new HttpResponse(_createAvail ? StatusCode.Ok : StatusCode.NotFound, _createAvail ? "Finished." : "Creating PNG...");
163169
_handlers["random_captcha"] = RandomCaptcha; //GET random captcha or solve this (POST)
164-
} Set_Timer_To_Delete_Generated_Images();
165-
}private static bool allowReput = false; private static bool bypassValidation = false; /*//true if need to make bypassValidation for posts in containers, while png-collect is processing.*/
170+
171+
// _handlers ["echo-string"] = EchoString; //return the same string, utf-8
172+
// _handlers ["echo-binary-string"] = EchoBinaryString; //return bytes of binary string encoded with iso-8859-1
173+
// _handlers_bin ["echo-bytes"] = EchoBytes; //return bytes back
174+
}
175+
176+
Set_Timer_To_Delete_Generated_Images();
177+
}
178+
179+
private static bool allowReput = false;
180+
private static bool bypassValidation = false;
181+
/*//true if need to make bypassValidation for posts in containers, while png-collect is processing.*/
182+
183+
//echo string, in any encoding.
184+
private HttpResponse EchoString(string stringGET = null, string stringPOST = null){
185+
string data = param_GET_POST(stringGET, stringPOST);
186+
return new HttpResponse(StatusCode.Ok, data);
187+
}
188+
189+
//echo binary string, as binary response
190+
private HttpResponse EchoBinaryString(string stringGET = null, string stringPOST = null){
191+
string data = param_GET_POST(stringGET, stringPOST);
192+
//Console.WriteLine("EchoBinaryString: data.Length: "+data.Length);
193+
194+
System.Text.Encoding encoding = null;
195+
if(isLatin1String(data)){
196+
encoding = System.Text.Encoding.GetEncoding("iso-8859-1");
197+
}
198+
else{
199+
encoding = System.Text.Encoding.UTF8;
200+
}
201+
202+
return new HttpResponse(StatusCode.Ok, data, MimeType.Mime("App", encoding), true, encoding); //just return back, the binary string with bytes, in HTTP response.
203+
//"true", means response is binary, even if this is a string.
204+
}
205+
206+
//echo bytes from binary request.
207+
private HttpResponse EchoBytes(string path = null, byte[] bytes = null){
208+
try{
209+
return new HttpResponse(StatusCode.Ok, bytes, MimeType.Mime("App")); //just return binary data in HTTP response.
210+
}catch(Exception ex){
211+
Console.WriteLine(ex);
212+
return new HttpResponse(StatusCode.Ok, ex.Message, MimeType.Mime("Html")); //just return binary data in HTTP response.
213+
}
214+
}
166215

216+
//C#: check is string "iso-8859-1"-encoded or not (true/false)
217+
public static bool isLatin1String(string str){
218+
lock (_lockObj)
219+
{
220+
return (
221+
Regex.Match(
222+
str //for each char in str
223+
, @"[^\u0000-\u00FF]" //try to match not latin1-char, and stop
224+
, RegexOptions.None
225+
)
226+
.Length //then take Length of result
227+
) == 0 //and compare this to 0
228+
; //true/false (non-latin character was not found or was found)
229+
}
230+
}
231+
// JavaScript:
232+
//function isLatin1String(str){return (str.match(/[^\u0000-\u00FF]/) === null);} //check is string "iso-8859-1"-encoded or not (true/false)
233+
167234
/*
168235
This method return string with parameters, from request parameters.
169236
It return first or second parameter of request query (if second is available).
@@ -1066,7 +1133,7 @@ private HttpResponse ReAddPost(string replyTo, string content)
10661133
return new HttpResponse(StatusCode.Ok, JsonConvert.SerializeObject(post));
10671134
}
10681135

1069-
private object _lockObj = new object();
1136+
private static readonly object _lockObj = new object();
10701137

10711138
//Delete post "delete_once", or "delete_forever", if post was already "deleted_once".
10721139
private HttpResponse DeletePost(string hash, string notUsed = null)
@@ -1198,6 +1265,7 @@ private HttpResponse UndeletePost(string hash, string notUsed = null)
11981265
);
11991266
}
12001267

1268+
//Handle API-request, and return HttpResponse.
12011269
public HttpResponse Handle(HttpRequest request)
12021270
{
12031271
try
@@ -1206,20 +1274,41 @@ public HttpResponse Handle(HttpRequest request)
12061274
var cmd = splitted.Length < 2 ? "" : splitted[1];
12071275
var arg = splitted.Length < 3 ? "" : splitted[2];
12081276

1209-
if (_handlers.ContainsKey(cmd))
1210-
{
1211-
return _handlers[cmd](arg, request.Content);
1277+
if( //if
1278+
_handlers.ContainsKey(cmd) //handler contains in _handlers Dictionary
1279+
// && request.isBinary == false //and if it's not binary-request
1280+
//this was been commented, because binary requests can be accepted by handlers, as iso-8859-1 string, too.
1281+
){
1282+
return _handlers[cmd](arg, request.Content); //send request to handler, and return response.
1283+
//for binary-request, send request to "handler" with "iso-8859-1"-encoded string, and return response.
12121284
}
12131285

1214-
else
1286+
else if ( //else if
1287+
_handlers_bin.ContainsKey(cmd) //and if handler contains in binary handlers
1288+
)
12151289
{
1216-
return new ErrorHandler(StatusCode.BadRequest, "No such command: " + cmd + ". Available commands: " + JsonConvert.SerializeObject(_handlers.Keys.ToArray())).Handle(request);
1290+
if( //if
1291+
request.Content_bytes == null //but content not found
1292+
|| ( //or
1293+
(request.Content_bytes != null) //if it's found
1294+
&& (request.Content_bytes.Length == 0) //but empty
1295+
)
1296+
&& request.Content.Length != 0 //and if text found
1297+
){
1298+
// Console.WriteLine("request.Content"+request.Content); //it seems, like "iso-8859-1"-text sent, as string.
1299+
byte[] request_bytes = MimeType.DefaultBinaryEncoding.GetBytes(request.Content); //decode this to bytes, using reversive latin1 encoding.
1300+
return _handlers_bin[cmd](arg, request_bytes); //and send bytes to binary-handler, and return the binary response
1301+
}
1302+
else{ //else if it's was been bytes (Content-Type: application/octet-stream), and request.isBinary == true
1303+
return _handlers_bin[cmd](arg, request.Content_bytes); //send bytes to binary-handler, and return response
1304+
}
12171305
}
1306+
//else if "_handlers" or "_handlers_bin" dictionaries not contains handler "cmd" - return an error:
1307+
return new ErrorHandler(StatusCode.BadRequest, "No such command: " + cmd + ". Available commands: " + JsonConvert.SerializeObject(_handlers.Keys.ToArray())).Handle(request);
12181308
}
1219-
12201309
catch (Exception e)
12211310
{
1222-
return new ErrorHandler(StatusCode.InternalServerError, e.Message).Handle(request);
1311+
return new ErrorHandler(StatusCode.InternalServerError, e.Message).Handle(request); //or return an exception-message
12231312
}
12241313
}
12251314
}

0 commit comments

Comments
 (0)