3 This file is part of libmsphttp
4 Copyright © 2008 Mikkosoft Productions, Mikko Rasa
5 Distributed under the LGPL
8 #include <msp/strings/formatter.h>
9 #include <msp/strings/regex.h>
10 #include <msp/strings/utils.h>
19 Request::Request(const string &m, const string &p):
24 string Request::str() const
26 string result = format("%s %s %s\r\n", method, path, version_str(http_version));
27 result += str_common();
32 Request Request::parse(const string &str)
34 unsigned lf = str.find('\n');
35 vector<string> parts = split(str.substr(0, lf-(str[lf-1]=='\r')), ' ', 2);
37 throw InvalidParameterValue("Invalid request");
39 Request result(parts[0], parts[1]);
40 result.http_version = parse_version(parts[2]);
42 lf += result.parse_headers(str.substr(lf+1));
44 result.parse_content(str.substr(lf+1));
49 Request Request::from_url(const string &str)
51 Url url = parse_url(str);
52 if(url.scheme!="http")
53 throw InvalidParameterValue("Only http scheme is supported");
54 string path = url.path;
57 if(!url.query.empty())
63 Request result("GET", path);
64 result.set_header("Host", url.host);
65 result.set_header("Connection", "close");