]> git.tdb.fi Git - libs/net.git/blob - source/http/response.cpp
Add a dynamic receiver class for more flexible packet handling
[libs/net.git] / source / http / response.cpp
1 #include "response.h"
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.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         string::size_type lf = str.find('\n');
27         if(lf==0)
28                 throw invalid_argument("Response::parse");
29         vector<string> parts = split(str.substr(0, lf-(str[lf-1]=='\r')), ' ', 2);
30         if(parts.size()<2)
31                 throw invalid_argument("Response::parse");
32
33         result.http_version = parse_version(parts[0]);
34         result.status = static_cast<Status>(lexical_cast<unsigned>(parts[1]));
35
36         lf += result.parse_headers(str.substr(lf+1));
37
38         result.parse_content(str.substr(lf+1));
39
40         return result;
41 }
42
43 } // namespace Http
44 } // namespace Msp