aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/vmime/net/imap/IMAPConnection.cpp13
-rw-r--r--src/vmime/net/imap/IMAPParser.hpp94
-rw-r--r--src/vmime/net/imap/IMAPTag.cpp24
-rw-r--r--src/vmime/net/imap/IMAPTag.hpp8
4 files changed, 93 insertions, 46 deletions
diff --git a/src/vmime/net/imap/IMAPConnection.cpp b/src/vmime/net/imap/IMAPConnection.cpp
index 2ea48e34..df3da1c3 100644
--- a/src/vmime/net/imap/IMAPConnection.cpp
+++ b/src/vmime/net/imap/IMAPConnection.cpp
@@ -92,7 +92,6 @@ IMAPConnection::IMAPConnection(
}
m_parser = make_shared <IMAPParser>();
- m_parser->setTag(m_tag);
m_parser->setTracer(m_tracer);
}
@@ -287,7 +286,7 @@ void IMAPConnection::authenticate() {
shared_ptr <IMAPConnection> conn = dynamicCast <IMAPConnection>(shared_from_this());
IMAPCommand::LOGIN(username, password)->send(conn);
- scoped_ptr <IMAPParser::response> resp(m_parser->readResponse());
+ scoped_ptr <IMAPParser::response> resp(m_parser->readResponse(*m_tag));
if (resp->isBad()) {
@@ -409,7 +408,7 @@ void IMAPConnection::authenticateSASL() {
for (bool cont = true ; cont ; ) {
- scoped_ptr <IMAPParser::response> resp(m_parser->readResponse());
+ scoped_ptr <IMAPParser::response> resp(m_parser->readResponse(*m_tag));
if (resp->response_done &&
resp->response_done->response_tagged &&
@@ -522,7 +521,7 @@ void IMAPConnection::startTLS() {
IMAPCommand::STARTTLS()->send(dynamicCast <IMAPConnection>(shared_from_this()));
- scoped_ptr <IMAPParser::response> resp(m_parser->readResponse());
+ scoped_ptr <IMAPParser::response> resp(m_parser->readResponse(*m_tag));
if (resp->isBad() || resp->response_done->response_tagged->
resp_cond_state->status != IMAPParser::resp_cond_state::OK) {
@@ -624,7 +623,7 @@ void IMAPConnection::fetchCapabilities() {
IMAPCommand::CAPABILITY()->send(dynamicCast <IMAPConnection>(shared_from_this()));
- scoped_ptr <IMAPParser::response> resp(m_parser->readResponse());
+ scoped_ptr <IMAPParser::response> resp(m_parser->readResponse(*m_tag));
if (resp->response_done->response_tagged->
resp_cond_state->status == IMAPParser::resp_cond_state::OK) {
@@ -733,7 +732,7 @@ void IMAPConnection::initHierarchySeparator() {
IMAPCommand::LIST("", "")->send(dynamicCast <IMAPConnection>(shared_from_this()));
- scoped_ptr <IMAPParser::response> resp(m_parser->readResponse());
+ scoped_ptr <IMAPParser::response> resp(m_parser->readResponse(*m_tag));
if (resp->isBad() || resp->response_done->response_tagged->
resp_cond_state->status != IMAPParser::resp_cond_state::OK) {
@@ -801,7 +800,7 @@ void IMAPConnection::sendRaw(const byte_t* buffer, const size_t count) {
IMAPParser::response* IMAPConnection::readResponse(IMAPParser::literalHandler* lh) {
- return m_parser->readResponse(lh);
+ return m_parser->readResponse(*m_tag, lh);
}
diff --git a/src/vmime/net/imap/IMAPParser.hpp b/src/vmime/net/imap/IMAPParser.hpp
index 76019d2e..2aeefb53 100644
--- a/src/vmime/net/imap/IMAPParser.hpp
+++ b/src/vmime/net/imap/IMAPParser.hpp
@@ -271,24 +271,13 @@ public:
}
+ ~IMAPParser() {
- /** Set the tag currently used by this parser.
- *
- * @param tag IMAP command tag
- */
- void setTag(const shared_ptr <IMAPTag>& tag) {
-
- m_tag = tag;
+ for (auto it = m_pendingResponses.begin() ; it != m_pendingResponses.end() ; ++it) {
+ delete it->second;
+ }
}
- /** Return the tag currently used by this parser.
- *
- * @return IMAP command tag
- */
- shared_ptr <const IMAPTag> getTag() const {
-
- return m_tag.lock();
- }
/** Set the socket currently used by this parser to receive data
* from server.
@@ -613,13 +602,12 @@ public:
DECLARE_COMPONENT(xtag)
- bool parseImpl(IMAPParser& parser, string& line, size_t* currentPos) {
+ bool parseImpl(IMAPParser& /* parser */, string& line, size_t* currentPos) {
size_t pos = *currentPos;
bool end = false;
- string tagString;
tagString.reserve(10);
while (!end && pos < line.length()) {
@@ -654,14 +642,11 @@ public:
}
}
- if (tagString == string(*parser.getTag())) {
- *currentPos = pos;
- return true;
- } else {
- // Invalid tag
- return false;
- }
+ *currentPos = pos;
+ return true;
}
+
+ string tagString;
};
@@ -1141,7 +1126,7 @@ public:
value = text->value;
}
- DEBUG_FOUND("string[quoted]", "<length=" << m_value.length() << ", value='" << m_value << "'>");
+ DEBUG_FOUND("string[quoted]", "<length=" << value.length() << ", value='" << value << "'>");
// literal ::= "{" number "}" CRLF *CHAR8
} else {
@@ -4389,7 +4374,7 @@ public:
size_t pos = *currentPos;
- VIMAP_PARSER_CHECK(IMAPParser::xtag);
+ VIMAP_PARSER_GET(IMAPParser::xtag, tag);
VIMAP_PARSER_CHECK(SPACE);
VIMAP_PARSER_GET(IMAPParser::resp_cond_state, resp_cond_state);
@@ -4409,6 +4394,7 @@ public:
}
+ std::unique_ptr <IMAPParser::xtag> tag;
std::unique_ptr <IMAPParser::resp_cond_state> resp_cond_state;
};
@@ -4569,22 +4555,53 @@ public:
// The main functions used to parse a response
//
- response* readResponse(literalHandler* lh = NULL) {
+ response* readResponse(const IMAPTag& tag, literalHandler* lh = NULL) {
- size_t pos = 0;
- string line = readLine();
+ while (true) {
- m_literalHandler = lh;
- response* resp = get <response>(line, &pos);
- m_literalHandler = NULL;
+ auto it = m_pendingResponses.find(std::string(tag));
- if (!resp) {
- throw exceptions::invalid_response("", m_errorResponseLine);
- }
+ if (it != m_pendingResponses.end()) {
+ auto* resp = it->second;
+ m_pendingResponses.erase(it);
+ return resp;
+ }
+
+ size_t pos = 0;
+ string line = readLine();
+
+ m_literalHandler = lh;
+ response* resp = get <response>(line, &pos);
+ m_literalHandler = NULL;
+
+ if (!resp) {
+ throw exceptions::invalid_response("", m_errorResponseLine);
+ }
+
+ resp->setErrorLog(lastLine());
- resp->setErrorLog(lastLine());
+ // If there is a continue_req, return the response immediately
+ for (auto &respData : resp->continue_req_or_response_data) {
+ if (respData->continue_req) {
+ return resp;
+ }
+ }
+
+ // Else, return response if the tag is the one we expect
+ if (resp->response_done && resp->response_done->response_tagged &&
+ resp->response_done->response_tagged->tag) {
- return (resp);
+ if (tag == resp->response_done->response_tagged->tag->tagString) {
+ return resp;
+ } else {
+ // Not our response tag, cache it for later
+ m_pendingResponses[resp->response_done->response_tagged->tag->tagString] = resp;
+ }
+ } else {
+ // Untagged response
+ return resp;
+ }
+ }
}
@@ -4740,7 +4757,6 @@ public:
private:
- weak_ptr <IMAPTag> m_tag;
weak_ptr <socket> m_socket;
shared_ptr <tracer> m_tracer;
@@ -4758,6 +4774,8 @@ private:
string m_lastLine;
string m_errorResponseLine;
+ std::map <std::string, response*> m_pendingResponses;
+
public:
/** Read a line from the input buffer. The function blocks until a
diff --git a/src/vmime/net/imap/IMAPTag.cpp b/src/vmime/net/imap/IMAPTag.cpp
index 69f48477..52cd2123 100644
--- a/src/vmime/net/imap/IMAPTag.cpp
+++ b/src/vmime/net/imap/IMAPTag.cpp
@@ -85,6 +85,30 @@ const IMAPTag IMAPTag::operator++(int) {
}
+bool IMAPTag::operator<(const IMAPTag &other) const {
+
+ return m_number < other.m_number;
+}
+
+
+bool IMAPTag::operator==(const IMAPTag &other) const {
+
+ return m_number == other.m_number;
+}
+
+
+bool IMAPTag::operator!=(const IMAPTag &other) const {
+
+ return m_number != other.m_number;
+}
+
+
+bool IMAPTag::operator==(const std::string &tag) const {
+
+ return m_tag == tag;
+}
+
+
int IMAPTag::maximumNumber() const {
return sm_maxNumber - 1;
diff --git a/src/vmime/net/imap/IMAPTag.hpp b/src/vmime/net/imap/IMAPTag.hpp
index d4292fde..cbab9429 100644
--- a/src/vmime/net/imap/IMAPTag.hpp
+++ b/src/vmime/net/imap/IMAPTag.hpp
@@ -44,11 +44,11 @@ class VMIME_EXPORT IMAPTag : public object {
private:
IMAPTag(const int number);
- IMAPTag(const IMAPTag& tag);
public:
IMAPTag();
+ IMAPTag(const IMAPTag& tag);
IMAPTag& operator++(); // ++IMAPTag
const IMAPTag operator++(int); // IMAPTag++
@@ -58,6 +58,12 @@ public:
operator string() const;
+ bool operator<(const IMAPTag &other) const;
+ bool operator==(const IMAPTag &other) const;
+ bool operator!=(const IMAPTag &other) const;
+
+ bool operator==(const std::string &tag) const;
+
private:
void generate();