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>
18 Request::Request(const string &m, const string &p):
23 string Request::str() const
25 string result=format("%s %s %s\r\n", method, path, version_str(http_version));
31 Request Request::parse(const string &str)
33 unsigned lf=str.find('\n');
34 vector<string> parts=split(str.substr(0, lf-(str[lf-1]=='\r')), ' ', 2);
36 throw InvalidParameterValue("Invalid request");
38 Request result(parts[0], parts[1]);
39 result.http_version=parse_version(parts[2]);
41 lf+=result.parse_headers(str.substr(lf+1));
43 result.parse_content(str.substr(lf+1));
48 Request Request::from_url(const string &url)
50 if(RegMatch match=Regex("^http://([a-zA-Z0-9.-]+(:[0-9]+)?)(/[^ #]*)?$").match(url))
52 string host=match[1].str;
53 string path=match[3].str;
57 Request result("GET", path);
58 result.set_header("Host", host);
59 result.set_header("Connection", "close");
64 throw InvalidParameterValue("Invalid URL");