3 #include <msp/core/maputils.h>
4 #include <msp/strings/format.h>
5 #include <msp/strings/utils.h>
12 void Message::set_header(const string &hdr, const string &val)
14 headers[normalize_header_name(hdr)] = val;
17 bool Message::has_header(const string &hdr) const
19 return headers.count(normalize_header_name(hdr));
22 const string &Message::get_header(const string &hdr) const
24 return get_item(headers, normalize_header_name(hdr));
27 void Message::add_content(const string &d)
30 if(headers.count("Content-Type")==0)
31 set_header("Content-Type", "text/plain");
32 set_header("Content-Length", lexical_cast<string>(content.size()));
35 void Message::set_user_data(const Variant &d)
40 unsigned Message::parse_content(const string &d)
45 auto i = headers.find("Content-Length");
48 string::size_type needed = lexical_cast<string::size_type>(i->second)-content.size();
49 string::size_type len = min(needed, d.size());
51 content.append(d, 0, len);
59 i = headers.find("Transfer-Encoding");
60 if(i!=headers.end() && strcasecmp(i->second, "chunked")==0)
62 string::size_type pos = 0;
63 while(!complete && pos<d.size())
67 string::size_type lf = d.find('\n', pos);
70 chunk_length = lexical_cast<unsigned>(strip(d.substr(pos, lf-pos)), "x");
77 string::size_type len = min(chunk_length, d.size()-pos);
78 content.append(d, pos, len);
80 if((pos = d.find('\n', pos+len))!=string::npos)
92 unsigned Message::parse_headers(const string &d)
94 string::size_type start = 0;
97 string::size_type lf = d.find('\n', start);
99 throw invalid_argument("Message::parse_headers");
100 if(lf==start || (d[start]=='\r' && lf==start+1))
103 string::size_type colon = d.find(':', start);
105 throw invalid_argument("Message::parse_headers");
107 set_header(d.substr(start, colon-start), strip(d.substr(colon+1, lf-colon-1)));
113 string Message::str_common() const
117 for(auto &kvp: headers)
118 if(kvp.first[0]!='-')
119 result += format("%s: %s\r\n", kvp.first, kvp.second);
126 string Message::normalize_header_name(const string &hdr) const
136 c = toupper(static_cast<unsigned char>(c));
140 c = tolower(static_cast<unsigned char>(c));