]> git.tdb.fi Git - libs/net.git/blob - source/message.cpp
Initial revision
[libs/net.git] / source / message.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 <cstdlib>
9 #include <msp/strings/formatter.h>
10 #include <msp/strings/utils.h>
11 #include "message.h"
12
13 using namespace std;
14
15 #include <iostream>
16
17 namespace Msp {
18 namespace Http {
19
20 void Message::set_header(const string &hdr, const string &val)
21 {
22         headers[tolower(hdr)]=val;
23 }
24
25 const string &Message::get_header(const string &hdr) const
26 {
27         HeaderMap::const_iterator i=headers.find(hdr);
28         if(i==headers.end())
29                 throw KeyError(format("Header %s is not defined", hdr));
30
31         return i->second;
32 }
33
34 void Message::add_data(const string &d)
35 {
36         data+=d;
37         if(headers.count("content-type")==0)
38                 set_header("content-type", "text/plain");
39         set_header("content-length", lexical_cast(data.size()));
40 }
41
42 unsigned Message::parse_data(const string &d)
43 {
44         if(complete)
45                 return 0;
46
47         HeaderMap::const_iterator i=headers.find("content-length");
48         if(i!=headers.end())
49         {
50                 unsigned needed=lexical_cast<unsigned>(i->second)-data.size();
51                 unsigned len=min(needed, d.size());
52                 
53                 data.append(d, 0, len);
54
55                 if(len==needed)
56                         complete=true;
57                 
58                 return len;
59         }
60
61         i=headers.find("transfer-encoding");
62         if(i!=headers.end() && strcasecmp(i->second, "chunked")==0)
63         {
64                 unsigned pos=0;
65                 while(!complete && pos<d.size())
66                 {
67                         if(chunk_length==0)
68                         {
69                                 unsigned lf=d.find('\n', pos);
70                                 if(lf==string::npos)
71                                         return pos;
72                                 // XXX strtoul
73                                 chunk_length=strtoul(strip(d.substr(pos, lf-pos)).c_str(), 0, 16);
74                                 if(chunk_length==0)
75                                         complete=true;
76                                 pos=lf+1;
77                         }
78                         else
79                         {
80                                 unsigned len=min(chunk_length, d.size()-pos);
81                                 data.append(d, pos, len);
82                                 chunk_length-=len;
83                                 if((pos=d.find('\n', pos+len))!=string::npos)
84                                         ++pos;
85                         }
86                 }
87
88                 return pos;
89         }
90
91         return 0;
92 }
93
94 Message::Message():
95         http_version(0x11),
96         chunk_length(0),
97         complete(false)
98 { }
99
100 string Message::str_common() const
101 {
102         string result;
103
104         for(HeaderMap::const_iterator i=headers.begin(); i!=headers.end(); ++i)
105                 result+=format("%s: %s\r\n", i->first, i->second);
106         result+="\r\n";
107         result+=data;
108
109         return result;
110 }
111
112 } // namespace Http
113 } // namespace Msp