request.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 by Marc Boris Duerner, Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef cxxtools_Http_Request_h
30 #define cxxtools_Http_Request_h
31 
32 #include <cxxtools/http/api.h>
34 #include <string>
35 #include <sstream>
36 
37 namespace cxxtools {
38 
39 namespace http {
40 
41 class Request
42 {
43  RequestHeader _header;
44  std::ostringstream _body;
45 
46  public:
47  struct Auth
48  {
49  std::string user;
50  std::string password;
51  };
52 
53  explicit Request(const std::string& url = std::string())
54  : _header(url)
55  { }
56 
58  { return _header; }
59 
60  const RequestHeader& header() const
61  { return _header; }
62 
63  void setHeader(const char* key, const char* value)
64  {
65  _header.setHeader(key, value);
66  }
67 
68  void addHeader(const char* key, const char* value)
69  {
70  _header.addHeader(key, value);
71  }
72 
73  void removeHeader(const char* key)
74  {
75  _header.removeHeader(key);
76  }
77 
78  const char* getHeader(const char* key) const
79  {
80  return _header.getHeader(key);
81  }
82 
83  bool hasHeader(const char* key) const
84  {
85  return _header.hasHeader(key);
86  }
87 
88  void clear()
89  {
90  _header.clear();
91  _body.clear();
92  _body.str(std::string());
93  }
94 
95  const std::string& url() const
96  { return _header.url(); }
97 
98  void url(const std::string& u)
99  { _header.url(u); }
100 
101  const std::string& method() const
102  { return _header.method(); }
103 
104  void method(const std::string& m)
105  { _header.method(m); }
106 
107  const std::string& qparams() const
108  { return _header.qparams(); }
109 
110  void qparams(const std::string& q)
111  { _header.qparams(q); }
112 
113  std::string bodyStr() const
114  { return _body.str(); }
115 
116  std::ostream& body()
117  { return _body; }
118 
119  std::size_t bodySize() const
120  { return _body.str().size(); }
121 
122  void sendBody(std::ostream& out) const
123  { out << _body.str(); }
124 
125  Auth auth() const;
126 
127 };
128 
129 } // namespace http
130 
131 } // namespace cxxtools
132 
133 #endif