2 #include <msp/strings/format.h>
3 #include <msp/strings/regex.h>
4 #include <msp/strings/utils.h>
12 Request::Request(const string &m, const string &p):
16 if(path.find(' ')!=string::npos)
17 throw invalid_argument("Request::Request");
20 string Request::str() const
22 string result = format("%s %s %s\r\n", method, path, version_str(http_version));
23 result += str_common();
28 Request Request::parse(const string &str)
30 string::size_type lf = str.find('\n');
32 throw invalid_argument("Request::parse");
33 vector<string> parts = split(str.substr(0, lf-(str[lf-1]=='\r')), ' ', 2);
35 throw invalid_argument("Request::parse");
37 Request result(parts[0], parts[1]);
38 result.http_version = parse_version(parts[2]);
40 lf += result.parse_headers(str.substr(lf+1));
42 result.parse_content(str.substr(lf+1));
47 Request Request::from_url(const string &str)
49 Url url = parse_url(str);
50 if(url.scheme!="http")
51 throw invalid_argument("Request::from_url");
53 string path = urlencode(url.path);
56 append(path, "?", url.query);
58 Request result("GET", path);
59 result.set_header("Host", url.host);
60 result.set_header("Connection", "close");