3 This file is part of libmsphttp
4 Copyright © 2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the LGPL
9 #include <msp/strings/formatter.h>
10 #include <msp/strings/utils.h>
20 void Message::set_header(const string &hdr, const string &val)
22 headers[tolower(hdr)]=val;
25 const string &Message::get_header(const string &hdr) const
27 HeaderMap::const_iterator i=headers.find(hdr);
29 throw KeyError(format("Header %s is not defined", hdr));
34 void Message::add_content(const string &d)
37 if(headers.count("content-type")==0)
38 set_header("content-type", "text/plain");
39 set_header("content-length", lexical_cast(content.size()));
42 void Message::set_user_data(const Variant &d)
47 unsigned Message::parse_content(const string &d)
52 HeaderMap::const_iterator i=headers.find("content-length");
55 unsigned needed=lexical_cast<unsigned>(i->second)-content.size();
56 unsigned len=min(needed, d.size());
58 content.append(d, 0, len);
66 i=headers.find("transfer-encoding");
67 if(i!=headers.end() && strcasecmp(i->second, "chunked")==0)
70 while(!complete && pos<d.size())
74 unsigned lf=d.find('\n', pos);
77 chunk_length=lexical_cast<unsigned>(strip(d.substr(pos, lf-pos)), "x");
84 unsigned len=min(chunk_length, d.size()-pos);
85 content.append(d, pos, len);
87 if((pos=d.find('\n', pos+len))!=string::npos)
104 string Message::str_common() const
108 for(HeaderMap::const_iterator i=headers.begin(); i!=headers.end(); ++i)
109 result+=format("%s: %s\r\n", i->first, i->second);