mirror of https://github.com/aria2/aria2
Added SocketBuffer::pushStrSwap
parent
4dad3ded15
commit
6ea1b68db1
|
@ -60,10 +60,6 @@
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
const std::string FtpConnection::A("A");
|
|
||||||
|
|
||||||
const std::string FtpConnection::I("I");
|
|
||||||
|
|
||||||
FtpConnection::FtpConnection
|
FtpConnection::FtpConnection
|
||||||
(cuid_t cuid,
|
(cuid_t cuid,
|
||||||
const SocketHandle& socket,
|
const SocketHandle& socket,
|
||||||
|
@ -89,7 +85,7 @@ bool FtpConnection::sendUser()
|
||||||
request += "\r\n";
|
request += "\r\n";
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_, "USER ********"));
|
cuid_, "USER ********"));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
}
|
}
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
return socketBuffer_.sendBufferIsEmpty();
|
return socketBuffer_.sendBufferIsEmpty();
|
||||||
|
@ -103,7 +99,7 @@ bool FtpConnection::sendPass()
|
||||||
request += "\r\n";
|
request += "\r\n";
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_, "PASS ********"));
|
cuid_, "PASS ********"));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
}
|
}
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
return socketBuffer_.sendBufferIsEmpty();
|
return socketBuffer_.sendBufferIsEmpty();
|
||||||
|
@ -112,18 +108,12 @@ bool FtpConnection::sendPass()
|
||||||
bool FtpConnection::sendType()
|
bool FtpConnection::sendType()
|
||||||
{
|
{
|
||||||
if(socketBuffer_.sendBufferIsEmpty()) {
|
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 ";
|
std::string request = "TYPE ";
|
||||||
request += type;
|
request += (option_->get(PREF_FTP_TYPE) == V_ASCII ? 'A' : 'I');
|
||||||
request += "\r\n";
|
request += "\r\n";
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_,request.c_str()));
|
cuid_,request.c_str()));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
}
|
}
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
return socketBuffer_.sendBufferIsEmpty();
|
return socketBuffer_.sendBufferIsEmpty();
|
||||||
|
@ -135,7 +125,7 @@ bool FtpConnection::sendPwd()
|
||||||
std::string request = "PWD\r\n";
|
std::string request = "PWD\r\n";
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_,request.c_str()));
|
cuid_,request.c_str()));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
}
|
}
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
return socketBuffer_.sendBufferIsEmpty();
|
return socketBuffer_.sendBufferIsEmpty();
|
||||||
|
@ -149,7 +139,7 @@ bool FtpConnection::sendCwd(const std::string& dir)
|
||||||
request += "\r\n";
|
request += "\r\n";
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_,request.c_str()));
|
cuid_,request.c_str()));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
}
|
}
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
return socketBuffer_.sendBufferIsEmpty();
|
return socketBuffer_.sendBufferIsEmpty();
|
||||||
|
@ -164,7 +154,7 @@ bool FtpConnection::sendMdtm()
|
||||||
request += "\r\n";
|
request += "\r\n";
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_, request.c_str()));
|
cuid_, request.c_str()));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
}
|
}
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
return socketBuffer_.sendBufferIsEmpty();
|
return socketBuffer_.sendBufferIsEmpty();
|
||||||
|
@ -179,7 +169,7 @@ bool FtpConnection::sendSize()
|
||||||
request += "\r\n";
|
request += "\r\n";
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_, request.c_str()));
|
cuid_, request.c_str()));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
}
|
}
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
return socketBuffer_.sendBufferIsEmpty();
|
return socketBuffer_.sendBufferIsEmpty();
|
||||||
|
@ -188,10 +178,10 @@ bool FtpConnection::sendSize()
|
||||||
bool FtpConnection::sendEpsv()
|
bool FtpConnection::sendEpsv()
|
||||||
{
|
{
|
||||||
if(socketBuffer_.sendBufferIsEmpty()) {
|
if(socketBuffer_.sendBufferIsEmpty()) {
|
||||||
static const std::string request("EPSV\r\n");
|
std::string request("EPSV\r\n");
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_, request.c_str()));
|
cuid_, request.c_str()));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
}
|
}
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
return socketBuffer_.sendBufferIsEmpty();
|
return socketBuffer_.sendBufferIsEmpty();
|
||||||
|
@ -200,10 +190,10 @@ bool FtpConnection::sendEpsv()
|
||||||
bool FtpConnection::sendPasv()
|
bool FtpConnection::sendPasv()
|
||||||
{
|
{
|
||||||
if(socketBuffer_.sendBufferIsEmpty()) {
|
if(socketBuffer_.sendBufferIsEmpty()) {
|
||||||
static const std::string request("PASV\r\n");
|
std::string request("PASV\r\n");
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_, request.c_str()));
|
cuid_, request.c_str()));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
}
|
}
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
return socketBuffer_.sendBufferIsEmpty();
|
return socketBuffer_.sendBufferIsEmpty();
|
||||||
|
@ -237,7 +227,7 @@ bool FtpConnection::sendEprt(const SharedHandle<SocketCore>& serverSocket)
|
||||||
request += util::uitos(addrinfo.second);
|
request += util::uitos(addrinfo.second);
|
||||||
request += "|\r\n";
|
request += "|\r\n";
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str()));
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST, cuid_, request.c_str()));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
}
|
}
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
return socketBuffer_.sendBufferIsEmpty();
|
return socketBuffer_.sendBufferIsEmpty();
|
||||||
|
@ -267,7 +257,7 @@ bool FtpConnection::sendPort(const SharedHandle<SocketCore>& serverSocket)
|
||||||
request += "\r\n";
|
request += "\r\n";
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_, request.c_str()));
|
cuid_, request.c_str()));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
}
|
}
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
return socketBuffer_.sendBufferIsEmpty();
|
return socketBuffer_.sendBufferIsEmpty();
|
||||||
|
@ -285,7 +275,7 @@ bool FtpConnection::sendRest(const SharedHandle<Segment>& segment)
|
||||||
request += "\r\n";
|
request += "\r\n";
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_, request.c_str()));
|
cuid_, request.c_str()));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
}
|
}
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
return socketBuffer_.sendBufferIsEmpty();
|
return socketBuffer_.sendBufferIsEmpty();
|
||||||
|
@ -300,7 +290,7 @@ bool FtpConnection::sendRetr()
|
||||||
request += "\r\n";
|
request += "\r\n";
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_, request.c_str()));
|
cuid_, request.c_str()));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
}
|
}
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
return socketBuffer_.sendBufferIsEmpty();
|
return socketBuffer_.sendBufferIsEmpty();
|
||||||
|
|
|
@ -109,7 +109,7 @@ void HttpConnection::sendRequest(const SharedHandle<HttpRequest>& httpRequest)
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_,
|
cuid_,
|
||||||
eraseConfidentialInfo(request).c_str()));
|
eraseConfidentialInfo(request).c_str()));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
SharedHandle<HttpRequestEntry> entry(new HttpRequestEntry(httpRequest));
|
SharedHandle<HttpRequestEntry> entry(new HttpRequestEntry(httpRequest));
|
||||||
outstandingHttpRequests_.push_back(entry);
|
outstandingHttpRequests_.push_back(entry);
|
||||||
|
@ -122,7 +122,7 @@ void HttpConnection::sendProxyRequest
|
||||||
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
|
||||||
cuid_,
|
cuid_,
|
||||||
eraseConfidentialInfo(request).c_str()));
|
eraseConfidentialInfo(request).c_str()));
|
||||||
socketBuffer_.pushStr(request);
|
socketBuffer_.pushStrSwap(request);
|
||||||
socketBuffer_.send();
|
socketBuffer_.send();
|
||||||
SharedHandle<HttpRequestEntry> entry(new HttpRequestEntry(httpRequest));
|
SharedHandle<HttpRequestEntry> entry(new HttpRequestEntry(httpRequest));
|
||||||
outstandingHttpRequests_.push_back(entry);
|
outstandingHttpRequests_.push_back(entry);
|
||||||
|
|
|
@ -156,14 +156,14 @@ const std::string& HttpServer::getRequestPath() const
|
||||||
return lastRequestHeader_->getRequestPath();
|
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);
|
feedResponse("200 OK", "", text, contentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpServer::feedResponse(const std::string& status,
|
void HttpServer::feedResponse(const std::string& status,
|
||||||
const std::string& headers,
|
const std::string& headers,
|
||||||
const std::string& text,
|
std::string& text,
|
||||||
const std::string& contentType)
|
const std::string& contentType)
|
||||||
{
|
{
|
||||||
std::string httpDate = Time().toHTTPDate();
|
std::string httpDate = Time().toHTTPDate();
|
||||||
|
@ -194,8 +194,8 @@ void HttpServer::feedResponse(const std::string& status,
|
||||||
|
|
||||||
header += "\r\n";
|
header += "\r\n";
|
||||||
A2_LOG_DEBUG(fmt("HTTP Server sends response:\n%s", header.c_str()));
|
A2_LOG_DEBUG(fmt("HTTP Server sends response:\n%s", header.c_str()));
|
||||||
socketBuffer_.pushStr(header);
|
socketBuffer_.pushStrSwap(header);
|
||||||
socketBuffer_.pushStr(text);
|
socketBuffer_.pushStrSwap(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t HttpServer::sendResponse()
|
ssize_t HttpServer::sendResponse()
|
||||||
|
|
|
@ -84,11 +84,11 @@ public:
|
||||||
|
|
||||||
const std::string& getRequestPath() const;
|
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,
|
void feedResponse(const std::string& status,
|
||||||
const std::string& headers,
|
const std::string& headers,
|
||||||
const std::string& text,
|
std::string& text,
|
||||||
const std::string& contentType);
|
const std::string& contentType);
|
||||||
|
|
||||||
bool authenticate();
|
bool authenticate();
|
||||||
|
|
|
@ -123,9 +123,10 @@ bool HttpServerCommand::execute()
|
||||||
}
|
}
|
||||||
if(!httpServer_->authenticate()) {
|
if(!httpServer_->authenticate()) {
|
||||||
httpServer_->disableKeepAlive();
|
httpServer_->disableKeepAlive();
|
||||||
|
std::string text;
|
||||||
httpServer_->feedResponse("401 Unauthorized",
|
httpServer_->feedResponse("401 Unauthorized",
|
||||||
"WWW-Authenticate: Basic realm=\"aria2\"",
|
"WWW-Authenticate: Basic realm=\"aria2\"",
|
||||||
"","text/html");
|
text,"text/html");
|
||||||
Command* command =
|
Command* command =
|
||||||
new HttpServerResponseCommand(getCuid(), httpServer_, e_, socket_);
|
new HttpServerResponseCommand(getCuid(), httpServer_, e_, socket_);
|
||||||
e_->addCommand(command);
|
e_->addCommand(command);
|
||||||
|
|
|
@ -69,6 +69,8 @@ SocketBuffer::StringBufEntry::StringBufEntry(const std::string& s)
|
||||||
: str_(s)
|
: str_(s)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
SocketBuffer::StringBufEntry::StringBufEntry() {}
|
||||||
|
|
||||||
ssize_t SocketBuffer::StringBufEntry::send
|
ssize_t SocketBuffer::StringBufEntry::send
|
||||||
(const SharedHandle<SocketCore>& socket, size_t offset)
|
(const SharedHandle<SocketCore>& socket, size_t offset)
|
||||||
{
|
{
|
||||||
|
@ -80,6 +82,11 @@ bool SocketBuffer::StringBufEntry::final(size_t offset) const
|
||||||
return str_.size() <= offset;
|
return str_.size() <= offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SocketBuffer::StringBufEntry::swap(std::string& s)
|
||||||
|
{
|
||||||
|
str_.swap(s);
|
||||||
|
}
|
||||||
|
|
||||||
SocketBuffer::SocketBuffer(const SharedHandle<SocketCore>& socket):
|
SocketBuffer::SocketBuffer(const SharedHandle<SocketCore>& socket):
|
||||||
socket_(socket), offset_(0) {}
|
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<StringBufEntry> e(new StringBufEntry());
|
||||||
|
e->swap(data);
|
||||||
|
bufq_.push_back(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ssize_t SocketBuffer::send()
|
ssize_t SocketBuffer::send()
|
||||||
{
|
{
|
||||||
size_t totalslen = 0;
|
size_t totalslen = 0;
|
||||||
|
|
|
@ -71,9 +71,11 @@ private:
|
||||||
class StringBufEntry:public BufEntry {
|
class StringBufEntry:public BufEntry {
|
||||||
public:
|
public:
|
||||||
StringBufEntry(const std::string& s);
|
StringBufEntry(const std::string& s);
|
||||||
|
StringBufEntry();
|
||||||
virtual ssize_t send
|
virtual ssize_t send
|
||||||
(const SharedHandle<SocketCore>& socket, size_t offset);
|
(const SharedHandle<SocketCore>& socket, size_t offset);
|
||||||
virtual bool final(size_t offset) const;
|
virtual bool final(size_t offset) const;
|
||||||
|
void swap(std::string& s);
|
||||||
private:
|
private:
|
||||||
std::string str_;
|
std::string str_;
|
||||||
};
|
};
|
||||||
|
@ -103,6 +105,11 @@ public:
|
||||||
// Feeds data into queue. This function doesn't send data.
|
// Feeds data into queue. This function doesn't send data.
|
||||||
void pushStr(const std::string& 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.
|
// Sends data in queue. Returns the number of bytes sent.
|
||||||
ssize_t send();
|
ssize_t send();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue