]> git.tdb.fi Git - libs/net.git/blob - source/request.cpp
Initial revision
[libs/net.git] / source / request.cpp
1 /* $Id$
2
3 This file is part of libmsphttp
4 Copyright © 2008  Mikkosoft Productions, Mikko Rasa
5 Distributed under the LGPL
6 */
7
8 #include <msp/strings/formatter.h>
9 #include <msp/strings/regex.h>
10 #include <msp/strings/utils.h>
11 #include "request.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace Http {
17
18 Request::Request(const string &m, const string &p):
19         method(m),
20         path(p)
21 { }
22
23 string Request::str() const
24 {
25         string result=format("%s %s %s\r\n", method, path, version_str(http_version));
26         result+=str_common();
27
28         return result;
29 }
30
31 Request Request::from_url(const string &url)
32 {
33         if(RegMatch match=Regex("^http://([a-zA-Z0-9.-]+)(:([0-9]+))?(/[^ #]*)?$").match(url))
34         {
35                 string host=match[1].str;
36                 string port="80";
37                 if(match[3])
38                         port=match[3].str;
39                 string path=match[4].str;
40                 if(path.empty())
41                         path="/";
42
43                 Request result("GET", path);
44                 result.set_header("host", host);
45                 result.set_header("x-port", port);
46
47                 return result;
48         }
49         else
50                 throw InvalidParameterValue("Invalid URL");
51 }
52
53 } // namespace Http
54 } // namespace Msp