forked from aria2/aria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractCommand.cc
946 lines (854 loc) · 29.6 KB
/
AbstractCommand.cc
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
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "AbstractCommand.h"
#include <algorithm>
#include "Request.h"
#include "DownloadEngine.h"
#include "Option.h"
#include "PeerStat.h"
#include "SegmentMan.h"
#include "Logger.h"
#include "Segment.h"
#include "DlAbortEx.h"
#include "DlRetryEx.h"
#include "DownloadFailureException.h"
#include "CreateRequestCommand.h"
#include "InitiateConnectionCommandFactory.h"
#include "StreamCheckIntegrityEntry.h"
#include "PieceStorage.h"
#include "SocketCore.h"
#include "message.h"
#include "prefs.h"
#include "fmt.h"
#include "ServerStat.h"
#include "RequestGroupMan.h"
#include "A2STR.h"
#include "util.h"
#include "LogFactory.h"
#include "DownloadContext.h"
#include "wallclock.h"
#include "NameResolver.h"
#include "uri.h"
#include "FileEntry.h"
#include "error_code.h"
#include "SocketRecvBuffer.h"
#include "ChecksumCheckIntegrityEntry.h"
#ifdef ENABLE_ASYNC_DNS
# include "AsyncNameResolver.h"
# include "AsyncNameResolverMan.h"
#endif // ENABLE_ASYNC_DNS
namespace aria2 {
AbstractCommand::AbstractCommand(
cuid_t cuid, const std::shared_ptr<Request>& req,
const std::shared_ptr<FileEntry>& fileEntry, RequestGroup* requestGroup,
DownloadEngine* e, const std::shared_ptr<SocketCore>& s,
const std::shared_ptr<SocketRecvBuffer>& socketRecvBuffer,
bool incNumConnection)
: Command(cuid),
req_(req),
fileEntry_(fileEntry),
socket_(s),
socketRecvBuffer_(socketRecvBuffer),
#ifdef ENABLE_ASYNC_DNS
asyncNameResolverMan_(make_unique<AsyncNameResolverMan>()),
#endif // ENABLE_ASYNC_DNS
requestGroup_(requestGroup),
e_(e),
checkPoint_(global::wallclock()),
serverStatTimer_(global::wallclock()),
timeout_(requestGroup->getTimeout()),
checkSocketIsReadable_(false),
checkSocketIsWritable_(false),
incNumConnection_(incNumConnection)
{
if (socket_ && socket_->isOpen()) {
setReadCheckSocket(socket_);
}
if (incNumConnection_) {
requestGroup->increaseStreamConnection();
}
requestGroup_->increaseStreamCommand();
requestGroup_->increaseNumCommand();
#ifdef ENABLE_ASYNC_DNS
configureAsyncNameResolverMan(asyncNameResolverMan_.get(), e_->getOption());
#endif // ENABLE_ASYNC_DNS
}
AbstractCommand::~AbstractCommand()
{
disableReadCheckSocket();
disableWriteCheckSocket();
#ifdef ENABLE_ASYNC_DNS
asyncNameResolverMan_->disableNameResolverCheck(e_, this);
#endif // ENABLE_ASYNC_DNS
requestGroup_->decreaseNumCommand();
requestGroup_->decreaseStreamCommand();
if (incNumConnection_) {
requestGroup_->decreaseStreamConnection();
}
}
void AbstractCommand::useFasterRequest(
const std::shared_ptr<Request>& fasterRequest)
{
A2_LOG_INFO(fmt("CUID#%" PRId64 " - Use faster Request hostname=%s, port=%u",
getCuid(), fasterRequest->getHost().c_str(),
fasterRequest->getPort()));
// Cancel current Request object and use faster one.
fileEntry_->removeRequest(req_);
e_->setNoWait(true);
e_->addCommand(
InitiateConnectionCommandFactory::createInitiateConnectionCommand(
getCuid(), fasterRequest, fileEntry_, requestGroup_, e_));
}
bool AbstractCommand::shouldProcess() const
{
if (checkSocketIsReadable_) {
if (readEventEnabled()) {
return true;
}
if (socketRecvBuffer_ && !socketRecvBuffer_->bufferEmpty()) {
return true;
}
if (socket_ && socket_->getRecvBufferedLength()) {
return true;
}
}
if (checkSocketIsWritable_ && writeEventEnabled()) {
return true;
}
#ifdef ENABLE_ASYNC_DNS
const auto resolverChecked = asyncNameResolverMan_->resolverChecked();
if (resolverChecked && asyncNameResolverMan_->getStatus() != 0) {
return true;
}
if (!checkSocketIsReadable_ && !checkSocketIsWritable_ && !resolverChecked) {
return true;
}
#else // ENABLE_ASYNC_DNS
if (!checkSocketIsReadable_ && !checkSocketIsWritable_) {
return true;
}
#endif // ENABLE_ASYNC_DNS
return noCheck();
}
bool AbstractCommand::execute()
{
A2_LOG_DEBUG(fmt("CUID#%" PRId64
" - socket: read:%d, write:%d, hup:%d, err:%d",
getCuid(), readEventEnabled(), writeEventEnabled(),
hupEventEnabled(), errorEventEnabled()));
try {
if (requestGroup_->downloadFinished() || requestGroup_->isHaltRequested()) {
return true;
}
if (req_ && req_->removalRequested()) {
A2_LOG_DEBUG(fmt("CUID#%" PRId64
" - Discard original URI=%s because it is"
" requested.",
getCuid(), req_->getUri().c_str()));
return prepareForRetry(0);
}
auto sm = getSegmentMan();
if (getPieceStorage()) {
segments_.clear();
sm->getInFlightSegment(segments_, getCuid());
if (req_ && segments_.empty()) {
// This command previously has assigned segments, but it is
// canceled. So discard current request chain. Plus, if no
// segment is available when http pipelining is used.
A2_LOG_DEBUG(fmt("CUID#%" PRId64
" - It seems previously assigned segments"
" are canceled. Restart.",
getCuid()));
// Request::isPipeliningEnabled() == true means aria2
// accessed the remote server and discovered that the server
// supports pipelining.
if (req_ && req_->isPipeliningEnabled()) {
e_->poolSocket(req_, createProxyRequest(), socket_);
}
return prepareForRetry(0);
}
// TODO it is not needed to check other PeerStats every time.
// Find faster Request when no segment split is allowed.
if (req_ && fileEntry_->countPooledRequest() > 0 &&
requestGroup_->getPendingLength() < calculateMinSplitSize() * 2) {
auto fasterRequest = fileEntry_->findFasterRequest(req_);
if (fasterRequest) {
useFasterRequest(fasterRequest);
return true;
}
}
// Don't use this feature if PREF_MAX_{OVERALL_}DOWNLOAD_LIMIT
// is used or total length is unknown.
if (req_ && fileEntry_->getLength() > 0 &&
e_->getRequestGroupMan()->getMaxOverallDownloadSpeedLimit() == 0 &&
requestGroup_->getMaxDownloadSpeedLimit() == 0 &&
serverStatTimer_.difference(global::wallclock()) >= 10_s) {
serverStatTimer_ = global::wallclock();
std::vector<std::pair<size_t, std::string>> usedHosts;
if (getOption()->getAsBool(PREF_SELECT_LEAST_USED_HOST)) {
getDownloadEngine()->getRequestGroupMan()->getUsedHosts(usedHosts);
}
auto fasterRequest = fileEntry_->findFasterRequest(
req_, usedHosts, e_->getRequestGroupMan()->getServerStatMan());
if (fasterRequest) {
useFasterRequest(fasterRequest);
return true;
}
}
}
if (shouldProcess()) {
checkPoint_ = global::wallclock();
if (!getPieceStorage()) {
return executeInternal();
}
if (!req_ || req_->getMaxPipelinedRequest() == 1 ||
// Why the following condition is necessary? That's because
// For single file download, SegmentMan::getSegment(cuid)
// is more efficient.
getDownloadContext()->getFileEntries().size() == 1) {
size_t maxSegments = req_ ? req_->getMaxPipelinedRequest() : 1;
size_t minSplitSize = calculateMinSplitSize();
while (segments_.size() < maxSegments) {
auto segment = sm->getSegment(getCuid(), minSplitSize);
if (!segment) {
break;
}
segments_.push_back(segment);
}
if (segments_.empty()) {
// TODO socket could be pooled here if pipelining is
// enabled... Hmm, I don't think if pipelining is enabled
// it does not go here.
A2_LOG_INFO(fmt(MSG_NO_SEGMENT_AVAILABLE, getCuid()));
// When all segments are ignored in SegmentMan, there are
// no URIs available, so don't retry.
if (sm->allSegmentsIgnored()) {
A2_LOG_DEBUG("All segments are ignored.");
// This will execute other idle Commands and let them
// finish quickly.
e_->setRefreshInterval(std::chrono::milliseconds(0));
return true;
}
return prepareForRetry(1);
}
}
else {
// For multi-file downloads
size_t minSplitSize = calculateMinSplitSize();
size_t maxSegments = req_->getMaxPipelinedRequest();
if (segments_.size() < maxSegments) {
sm->getSegment(segments_, getCuid(), minSplitSize, fileEntry_,
maxSegments);
}
if (segments_.empty()) {
return prepareForRetry(0);
}
}
return executeInternal();
}
if (errorEventEnabled()) {
// older kernel may report "connection refused" here.
auto ss = e_->getRequestGroupMan()->getOrCreateServerStat(
req_->getHost(), req_->getProtocol());
ss->setError();
throw DL_RETRY_EX(
fmt(MSG_NETWORK_PROBLEM, socket_->getSocketError().c_str()));
}
if (checkPoint_.difference(global::wallclock()) >= timeout_) {
// timeout triggers ServerStat error state.
auto ss = e_->getRequestGroupMan()->getOrCreateServerStat(
req_->getHost(), req_->getProtocol());
ss->setError();
// When DNS query was timeout, req_->getConnectedAddr() is
// empty.
if (!req_->getConnectedAddr().empty()) {
// Purging IP address cache to renew IP address.
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Marking IP address %s as bad",
getCuid(), req_->getConnectedAddr().c_str()));
e_->markBadIPAddress(req_->getConnectedHostname(),
req_->getConnectedAddr(),
req_->getConnectedPort());
}
if (e_->findCachedIPAddress(req_->getConnectedHostname(),
req_->getConnectedPort())
.empty()) {
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - All IP addresses were marked bad."
" Removing Entry.",
getCuid()));
e_->removeCachedIPAddress(req_->getConnectedHostname(),
req_->getConnectedPort());
}
throw DL_RETRY_EX2(EX_TIME_OUT, error_code::TIME_OUT);
}
addCommandSelf();
return false;
}
catch (DlAbortEx& err) {
requestGroup_->setLastErrorCode(err.getErrorCode(), err.what());
if (req_) {
A2_LOG_ERROR_EX(
fmt(MSG_DOWNLOAD_ABORTED, getCuid(), req_->getUri().c_str()),
DL_ABORT_EX2(fmt("URI=%s", req_->getCurrentUri().c_str()), err));
fileEntry_->addURIResult(req_->getUri(), err.getErrorCode());
if (err.getErrorCode() == error_code::CANNOT_RESUME) {
requestGroup_->increaseResumeFailureCount();
}
}
else {
A2_LOG_DEBUG_EX(EX_EXCEPTION_CAUGHT, err);
}
onAbort();
tryReserved();
return true;
}
catch (DlRetryEx& err) {
assert(req_);
A2_LOG_INFO_EX(
fmt(MSG_RESTARTING_DOWNLOAD, getCuid(), req_->getUri().c_str()),
DL_RETRY_EX2(fmt("URI=%s", req_->getCurrentUri().c_str()), err));
req_->addTryCount();
req_->resetRedirectCount();
req_->resetUri();
const int maxTries = getOption()->getAsInt(PREF_MAX_TRIES);
bool isAbort = maxTries != 0 && req_->getTryCount() >= maxTries;
if (isAbort) {
A2_LOG_INFO(fmt(MSG_MAX_TRY, getCuid(), req_->getTryCount()));
A2_LOG_ERROR_EX(
fmt(MSG_DOWNLOAD_ABORTED, getCuid(), req_->getUri().c_str()), err);
fileEntry_->addURIResult(req_->getUri(), err.getErrorCode());
requestGroup_->setLastErrorCode(err.getErrorCode(), err.what());
if (err.getErrorCode() == error_code::CANNOT_RESUME) {
requestGroup_->increaseResumeFailureCount();
}
onAbort();
tryReserved();
return true;
}
if (err.getErrorCode() == error_code::HTTP_SERVICE_UNAVAILABLE) {
Timer wakeTime(global::wallclock());
wakeTime.advance(
std::chrono::seconds(getOption()->getAsInt(PREF_RETRY_WAIT)));
req_->setWakeTime(wakeTime);
}
return prepareForRetry(0);
}
catch (DownloadFailureException& err) {
requestGroup_->setLastErrorCode(err.getErrorCode(), err.what());
if (req_) {
A2_LOG_ERROR_EX(
fmt(MSG_DOWNLOAD_ABORTED, getCuid(), req_->getUri().c_str()),
DL_ABORT_EX2(fmt("URI=%s", req_->getCurrentUri().c_str()), err));
fileEntry_->addURIResult(req_->getUri(), err.getErrorCode());
}
else {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, err);
}
requestGroup_->setHaltRequested(true);
getDownloadEngine()->setRefreshInterval(std::chrono::milliseconds(0));
return true;
}
}
void AbstractCommand::tryReserved()
{
if (getDownloadContext()->getFileEntries().size() == 1) {
const auto& entry = getDownloadContext()->getFirstFileEntry();
// Don't create new command if currently file length is unknown
// and there are no URI left. Because file length is unknown, we
// can assume that there are no in-flight request object.
if (entry->getLength() == 0 && entry->getRemainingUris().empty()) {
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Not trying next request."
" No reserved/pooled request is remaining and"
" total length is still unknown.",
getCuid()));
return;
}
}
A2_LOG_DEBUG(
fmt("CUID#%" PRId64 " - Trying reserved/pooled request.", getCuid()));
std::vector<std::unique_ptr<Command>> commands;
requestGroup_->createNextCommand(commands, e_, 1);
e_->setNoWait(true);
e_->addCommand(std::move(commands));
}
bool AbstractCommand::prepareForRetry(time_t wait)
{
if (getPieceStorage()) {
getSegmentMan()->cancelSegment(getCuid());
}
if (req_) {
// Reset persistentConnection and maxPipelinedRequest to handle
// the situation where remote server returns Connection: close
// after several pipelined requests.
req_->supportsPersistentConnection(true);
req_->setMaxPipelinedRequest(1);
fileEntry_->poolRequest(req_);
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Pooling request URI=%s", getCuid(),
req_->getUri().c_str()));
if (getSegmentMan()) {
getSegmentMan()->recognizeSegmentFor(fileEntry_);
}
}
auto command =
make_unique<CreateRequestCommand>(getCuid(), requestGroup_, e_);
if (wait == 0) {
e_->setNoWait(true);
}
else {
// We don't use wait so that Command can be executed by
// DownloadEngine::setRefreshInterval(std::chrono::milliseconds(0)).
command->setStatus(Command::STATUS_INACTIVE);
}
e_->addCommand(std::move(command));
return true;
}
void AbstractCommand::onAbort()
{
if (req_) {
fileEntry_->removeIdenticalURI(req_->getUri());
fileEntry_->removeRequest(req_);
}
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Aborting download", getCuid()));
if (!getPieceStorage()) {
return;
}
getSegmentMan()->cancelSegment(getCuid());
// Don't do following process if BitTorrent is involved or files
// in DownloadContext is more than 1. The latter condition is
// limitation of current implementation.
if (getOption()->getAsBool(PREF_ALWAYS_RESUME) || !fileEntry_ ||
getDownloadContext()->getNetStat().getSessionDownloadLength() != 0 ||
requestGroup_->p2pInvolved() ||
getDownloadContext()->getFileEntries().size() != 1) {
return;
}
const int maxTries = getOption()->getAsInt(PREF_MAX_RESUME_FAILURE_TRIES);
if (!(maxTries > 0 && requestGroup_->getResumeFailureCount() >= maxTries) &&
!fileEntry_->emptyRequestUri()) {
return;
}
// Local file exists, but given servers(or at least contacted
// ones) doesn't support resume. Let's restart download from
// scratch.
A2_LOG_NOTICE(fmt(_("CUID#%" PRId64 " - Failed to resume download."
" Download from scratch."),
getCuid()));
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Gathering URIs that has CANNOT_RESUME"
" error",
getCuid()));
// Set PREF_ALWAYS_RESUME to A2_V_TRUE to avoid repeating this
// process.
getOption()->put(PREF_ALWAYS_RESUME, A2_V_TRUE);
std::deque<URIResult> res;
fileEntry_->extractURIResult(res, error_code::CANNOT_RESUME);
if (res.empty()) {
return;
}
getSegmentMan()->cancelAllSegments();
getSegmentMan()->eraseSegmentWrittenLengthMemo();
getPieceStorage()->markPiecesDone(0);
std::vector<std::string> uris;
uris.reserve(res.size());
std::transform(std::begin(res), std::end(res), std::back_inserter(uris),
std::mem_fn(&URIResult::getURI));
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - %lu URIs found.", getCuid(),
static_cast<unsigned long int>(uris.size())));
fileEntry_->addUris(std::begin(uris), std::end(uris));
getSegmentMan()->recognizeSegmentFor(fileEntry_);
}
void AbstractCommand::disableReadCheckSocket()
{
if (!checkSocketIsReadable_) {
return;
}
e_->deleteSocketForReadCheck(readCheckTarget_, this);
checkSocketIsReadable_ = false;
readCheckTarget_.reset();
}
void AbstractCommand::setReadCheckSocket(
const std::shared_ptr<SocketCore>& socket)
{
if (!socket->isOpen()) {
disableReadCheckSocket();
return;
}
if (checkSocketIsReadable_) {
if (*readCheckTarget_ != *socket) {
e_->deleteSocketForReadCheck(readCheckTarget_, this);
e_->addSocketForReadCheck(socket, this);
readCheckTarget_ = socket;
}
return;
}
e_->addSocketForReadCheck(socket, this);
checkSocketIsReadable_ = true;
readCheckTarget_ = socket;
}
void AbstractCommand::setReadCheckSocketIf(
const std::shared_ptr<SocketCore>& socket, bool pred)
{
if (pred) {
setReadCheckSocket(socket);
return;
}
disableReadCheckSocket();
}
void AbstractCommand::disableWriteCheckSocket()
{
if (!checkSocketIsWritable_) {
return;
}
e_->deleteSocketForWriteCheck(writeCheckTarget_, this);
checkSocketIsWritable_ = false;
writeCheckTarget_.reset();
}
void AbstractCommand::setWriteCheckSocket(
const std::shared_ptr<SocketCore>& socket)
{
if (!socket->isOpen()) {
disableWriteCheckSocket();
return;
}
if (checkSocketIsWritable_) {
if (*writeCheckTarget_ != *socket) {
e_->deleteSocketForWriteCheck(writeCheckTarget_, this);
e_->addSocketForWriteCheck(socket, this);
writeCheckTarget_ = socket;
}
return;
}
e_->addSocketForWriteCheck(socket, this);
checkSocketIsWritable_ = true;
writeCheckTarget_ = socket;
}
void AbstractCommand::setWriteCheckSocketIf(
const std::shared_ptr<SocketCore>& socket, bool pred)
{
if (pred) {
setWriteCheckSocket(socket);
return;
}
disableWriteCheckSocket();
}
void AbstractCommand::swapSocket(std::shared_ptr<SocketCore>& socket)
{
disableReadCheckSocket();
disableWriteCheckSocket();
socket_.swap(socket);
}
namespace {
// Constructs proxy URI, merging username and password if they are
// defined.
std::string makeProxyUri(PrefPtr proxyPref, PrefPtr proxyUser,
PrefPtr proxyPasswd, const Option* option)
{
uri::UriStruct us;
if (!uri::parse(us, option->get(proxyPref))) {
return "";
}
if (option->defined(proxyUser)) {
us.username = option->get(proxyUser);
}
if (option->defined(proxyPasswd)) {
us.password = option->get(proxyPasswd);
us.hasPassword = true;
}
return uri::construct(us);
}
} // namespace
namespace {
// Returns proxy option value for the given protocol.
std::string getProxyOptionFor(PrefPtr proxyPref, PrefPtr proxyUser,
PrefPtr proxyPasswd, const Option* option)
{
std::string uri = makeProxyUri(proxyPref, proxyUser, proxyPasswd, option);
if (uri.empty()) {
return makeProxyUri(PREF_ALL_PROXY, PREF_ALL_PROXY_USER,
PREF_ALL_PROXY_PASSWD, option);
}
return uri;
}
} // namespace
// Returns proxy URI for given protocol. If no proxy URI is defined,
// then returns an empty string.
std::string getProxyUri(const std::string& protocol, const Option* option)
{
if (protocol == "http") {
return getProxyOptionFor(PREF_HTTP_PROXY, PREF_HTTP_PROXY_USER,
PREF_HTTP_PROXY_PASSWD, option);
}
if (protocol == "https") {
return getProxyOptionFor(PREF_HTTPS_PROXY, PREF_HTTPS_PROXY_USER,
PREF_HTTPS_PROXY_PASSWD, option);
}
if (protocol == "ftp" || protocol == "sftp") {
return getProxyOptionFor(PREF_FTP_PROXY, PREF_FTP_PROXY_USER,
PREF_FTP_PROXY_PASSWD, option);
}
return A2STR::NIL;
}
namespace {
// Returns true if proxy is defined for the given protocol. Otherwise
// returns false.
bool isProxyRequest(const std::string& protocol,
const std::shared_ptr<Option>& option)
{
std::string proxyUri = getProxyUri(protocol, option.get());
return !proxyUri.empty();
}
} // namespace
namespace {
bool inNoProxy(const std::shared_ptr<Request>& req, const std::string& noProxy)
{
std::vector<Scip> entries;
util::splitIter(std::begin(noProxy), std::end(noProxy),
std::back_inserter(entries), ',', true);
if (entries.empty()) {
return false;
}
for (const auto& e : entries) {
const auto slashpos = std::find(e.first, e.second, '/');
if (slashpos == e.second) {
if (util::noProxyDomainMatch(req->getHost(),
std::string(e.first, e.second))) {
return true;
}
continue;
}
// TODO We don't resolve hostname here. More complete
// implementation is that we should first resolve
// hostname(which may result in several IP addresses) and
// evaluates against all of them
std::string ip(e.first, slashpos);
uint32_t bits;
if (!util::parseUIntNoThrow(bits, std::string(slashpos + 1, e.second))) {
continue;
}
if (util::inSameCidrBlock(ip, req->getHost(), bits)) {
return true;
}
}
return false;
}
} // namespace
bool AbstractCommand::isProxyDefined() const
{
return isProxyRequest(req_->getProtocol(), getOption()) &&
!inNoProxy(req_, getOption()->get(PREF_NO_PROXY));
}
std::shared_ptr<Request> AbstractCommand::createProxyRequest() const
{
std::shared_ptr<Request> proxyRequest;
if (inNoProxy(req_, getOption()->get(PREF_NO_PROXY))) {
return proxyRequest;
}
std::string proxy = getProxyUri(req_->getProtocol(), getOption().get());
if (!proxy.empty()) {
proxyRequest = std::make_shared<Request>();
if (proxyRequest->setUri(proxy)) {
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Using proxy", getCuid()));
}
else {
A2_LOG_DEBUG(
fmt("CUID#%" PRId64 " - Failed to parse proxy string", getCuid()));
proxyRequest.reset();
}
}
return proxyRequest;
}
std::string AbstractCommand::resolveHostname(std::vector<std::string>& addrs,
const std::string& hostname,
uint16_t port)
{
if (util::isNumericHost(hostname)) {
addrs.push_back(hostname);
return hostname;
}
e_->findAllCachedIPAddresses(std::back_inserter(addrs), hostname, port);
if (!addrs.empty()) {
auto ipaddr = addrs.front();
A2_LOG_INFO(fmt(MSG_DNS_CACHE_HIT, getCuid(), hostname.c_str(),
strjoin(std::begin(addrs), std::end(addrs), ", ").c_str()));
return ipaddr;
}
std::string ipaddr;
#ifdef ENABLE_ASYNC_DNS
if (getOption()->getAsBool(PREF_ASYNC_DNS)) {
if (!asyncNameResolverMan_->started()) {
asyncNameResolverMan_->startAsync(hostname, e_, this);
}
switch (asyncNameResolverMan_->getStatus()) {
case -1:
if (!isProxyRequest(req_->getProtocol(), getOption())) {
e_->getRequestGroupMan()
->getOrCreateServerStat(req_->getHost(), req_->getProtocol())
->setError();
}
throw DL_ABORT_EX2(fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(),
hostname.c_str(),
asyncNameResolverMan_->getLastError().c_str()),
error_code::NAME_RESOLVE_ERROR);
case 0:
return A2STR::NIL;
case 1:
asyncNameResolverMan_->getResolvedAddress(addrs);
if (addrs.empty()) {
throw DL_ABORT_EX2(fmt(MSG_NAME_RESOLUTION_FAILED, getCuid(),
hostname.c_str(), "No address returned"),
error_code::NAME_RESOLVE_ERROR);
}
break;
}
}
else
#endif // ENABLE_ASYNC_DNS
{
NameResolver res;
res.setSocktype(SOCK_STREAM);
if (e_->getOption()->getAsBool(PREF_DISABLE_IPV6)) {
res.setFamily(AF_INET);
}
res.resolve(addrs, hostname);
}
A2_LOG_INFO(fmt(MSG_NAME_RESOLUTION_COMPLETE, getCuid(), hostname.c_str(),
strjoin(std::begin(addrs), std::end(addrs), ", ").c_str()));
for (const auto& addr : addrs) {
e_->cacheIPAddress(hostname, addr, port);
}
ipaddr = e_->findCachedIPAddress(hostname, port);
return ipaddr;
}
void AbstractCommand::prepareForNextAction(
std::unique_ptr<CheckIntegrityEntry> checkEntry)
{
std::vector<std::unique_ptr<Command>> commands;
requestGroup_->processCheckIntegrityEntry(commands, std::move(checkEntry),
e_);
e_->addCommand(std::move(commands));
e_->setNoWait(true);
}
bool AbstractCommand::checkIfConnectionEstablished(
const std::shared_ptr<SocketCore>& socket,
const std::string& connectedHostname, const std::string& connectedAddr,
uint16_t connectedPort)
{
std::string error = socket->getSocketError();
if (error.empty()) {
return true;
}
// See also InitiateConnectionCommand::executeInternal()
e_->markBadIPAddress(connectedHostname, connectedAddr, connectedPort);
if (e_->findCachedIPAddress(connectedHostname, connectedPort).empty()) {
e_->removeCachedIPAddress(connectedHostname, connectedPort);
// Don't set error if proxy server is used and its method is GET.
if (resolveProxyMethod(req_->getProtocol()) != V_GET ||
!isProxyRequest(req_->getProtocol(), getOption())) {
e_->getRequestGroupMan()
->getOrCreateServerStat(req_->getHost(), req_->getProtocol())
->setError();
}
throw DL_RETRY_EX(fmt(MSG_ESTABLISHING_CONNECTION_FAILED, error.c_str()));
}
A2_LOG_INFO(fmt(MSG_CONNECT_FAILED_AND_RETRY, getCuid(),
connectedAddr.c_str(), connectedPort));
e_->setNoWait(true);
e_->addCommand(
InitiateConnectionCommandFactory::createInitiateConnectionCommand(
getCuid(), req_, fileEntry_, requestGroup_, e_));
return false;
}
const std::string&
AbstractCommand::resolveProxyMethod(const std::string& protocol) const
{
if (getOption()->get(PREF_PROXY_METHOD) == V_TUNNEL || protocol == "https" ||
protocol == "sftp") {
return V_TUNNEL;
}
return V_GET;
}
const std::shared_ptr<Option>& AbstractCommand::getOption() const
{
return requestGroup_->getOption();
}
void AbstractCommand::createSocket()
{
socket_ = std::make_shared<SocketCore>();
}
int32_t AbstractCommand::calculateMinSplitSize() const
{
if (req_ && req_->isPipeliningEnabled()) {
return getDownloadContext()->getPieceLength();
}
return getOption()->getAsInt(PREF_MIN_SPLIT_SIZE);
}
void AbstractCommand::setRequest(const std::shared_ptr<Request>& request)
{
req_ = request;
}
void AbstractCommand::resetRequest() { req_.reset(); }
void AbstractCommand::setFileEntry(const std::shared_ptr<FileEntry>& fileEntry)
{
fileEntry_ = fileEntry;
}
void AbstractCommand::setSocket(const std::shared_ptr<SocketCore>& s)
{
socket_ = s;
}
const std::shared_ptr<DownloadContext>&
AbstractCommand::getDownloadContext() const
{
return requestGroup_->getDownloadContext();
}
const std::shared_ptr<SegmentMan>& AbstractCommand::getSegmentMan() const
{
return requestGroup_->getSegmentMan();
}
const std::shared_ptr<PieceStorage>& AbstractCommand::getPieceStorage() const
{
return requestGroup_->getPieceStorage();
}
void AbstractCommand::checkSocketRecvBuffer()
{
if (socketRecvBuffer_->bufferEmpty() &&
socket_->getRecvBufferedLength() == 0) {
return;
}
setStatus(Command::STATUS_ONESHOT_REALTIME);
e_->setNoWait(true);
}
void AbstractCommand::addCommandSelf()
{
e_->addCommand(std::unique_ptr<Command>(this));
}
} // namespace aria2