C++ Rest SDK
The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
http_headers.h
1 /***
2 * ==++==
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * ==--==
17 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
18 *
19 * For the latest on this and related APIs, please see http://casablanca.codeplex.com.
20 *
21 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
22 ****/
23 #pragma once
24 
25 #include <map>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 #include <system_error>
30 #include "cpprest/asyncrt_utils.h"
31 
32 namespace web { namespace http {
33 
42 template<typename key_type, typename _t>
43 CASABLANCA_DEPRECATED("This API is deprecated and will be removed in a future release, std::istringstream instead.")
44 bool bind(const key_type &text, _t &ref) // const
45 {
46  utility::istringstream_t iss(text);
47  iss >> ref;
48  if (iss.fail() || !iss.eof())
49  {
50  return false;
51  }
52 
53  return true;
54 }
55 
64 template <typename key_type>
65 CASABLANCA_DEPRECATED("This API is deprecated and will be removed in a future release.")
66 bool bind(const key_type &text, utility::string_t &ref) //const
67 {
68  ref = text;
69  return true;
70 }
71 
76 {
77 public:
80  {
81  bool operator()(const utility::string_t &str1, const utility::string_t &str2) const
82  {
83 #ifdef _WIN32
84  return _wcsicmp(str1.c_str(), str2.c_str()) < 0;
85 #else
86  return utility::cmp::icmp(str1, str2) < 0;
87 #endif
88  }
89  };
90 
94  typedef std::map<utility::string_t, utility::string_t, _case_insensitive_cmp>::key_type key_type;
95  typedef std::map<utility::string_t, utility::string_t, _case_insensitive_cmp>::key_compare key_compare;
96  typedef std::map<utility::string_t, utility::string_t, _case_insensitive_cmp>::allocator_type allocator_type;
97  typedef std::map<utility::string_t, utility::string_t, _case_insensitive_cmp>::size_type size_type;
98  typedef std::map<utility::string_t, utility::string_t, _case_insensitive_cmp>::difference_type difference_type;
99  typedef std::map<utility::string_t, utility::string_t, _case_insensitive_cmp>::pointer pointer;
100  typedef std::map<utility::string_t, utility::string_t, _case_insensitive_cmp>::const_pointer const_pointer;
101  typedef std::map<utility::string_t, utility::string_t, _case_insensitive_cmp>::reference reference;
102  typedef std::map<utility::string_t, utility::string_t, _case_insensitive_cmp>::const_reference const_reference;
103  typedef std::map<utility::string_t, utility::string_t, _case_insensitive_cmp>::iterator iterator;
104  typedef std::map<utility::string_t, utility::string_t, _case_insensitive_cmp>::const_iterator const_iterator;
105  typedef std::map<utility::string_t, utility::string_t, _case_insensitive_cmp>::reverse_iterator reverse_iterator;
106  typedef std::map<utility::string_t, utility::string_t, _case_insensitive_cmp>::const_reverse_iterator const_reverse_iterator;
107 
112 
117  http_headers(const http_headers &other) : m_headers(other.m_headers) {}
118 
124  {
125  if(this != &other)
126  {
127  m_headers = other.m_headers;
128  }
129  return *this;
130  }
131 
136  http_headers(http_headers &&other) : m_headers(std::move(other.m_headers)) {}
137 
143  {
144  if(this != &other)
145  {
146  m_headers = std::move(other.m_headers);
147  }
148  return *this;
149  }
150 
157  template<typename _t1>
158  void add(const key_type& name, const _t1& value)
159  {
160  if (has(name))
161  {
162  m_headers[name] = m_headers[name].append(_XPLATSTR(", ") + utility::conversions::print_string(value));
163  }
164  else
165  {
166  m_headers[name] = utility::conversions::print_string(value);
167  }
168  }
169 
174  void remove(const key_type& name)
175  {
176  m_headers.erase(name);
177  }
178 
182  void clear() { m_headers.clear(); }
183 
189  bool has(const key_type& name) const { return m_headers.find(name) != m_headers.end(); }
190 
195  size_type size() const { return m_headers.size(); }
196 
201  bool empty() const { return m_headers.empty(); }
202 
206  utility::string_t & operator[](const key_type &name) { return m_headers[name]; }
207 
214  iterator find(const key_type &name) { return m_headers.find(name); }
215  const_iterator find(const key_type &name) const { return m_headers.find(name); }
216 
223  template<typename _t1>
224  bool match(const key_type &name, _t1 &value) const
225  {
226  auto iter = m_headers.find(name);
227  if (iter != m_headers.end())
228  {
229  // Check to see if doesn't have a value.
230  if(iter->second.empty())
231  {
232  bind_impl(iter->second, value);
233  return true;
234  }
235  return bind_impl(iter->second, value);
236  }
237  else
238  {
239  return false;
240  }
241  }
242 
247  iterator begin() { return m_headers.begin(); }
248  const_iterator begin() const { return m_headers.begin(); }
249 
254  iterator end() { return m_headers.end(); }
255  const_iterator end() const { return m_headers.end(); }
256 
261  _ASYNCRTIMP utility::size64_t content_length() const;
262 
267  _ASYNCRTIMP void set_content_length(utility::size64_t length);
268 
273  _ASYNCRTIMP utility::string_t content_type() const;
274 
279  _ASYNCRTIMP void set_content_type(utility::string_t type);
280 
285  _ASYNCRTIMP utility::string_t cache_control() const;
286 
291  _ASYNCRTIMP void set_cache_control(utility::string_t control);
292 
297  _ASYNCRTIMP utility::string_t date() const;
298 
303  _ASYNCRTIMP void set_date(const utility::datetime& date);
304 
305 private:
306 
307  template<typename _t>
308  bool bind_impl(const key_type &text, _t &ref) const
309  {
310  utility::istringstream_t iss(text);
311  iss.imbue(std::locale::classic());
312  iss >> ref;
313  if (iss.fail() || !iss.eof())
314  {
315  return false;
316  }
317 
318  return true;
319  }
320 
321  bool bind_impl(const key_type &text, ::utility::string_t &ref) const
322  {
323  ref = text;
324  return true;
325  }
326 
327  // Headers are stored in a map with case insensitive key.
328  std::map<utility::string_t, utility::string_t, _case_insensitive_cmp> m_headers;
329 };
330 
331 }}
iterator end()
Returns an iterator referring to the past-the-end header field.
Definition: http_headers.h:254
bool empty() const
Tests to see if there are any header fields.
Definition: http_headers.h:201
Represents HTTP headers, acts like a map.
Definition: http_headers.h:75
bool bind(const key_type &text, _t &ref)
Binds an individual reference to a string value.
Definition: http_headers.h:44
_ASYNCRTIMP utility::size64_t content_length() const
Gets the content length of the message.
http_headers()
Constructs an empty set of HTTP headers.
Definition: http_headers.h:111
Definition: asyncrt_utils.h:370
The web namespace contains functionality common to multiple protocols like HTTP and WebSockets...
Definition: base_uri.h:37
bool has(const key_type &name) const
Checks if there is a header with the given key.
Definition: http_headers.h:189
_ASYNCRTIMP void set_date(const utility::datetime &date)
Sets the date header of the message.
utility::string_t & operator[](const key_type &name)
Returns a reference to header field with given name, if there is no header field one is inserted...
Definition: http_headers.h:206
_ASYNCRTIMP void set_content_type(utility::string_t type)
Sets the content type of the message.
iterator begin()
Returns an iterator referring to the first header field.
Definition: http_headers.h:247
http_headers(http_headers &&other)
Move constructor.
Definition: http_headers.h:136
http_headers(const http_headers &other)
Copy constructor.
Definition: http_headers.h:117
void add(const key_type &name, const _t1 &value)
Adds a header field using the '<<' operator.
Definition: http_headers.h:158
size_type size() const
Returns the number of header fields.
Definition: http_headers.h:195
_ASYNCRTIMP utility::string_t date() const
Gets the date header of the message.
bool match(const key_type &name, _t1 &value) const
Attempts to match a header field with the given name using the '>>' operator.
Definition: http_headers.h:224
iterator find(const key_type &name)
Checks if a header field exists with given name and returns an iterator if found. Otherwise and itera...
Definition: http_headers.h:214
std::map< utility::string_t, utility::string_t, _case_insensitive_cmp >::key_type key_type
STL-style typedefs
Definition: http_headers.h:94
_ASYNCRTIMP utility::string_t content_type() const
Gets the content type of the message.
http_headers & operator=(const http_headers &other)
Assignment operator.
Definition: http_headers.h:123
void clear()
Removes all elements from the headers.
Definition: http_headers.h:182
_ASYNCRTIMP void set_content_length(utility::size64_t length)
Sets the content length of the message.
http_headers & operator=(http_headers &&other)
Move assignment operator.
Definition: http_headers.h:142
Function object to perform case insensitive comparison of wstrings.
Definition: http_headers.h:79
Various utilities for string conversions and date and time manipulation.
Definition: asyncrt_utils.h:50
_ASYNCRTIMP void set_cache_control(utility::string_t control)
Sets the cache control header of the message.
_ASYNCRTIMP utility::string_t cache_control() const
Gets the cache control header of the message.