Added SocketBuffer::pushStrSwap

pull/2/head
Tatsuhiro Tsujikawa 2011-11-05 17:28:48 +09:00
parent 4dad3ded15
commit 6ea1b68db1
7 changed files with 49 additions and 35 deletions

View File

@ -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<SocketCore>& 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<SocketCore>& 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>& 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();

View File

@ -109,7 +109,7 @@ void HttpConnection::sendRequest(const SharedHandle<HttpRequest>& httpRequest)
A2_LOG_INFO(fmt(MSG_SENDING_REQUEST,
cuid_,
eraseConfidentialInfo(request).c_str()));
socketBuffer_.pushStr(request);
socketBuffer_.pushStrSwap(request);
socketBuffer_.send();
SharedHandle<HttpRequestEntry> 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<HttpRequestEntry> entry(new HttpRequestEntry(httpRequest));
outstandingHttpRequests_.push_back(entry);

View File

@ -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()

View File

@ -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();

View File

@ -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);

View File

@ -69,6 +69,8 @@ SocketBuffer::StringBufEntry::StringBufEntry(const std::string& s)
: str_(s)
{}
SocketBuffer::StringBufEntry::StringBufEntry() {}
ssize_t SocketBuffer::StringBufEntry::send
(const SharedHandle<SocketCore>& 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<SocketCore>& 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<StringBufEntry> e(new StringBufEntry());
e->swap(data);
bufq_.push_back(e);
}
}
ssize_t SocketBuffer::send()
{
size_t totalslen = 0;

View File

@ -71,9 +71,11 @@ private:
class StringBufEntry:public BufEntry {
public:
StringBufEntry(const std::string& s);
StringBufEntry();
virtual ssize_t send
(const SharedHandle<SocketCore>& 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();