From 9a38c102dc4ff4a0a7cc96ccef7cd1400f37eea5 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 3 Jul 2013 23:42:55 +0900 Subject: [PATCH] Use std::unique_ptr for HttpResponse instead of std::shared_ptr --- src/HttpConnection.cc | 8 ++-- src/HttpConnection.h | 2 +- src/HttpDownloadCommand.cc | 4 +- src/HttpDownloadCommand.h | 4 +- src/HttpResponseCommand.cc | 82 +++++++++++++++------------------- src/HttpResponseCommand.h | 11 +++-- src/HttpSkipResponseCommand.cc | 4 +- src/HttpSkipResponseCommand.h | 4 +- 8 files changed, 53 insertions(+), 66 deletions(-) diff --git a/src/HttpConnection.cc b/src/HttpConnection.cc index 9f49c1d8..f3925295 100644 --- a/src/HttpConnection.cc +++ b/src/HttpConnection.cc @@ -129,7 +129,7 @@ void HttpConnection::sendProxyRequest sendRequest(httpRequest, httpRequest->createProxyRequest()); } -std::shared_ptr HttpConnection::receiveResponse() +std::unique_ptr HttpConnection::receiveResponse() { if(outstandingHttpRequests_.empty()) { throw DL_ABORT_EX(EX_NO_HTTP_REQUEST_ENTRY_FOUND); @@ -140,24 +140,24 @@ std::shared_ptr HttpConnection::receiveResponse() throw DL_RETRY_EX(EX_GOT_EOF); } } - std::shared_ptr httpResponse; const auto& proc = outstandingHttpRequests_.front()->getHttpHeaderProcessor(); if(proc->parse(socketRecvBuffer_->getBuffer(), socketRecvBuffer_->getBufferLength())) { A2_LOG_INFO(fmt(MSG_RECEIVE_RESPONSE, cuid_, proc->getHeaderString().c_str())); - httpResponse.reset(new HttpResponse()); + auto httpResponse = make_unique(); httpResponse->setCuid(cuid_); httpResponse->setHttpHeader(proc->getResult()); httpResponse->setHttpRequest(outstandingHttpRequests_.front()-> getHttpRequest()); socketRecvBuffer_->shiftBuffer(proc->getLastBytesProcessed()); outstandingHttpRequests_.pop_front(); + return httpResponse; } else { socketRecvBuffer_->shiftBuffer(proc->getLastBytesProcessed()); + return std::unique_ptr{}; } - return httpResponse; } bool HttpConnection::isIssued(const std::shared_ptr& segment) const diff --git a/src/HttpConnection.h b/src/HttpConnection.h index 0d049734..70c133f8 100644 --- a/src/HttpConnection.h +++ b/src/HttpConnection.h @@ -117,7 +117,7 @@ public: * * @return HttpResponse or 0 if whole response header is not received */ - std::shared_ptr receiveResponse(); + std::unique_ptr receiveResponse(); std::shared_ptr getFirstHttpRequest() const; diff --git a/src/HttpDownloadCommand.cc b/src/HttpDownloadCommand.cc index 93cca2d0..1a76c88b 100644 --- a/src/HttpDownloadCommand.cc +++ b/src/HttpDownloadCommand.cc @@ -60,13 +60,13 @@ HttpDownloadCommand::HttpDownloadCommand const std::shared_ptr& req, const std::shared_ptr& fileEntry, RequestGroup* requestGroup, - const std::shared_ptr& httpResponse, + std::unique_ptr httpResponse, const std::shared_ptr& httpConnection, DownloadEngine* e, const std::shared_ptr& socket) : DownloadCommand(cuid, req, fileEntry, requestGroup, e, socket, httpConnection->getSocketRecvBuffer()), - httpResponse_(httpResponse), + httpResponse_(std::move(httpResponse)), httpConnection_(httpConnection) {} diff --git a/src/HttpDownloadCommand.h b/src/HttpDownloadCommand.h index 486da7b9..fedcd065 100644 --- a/src/HttpDownloadCommand.h +++ b/src/HttpDownloadCommand.h @@ -44,7 +44,7 @@ class HttpConnection; class HttpDownloadCommand : public DownloadCommand { private: - std::shared_ptr httpResponse_; + std::unique_ptr httpResponse_; std::shared_ptr httpConnection_; protected: virtual bool prepareForNextSegment(); @@ -54,7 +54,7 @@ public: const std::shared_ptr& req, const std::shared_ptr& fileEntry, RequestGroup* requestGroup, - const std::shared_ptr& httpResponse, + std::unique_ptr httpResponse, const std::shared_ptr& httpConnection, DownloadEngine* e, const std::shared_ptr& s); diff --git a/src/HttpResponseCommand.cc b/src/HttpResponseCommand.cc index c2ee84d6..fc4b9021 100644 --- a/src/HttpResponseCommand.cc +++ b/src/HttpResponseCommand.cc @@ -85,7 +85,7 @@ namespace aria2 { namespace { std::shared_ptr getTransferEncodingStreamFilter -(const std::shared_ptr& httpResponse, +(HttpResponse* httpResponse, const std::shared_ptr& delegate = std::shared_ptr()) { std::shared_ptr filter; @@ -108,7 +108,7 @@ std::shared_ptr getTransferEncodingStreamFilter namespace { std::shared_ptr getContentEncodingStreamFilter -(const std::shared_ptr& httpResponse, +(HttpResponse* httpResponse, const std::shared_ptr& delegate = std::shared_ptr()) { std::shared_ptr filter; @@ -152,7 +152,7 @@ HttpResponseCommand::~HttpResponseCommand() {} bool HttpResponseCommand::executeInternal() { std::shared_ptr httpRequest =httpConnection_->getFirstHttpRequest(); - std::shared_ptr httpResponse = httpConnection_->receiveResponse(); + auto httpResponse = httpConnection_->receiveResponse(); if(!httpResponse) { // The server has not responded to our request yet. // For socket->wantRead() == true, setReadCheckSocket(socket) is already @@ -234,7 +234,7 @@ bool HttpResponseCommand::executeInternal() if(statusCode == 404) { getRequestGroup()->increaseAndValidateFileNotFoundCount(); } - return skipResponseBody(httpResponse); + return skipResponseBody(std::move(httpResponse)); } if(getFileEntry()->isUniqueProtocol()) { // Redirection should be considered here. We need to parse @@ -269,7 +269,7 @@ bool HttpResponseCommand::executeInternal() // If both transfer-encoding and total length is specified, we // assume we can do segmented downloading - if(totalLength == 0 || shouldInflateContentEncoding(httpResponse)) { + if(totalLength == 0 || shouldInflateContentEncoding(httpResponse.get())) { // we ignore content-length when inflate is required getFileEntry()->setLength(0); if(getRequest()->getMethod() == Request::METHOD_GET && @@ -279,9 +279,9 @@ bool HttpResponseCommand::executeInternal() // server says the size of file is 0 explicitly. getDownloadContext()->markTotalLengthIsUnknown(); } - return handleOtherEncoding(httpResponse); + return handleOtherEncoding(std::move(httpResponse)); } else { - return handleDefaultEncoding(httpResponse); + return handleDefaultEncoding(std::move(httpResponse)); } } else { #ifdef ENABLE_MESSAGE_DIGEST @@ -308,17 +308,15 @@ bool HttpResponseCommand::executeInternal() // Also we can't resume in this case too. So truncate the file // anyway. getPieceStorage()->getDiskAdaptor()->truncate(0); + auto teFilter = getTransferEncodingStreamFilter + (httpResponse.get(), + getContentEncodingStreamFilter(httpResponse.get())); getDownloadEngine()->addCommand - (createHttpDownloadCommand - (httpResponse, - getTransferEncodingStreamFilter - (httpResponse, - getContentEncodingStreamFilter(httpResponse)))); + (createHttpDownloadCommand(std::move(httpResponse), teFilter)); } else { + auto teFilter = getTransferEncodingStreamFilter(httpResponse.get()); getDownloadEngine()->addCommand - (createHttpDownloadCommand - (httpResponse, - getTransferEncodingStreamFilter(httpResponse))); + (createHttpDownloadCommand(std::move(httpResponse), teFilter)); } return true; } @@ -332,7 +330,7 @@ void HttpResponseCommand::updateLastModifiedTime(const Time& lastModified) } bool HttpResponseCommand::shouldInflateContentEncoding -(const std::shared_ptr& httpResponse) +(HttpResponse* httpResponse) { // Basically, on the fly inflation cannot be made with segment // download, because in each segment we don't know where the date @@ -347,12 +345,10 @@ bool HttpResponseCommand::shouldInflateContentEncoding } bool HttpResponseCommand::handleDefaultEncoding -(const std::shared_ptr& httpResponse) +(std::unique_ptr httpResponse) { - std::shared_ptr httpRequest = httpResponse->getHttpRequest(); - std::shared_ptr progressInfoFile - (new DefaultBtProgressInfoFile - (getDownloadContext(), std::shared_ptr(), getOption().get())); + auto progressInfoFile = std::make_shared + (getDownloadContext(), std::shared_ptr{}, getOption().get()); getRequestGroup()->adjustFilename(progressInfoFile); getRequestGroup()->initPieceStorage(); @@ -361,8 +357,7 @@ bool HttpResponseCommand::handleDefaultEncoding return true; } - std::shared_ptr checkEntry = - getRequestGroup()->createCheckIntegrityEntry(); + auto checkEntry = getRequestGroup()->createCheckIntegrityEntry(); if(!checkEntry) { return true; } @@ -370,8 +365,7 @@ bool HttpResponseCommand::handleDefaultEncoding // We have to make sure that command that has Request object must // have segment after PieceStorage is initialized. See // AbstractCommand::execute() - std::shared_ptr segment = - getSegmentMan()->getSegmentWithIndex(getCuid(), 0); + auto segment = getSegmentMan()->getSegmentWithIndex(getCuid(), 0); // pipelining requires implicit range specified. But the request for // this response most likely dones't contains range header. This means // we can't continue to use this socket because server sends all entity @@ -380,10 +374,9 @@ bool HttpResponseCommand::handleDefaultEncoding if(getRequest()->getMethod() == Request::METHOD_GET && segment && segment->getPositionToWrite() == 0 && !getRequest()->isPipeliningEnabled()) { + auto teFilter = getTransferEncodingStreamFilter(httpResponse.get()); checkEntry->pushNextCommand - (createHttpDownloadCommand - (httpResponse, - getTransferEncodingStreamFilter(httpResponse))); + (createHttpDownloadCommand(std::move(httpResponse), teFilter)); } else { getSegmentMan()->cancelSegment(getCuid()); getFileEntry()->poolRequest(getRequest()); @@ -399,10 +392,8 @@ bool HttpResponseCommand::handleDefaultEncoding } bool HttpResponseCommand::handleOtherEncoding -(const std::shared_ptr& httpResponse) { +(std::unique_ptr httpResponse) { // We assume that RequestGroup::getTotalLength() == 0 here - std::shared_ptr httpRequest = httpResponse->getHttpRequest(); - if(getOption()->getAsBool(PREF_DRY_RUN)) { getRequestGroup()->initPieceStorage(); onDryRunFileFound(); @@ -418,10 +409,8 @@ bool HttpResponseCommand::handleOtherEncoding // In this context, knowsTotalLength() is true only when the file is // really zero-length. - std::shared_ptr streamFilter = - getTransferEncodingStreamFilter - (httpResponse, - getContentEncodingStreamFilter(httpResponse)); + auto streamFilter = getTransferEncodingStreamFilter + (httpResponse.get(), getContentEncodingStreamFilter(httpResponse.get())); // If chunked transfer-encoding is specified, we have to read end of // chunk markers(0\r\n\r\n, for example). bool chunkedUsed = streamFilter && @@ -471,8 +460,8 @@ bool HttpResponseCommand::handleOtherEncoding #ifdef ENABLE_MESSAGE_DIGEST if(getDownloadContext()->isChecksumVerificationNeeded()) { A2_LOG_DEBUG("Verify checksum for zero-length file"); - std::shared_ptr entry - (new ChecksumCheckIntegrityEntry(getRequestGroup())); + auto entry = std::make_shared + (getRequestGroup()); entry->initValidator(); getDownloadEngine()->getCheckIntegrityMan()->pushEntry(entry); } else @@ -489,29 +478,28 @@ bool HttpResponseCommand::handleOtherEncoding getSegmentMan()->getSegmentWithIndex(getCuid(), 0); getDownloadEngine()->addCommand - (createHttpDownloadCommand(httpResponse, streamFilter)); + (createHttpDownloadCommand(std::move(httpResponse), streamFilter)); return true; } bool HttpResponseCommand::skipResponseBody -(const std::shared_ptr& httpResponse) +(std::unique_ptr httpResponse) { - std::shared_ptr filter = - getTransferEncodingStreamFilter(httpResponse); + auto filter = getTransferEncodingStreamFilter(httpResponse.get()); // We don't use Content-Encoding here because this response body is just // thrown away. - + auto httpResponsePtr = httpResponse.get(); auto command = make_unique (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), - httpConnection_, httpResponse, + httpConnection_, std::move(httpResponse), getDownloadEngine(), getSocket()); command->installStreamFilter(filter); // If request method is HEAD or the response body is zero-length, // set command's status to real time so that avoid read check blocking if(getRequest()->getMethod() == Request::METHOD_HEAD || - (httpResponse->getEntityLength() == 0 && - !httpResponse->isTransferEncodingSpecified())) { + (httpResponsePtr->getEntityLength() == 0 && + !httpResponsePtr->isTransferEncodingSpecified())) { command->setStatusRealtime(); // If entity length == 0, then socket read/write check must be disabled. command->disableSocketCheck(); @@ -542,14 +530,14 @@ bool decideFileAllocation std::unique_ptr HttpResponseCommand::createHttpDownloadCommand -(const std::shared_ptr& httpResponse, +(std::unique_ptr httpResponse, const std::shared_ptr& filter) { auto command = make_unique (getCuid(), getRequest(), getFileEntry(), getRequestGroup(), - httpResponse, httpConnection_, + std::move(httpResponse), httpConnection_, getDownloadEngine(), getSocket()); command->setStartupIdleTime(getOption()->getAsInt(PREF_STARTUP_IDLE_TIME)); command->setLowestDownloadSpeedLimit diff --git a/src/HttpResponseCommand.h b/src/HttpResponseCommand.h index 15b6bb87..85a9dfff 100644 --- a/src/HttpResponseCommand.h +++ b/src/HttpResponseCommand.h @@ -63,13 +63,13 @@ class HttpResponseCommand : public AbstractCommand { private: std::shared_ptr httpConnection_; - bool handleDefaultEncoding(const std::shared_ptr& httpResponse); - bool handleOtherEncoding(const std::shared_ptr& httpResponse); - bool skipResponseBody(const std::shared_ptr& httpResponse); + bool handleDefaultEncoding(std::unique_ptr httpResponse); + bool handleOtherEncoding(std::unique_ptr httpResponse); + bool skipResponseBody(std::unique_ptr httpResponse); std::unique_ptr createHttpDownloadCommand - (const std::shared_ptr& httpResponse, + (std::unique_ptr httpResponse, const std::shared_ptr& streamFilter); void updateLastModifiedTime(const Time& lastModified); @@ -88,8 +88,7 @@ private: protected: bool executeInternal(); - bool shouldInflateContentEncoding - (const std::shared_ptr& httpResponse); + bool shouldInflateContentEncoding(HttpResponse* httpResponse); public: HttpResponseCommand(cuid_t cuid, diff --git a/src/HttpSkipResponseCommand.cc b/src/HttpSkipResponseCommand.cc index dc37f4ee..6cfa9706 100644 --- a/src/HttpSkipResponseCommand.cc +++ b/src/HttpSkipResponseCommand.cc @@ -69,13 +69,13 @@ HttpSkipResponseCommand::HttpSkipResponseCommand const std::shared_ptr& fileEntry, RequestGroup* requestGroup, const std::shared_ptr& httpConnection, - const std::shared_ptr& httpResponse, + std::unique_ptr httpResponse, DownloadEngine* e, const std::shared_ptr& s) : AbstractCommand(cuid, req, fileEntry, requestGroup, e, s, httpConnection->getSocketRecvBuffer()), httpConnection_(httpConnection), - httpResponse_(httpResponse), + httpResponse_(std::move(httpResponse)), streamFilter_(new NullSinkStreamFilter()), sinkFilterOnly_(true), totalLength_(httpResponse_->getEntityLength()), diff --git a/src/HttpSkipResponseCommand.h b/src/HttpSkipResponseCommand.h index d30e1889..0e486055 100644 --- a/src/HttpSkipResponseCommand.h +++ b/src/HttpSkipResponseCommand.h @@ -47,7 +47,7 @@ class HttpSkipResponseCommand : public AbstractCommand { private: std::shared_ptr httpConnection_; - std::shared_ptr httpResponse_; + std::unique_ptr httpResponse_; std::shared_ptr streamFilter_; @@ -69,7 +69,7 @@ public: const std::shared_ptr& fileEntry, RequestGroup* requestGroup, const std::shared_ptr& httpConnection, - const std::shared_ptr& httpResponse, + std::unique_ptr httpResponse, DownloadEngine* e, const std::shared_ptr& s);