1 // include/HttpRequest.hh
2 // This file is part of libpbe; see http://anyterm.org/
3 // (C) 2006-2008 Philip Endecott
4 
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 
20 #ifndef libpbe_HttpRequest_hh
21 #define libpbe_HttpRequest_hh
22 
23 #include "URI.hh"
24 
25 #include <string>
26 #include <map>
27 
28 
29 namespace pbe {
30 
31 
32 struct HttpRequest {
33   std::string method;
34   std::string host;
35   std::string abs_path;
36   std::string query;
37   std::string http_version;
38   std::string userinfo;
39   typedef std::map<std::string,std::string> headers_t;
40   headers_t headers;
41   std::string body;
42 
HttpRequestpbe::HttpRequest43   HttpRequest() {}
44 
HttpRequestpbe::HttpRequest45   HttpRequest(const URI& uri, std::string method_="GET", std::string http_version_="HTTP/1.1"):
46     method(method_),
47     host(uri.host),
48     abs_path(uri.abs_path),
49     query(uri.query),
50     http_version(http_version_),
51     userinfo(uri.userinfo)
52   {
53     headers["Host"]=host;
54   }
55 
request_linepbe::HttpRequest56   std::string request_line() const {
57     return method
58           +" "
59           +abs_path + (query.empty() ? std::string() : "?"+query)
60           +" "
61           +http_version+"\r\n";
62   }
63 
headers_strpbe::HttpRequest64   std::string headers_str() const {
65     std::string s;
66     for (headers_t::const_iterator i = headers.begin();
67          i != headers.end(); ++i) {
68       s += i->first + ": " + i->second + "\r\n";
69     }
70     return s;
71   }
72 
73 };
74 
75 
76 };
77 
78 
79 #endif
80