forked from nanoboard/nanoboard
/
TcpServer.cs
913 lines (819 loc) · 40.9 KB
/
TcpServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
using System;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using NDB; //to run _db.UploadPost(post_in_JSON) here.
using System.Linq; //byteArray.ToList()
namespace NServer
{
/*
Low-level limited implementation of HTTP server based on TCP socket.
*/
class TcpServer
{
private TcpListener _server; //full-server
private Boolean _isRunning; //is running full server or not?
private TcpListener _server_lite; //lite-server
private Boolean _isRunning_lite; //is running lite server or not?
private bool lite_server = false; //by default, lite-server is enabled. Run nanodb as "nanodb old" to disable it, or "nanodb lite" (to enable).
private string no_tcp_delay = Configurator.Instance.GetValue("no_tcp_delay", "true"); //get value once to compare with it
public event Action<HttpConnection> ConnectionAdded; //for full-server
public event Action<HttpConnection> ConnectionAdded_lite; //for lite-server
//modes to working with large JSON data from POST-request to "../api/upload-posts/"
//True, if need to Upload_Posts from large POST-request, by reading JSON from TCP-stream, after send POST-request with large JSON data. Else - false.
// Warning, in this case, client can be disconnected.
private bool Upload_Posts_From_TCP_Stream = false; //start "nanodb.exe large_POST_mode1", (default mode)
//True, if need to Save the large data from POST request to cache-file, and Upload_Posts then, from file. Else - false.
private bool Save_In_Cache_File_The_Large_POST_Request = false; //start "nanodb.exe large_POST_mode2",
//True, if need to Upload_Posts from file with JSON-cache
// This value will be automatically true, if POST-request with JSON will send to /api/upload-posts/, and if request saved in cache-file.
private bool Upload_Posts_From_File_With_JSON_Cache = false;
//To disable auto-Uploading posts post-by-post, run "nanodb.exe lite large_POST_mode0" (I got OutOfMemoryException for large POST-requests with JSON over 30 MB in tests);
private string path = @"temp/Saved_cache_of_Large_POST_request_"+((string)(((Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds)).ToString()))+".txt"; //use unique filename: unix-timestamp
private NDB.PostDb _db; //to run _db.UploadPost(post_in_JSON).
public static bool allowReput = false;
public static bool bypassValidation = false; //set skip validate pow and captcha? (true if yes, or false, by default)
object _lock = new object(); //object to lock code for one stream
public static bool do_not_delete_cache_file = false; //if no need to delete cache file, and need to generate new file to store posts as JSON. large_POST_mode3
public TcpServer(
NDB.PostDb db
, string ip
, int port
, bool enable_lite_server = false
, string large_POST_mode = "0"
, bool setAllowReput = false
, bool setByPassValidation = false
) //lite_server and mode - from Program.cs
{
if ( large_POST_mode == "1" ) {
Console.WriteLine("\nMode 1:\nPosts will be uploaded one by one,\nfrom TCP stream, if POST data will be large.\n");
Upload_Posts_From_TCP_Stream = true;
Save_In_Cache_File_The_Large_POST_Request = false;
}else if ( large_POST_mode == "2" || large_POST_mode == "3") {if(large_POST_mode == "3"){do_not_delete_cache_file = true;}
Console.WriteLine("\nMode "+((do_not_delete_cache_file == true)?"3":"2")+":\nPosts will be cached in cache-file"+((do_not_delete_cache_file == true)?"(s)":"")+" in \"temp\"-folder\nif POST data will be large, then uploaded from cache.\n");
Upload_Posts_From_TCP_Stream = false;
Save_In_Cache_File_The_Large_POST_Request = true;
}else{
Console.WriteLine("\nMode 0 (old mode, OutOfMemoryException):\nPosts will be uploaded after all POST-data,\neven if data will be large.\n");
Upload_Posts_From_TCP_Stream = false;
Save_In_Cache_File_The_Large_POST_Request = false;
} allowReput = setAllowReput; bypassValidation = setByPassValidation;
lite_server = enable_lite_server;
// Console.WriteLine("TcpServer.cs. TcpServer. lite_server: "+lite_server+", large_POST_mode: "+large_POST_mode);
_db = db;
Console.WriteLine("Listening on port " + port + " (private server)");
if(lite_server == true){
Console.WriteLine("Listening on port " + (port+1) + " (public server_lite)");
}
if (ip == "localhost")
{
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
Console.WriteLine(ipAddress.ToString() + ":" + port);
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, port);
_server = new TcpListener(ipLocalEndPoint);
if(lite_server == true){
IPEndPoint ipLocalEndPoint_lite = new IPEndPoint(ipAddress, port+1);
_server_lite = new TcpListener(ipLocalEndPoint_lite);
}
}
else
{
_server = new TcpListener(IPAddress.Parse(ip), port);
if(lite_server == true){
_server_lite = new TcpListener(IPAddress.Parse(ip), port+1);
}
}
}
public void Run()
{
// Console.WriteLine("TcpServer.cs. Run. lite_server: "+lite_server);
_server.Start();
_isRunning = true;
if(lite_server == true){
_server_lite.Start();
_isRunning_lite = true;
}
LoopClients();
}
public void Stop()
{
// Console.WriteLine("TcpServer.cs. Stop. lite_server: "+lite_server);
Console.WriteLine("Server was shut down");
_isRunning = false;
_server.Stop();
if(lite_server == true){
_isRunning_lite = false;
_server_lite.Stop();
}
}
private void LoopClients()
{
// Console.WriteLine("TcpServer.cs. LoopClients. lite_server: "+lite_server);
if(lite_server == false){ //if lite_server is disabled - listen only main full-server in one loop
while (_isRunning)
{
if (!_server.Pending())
{
Thread.Sleep(100);
continue;
}
try
{
TcpClient newClient = _server.AcceptTcpClient();
Thread t = new Thread(()=>HandleClient(newClient, lite_server)); //here is full-server so second parameter is = false (is_lite_server?)
t.Start();
}
catch
{
}
}
}
else{ //else listen both servers full server and lite-server.
bool continue1 = false;
bool continue2 = false;
while (_isRunning || _isRunning_lite)
{
if (!_server.Pending())
{
Thread.Sleep(100);
continue1 = true;
}
if(continue1 == false){
try
{
TcpClient newClient = _server.AcceptTcpClient();
Thread t = new Thread(()=>HandleClient(newClient, continue1)); //here is full-server so second parameter is = false (is_lite_server?)
t.Start();
}
catch
{
}
}
if (!_server_lite.Pending())
{
Thread.Sleep(100);
continue2 = true;
}
if(continue2 == false){
try
{
TcpClient newClient_lite = _server_lite.AcceptTcpClient();
Thread t_lite = new Thread(()=>HandleClient(newClient_lite, !continue2)); //here is lite-server, so second parameter is true (is_lite_server?)
t_lite.Start();
}
catch
{
}
}
continue1 = false;
continue2 = false;
}
//end the loop to listen two servers
}
}
private bool Save_Large_data_From_POST_Request(string cached_data, string pathway = ""){
string specified_path = ((pathway != "")? pathway : path);
// This text is added only once to the file.
if (!File.Exists(specified_path))
{
// Create a file to write to.
string[] createText = { cached_data };
File.WriteAllLines(specified_path, createText, Encoding.UTF8);
}
// This text is always added, making the file longer over time
// if it is not deleted.
//cached_data = cached_data + Environment.NewLine;
else{
long cache_length = new System.IO.FileInfo(specified_path).Length;
if(cache_length >= 256 * 1024 * 1024){ //256 MB - max file-size for cache-file.
return false;
}else{
File.AppendAllText(specified_path, cached_data, Encoding.UTF8);
}
}
return true;
}
//UploadPosts as JSON, from (string) readData, parse this string, then update (int) accepted_post, and (int) uploaded_posts,
//and if need (bool) substring_end string from '"}]' to '"', and add '}', then, parse it and UploadPost from parsed JSON.
private Object[] Upload_Posts_from_JSON_string(string readData, int accepted_posts, int uploaded_posts, bool substring_end){
lock(_lock){
if(
readData.Contains("\"}]") //if this is full array with posts
&& readData.Contains("[{\"hash\":\"") //and if this array is beginning from this
){
//cut POST headers from second request, cached and appended in the same file.
Console.WriteLine("Cut headers for another request.");
readData = (readData.Split(new string[] { "\"}]" }, StringSplitOptions.None)[0]) //cut '"}]' in the end of array from previous cached POST-request.
+"\"},{\"hash\":\"" //add this in the end: '"}]' -> '"},{"hash":"'
+readData.Split(new string[] { "[{\"hash\":\"" }, StringSplitOptions.None)[1] //split by '[{"hash":"', and extract second part.
;
//after this, readData must to be replaced, like this:
//...blahblah-the-end-of-array-with-posts-from-previous-cached-request"}]\nBLAH-BLAH-CACHED-HEADERS-OF-THE-CURRENT-REQUEST\n[{"hash":"blahblah-array-with-posts-of-current-request"}]
//->
//...blahblah-the-end-of-array-with-posts-from-previous-cached-request"},{"hash":"blahblah-array-with-posts-of-current-request"}]
//and after this all, this data can be splitted by '},{', to extract each post, and make PutPost for this one-by-one.
}
string [] output = readData.Split(new string[] { "},{" }, StringSplitOptions.None); //split string like '...post},{post},{post},{post...' by '},{', to extract posts.
// Console.WriteLine("Upload_Posts_from_JSON_string: after split readData by (\"},{\"), output.Length: "+output.Length);
string the_rest = "";
for(var i = 1; i<=output.Length; i++){
if(i == output.Length){
if(substring_end && output[(i-1)].EndsWith("\"}]")){
output[(i-1)] = output[(i-1)].Substring(0, output[(i-1)].Length-2);
}
}
try{
if(output[(i-1)].EndsWith("\"")){
string post_in_JSON = ((output[(i-1)].StartsWith("{")?"":"{")+output[(i-1)]+"}"); //add "{" and "}" for all not first elements, because first element contains '{"hash":"'
try{
Post post_data = Newtonsoft.Json.JsonConvert.DeserializeObject<Post>(post_in_JSON);
// Console.Write((accepted_posts+i)+": Try uploading post from request data: №i: "+i);
bool status = _db.UploadPost(post_in_JSON, allowReput, bypassValidation);
if(status==true){
uploaded_posts += 1;
// Console.Write(" TRUE!\n");
}else{
// Console.Write(" False.\n");
}
}catch // (Exception except)
{
accepted_posts -= 1; //remove this part from the number of accepted posts.
if(i == output.Length){
the_rest = (output[(i-1)].StartsWith("{")?"":"{")+output[(i-1)]; //add "{" in the last partial JSON, because "{" not added for first element.
}
}
}else if(i == output.Length){
the_rest = (output[(i-1)].StartsWith("{")?"":"{")+output[(i-1)]; //add "{" in the last partial JSON, because "{" not added for first element.
accepted_posts -= 1; //remove this part from the number of accepted posts.
//add this in beginning of next part, in the future, to try parse and upload the whole post.
}
}
catch{
if(i == output.Length){
the_rest = (output[(output.Length-1)].StartsWith("{")?"":"{")+output[(output.Length-1)];
readData = the_rest; //leave the rest in readData, and continue to read...
}
break;
}
if(i == output.Length){
readData = the_rest; //leave the rest in readData, and continue to read...
}
}
accepted_posts += output.Length;
return new Object[]{(string)readData, (int)accepted_posts, (int)uploaded_posts}; //return Object array with different types.
}
}
/*
UploadPosts from JSON in cached file.
*/
private string[] Upload_Posts_From_Saved_File(string path, string headers, bool do_not_delete_cache_file = false){ //file path, and headers for response.
string readData = ""; //string to return
int accepted_posts = 0; //number of accepted posts to return
int uploaded_posts = 0; //number of uploaded posts to return
int _bufferSize = 16384; //buffer size (default 16KB * 1024B/KB)
bool is_first_block = true; //number of block (will be incremented)
int large_limit_bytes = 131072; //8 blocks.
if( new FileInfo( path ).Length == 0 ) //if file.Length == 0
{
string error = "TcpClient.cs. Upload_Posts_From_Saved_File. File "+path+" is empty! return."; //save this error
Console.WriteLine(error); //show this in console.
return new string[]{error, path}; //and return as string.
}
StringBuilder stringBuilder = new StringBuilder(); //define stringBuilder.
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read); //Open the file in path in fileStream
string trimmed_block = ""; //define this string to save trimmed block in the end.
using (StreamReader streamReader = new StreamReader(fileStream)) //use streamReader to read fileStream.
{
char[] fileContents = new char[_bufferSize]; //Define char array with length of _bufferSize.
int charsRead = _bufferSize; //define int variable to save readed chars (_bufferSize by defalt)
while (charsRead > 0) //while chars reading, and not end of blocks
{
fileContents = new char[charsRead]; //resize char array up to readed chars
try{ //try
charsRead = streamReader.Read(fileContents, 0, _bufferSize); //to read characters by blocks _bufferSize.
}
catch{ //on catch
break; //just break
}
trimmed_block = (new string(fileContents)).Substring(0, charsRead); //trim last block from null bytes, by substring this string
stringBuilder.Append(trimmed_block); //append each trimmed_block to string builder, and let his length growing.
if(is_first_block == true){ //for first block (true at starting)
readData = stringBuilder.ToString(); //get readData
string [] splitted = readData.Split(new string[] { "[{\"hash\":\"" }, StringSplitOptions.None); //make one split, to get two elements in array.
readData = "{\"hash\":\""+splitted[1]; //extract the part of JSON, and skip headers.
stringBuilder.Length = 0; //clear stringBuilder
stringBuilder.Capacity = 0; //clear stringBuilder
stringBuilder.Append(readData); //append modified readData in clean stringBuilder.
is_first_block = false; //Do not repeat this all anymore.
}
if(stringBuilder.Length >= (large_limit_bytes) || (streamReader.Peek() < 0)){ //if length of StringBuilder need to reduce, or if no any next character in file
Object [] result_of_uploading = Upload_Posts_from_JSON_string //upload posts from JSON
(
stringBuilder.ToString(), //put the partial JSON from stringBuilder, as string.
accepted_posts, //put this statistic variable
uploaded_posts, //and this
( ( streamReader.Peek() < 0 ) ? true : false ) //for last record true, when no any more characters in fileStream.
)
;
readData = (string) result_of_uploading[0]; //extract readData as result of uploading posts
accepted_posts = (int) result_of_uploading[1]; //extract the number of accepted posts
uploaded_posts = (int) result_of_uploading[2]; //extract the number of successfully uploaded posts.
stringBuilder.Length = 0; //clear string builder
stringBuilder.Capacity = 0; //Length and Capacity
stringBuilder.Append(readData); //and append the rest of result to stringBuilder, for another iteration.
if( streamReader.Peek() < 0 ){ //for last part of file, with full JSON
if(readData == ""){ //check is this empty string, and if empty
readData = (string)( //make response with headers
((string)result_of_uploading[0] == "")
? (headers+"posts_uploading_large_data: ["+accepted_posts+", "+uploaded_posts+"]")
: headers+result_of_uploading[0]
)
;
}
break; //and break from cycle.
}//else - continue
}else{
//in this case of small stringBuilder - continue filling the stringBuilder with JSON, in following next iterations.
}
} //Do this all in cycle, while blocks available in file.
} //and do not using streamReader.
if(do_not_delete_cache_file == false){
File.Delete(path);
}
else{
path = @"temp/Saved_cache_of_Large_POST_request_"+
(
(string)
(
(Int32)
(
DateTime.UtcNow.Subtract(
new DateTime(1970, 1, 1)
)
)
.TotalSeconds
)
.ToString()
)
+".txt"
; //then delete file with cached JSON, or generate new filename, and don't delete previous file.
}
_db.ReadRefs_private();
return new string[]{readData, path}; //and return response with headers.
}
private static bool data_still_writting = false; //this will be true, while data is writting in NetworkStream, then - false.
private static bool Network_stream_writting_error = false; //this will be true, when error writting, and false, after .Dispose() NetworkStream.
private static void async_callback(IAsyncResult async_result){ //this is async-callback for stream.BeginWrite(), this will be runned on start write.
NetworkStream client_NetworkStream = (NetworkStream)(async_result).AsyncState;
try{
client_NetworkStream.EndWrite(async_result); //this command will wait write all data, after stream.BeginWrite, and runing this callback function.
// Console.WriteLine((DateTime.Now.ToString("HH:mm:ss.ffffff"))+", data_still_writting = false... no catch...");
data_still_writting = false; //now, turn this false;
}
catch
{
// Console.WriteLine((DateTime.Now.ToString("HH:mm:ss.ffffff"))+", data_still_writting = false... catch...");
data_still_writting = false; //now, turn this false;
//if error, do nothing
}
}
private bool Write_bytes_in_TCPSocket_block_by_block(NetworkStream stream, byte[] bu){
//The MSDN documentation says the default size of the send and receive buffers for TcpClient is 8192 bytes, or 8K.
//The documentation does not specify a limit as to how big these buffers can be.
int write_block_size = 8192; //8 KB block //lagging without async-write
int blocks = bu.Length / write_block_size;
int rest_length = bu.Length % write_block_size;
for(int block_num = 0; (block_num * write_block_size)<=bu.Length; block_num += 1){
while(
!stream.CanWrite
|| data_still_writting == true
|| Network_stream_writting_error == true
){ //if cann't write to the stream, or if data still writting
Thread.Sleep(1); //just sleep 1 millisecond
}
lock(_lock){
try{
data_still_writting = true; //set this true
// Console.WriteLine((DateTime.Now.ToString("HH:mm:ss.ffffff"))+", still_writting...");
//BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)
//Begins an asynchronous write to a stream.
stream.BeginWrite(
bu, //buffer
(block_num * write_block_size), //start offset in buffer
( ( block_num == blocks ) ? rest_length : write_block_size), //data length to write
new AsyncCallback(async_callback), //callback
stream //NetworkStream
); //NOW OK.
}
catch //(Exception ex) //get exception to show it
{
data_still_writting = false;
Network_stream_writting_error = true;
return false;
}
}
// Console.WriteLine("unlock...");
}
data_still_writting = false;
Network_stream_writting_error = false;
return true;
}
private void TryToApplyTcpDelay(
ref bool noTcpDelay
, ref List<byte> raw
)
{
if (!noTcpDelay){
if (raw.Count == 0 || raw[0] == (byte)'P'){ //on start reading, or if POST-request
//Console.WriteLine("Byte 0 == 'P':"+(raw[0] == (byte)'P')+", and this seems, like a POST-request");
Thread.Sleep(50); //set delay, and wait 50 milliseconds...
}
}
}
private void AddReadedBytesFromBlock(
ref byte[] block
, ref int len
, ref string readData
, ref List<byte> raw
, ref System.Text.Encoding enc //latin1 for binary requests, and utf8 for text-requests
)
{
//If text request
if( readData != null )
{
string BlockAsString = enc.GetString(block, 0, len);
readData += BlockAsString;
}
//If binary-request
if( raw != null )
{
for (int i = 0; i < len; i++){ //and for only readed bytes (null bytes contains in buffer too)
raw.Add(block[i]); //add bytes to list of bytes.
}
}
}
private void TryToExtractHeaderFromRequest(
ref bool isHeadersReading
, ref int block_size
, ref int MaxHeadersSize
, ref int readed_blocks
, ref bool is_binary_request
, ref string headers //here can be null
, ref string readData //here can be null
, ref System.Text.Encoding enc //here can be null
, ref List<byte> raw //here can be null
)
{
if(isHeadersReading == true){
if((readed_blocks * block_size <= MaxHeadersSize)) //if number of readed blocks
{
if(readData.IndexOf("\r\n\r\n") == -1){
headers += readData;
}
else{
headers += readData
.Split( //split readData
new string[]{ "\r\n\r\n" } //by "\r\n\r\n"
,
StringSplitOptions.None //this is a string separator, between headers and content
)[0] //and take the first part, with headers
;
isHeadersReading = false;
}
}
else{
isHeadersReading = false;
}
}
//When headers already reading
//Check is binary or or text request
if(isHeadersReading == false){
if(
(headers != null)
&& (headers.Contains("Content-Type: application/octet-stream"))
){
is_binary_request = true;
if(readData != null){
if(raw != null){
raw = ((byte[])MimeType.DefaultBinaryEncoding.GetBytes(readData)).ToList();
}
readData = null;
}
else{
//leave raw as is, if this exists.
readData = null; //and leave this null.
}
}
else{
if(readData == null){
if(raw != null){
// Console.WriteLine("TcpServer.cs. Was been: readData: "+readData);
readData = MimeType.DefaultTextEncoding.GetString(raw.ToArray()); //change encoding to UTF-8
// Console.WriteLine("TcpServer.cs. after change encoding, readData: "+readData);
raw = null;
}
//else leave raw and readData as is
}
//else leave readData as is, or null.
}
enc = (is_binary_request == true) //and set the current encoding
? MimeType.DefaultBinaryEncoding
: MimeType.DefaultTextEncoding
;
}
}
private void TryUpdaterequestLength ( //try to update
ref bool isHeadersReading // if header reading ended (true/false)
, ref int requestLength //this value
, ref string headers //from headers
, int readDataLength //or optionally, from readDataLength
)
{
if( (headers != null) && (isHeadersReading == false)){
if (requestLength == 0)
{
if(headers.Contains("Content-Length"))
{
var match = System.Text.RegularExpressions.Regex.Match(headers, "Content-Length: [0-9]+");
if (match.Success)
{
string MatchedValue = match.Value;
//Console.WriteLine("MatchedValue: "+MatchedValue);
string ValueFromHeaders = MatchedValue.Split(' ')[1];
//Console.WriteLine("ValueFromHeaders: "+ValueFromHeaders);
//requestLength = (int) int.Parse(ValueFromHeaders);
requestLength = nbpack.NBPackMain.parse_number(ValueFromHeaders); //add content-Length
requestLength += headers.Length; //and add length of headers
}
}
else if(readDataLength > 0){
requestLength += (int)readDataLength;
}
}
}
}
private void TryToLoadLargeRequestWithPostsInJSON(
ref string readData
, ref List<byte> raw
, ref bool posts_uploading_large_data
, ref int large_limit_bytes
, ref int temp_readData_length
, ref string headers
, ref int uploaded_bytes
, ref bool too_large
, ref int accepted_posts
, ref int uploaded_posts
, ref int round_uploading
, ref int large_size
, ref System.Text.Encoding enc
)
{
enc = MimeType.DefaultTextEncoding; //use utf8
if(readData.Length >= ( large_limit_bytes ) ){ //if bytelimit to accept JSON.
temp_readData_length = readData.Length; //save readData.Length
if(readData.Contains("/api/upload-posts") && Upload_Posts_From_TCP_Stream == true){ //if this was been request to "/api/upload-posts/" and need to UploadPost from stream...
posts_uploading_large_data = true; //turn this to on, true.
string [] splitted = readData.Split(new string[] { "\r\n\r\n[{\"hash\":\"" }, StringSplitOptions.None); //make one split, and get two elements in array.
headers = splitted[0]; //extract headers and save it
readData = "{\"hash\":\""+splitted[1]; //extract JSON, and skip headers.
uploaded_bytes += headers.Length + 1; //headers + '[';
}else if(posts_uploading_large_data == true || too_large == true){ //if flag already specified.
//just continue
}else if(Save_In_Cache_File_The_Large_POST_Request == true){ //if this mode
string [] splitted = readData.Split(new string[] { "\r\n\r\n[{\"hash\":\"" }, StringSplitOptions.None); //make one split, and two elements in array.
headers = splitted[0]; //extract headers and save it
too_large = true; //set this flag
if(readData.Contains("/api/upload-posts/")){ //if this was been request to "/api/upload-posts/"
Upload_Posts_From_File_With_JSON_Cache = true; //turn this on to save the file.
}
}
if(posts_uploading_large_data == true){ //if mode 1
Object [] result_of_uploading = Upload_Posts_from_JSON_string(readData, accepted_posts, uploaded_posts, false); //try to addPosts from readData.
readData = (string) result_of_uploading[0]; //update this value
accepted_posts = (int) result_of_uploading[1]; //update this value
uploaded_posts = (int) result_of_uploading[2]; //update this value
raw = new List<Byte>(enc.GetBytes(readData)); //update this value
round_uploading += 1; //increment it
uploaded_bytes += temp_readData_length - ( ( round_uploading == 1 ) ? headers.Length : 0 ) - readData.Length; //update uploaded_bytes
}else if(too_large == true){ //if this just large file
large_size += readData.Length; //update large_size
Save_Large_data_From_POST_Request(readData); //write the part of request (or JSON) in file, to UploadPosts from this file.
readData = ""; //set this as empty string
raw = new List<byte>(); //clear byte-array.
}
}
}
private void TryToUploadIntoDataBaseThePostsFromLoadedRequestWithJSON(
ref bool posts_uploading_large_data
, ref string readData
, ref List<byte> raw
, ref string headers
, ref int accepted_posts
, ref int uploaded_posts
, ref bool too_large
)
{
if(posts_uploading_large_data == true){ //if need to UploadPosts while reading TcpStream, here is the rest.
try{ //try to do this
Object [] result_of_uploading = Upload_Posts_from_JSON_string(readData, accepted_posts, uploaded_posts, true); //upload posts from the rest.
accepted_posts = (int)result_of_uploading[1]; //update this value
uploaded_posts = (int)result_of_uploading[2]; //and this
readData = (string)(
(
(string)result_of_uploading[0] == "") //if empty string returned.
? (headers+"posts_uploading_large_data: ["+accepted_posts+", "+uploaded_posts+"]") //return this
: headers+result_of_uploading[0] //or this
);
raw = new List<Byte>(Encoding.ASCII.GetBytes(readData)); //update raw - list of bytes.
}
catch (Exception exception){ //on catch
Console.WriteLine("TcpServer.cs. HandleClient. Exception: "+exception); //show exception
}
posts_uploading_large_data = false; //and turn off this now.
}else if(too_large == true && Upload_Posts_From_File_With_JSON_Cache == true){ //if this was been large data, and if this is was bee request to "/api/upload-posts/"
//just read the cached query from cache file, and remove this file.
Save_Large_data_From_POST_Request(readData); //write the rest JSON, in the cache-file, to UploadPost from this file.
Console.WriteLine("Try to load posts from json in file.");
string[] result_of_upload = Upload_Posts_From_Saved_File(path, headers, do_not_delete_cache_file);
readData = result_of_upload[0];
path = result_of_upload[1];
//after this need to read this file, and upload posts from this
//(maybe need to use global variables path and do_not_delete_cache_file),
//and don't pass this as arguments.
raw = new List<Byte>(Encoding.ASCII.GetBytes(readData)); //then update this value, from new readData value
Upload_Posts_From_File_With_JSON_Cache = false; //turn off this, now.
Console.WriteLine("Done."); //show this.
}else if(too_large == true){ //if was been writted in cache another large request-data
System.IO.File.WriteAllText(path, string.Empty); //just clear the file
too_large = false; //and turn off this now, and do not do nothing.
}
}
private void WriteBytesOfResponse(
ref NetworkStream stream
, ref byte[] ba
, ref byte[] bu
, ref TcpClient client
, ref string write_function
)
{
try
{ //send response for client.
stream.Write(ba, 0, ba.Length); //write headers
bool was_writted = Write_bytes_in_TCPSocket_block_by_block(stream, bu); //use separate method, to write response block-by-block
stream.Dispose(); //clear memory from stream.
if( was_writted == false ){ //if error
client.Close(); //close client
Network_stream_writting_error = false; //and turn this off
}
}
catch (Exception ex) //if exception
{
Console.WriteLine("TcpServer.cs. HandleClient. ConnectionAdded. in "+ write_function +".\nException:\n"+ex); //show this
}
finally
{
stream.Dispose(); //clear memory from stream.
client.Close(); //and close client
}
}
private void HandleClient(TcpClient client, bool is_lite_server) //HandleClient on the full-server and on the lite-server.
{
NetworkStream stream = client.GetStream(); //Get NetworkStream from TcpClient
String readData = ""; //empty string with request data, as string.
bool noTcpDelay = (no_tcp_delay == "true"); //dont read config-3.json every time, again and again, and just compare with already extracted value.
stream.ReadTimeout = noTcpDelay ? 15 : 100; //set different TCP-delay
int block_size = 16384; //number of bytes for block-size, to read data block by block.
byte[] block = new byte[block_size]; //define empty block 16KBytes
int len = -1; //define this
int requestLength = 0; //this value will be updated from Content-Length, in headers (if exists).
List<byte> raw = new List<byte>(); //list with bytes of request-data.
bool too_large = false; //this will be true, when data of request is too large.
int large_size = 0; //bytesize, bytelength of large request data.
int large_limit_bytes = 4*1024*1024; //this is a bytelimit for small requests.
// int large_limit_bytes = 131072; //test small bytelimit.
string headers = null; //this string will contains headers of large request with posts in JSON which is sent on "../api/upload-posts/"
bool posts_uploading_large_data = false; //this will be true if Upload_Posts_From_TCP_Stream == true, and large data in POST-query on "../api/upload-posts/"
int accepted_posts = 0; //this is a number of accepted posts to uploading this.
int uploaded_posts = 0; //this is a number of posts, which are sucessfully added in DataBase, after UploadPost.
int uploaded_bytes = 0; //this is a number of uploaded bytes.
int round_uploading = 0; //this is a number of round uploading.
int temp_readData_length = 0; //this is a readData.Length, before all modifications of this string.
bool is_binary_request = false; //binary - true, text - false (by default);
int MaxHeadersSize = 48*1024; //48 KiloBytes is max size for headers;
int readed_blocks = 0; //0 on start.
int MaxForHeader = readed_blocks * block_size; //Max blocks for header.
bool isHeadersReading = true; //false, when reading data.
System.Text.Encoding enc = null; //Define this variable, to set it then.
do
{
try
{
enc = MimeType.DefaultBinaryEncoding; //latin1 (iso-8859-1) is ASCII-compatible encoding for headers, UTF8-bytes, and binary-requests.
TryToApplyTcpDelay(ref noTcpDelay, ref raw); //wait if need
if(!client.Connected || !stream.DataAvailable){
break;
}
len = stream.Read(block, 0, block.Length);
readed_blocks++; //add 1 to this value, if block sucessfully readed.
AddReadedBytesFromBlock(ref block, ref len, ref readData, ref raw, ref enc);
TryToExtractHeaderFromRequest( ref isHeadersReading, ref block_size, ref MaxHeadersSize, ref readed_blocks, ref is_binary_request, ref headers, ref readData, ref enc, ref raw);
TryUpdaterequestLength( ref isHeadersReading, ref requestLength, ref headers, ( (readData != null) ? readData.Length : 0 ) );
TryToLoadLargeRequestWithPostsInJSON(
ref readData, ref raw, ref posts_uploading_large_data, ref large_limit_bytes, ref temp_readData_length,
ref headers, ref uploaded_bytes, ref too_large, ref accepted_posts, ref uploaded_posts,
ref round_uploading, ref large_size, ref enc
);
}
catch //(Exception ex)
{
//Console.WriteLine("TcpServer.cs, HandleClient, do-while, catch: "+ex);
TryToApplyTcpDelay(ref noTcpDelay, ref raw); //wait if need
TryToExtractHeaderFromRequest( ref isHeadersReading, ref block_size, ref MaxHeadersSize, ref readed_blocks, ref is_binary_request, ref headers, ref readData, ref enc, ref raw);
TryUpdaterequestLength( ref isHeadersReading, ref requestLength, ref headers, ( (readData != null) ? readData.Length : 0 ) );
if(posts_uploading_large_data == true){ //if need to read and upload from TCP-NetworkStream
requestLength -= uploaded_bytes; //this was been already uploaded, just update content-length
}else if(too_large == true && requestLength !=0){ //if was been cached, and requestLength not null
requestLength -= large_size; //update content-length
}
len = -1;
}
}
while (
client.Connected
&& stream.DataAvailable
&& (
(len > 0)
|| (raw.Count < requestLength)
)
);
//This cycle try to read data block-by-block, and add this to request.
//Large data was been processed block-by-block, and in the end of this cycle will be returned the rest, and need to process this.
TryToUploadIntoDataBaseThePostsFromLoadedRequestWithJSON(ref posts_uploading_large_data, ref readData, ref raw, ref headers, ref accepted_posts, ref uploaded_posts, ref too_large);
//Console.WriteLine("readData.Length: "+ ( (readData != null) ? readData.Length.ToString() : "null" ) );
//Console.WriteLine("raw.Count: "+ ( (raw != null) ? raw.Count.ToString() : "null" ));
//after this all...
try
{
byte[] ReadRawBytes = ( (raw != null) ? raw.ToArray() : null);
if ((is_lite_server == false && ConnectionAdded != null) || (is_lite_server == true && ConnectionAdded_lite!=null)) //ConnectionAdded!
{
//Console.WriteLine("TcpServer.cs - Open HTTPConnection headers.Length: "+headers.Length);
HttpConnection connect_client =
new HttpConnection
(
ReadRawBytes //raw-bytes of request
, readData //request as string
, (ascii, TextResponse) => //write TextResponse function. See HttpConnection.cs Response-function
{
//send response
byte[] ba = ascii == null ? new byte[0] : Encoding.ASCII.GetBytes(ascii); //write headers of response.
if(
is_binary_request == true //if request was been binary, but need to write text, means a text response
&& !DbApiHandler.isLatin1String(TextResponse) //if TextResponse is not latin1-encoded string
){
enc = MimeType.DefaultTextEncoding; //use UTF-8
}
byte[] bu = TextResponse == null ? new byte[0] : enc.GetBytes(TextResponse); //response encoded by utf8 or iso-8859-1 (for binary request)
string write_function = "(1-st function, write TextResponse)";
WriteBytesOfResponse(ref stream, ref ba, ref bu, ref client, ref write_function);
}
, (ascii, bytes) => //or write bytes for binary response. See HttpConnection.cs Response-function
{
byte[] ba = Encoding.ASCII.GetBytes(ascii);
byte[] bu = bytes;
string write_function = "(2-nd function, write text)";
WriteBytesOfResponse(ref stream, ref ba, ref bu, ref client, ref write_function);
}
, headers // (optionally) - specify extracted headers
// , null //with null here, parsing headers from readData or ReadRawBytes, in HttpRequest.cs (test)
//Binary request contains bytes, but not contains text, text request contains text, but not contains bytes.
//If text, it will be convert to bytes. See HttpConnection.cs
// , is_binary_request // (optionally) - specify "true", if binary request, and "false" if text-request.
// , false //, true //set this binary flag for any request (test).
);
if(is_lite_server == false && ConnectionAdded != null){
ConnectionAdded(connect_client);
}else if(is_lite_server == true && ConnectionAdded_lite!=null){
ConnectionAdded_lite(connect_client);
}
}
}
catch(Exception ex)
{
Console.WriteLine("Something is wrong with connection between your browser and nanoboard client...");
Console.WriteLine("But that's ok, don't worry.");
Console.WriteLine("Exception: "+ex);
}
finally
{
stream.Dispose(); //clear memory from stream.
client.Close();
}
}
}
}