]> git.tdb.fi Git - libs/net.git/blob - source/request.cpp
Style update: spaces around assignments
[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 #include "utils.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace Http {
18
19 Request::Request(const string &m, const string &p):
20         method(m),
21         path(p)
22 { }
23
24 string Request::str() const
25 {
26         string result = format("%s %s %s\r\n", method, path, version_str(http_version));
27         result += str_common();
28
29         return result;
30 }
31
32 Request Request::parse(const string &str)
33 {
34         unsigned lf = str.find('\n');
35         vector<string> parts = split(str.substr(0, lf-(str[lf-1]=='\r')), ' ', 2);
36         if(parts.size()<3)
37                 throw InvalidParameterValue("Invalid request");
38
39         Request result(parts[0], parts[1]);
40         result.http_version = parse_version(parts[2]);
41
42         lf += result.parse_headers(str.substr(lf+1));
43
44         result.parse_content(str.substr(lf+1));
45
46         return result;
47 }
48
49 Request Request::from_url(const string &str)
50 {
51         Url url = parse_url(str);
52         if(url.scheme!="http")
53                 throw InvalidParameterValue("Only http scheme is supported");
54         string path = url.path;
55         if(path.empty())
56                 path = "/";
57         if(!url.query.empty())
58         {
59                 path += '?';
60                 path += url.query;
61         }
62
63         Request result("GET", path);
64         result.set_header("Host", url.host);
65         result.set_header("Connection", "close");
66
67         return result;
68 }
69
70 } // namespace Http
71 } // namespace Msp