-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackend.cpp
More file actions
2051 lines (1982 loc) · 80 KB
/
Copy pathBackend.cpp
File metadata and controls
2051 lines (1982 loc) · 80 KB
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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef CURL
#include "Backend.hpp"
#include "Http.hpp"
#include "Cache.hpp"
#include "Common.hpp"
#include <iostream>
#include <fcntl.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <chrono>
#ifdef DEBUGFASTCGI
#include <sys/time.h>
#endif
//curl -v -H "Accept-Encoding: gzip" -o style.css.gz 'http://cdn.bolivia-online.com/ultracopier-static.first-world.info/css/style.css'
std::unordered_map<std::string,Backend::BackendList *> Backend::addressToHttp;
std::unordered_map<std::string,Backend::BackendList *> Backend::addressToHttps;
uint32_t Backend::maxBackend=64;
bool Backend::forceHttpClose=false;
#ifdef DEBUGFASTCGI
std::unordered_set<Backend *> Backend::backendToDebug;
#endif
uint16_t Backend::https_portBE=0;
const SSL_METHOD *Backend::meth=nullptr;
Backend::Backend(BackendList * backendList) :
http(nullptr),
https(false),
wasTCPConnected(false),
downloadFinishedCount(0),
lastActivitymsTimestamps(0),
backendList(backendList),
ctx(nullptr),
ssl(nullptr)
{
#ifdef DEBUGFASTCGI
backendToDebug.insert(this);
#endif
lastActivitymsTimestamps=Common::msFrom1970();
this->kind=EpollObject::Kind::Kind_Backend;
}
Backend::~Backend()
{
//to be safe, delete when all is stable
if(backendList!=nullptr)
{
unsigned int index=0;
while(index<backendList->busy.size())
{
if(backendList->busy.at(index)==this)
{
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << " workaround activated, ERROR should not be exists, backend not correctly unregistred (abort)" << std::endl;
backendList->busy.erase(backendList->busy.cbegin()+index);
abort();
}
else
index++;
}
}
for( const auto &n : Backend::addressToHttp )
{
unsigned int index=0;
while(index<n.second->busy.size())
{
if(n.second->busy.at(index)==this)
{
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << " workaround activated, ERROR should not be exists, backend not correctly unregistred (abort)" << std::endl;
n.second->busy.erase(n.second->busy.cbegin()+index);
abort();
}
else
index++;
}
index=0;
while(index<n.second->idle.size())
{
if(n.second->idle.at(index)==this)
{
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << " workaround activated, ERROR should not be exists, backend not correctly unregistred (abort)" << std::endl;
n.second->idle.erase(n.second->idle.cbegin()+index);
abort();
}
else
index++;
}
}
for( const auto &n : Backend::addressToHttps )
{
unsigned int index=0;
while(index<n.second->busy.size())
{
if(n.second->busy.at(index)==this)
{
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << " workaround activated, ERROR should not be exists, backend not correctly unregistred (abort)" << std::endl;
n.second->busy.erase(n.second->busy.cbegin()+index);
abort();
}
else
index++;
}
index=0;
while(index<n.second->idle.size())
{
if(n.second->idle.at(index)==this)
{
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << " workaround activated, ERROR should not be exists, backend not correctly unregistred (abort)" << std::endl;
n.second->idle.erase(n.second->idle.cbegin()+index);
abort();
}
else
index++;
}
}
#ifdef DEBUGFASTCGI
if(backendToDebug.find(this)!=backendToDebug.cend())
backendToDebug.erase(this);
else
{
std::cerr << "Backend Entry not found into global list, abort()" << std::endl;
abort();
}
#endif
#ifdef DEBUGFASTCGI
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
if(fd!=-1)
{
std::cerr << "EPOLL_CTL_DEL Http: " << fd << std::endl;
Cache::unregisterCacheFD(fd);
if(epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL)==-1)
std::cerr << "EPOLL_CTL_DEL Http: " << fd << ", errno: " << errno << std::endl;
}
if(http!=nullptr)
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " " << this << ": http->backend=nullptr; (destructor), http: " << http << std::endl;
#endif
Http *http=this->http;
this->http=nullptr;
http->backend=nullptr;
http->backendList=nullptr;
/* when domain not exists: https://cdn.confiared.com/unknown.domain.com/index.css
* When The remote server have close the connexion */
http->backendErrorAndDisconnect("Backend destructor called when remain http connected");
http->disconnectFrontend(false);
}
if(backendList!=nullptr)
{
size_t index=0;
while(index<backendList->busy.size())
{
if(backendList->busy.at(index)==this)
{
backendList->busy.erase(backendList->busy.cbegin()+index);
break;
}
index++;
}
index=0;
while(index<backendList->idle.size())
{
if(backendList->idle.at(index)==this)
{
backendList->idle.erase(backendList->idle.cbegin()+index);
break;
}
index++;
}
}
closeSSL();
}
void Backend::close()//call externally from Http::detectTimeout()
{
#ifdef DEBUGFASTCGI
std::cerr << "Backend::close() " << this << " " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
//abort(); -> do this test
if(fd!=-1)
{
Cache::unregisterCacheFD(fd);
epoll_ctl(epollfd,EPOLL_CTL_DEL, fd, NULL);
#ifdef DEBUGFASTCGI
std::cerr << "Backend::close() fd: " << fd << " " << this << " " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
::close(fd);
//prevent multiple loop call
fd=-1;
if(backendList!=nullptr)
{
if(!backendList->pending.empty())
std::cerr << "Backend::close() AND !backendList->pending.empty() fd: " << fd << " " << this << " " << __FILE__ << ":" << __LINE__ << std::endl;
}
/* not fix like this, generate http remain attached
* if(backendList!=nullptr)
{
size_t index=0;
while(index<backendList->busy.size())
{
if(backendList->busy.at(index)==this)
{
#ifdef DEBUGFASTCGI
std::cerr << "Backend::close() " << this << " " << __FILE__ << ":" << __LINE__ << " ERROR should not pass here" << std::endl;
#endif
backendList->busy.erase(backendList->busy.cbegin()+index);
break;
}
index++;
}
index=0;
while(index<backendList->idle.size())
{
if(backendList->idle.at(index)==this)
{
backendList->idle.erase(backendList->idle.cbegin()+index);
break;
}
index++;
}
}*/
}
closeSSL();
}
void Backend::closeSSL()
{
if(ssl!=nullptr)
{
SSL_free(ssl);
ssl=nullptr;
}
if(ctx!=NULL)
{
SSL_CTX_free(ctx);
ctx=nullptr;
}
}
void Backend::failAttachedHttp(const std::string &reason)
{
// Mirrors the resume-retry-exhausted detach pattern in parseEvent ~L380:
// remove this Backend from backendList->busy and null out the
// cross-pointers between this Backend and its Http BEFORE routing the
// error. Two reasons:
// 1. backendErrorAndDisconnect → retryAfterError → tryConnectInternal
// assigns the Http a *new* Backend; if `this` were still in busy,
// the next checkBackend() integrity sweep would find a Backend in
// busy with http==nullptr and abort (Backend.cpp:1997).
// 2. After this call returns, the Http may have entered the deferred-
// delete queue, so the caller must NOT dereference `http` again.
if(http==nullptr)
return;
if(backendList!=nullptr)
{
size_t index=0;
while(index<backendList->busy.size())
{
if(backendList->busy.at(index)==this)
{
backendList->busy.erase(backendList->busy.cbegin()+index);
break;
}
index++;
}
}
Http *httpToFail=http;
http=nullptr;
httpToFail->backend=nullptr;
httpToFail->backendList=nullptr;
httpToFail->backendErrorAndDisconnect(reason);
}
void Backend::remoteSocketClosed()
{
#ifdef DEBUGFASTCGI
checkBackend();
#endif
remoteSocketClosedInternal();
#ifdef DEBUGFASTCGI
checkBackend();
#endif
}
void Backend::remoteSocketClosedInternal()
{
#ifdef DEBUGFASTCGI
std::cerr << "Backend::remoteSocketClosed() " << this << " " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
#ifdef DEBUGFILEOPEN
std::cerr << "Backend::remoteSocketClosed(), fd: " << fd << std::endl;
#endif
if(fd!=-1)
{
#ifdef DEBUGFASTCGI
std::cerr << "EPOLL_CTL_DEL remoteSocketClosed Http: " << fd << std::endl;
#endif
Cache::unregisterCacheFD(fd);
if(epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL)==-1)
std::cerr << "EPOLL_CTL_DEL remoteSocketClosed Http: " << fd << ", errno: " << errno << std::endl;
#ifdef DEBUGFASTCGI
std::cerr << "close() fd: " << fd << " " << this << " " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
::close(fd);
fd=-1;
}
closeSSL();
if(http!=nullptr)
http->resetRequestSended();
#ifdef DEBUGFASTCGI
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
if(backendList!=nullptr)
{
if(!wasTCPConnected)
{
size_t index=0;
while(index<backendList->busy.size())
{
if(backendList->busy.at(index)==this)
{
backendList->busy.erase(backendList->busy.cbegin()+index);
break;
}
index++;
}
if(!backendList->pending.empty() && backendList->busy.empty())
{
#ifdef DEBUGFASTCGI
std::cerr << "Tcp connect problem, abort asll pending fd: " << fd << " " << this << " " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
const std::string error("Tcp connect problem");
/*forget the pedding will retry after or timeout
why not quit if TCP connect? can be timeout on specific connexion but the TCP is well accessible
size_t index=0;
while(index<backendList->pending.size())
{
Http *http=backendList->pending.at(index);
http->backendError(error);//drop from list, then delete http
http->disconnectFrontend(true);
http->disconnectBackend();
index++;
}*/
}
Http *httpTempToPassCheckBackend=http;
http=nullptr;
if(httpTempToPassCheckBackend!=nullptr)
{
httpTempToPassCheckBackend->backend=nullptr;
httpTempToPassCheckBackend->backendList=nullptr;
// TCP connect failed (refused, host unreachable, RST during
// handshake) before any reply byte arrived. Surface the
// failure to the attached Http now instead of leaving it in
// Status_WaitTheContent until --maxreadtime fires (~20s) and
// the user gets a generic "Timeout into reply header". The
// 2-retry budget inside backendErrorAndDisconnect still kicks
// in for transient flakes — only a permanent connect failure
// (no live origin on this IP/port, IP-range filter, etc.)
// propagates 500 to the client straight away.
httpTempToPassCheckBackend->backendErrorAndDisconnect("TCP connect failed");
}
#ifdef DEBUGFASTCGI
std::cerr << "remoteSocketClosed and was NOT TCP connected " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
#ifdef DEBUGFASTCGI
checkBackend();
#endif
return;
}
else
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " was TCP connected, backendList: " << (void *)backendList << ", backendList->busy.size(): " << std::to_string(backendList->busy.size()) << std::endl;
#endif
size_t index=0;
while(index<backendList->busy.size())
{
if(backendList->busy.at(index)==this)
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " located into busy to destroy: " << this << std::endl;
#endif
backendList->busy.erase(backendList->busy.cbegin()+index);
if(http!=nullptr)
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " backend destroy but had http client connected, try reasign" << std::endl;
#endif
// If the origin already started sending body bytes (mid-body
// disconnect), a naive reassign would resend the request to a
// fresh backend and append the second response on top of the
// first — corrupting the cache file (mission item 5: never cache
// a partial download as complete) and making nginx report
// "upstream sent more data than Content-Length". Instead, we
// invoke prepareForResume() to (a) reset header-parsing state and
// (b) record `resumeOffset = bytes already in cache`. The next
// sendRequest() emits `Range: bytes=<resumeOffset>-`. If origin
// honours it (206 Partial Content), the suffix appends cleanly;
// if origin replies 200 (Range ignored), writeToCache discards
// the duplicate prefix bytes from the new body so the client's
// FastCGI stream stays continuous — mission item 5's recover-or-
// reset semantics. Retry budget caps at 2 attempts.
if(http->getContentwritten()>0 || http->headerWriten)
{
if(http->retryCount>=2)
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " resume retry budget exhausted (" << (int)http->retryCount << "); failing" << std::endl;
#endif
Http *httpToFail=http;
http=nullptr;
httpToFail->backend=nullptr;
httpToFail->backendList=nullptr;
httpToFail->backendErrorAndDisconnect("Backend closed mid-body, retry exhausted");
#ifdef DEBUGFASTCGI
checkBackend();
#endif
return;
}
http->retryCount++;
http->prepareForResume();
}
/*if(http->requestSended)
{
std::cerr << "reassign but request already send" << std::endl;
http->parseNonHttpError(Backend::NonHttpError_AlreadySend);
return;
}*/
#ifdef DEBUGFASTCGI
if(http->requestSended)
std::cerr << "reassign but request already send" << std::endl;
#endif
Http *httpTempToPassCheckBackend=http;
http=nullptr;
httpTempToPassCheckBackend->requestSended=false;
// No checkBackend() here: we just erased `this` from
// backendList->busy and detached its http, so busy.size()
// is transiently old-1. If pending is non-empty (the
// queue hasn't drained), the invariant
// `pending.empty() || busy.size()>=maxBackend` is
// violated until the reassign below pushes the
// replacement backend back into busy. The post-reassign
// checkBackend() calls (after the idle/new branches)
// verify the invariant once steady-state is restored.
// Push the Http's read-side activity timestamp forward to
// "now" so the next CheckTimeout sweep does NOT immediately
// fire Http::detectTimeout against this Http. The timestamp
// it carries reflects the last byte received on the *old*
// backend (which is by definition stale — that's why we're
// reassigning). Without this reset, Http::detectTimeout
// races the reassign and ends up calling tryConnectInternal
// on an Http that already has a fresh backend, tripping the
// `backend->http==this` invariant.
httpTempToPassCheckBackend->resetActivityTimestampForReassign();
//reassign to idle backend
if(!backendList->idle.empty())
{
//assign to idle backend and become busy
Backend *backend=backendList->idle.back();
backendList->idle.pop_back();
backendList->busy.push_back(backend);
backend->http=httpTempToPassCheckBackend;
#ifdef DEBUGFASTCGI
std::cerr << "reassign to idle backend, backend: " << this << " http: " << httpTempToPassCheckBackend << ": http->backend=" << backend << " " << __FILE__ << ":" << __LINE__ << " isValid: " << backend->isValid() << std::endl;
#endif
httpTempToPassCheckBackend->backend=backend;
httpTempToPassCheckBackend->backendList=backendList;
httpTempToPassCheckBackend->readyToWrite();
#ifdef DEBUGFASTCGI
httpTempToPassCheckBackend->checkBackend();
checkBackend();
#endif
}
//reassign to new backend
else
{
Backend *newBackend=new Backend(backendList);
if(!newBackend->tryConnectInternal(backendList->s))
{
//todo abort client
#ifdef DEBUGFASTCGI
checkBackend();
#endif
return;
}
newBackend->http=httpTempToPassCheckBackend;
#ifdef DEBUGFASTCGI
std::cerr << "reassign to new backend, backend: " << this << " http: " << httpTempToPassCheckBackend << ": http->backend=" << newBackend << " " << __FILE__ << ":" << __LINE__ << " isValid: " << newBackend->isValid() << std::endl;
#endif
httpTempToPassCheckBackend->backend=newBackend;
httpTempToPassCheckBackend->backendList=backendList;
backendList->busy.push_back(newBackend);
#ifdef DEBUGFASTCGI
httpTempToPassCheckBackend->checkBackend();
checkBackend();
#endif
}
return;
}
if(backendList->busy.empty() && backendList->idle.empty() && backendList->pending.empty())
{
#ifdef DEBUGFASTCGI
std::string host="Unknown IPv6";
char str[INET6_ADDRSTRLEN];
if (inet_ntop(AF_INET6, &backendList->s.sin6_addr, str, INET6_ADDRSTRLEN) != NULL)
host=str;
std::cerr << __FILE__ << ":" << __LINE__ << " addressToHttp.erase(): " << host << std::endl;
#endif
std::string addr((char *)&backendList->s.sin6_addr,16);
#ifdef FORCEDPORT
if(backendList->s.sin6_port == htobe16(FORCEDPORT))
#else
if(backendList->s.sin6_port == htobe16(80))
#endif
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " addressToHttp.erase(): " << host << ":" << be16toh(backendList->s.sin6_port) << std::endl;
if(addressToHttp.at(addr)!=backendList)
{
std::cerr << "intented erase backend list is not same than current (abort)" << std::endl;
abort();
}
if(!addressToHttp.at(addr)->busy.empty() || !addressToHttp.at(addr)->idle.empty() || !addressToHttp.at(addr)->pending.empty())
{
std::cerr << "intented erase backend list have pending request (abort)" << std::endl;
abort();
}
#endif
addressToHttp.erase(addr);
}
else
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " addressToHttp.erase(): " << host << ":" << be16toh(backendList->s.sin6_port) << std::endl;
if(addressToHttps.at(addr)!=backendList)
{
std::cerr << "intented erase backend list is not same than current (abort)" << std::endl;
abort();
}
if(!addressToHttps.at(addr)->busy.empty() || !addressToHttps.at(addr)->idle.empty() || !addressToHttps.at(addr)->pending.empty())
{
std::cerr << "intented erase backend list have pending request (abort)" << std::endl;
abort();
}
#ifdef FORCEDPORT_TLS
if(be16toh(backendList->s.sin6_port)!=FORCEDPORT_TLS)
#else
if(be16toh(backendList->s.sin6_port)!=443)
#endif
{
std::cerr << "intented erase backend list have wrong port (abort)" << std::endl;
abort();
}
#endif
addressToHttps.erase(addr);
}
}
backendList=nullptr;
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << std::endl;
#endif
break;
}
index++;
}
#ifdef DEBUGFASTCGI
if(index<backendList->busy.size())
std::cerr << __FILE__ << ":" << __LINE__ << " " << this << " found into busy" << std::endl;
else
std::cerr << __FILE__ << ":" << __LINE__ << " " << this << " NOT found into busy" << std::endl;
#endif
index=0;
if(backendList!=nullptr)
{
while(index<backendList->idle.size())
{
if(backendList->idle.at(index)==this)
{
backendList->idle.erase(backendList->idle.cbegin()+index);
break;
}
index++;
}
#ifdef DEBUGFASTCGI
if(index<backendList->idle.size())
std::cerr << __FILE__ << ":" << __LINE__ << " " << this << " found into idle" << std::endl;
else
std::cerr << __FILE__ << ":" << __LINE__ << " " << this << " NOT found into idle" << std::endl;
#endif
}
}
}
#ifdef DEBUGFASTCGI
checkBackend();
#endif
}
//after this, the backend should not point to http previous http at least, http should be nullptr
void Backend::downloadFinished()
{
#ifdef DEBUGFASTCGI
checkBackend();
#endif
//after this, the backend should not point to http previous http at least
Http *oldhttp=http;
downloadFinishedInternal();
#ifdef DEBUGFASTCGI
if(http==oldhttp)
{
std::cerr << __FILE__ << ":" << __LINE__ << " http: " << http << " backend: " << this << " http==oldhttp after downloadFinishedInternal(); (abort)" << std::endl;
abort();
}
/* WRONG: this backend can now have another http to do
* if(http!=nullptr)
{
std::cerr << __FILE__ << ":" << __LINE__ << " http: " << http << " backend: " << this << " http!=nullptr after downloadFinishedInternal(); (abort)" << std::endl;
abort();
}*/
std::cerr << __FILE__ << ":" << __LINE__ << " http: " << http << " backend: " << this<< " after downloadFinishedInternal()" << std::endl;
#endif
#ifdef DEBUGFASTCGI
checkBackend();
#endif
}
void Backend::downloadFinishedInternal()
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " http " << http << " is finished, should be destruct Backend::downloadFinished() " << this << std::endl;
if(http==nullptr)
std::cerr << __FILE__ << ":" << __LINE__ << "Backend::downloadFinished() http==nullptr bug suspected WARNING " << this << std::endl;
#endif
if(backendList==nullptr)
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " http " << http << " backendList==nullptr return" << std::endl;
#endif
http=nullptr;
return;
}
if(wasTCPConnected)
downloadFinishedCount++;
if(!wasTCPConnected || Backend::forceHttpClose)
{
const std::string error("Tcp connect problem");
size_t index=0;
while(index<backendList->busy.size())
{
if(backendList->busy.at(index)==this)
{
backendList->busy.erase(backendList->busy.cbegin()+index);
break;
}
index++;
}
/* firstly the current need retry
if(!backendList->pending.empty() && backendList->busy.empty())
{
size_t index=0;
while(index<backendList->pending.size())
{
Http *http=backendList->pending.at(index);
http->backendError(error);
http->disconnectFrontend(false);
//http->disconnectBackend();-> no backend because pending
//delete http;
index++;
}
}*/
if(http!=nullptr)
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " " << this << " Backend::downloadFinished() NOT TRY AGAIN, http was: " << http << std::endl;
#endif
#ifdef DEBUGFASTCGI
http->checkBackend();
#endif
Http *httpTempToPassCheckBackend=http;
http=nullptr;
httpTempToPassCheckBackend->backend=nullptr;
httpTempToPassCheckBackend->backendList=nullptr;
httpTempToPassCheckBackend->backendErrorAndDisconnect(error);//disconnect client like http->disconnectFrontend();
//http->disconnectBackend();
//delete http;
#ifdef DEBUGFASTCGI
httpTempToPassCheckBackend->checkBackend();
#endif
}
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " " << this << " Backend::downloadFinished() call close() http: " << http << " fd: " << fd << std::endl;
#endif
close();
/* corruption loop:
* ==1400295== by 0x14F693: Http::checkBackend() (Http.cpp:3122)
==1400295== by 0x11AD93: Backend::downloadFinishedInternal() (Backend.cpp:551)
==1400295== by 0x11A8EF: Backend::downloadFinished() (Backend.cpp:482)
==1400295== by 0x148B27: Http::disconnectBackend(bool) (Http.cpp:2100)
==1400295== by 0x119957: Backend::remoteSocketClosed() (Backend.cpp:306)
==1400295== by 0x11AE63: Backend::downloadFinishedInternal() (Backend.cpp:568)
==1400295== by 0x11A8EF: Backend::downloadFinished() (Backend.cpp:482)
==1400295== by 0x148B27: Http::disconnectBackend(bool) (Http.cpp:2100)
remoteSocketClosed();
*/
/// \todo, check if need static to delete here
return;
}
if(backendList->pending.empty())
{
size_t index=0;
while(index<backendList->busy.size())
{
if(backendList->busy.at(index)==this)
{
backendList->busy.erase(backendList->busy.cbegin()+index);
break;
}
index++;
}
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " http: " << http << std::endl;
#endif
if(this->isValid())
backendList->idle.push_back(this);
#ifdef DEBUGFASTCGI
std::cerr << this << " backend, " << http << ": http->backend=null + http=nullptr" << " " << __FILE__ << ":" << __LINE__ << " isValid: " << this->isValid() << std::endl;
#endif
/** \todo fix clean client disconnection from here
* void Http::disconnectBackend(const bool fromDestructor) if timeout before start download, have client list
* **/
/*http->disconnectFrontend();
* ==30297== at 0x55EF820: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22)
==30297== by 0x12EE3B: Client::continueRead() (Client.cpp:1655)
==30297== by 0x12F6F6: Client::readyToWrite() (Client.cpp:1751)
==30297== by 0x123CE7: Client::parseEvent(epoll_event const&) (Client.cpp:100)
==30297== by 0x10DEB4: main (main.cpp:202)
==30297== Address 0x6624560 is 16 bytes inside a block of size 448 free'd
==30297== at 0x4C2D2DB: operator delete(void*) (vg_replace_malloc.c:576)
==30297== by 0x1357C1: Http::~Http() (Http.cpp:153)
==30297== by 0x10DD15: main (main.cpp:179)
==30297== Block was alloc'd at
==30297== at 0x4C2C21F: operator new(unsigned long) (vg_replace_malloc.c:334)
==30297== by 0x12C837: Client::dnsRight(sockaddr_in6 const&) (Client.cpp:1321)
==30297== by 0x1490D5: Dns::parseEvent(epoll_event const&) (Dns.cpp:402)
==30297== by 0x10DF90: main (main.cpp:224)
*/
//http->backend=nullptr;
//http->disconnectBackend();->generate cache corruption
//delete http;
//try 30/06/2021
Http *httpTempToPassCheckBackend=http;
http=nullptr;
httpTempToPassCheckBackend->disconnectFrontend(false);
//http->disconnectBackend();
httpTempToPassCheckBackend->backend=nullptr;
httpTempToPassCheckBackend->backendList=nullptr;
#ifdef DEBUGFASTCGI
httpTempToPassCheckBackend->checkBackend();
#endif
}
else
{
#ifdef DEBUGFASTCGI
std::cerr << this << " http: " << http << ": http->backend=null and !backendList->pending.empty() cachePath " << http->cachePath << " " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
Http *httpTempToPassCheckBackend=http;
httpTempToPassCheckBackend->backend=nullptr;
httpTempToPassCheckBackend->backendList=nullptr;
//http->disconnectBackend();->generate cache corruption
//delete http;-> generate crash
http=nullptr;
startNextPending();
#ifdef DEBUGFASTCGI
httpTempToPassCheckBackend->checkBackend();
#endif
}
#ifdef DEBUGFASTCGI
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << " end of Backend::downloadFinished() http: " << http << std::endl;
if(http==nullptr)
{
for( const auto &n : Backend::addressToHttp )
{
unsigned int index=0;
while(index<n.second->busy.size())
{
if(n.second->busy.at(index)==this)
{
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << " workaround activated, ERROR should not be exists, backend not correctly unregistred (abort)" << std::endl;
n.second->busy.erase(n.second->busy.cbegin()+index);
abort();
}
else
index++;
}
}
for( const auto &n : Backend::addressToHttps )
{
unsigned int index=0;
while(index<n.second->busy.size())
{
if(n.second->busy.at(index)==this)
{
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << " workaround activated, ERROR should not be exists, backend not correctly unregistred (abort)" << std::endl;
n.second->busy.erase(n.second->busy.cbegin()+index);
abort();
}
else
index++;
}
}
bool foundIntoIdle=false;
for( const auto &n : Backend::addressToHttp )
{
unsigned int index=0;
while(index<n.second->idle.size())
{
if(n.second->idle.at(index)==this)
foundIntoIdle=true;
index++;
}
}
for( const auto &n : Backend::addressToHttps )
{
unsigned int index=0;
while(index<n.second->idle.size())
{
if(n.second->idle.at(index)==this)
foundIntoIdle=true;
index++;
}
}
if(!foundIntoIdle)
{
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << " http==nullptr and not found in idle (abort)" << std::endl;
abort();
}
}
else
{
bool foundIntoBusy=false;
for( const auto &n : Backend::addressToHttp )
{
unsigned int index=0;
while(index<n.second->busy.size())
{
if(n.second->busy.at(index)==this)
foundIntoBusy=true;
index++;
}
}
for( const auto &n : Backend::addressToHttps )
{
unsigned int index=0;
while(index<n.second->busy.size())
{
if(n.second->busy.at(index)==this)
foundIntoBusy=true;
index++;
}
}
if(!foundIntoBusy)
{
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << " http==nullptr and not found in idle (abort)" << std::endl;
abort();
}
}
#endif
}
void Backend::startNextPending()
{
startNextPendingInternal();
#ifdef DEBUGFASTCGI
checkBackend();
#endif
}
void Backend::startNextPendingInternal()
{
if(backendList==nullptr)
return;
if(backendList->pending.empty())
return;
#ifdef DEBUGFASTCGI
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << " Backend::startNextPending()" << std::endl;
#endif
bool haveFoundPending=false;
bool haveUrlAndFrontendConnected=false;
do
{
Http * httpToGet=backendList->pending.front();
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " " << httpToGet << ": cachePath " << httpToGet->cachePath << std::endl;
httpToGet->checkBackend();
#endif
#ifdef DEBUGFASTCGI
if(Http::httpToDebug.find(httpToGet)==Http::httpToDebug.cend())
{
std::cerr << __FILE__ << ":" << __LINE__ << ", try get from backend: " << this << " the http already deleted: " << httpToGet << " (abort)" << std::endl;
abort();
}
#endif
backendList->pending.erase(backendList->pending.cbegin());
httpToGet->pending=true;
haveUrlAndFrontendConnected=httpToGet->haveUrlAndFrontendConnected();
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " http: " << httpToGet << ": cachePath " << httpToGet->cachePath << " backend " << this << std::endl;
#endif
if(haveUrlAndFrontendConnected)
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << ", link backend: " << this << " with http " << httpToGet << " old: " << http << std::endl;
#endif
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " http: " << httpToGet << ": cachePath " << httpToGet->cachePath << " backend " << this << std::endl;
#endif
http=httpToGet;
#ifdef DEBUGFASTCGI
//http->checkBackend();
#endif
http->backend=this;
http->backendList=backendList;
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " http: " << httpToGet << ": cachePath " << httpToGet->cachePath << " backend " << this << std::endl;
#endif
#ifdef DEBUGFASTCGI
http->checkBackend();
#endif
http->readyToWrite();
#ifdef DEBUGFASTCGI
http->checkBackend();
#endif
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " http: " << httpToGet << ": cachePath " << httpToGet->cachePath << " backend " << this << std::endl;
#endif
haveFoundPending=true;
}
else
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " http: " << httpToGet << ": cachePath " << httpToGet->cachePath << " backend " << this << std::endl;
httpToGet->checkBackend();
#endif
httpToGet->backendErrorAndDisconnect("Internal error, !haveUrlAndFrontendConnected");
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " http: " << httpToGet << ": cachePath " << httpToGet->cachePath << " backend " << this << std::endl;
#endif
httpToGet->disconnectFrontend(false);
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " http: " << httpToGet << ": cachePath " << httpToGet->cachePath << " backend " << this << std::endl;
#endif
httpToGet->disconnectBackend();
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << " http: " << httpToGet << ": cachePath " << httpToGet->cachePath << " backend " << this << std::endl;
#endif
//delete httpToGet;
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << ", http buggy or without client, skipped http: " << httpToGet << " backend " << this << std::endl;
#endif
}
} while(haveUrlAndFrontendConnected==false && !backendList->pending.empty());
if(!haveFoundPending)
{
size_t index=0;
while(index<backendList->busy.size())
{
if(backendList->busy.at(index)==this)
{
backendList->busy.erase(backendList->busy.cbegin()+index);
break;
}
index++;
}
#ifdef DEBUGFASTCGI
std::cerr << this << " " << __FILE__ << ":" << __LINE__ << " " << " isValid: " << this->isValid() << " change from busy to idle" << std::endl;
#endif
if(this->isValid())
backendList->idle.push_back(this);