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_listener.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 *
20 * HTTP Library: HTTP listener (server-side) APIs
21 *
22 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
23 ****/
24 #pragma once
25 
26 #ifndef _CASA_HTTP_LISTENER_H
27 #define _CASA_HTTP_LISTENER_H
28 
29 #include <limits>
30 #include <functional>
31 
32 #if !defined(_WIN32) && !defined(__cplusplus_winrt)
33 #include <boost/asio/ssl.hpp>
34 #endif
35 
36 #include "cpprest/http_msg.h"
37 
38 #if !defined(_WIN32) || (_WIN32_WINNT >= _WIN32_WINNT_VISTA && !defined(__cplusplus_winrt))
39 
40 namespace web
41 {
42 namespace http
43 {
45 namespace experimental
46 {
48 namespace listener
49 {
50 
55 {
56 public:
57 
62  : m_timeout(utility::seconds(120))
63  {}
64 
70  : m_timeout(other.m_timeout)
71 #ifndef _WIN32
72  , m_ssl_context_callback(other.m_ssl_context_callback)
73 #endif
74  {}
75 
81  : m_timeout(std::move(other.m_timeout))
82 #ifndef _WIN32
83  , m_ssl_context_callback(std::move(other.m_ssl_context_callback))
84 #endif
85  {}
86 
92  {
93  if(this != &rhs)
94  {
95  m_timeout = rhs.m_timeout;
96 #ifndef _WIN32
97  m_ssl_context_callback = rhs.m_ssl_context_callback;
98 #endif
99  }
100  return *this;
101  }
102 
108  {
109  if(this != &rhs)
110  {
111  m_timeout = std::move(rhs.m_timeout);
112 #ifndef _WIN32
113  m_ssl_context_callback = std::move(rhs.m_ssl_context_callback);
114 #endif
115  }
116  return *this;
117  }
118 
123  utility::seconds timeout() const
124  {
125  return m_timeout;
126  }
127 
132  void set_timeout(utility::seconds timeout)
133  {
134  m_timeout = std::move(timeout);
135  }
136 
137 #ifndef _WIN32
138  const std::function<void(boost::asio::ssl::context&)>& get_ssl_context_callback() const
143  {
144  return m_ssl_context_callback;
145  }
146 
151  void set_ssl_context_callback(const std::function<void(boost::asio::ssl::context&)> &ssl_context_callback)
152  {
153  m_ssl_context_callback = ssl_context_callback;
154  }
155 #endif
156 
157 private:
158 
159  utility::seconds m_timeout;
160 #ifndef _WIN32
161  std::function<void(boost::asio::ssl::context&)> m_ssl_context_callback;
162 #endif
163 };
164 
165 namespace details
166 {
167 
172 {
173 public:
174 
176  : m_closed(true)
177  , m_close_task(pplx::task_from_result())
178  {
179  }
180 
181  _ASYNCRTIMP http_listener_impl(http::uri address);
182  _ASYNCRTIMP http_listener_impl(http::uri address, http_listener_config config);
183 
184  _ASYNCRTIMP pplx::task<void> open();
185  _ASYNCRTIMP pplx::task<void> close();
186 
191  _ASYNCRTIMP void handle_request(http::http_request msg);
192 
193  const http::uri & uri() const { return m_uri; }
194 
195  const http_listener_config & configuration() const { return m_config; }
196 
197  // Handlers
198  std::function<void(http::http_request)> m_all_requests;
199  std::map<http::method, std::function<void(http::http_request)>> m_supported_methods;
200 
201 private:
202 
203  // Default implementation for TRACE and OPTIONS.
204  void handle_trace(http::http_request message);
205  void handle_options(http::http_request message);
206 
207  // Gets a comma separated string containing the methods supported by this listener.
208  utility::string_t get_supported_methods() const;
209 
210  http::uri m_uri;
211  http_listener_config m_config;
212 
213  // Used to record that the listener is closed.
214  bool m_closed;
215  pplx::task<void> m_close_task;
216 };
217 
218 } // namespace details
219 
224 {
225 public:
226 
233  : m_impl(utility::details::make_unique<details::http_listener_impl>(std::move(address)))
234  {
235  }
236 
243  : m_impl(utility::details::make_unique<details::http_listener_impl>(std::move(address), std::move(config)))
244  {
245  }
246 
253  : m_impl(utility::details::make_unique<details::http_listener_impl>())
254  {
255  }
256 
261  _ASYNCRTIMP ~http_listener();
262 
268  {
269  return m_impl->open();
270  }
271 
284  {
285  return m_impl->close();
286  }
287 
292  void support(const std::function<void(http_request)> &handler)
293  {
294  m_impl->m_all_requests = handler;
295  }
296 
302  void support(const http::method &method, const std::function<void(http_request)> &handler)
303  {
304  m_impl->m_supported_methods[method] = handler;
305  }
306 
311  const http::uri & uri() const { return m_impl->uri(); }
312 
317  const http_listener_config & configuration() const { return m_impl->configuration(); }
318 
324  : m_impl(std::move(other.m_impl))
325  {
326  }
327 
333  {
334  if(this != &other)
335  {
336  m_impl = std::move(other.m_impl);
337  }
338  return *this;
339  }
340 
341 private:
342 
343  // No copying of listeners.
344  http_listener(const http_listener &other);
345  http_listener &operator=(const http_listener &other);
346 
347  std::unique_ptr<details::http_listener_impl> m_impl;
348 };
349 
350 }}}}
351 
352 #endif
353 #endif
http_listener()
Default constructor.
Definition: http_listener.h:252
utility::seconds timeout() const
Get the timeout
Definition: http_listener.h:123
Configuration class used to set various options when constructing and http_listener instance...
Definition: http_listener.h:54
http_listener(http::uri address)
Create a listener from a URI.
Definition: http_listener.h:232
A flexible, protocol independent URI implementation.
Definition: base_uri.h:151
void support(const http::method &method, const std::function< void(http_request)> &handler)
Add support for a specific HTTP method.
Definition: http_listener.h:302
The web namespace contains functionality common to multiple protocols like HTTP and WebSockets...
Definition: base_uri.h:37
_ASYNCRTIMP ~http_listener()
Destructor frees any held resources.
void support(const std::function< void(http_request)> &handler)
Add a general handler to support all requests.
Definition: http_listener.h:292
http_listener(http_listener &&other)
Move constructor.
Definition: http_listener.h:323
const http_listener_config & configuration() const
Get the configuration of this listener.
Definition: http_listener.h:317
http_listener_config & operator=(http_listener_config &&rhs)
Assignment operator.
Definition: http_listener.h:107
http_listener_config(http_listener_config &&other)
Move constructor.
Definition: http_listener.h:80
pplx::task< void > open()
Asynchronously open the listener, i.e. start accepting requests.
Definition: http_listener.h:267
utility::string_t method
Predefined method strings for the standard HTTP methods mentioned in the HTTP 1.1 specification...
Definition: http_msg.h:62
http_listener_config(const http_listener_config &other)
Copy constructor.
Definition: http_listener.h:69
void set_ssl_context_callback(const std::function< void(boost::asio::ssl::context &)> &ssl_context_callback)
Set the callback of ssl context
Definition: http_listener.h:151
_ASYNCRTIMP void handle_request(http::http_request msg)
Handler for all requests. The HTTP host uses this to dispatch a message to the pipeline.
http_listener_config()
Create an http_listener configuration with default options.
Definition: http_listener.h:61
const http::uri & uri() const
Get the URI of the listener.
Definition: http_listener.h:311
The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed as...
Definition: pplxtasks.h:4173
A class for listening and processing HTTP requests at a specific URI.
Definition: http_listener.h:223
const std::function< void(boost::asio::ssl::context &)> & get_ssl_context_callback() const
Get the callback of ssl context
Definition: http_listener.h:142
Represents an HTTP request.
Definition: http_msg.h:771
http_listener & operator=(http_listener &&other)
Move assignment operator.
Definition: http_listener.h:332
http_listener(http::uri address, http_listener_config config)
Create a listener with specified URI and configuration.
Definition: http_listener.h:242
Internal class for pointer to implementation design pattern.
Definition: http_listener.h:171
void set_timeout(utility::seconds timeout)
Set the timeout
Definition: http_listener.h:132
http_listener_config & operator=(const http_listener_config &rhs)
Assignment operator.
Definition: http_listener.h:91
pplx::task< void > close()
Asynchronously stop accepting requests and close all connections.
Definition: http_listener.h:283
Various utilities for string conversions and date and time manipulation.
Definition: asyncrt_utils.h:50