X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frequest.cpp;h=c948755ae78281cb1c36378de491a1cd0c948cd7;hb=c8df43e7794dc82d5604dfa612e464bbc5ca3cdd;hp=e63dc66f7487e08803380a6f82755528733c52ab;hpb=070d56e7b0036ca2e4234eb06dcae83ebfb3df34;p=libs%2Fnet.git diff --git a/source/request.cpp b/source/request.cpp index e63dc66..c948755 100644 --- a/source/request.cpp +++ b/source/request.cpp @@ -1,14 +1,8 @@ -/* $Id$ - -This file is part of libmsphttp -Copyright © 2008 Mikkosoft Productions, Mikko Rasa -Distributed under the LGPL -*/ - #include #include #include #include "request.h" +#include "utils.h" using namespace std; @@ -22,32 +16,49 @@ 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); + if(parts.size()<3) + throw invalid_argument("Request::parse"); + + Request result(parts[0], parts[1]); + result.http_version = parse_version(parts[2]); + + 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 invalid_argument("Request::from_url"); + + string path = url.path; + if(path.empty()) + path = "/"; + if(!url.query.empty()) { - string host=match[1].str; - string port="80"; - if(match[3]) - port=match[3].str; - string path=match[4].str; - if(path.empty()) - path="/"; - - Request result("GET", path); - result.set_header("host", host); - result.set_header("x-port", port); - - return result; + path += '?'; + path += url.query; } - else - throw InvalidParameterValue("Invalid URL"); + + Request result("GET", path); + result.set_header("Host", url.host); + result.set_header("Connection", "close"); + + return result; } } // namespace Http