void Message::setHeader(const MessageHeader& header) { MessageHeaderSeq::iterator it(_headers.begin()); while(it != _headers.end()) { MessageHeader* pHeader = *it; if(header.key() == pHeader->key()) { pHeader->setValue(header.value()); return; } ++it; } _headers.push_back(new MessageHeader(header)); }
size_t Message::headerLen() const { size_t nLen = 0; MessageHeaderSeq::const_iterator it(_headers.begin()); while(it != _headers.end()) { MessageHeader* pHeader = *it; if(pHeader != NULL) { nLen += (pHeader->key().size() + 2 + pHeader->value().size() + 2); } ++it; } return nLen; }
void Message::removeHeader(const std::string&key) { MessageHeaderSeq::iterator it(_headers.begin()); while(it != _headers.end()) { MessageHeader* pHeader = *it; if(key == pHeader->key()) { it = _headers.erase(it); } else { ++it; } } }
void Message::setHeader(const std::string& key, const std::string& value) { MessageHeaderSeq::iterator it(_headers.begin()); while(it != _headers.end()) { MessageHeader* pHeader = *it; if(key == pHeader->key()) { pHeader->setValue(value); return; } ++it; } _headers.push_back(new MessageHeader(key, value)); }
std::string Message::header(const std::string& key) const { std::string value; MessageHeaderSeq::const_iterator it(_headers.begin()); while(it != _headers.end()) { MessageHeader* pHeader = *it; if(key == pHeader->key()) { value = pHeader->value(); break; } ++it; } return value; }
std::string ResponseMessage::dump() const { // <protocol>/<version> code message CRLF // <headers> CRLF // <buf> std::ostringstream oss; oss << _protocol << "/" << _version << " " << _code << " " << _reason << "\r\n"; for(size_t i = 0; i < headerCount(); i++) { MessageHeader* pHeader = header(i); oss << pHeader->key() << ":" << pHeader->value() << "\r\n"; } oss << "\r\n"; oss << "Body (" << _bodyLen << "bytes)" << "\r\n"; return oss.str(); }
std::string RequestMessage::dump() const { // <verb> SP <url> SP <protocol/version> CRLF // <headers> CRLF // <buf> std::ostringstream oss; oss << _method << " " << _url << " " << _protocol << "/" << _version << "\r\n"; for(size_t i = 0; i < headerCount(); i++) { MessageHeader* pHeader = header(i); oss << pHeader->key() << ":" << pHeader->value() << "\r\n"; } oss << "\r\n"; oss << "Body (" << _bodyLen << "bytes)\r\n"; return oss.str(); }