diff --git a/src/FtpConnection.cc b/src/FtpConnection.cc index 9fc3ac80..30638fa2 100644 --- a/src/FtpConnection.cc +++ b/src/FtpConnection.cc @@ -60,10 +60,6 @@ namespace aria2 { -const std::string FtpConnection::A("A"); - -const std::string FtpConnection::I("I"); - FtpConnection::FtpConnection (cuid_t cuid, const SocketHandle& socket, @@ -89,7 +85,7 @@ bool FtpConnection::sendUser() request += "\r\n"; A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, "USER ********")); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); } socketBuffer_.send(); return socketBuffer_.sendBufferIsEmpty(); @@ -103,7 +99,7 @@ bool FtpConnection::sendPass() request += "\r\n"; A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, "PASS ********")); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); } socketBuffer_.send(); return socketBuffer_.sendBufferIsEmpty(); @@ -112,18 +108,12 @@ bool FtpConnection::sendPass() bool FtpConnection::sendType() { if(socketBuffer_.sendBufferIsEmpty()) { - std::string type; - if(option_->get(PREF_FTP_TYPE) == V_ASCII) { - type = FtpConnection::A; - } else { - type = FtpConnection::I; - } std::string request = "TYPE "; - request += type; + request += (option_->get(PREF_FTP_TYPE) == V_ASCII ? 'A' : 'I'); request += "\r\n"; A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_,request.c_str())); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); } socketBuffer_.send(); return socketBuffer_.sendBufferIsEmpty(); @@ -135,7 +125,7 @@ bool FtpConnection::sendPwd() std::string request = "PWD\r\n"; A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_,request.c_str())); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); } socketBuffer_.send(); return socketBuffer_.sendBufferIsEmpty(); @@ -149,7 +139,7 @@ bool FtpConnection::sendCwd(const std::string& dir) request += "\r\n"; A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_,request.c_str())); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); } socketBuffer_.send(); return socketBuffer_.sendBufferIsEmpty(); @@ -164,7 +154,7 @@ bool FtpConnection::sendMdtm() request += "\r\n"; A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); } socketBuffer_.send(); return socketBuffer_.sendBufferIsEmpty(); @@ -179,7 +169,7 @@ bool FtpConnection::sendSize() request += "\r\n"; A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); } socketBuffer_.send(); return socketBuffer_.sendBufferIsEmpty(); @@ -188,10 +178,10 @@ bool FtpConnection::sendSize() bool FtpConnection::sendEpsv() { if(socketBuffer_.sendBufferIsEmpty()) { - static const std::string request("EPSV\r\n"); + std::string request("EPSV\r\n"); A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); } socketBuffer_.send(); return socketBuffer_.sendBufferIsEmpty(); @@ -200,10 +190,10 @@ bool FtpConnection::sendEpsv() bool FtpConnection::sendPasv() { if(socketBuffer_.sendBufferIsEmpty()) { - static const std::string request("PASV\r\n"); + std::string request("PASV\r\n"); A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); } socketBuffer_.send(); return socketBuffer_.sendBufferIsEmpty(); @@ -237,7 +227,7 @@ bool FtpConnection::sendEprt(const SharedHandle& serverSocket) request += util::uitos(addrinfo.second); request += "|\r\n"; A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); } socketBuffer_.send(); return socketBuffer_.sendBufferIsEmpty(); @@ -267,7 +257,7 @@ bool FtpConnection::sendPort(const SharedHandle& serverSocket) request += "\r\n"; A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); } socketBuffer_.send(); return socketBuffer_.sendBufferIsEmpty(); @@ -285,7 +275,7 @@ bool FtpConnection::sendRest(const SharedHandle& segment) request += "\r\n"; A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); } socketBuffer_.send(); return socketBuffer_.sendBufferIsEmpty(); @@ -300,7 +290,7 @@ bool FtpConnection::sendRetr() request += "\r\n"; A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str())); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); } socketBuffer_.send(); return socketBuffer_.sendBufferIsEmpty(); diff --git a/src/HttpConnection.cc b/src/HttpConnection.cc index ced5090a..3eb55e40 100644 --- a/src/HttpConnection.cc +++ b/src/HttpConnection.cc @@ -109,7 +109,7 @@ void HttpConnection::sendRequest(const SharedHandle& httpRequest) A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, eraseConfidentialInfo(request).c_str())); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); socketBuffer_.send(); SharedHandle entry(new HttpRequestEntry(httpRequest)); outstandingHttpRequests_.push_back(entry); @@ -122,7 +122,7 @@ void HttpConnection::sendProxyRequest A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, eraseConfidentialInfo(request).c_str())); - socketBuffer_.pushStr(request); + socketBuffer_.pushStrSwap(request); socketBuffer_.send(); SharedHandle entry(new HttpRequestEntry(httpRequest)); outstandingHttpRequests_.push_back(entry); diff --git a/src/HttpServer.cc b/src/HttpServer.cc index 318635c6..8b2762fd 100644 --- a/src/HttpServer.cc +++ b/src/HttpServer.cc @@ -156,14 +156,14 @@ const std::string& HttpServer::getRequestPath() const return lastRequestHeader_->getRequestPath(); } -void HttpServer::feedResponse(const std::string& text, const std::string& contentType) +void HttpServer::feedResponse(std::string& text, const std::string& contentType) { feedResponse("200 OK", "", text, contentType); } void HttpServer::feedResponse(const std::string& status, const std::string& headers, - const std::string& text, + std::string& text, const std::string& contentType) { std::string httpDate = Time().toHTTPDate(); @@ -194,8 +194,8 @@ void HttpServer::feedResponse(const std::string& status, header += "\r\n"; A2_LOG_DEBUG(fmt("HTTP Server sends response:\n%s", header.c_str())); - socketBuffer_.pushStr(header); - socketBuffer_.pushStr(text); + socketBuffer_.pushStrSwap(header); + socketBuffer_.pushStrSwap(text); } ssize_t HttpServer::sendResponse() diff --git a/src/HttpServer.h b/src/HttpServer.h index c8dbc8c2..665c6d88 100644 --- a/src/HttpServer.h +++ b/src/HttpServer.h @@ -84,11 +84,11 @@ public: const std::string& getRequestPath() const; - void feedResponse(const std::string& text, const std::string& contentType); + void feedResponse(std::string& text, const std::string& contentType); void feedResponse(const std::string& status, const std::string& headers, - const std::string& text, + std::string& text, const std::string& contentType); bool authenticate(); diff --git a/src/HttpServerCommand.cc b/src/HttpServerCommand.cc index 4dd43a7c..9cb0ebe1 100644 --- a/src/HttpServerCommand.cc +++ b/src/HttpServerCommand.cc @@ -123,9 +123,10 @@ bool HttpServerCommand::execute() } if(!httpServer_->authenticate()) { httpServer_->disableKeepAlive(); + std::string text; httpServer_->feedResponse("401 Unauthorized", "WWW-Authenticate: Basic realm=\"aria2\"", - "","text/html"); + text,"text/html"); Command* command = new HttpServerResponseCommand(getCuid(), httpServer_, e_, socket_); e_->addCommand(command); diff --git a/src/SocketBuffer.cc b/src/SocketBuffer.cc index 010ba984..a7933fd8 100644 --- a/src/SocketBuffer.cc +++ b/src/SocketBuffer.cc @@ -69,6 +69,8 @@ SocketBuffer::StringBufEntry::StringBufEntry(const std::string& s) : str_(s) {} +SocketBuffer::StringBufEntry::StringBufEntry() {} + ssize_t SocketBuffer::StringBufEntry::send (const SharedHandle& socket, size_t offset) { @@ -80,6 +82,11 @@ bool SocketBuffer::StringBufEntry::final(size_t offset) const return str_.size() <= offset; } +void SocketBuffer::StringBufEntry::swap(std::string& s) +{ + str_.swap(s); +} + SocketBuffer::SocketBuffer(const SharedHandle& socket): socket_(socket), offset_(0) {} @@ -99,6 +106,15 @@ void SocketBuffer::pushStr(const std::string& data) } } +void SocketBuffer::pushStrSwap(std::string& data) +{ + if(data.size() > 0) { + SharedHandle e(new StringBufEntry()); + e->swap(data); + bufq_.push_back(e); + } +} + ssize_t SocketBuffer::send() { size_t totalslen = 0; diff --git a/src/SocketBuffer.h b/src/SocketBuffer.h index f2baeb49..8cdcf3a1 100644 --- a/src/SocketBuffer.h +++ b/src/SocketBuffer.h @@ -71,9 +71,11 @@ private: class StringBufEntry:public BufEntry { public: StringBufEntry(const std::string& s); + StringBufEntry(); virtual ssize_t send (const SharedHandle& socket, size_t offset); virtual bool final(size_t offset) const; + void swap(std::string& s); private: std::string str_; }; @@ -103,6 +105,11 @@ public: // Feeds data into queue. This function doesn't send data. void pushStr(const std::string& data); + // Feeds data into queue. This function doesn't send data. data is + // swapped with internal buffer, so after this call, data will be + // empty. + void pushStrSwap(std::string& data); + // Sends data in queue. Returns the number of bytes sent. ssize_t send();