]> git.tdb.fi Git - libs/net.git/blob - source/response.cpp
Rename data to content in Message
[libs/net.git] / source / response.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/utils.h>
10 #include "response.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace Http {
16
17 Response::Response(Status s):
18         status(s)
19 { }
20
21 string Response::str() const
22 {
23         string result=format("%s %d %s\r\n", version_str(http_version), int(status), status);
24         result+=str_common();
25
26         return result;
27 }
28
29 Response Response::parse(const string &str)
30 {
31         Response result;
32
33         unsigned lf=str.find('\n');
34         vector<string> parts=split(str.substr(0, lf), ' ');
35         if(parts.size()<2)
36                 throw InvalidParameterValue("Invalid response");
37
38         result.http_version=parse_version(parts[0]);
39         result.status=static_cast<Status>(lexical_cast<unsigned>(parts[1]));
40
41         unsigned start=lf+1;
42         while(1)
43         {
44                 lf=str.find('\n', start);
45                 if(lf==string::npos)
46                         throw InvalidParameterValue("Incomplete response");
47                 if(lf==start || (str[start]=='\r' && lf==start+1))
48                         break;
49
50                 unsigned colon=str.find(':', start);
51                 if(colon>lf)
52                         throw InvalidParameterValue("No colon in header");
53
54                 result.set_header(str.substr(start, colon-start), strip(str.substr(colon+1, lf-colon-1)));
55
56                 start=lf+1;
57         }
58
59         result.parse_content(str.substr(lf+1));
60
61         return result;
62 }
63
64 } // namespace Http
65 } // namespace Msp