X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frequest.cpp;h=ba47c4dcb4295a07b6d8b05cfc8f7d2a56e3a753;hb=df006bfbbe8e8b49a52296f42894d4a452ed4c90;hp=bcf36c991f727f5c539a6431304d68691dd5be92;hpb=b06cf07513fdd85e249398ebfce500fbf3dfb6a7;p=libs%2Fnet.git diff --git a/source/request.cpp b/source/request.cpp index bcf36c9..ba47c4d 100644 --- a/source/request.cpp +++ b/source/request.cpp @@ -9,6 +9,7 @@ Distributed under the LGPL #include #include #include "request.h" +#include "utils.h" using namespace std; @@ -22,46 +23,48 @@ Request::Request(const string &m, const string &p): string Request::str() const { - string result=format("%s %s %s\r\n", method, path, version_str(http_version)); - result+=str_common(); + string result = format("%s %s %s\r\n", method, path, version_str(http_version)); + result += str_common(); return result; } Request Request::parse(const string &str) { - unsigned lf=str.find('\n'); - vector parts=split(str.substr(0, lf-(str[lf-1]=='\r')), ' ', 2); + unsigned lf = str.find('\n'); + vector parts = split(str.substr(0, lf-(str[lf-1]=='\r')), ' ', 2); if(parts.size()<3) throw InvalidParameterValue("Invalid request"); Request result(parts[0], parts[1]); - result.http_version=parse_version(parts[2]); + result.http_version = parse_version(parts[2]); - lf+=result.parse_headers(str.substr(lf+1)); + lf += result.parse_headers(str.substr(lf+1)); result.parse_content(str.substr(lf+1)); return result; } -Request Request::from_url(const string &url) +Request Request::from_url(const string &str) { - if(RegMatch match=Regex("^http://([a-zA-Z0-9.-]+(:[0-9]+)?)(/[^ #]*)?$").match(url)) + Url url = parse_url(str); + if(url.scheme!="http") + throw InvalidParameterValue("Only http scheme is supported"); + string path = url.path; + if(path.empty()) + path = "/"; + if(!url.query.empty()) { - string host=match[1].str; - string path=match[3].str; - if(path.empty()) - path="/"; + path += '?'; + path += url.query; + } - Request result("GET", path); - result.set_header("Host", host); - result.set_header("Connection", "close"); + Request result("GET", path); + result.set_header("Host", url.host); + result.set_header("Connection", "close"); - return result; - } - else - throw InvalidParameterValue("Invalid URL"); + return result; } } // namespace Http