]> git.tdb.fi Git - libs/net.git/blob - source/http/response.cpp
85e279f579a35cd08a6e4ad446ef1836771a282e
[libs/net.git] / source / http / response.cpp
1 #include <msp/strings/format.h>
2 #include <msp/strings/utils.h>
3 #include "response.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace Http {
9
10 Response::Response(Status s):
11         status(s)
12 { }
13
14 string Response::str() const
15 {
16         string result = format("%s %d %s\r\n", version_str(http_version), static_cast<int>(status), status);
17         result += str_common();
18
19         return result;
20 }
21
22 Response Response::parse(const string &str)
23 {
24         Response result;
25
26         unsigned lf = str.find('\n');
27         vector<string> parts = split(str.substr(0, lf), ' ', 2);
28         if(parts.size()<2)
29                 throw invalid_argument("Response::parse");
30
31         result.http_version = parse_version(parts[0]);
32         result.status = static_cast<Status>(lexical_cast<unsigned>(parts[1]));
33
34         lf += result.parse_headers(str.substr(lf+1));
35
36         result.parse_content(str.substr(lf+1));
37
38         return result;
39 }
40
41 } // namespace Http
42 } // namespace Msp