HttpResourceTree.h
Go to the documentation of this file.
1 /****`
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * HttpResourceTree.h
8  *
9  * @author: 2017 - Slavey Karadzhov <slav@attachix.com>
10  *
11  ****/
12 
13 #pragma once
14 
15 #include "HttpResource.h"
16 
17 using HttpPathDelegate = Delegate<void(HttpRequest& request, HttpResponse& response)>;
18 
20 #define RESOURCE_PATH_DEFAULT String('*')
21 
26 class HttpResourceTree : public ObjectMap<String, HttpResource>
27 {
28 public:
33  {
34  set(RESOURCE_PATH_DEFAULT, resource);
35  return resource;
36  }
37 
41  HttpResource* setDefault(const HttpResourceDelegate& onRequestComplete)
42  {
43  return set(RESOURCE_PATH_DEFAULT, onRequestComplete);
44  }
45 
48  {
49  return set(RESOURCE_PATH_DEFAULT, callback);
50  }
51 
56  {
58  }
59 
60  using ObjectMap::set;
61 
62  template <class... Tail>
63  HttpResource* set(const String& path, HttpResource* resource, HttpResourcePlugin* plugin, Tail... plugins)
64  {
65  registerPlugin(plugin, plugins...);
66  set(path, resource);
67  resource->addPlugin(plugin, plugins...);
68  return resource;
69  }
70 
79  HttpResource* set(const String& path, const HttpResourceDelegate& onRequestComplete);
80 
90  template <class... Tail>
91  HttpResource* set(const String& path, const HttpResourceDelegate& onRequestComplete, HttpResourcePlugin* plugin,
92  Tail... plugins)
93  {
94  registerPlugin(plugin, plugins...);
95  auto res = set(path, onRequestComplete);
96  res->addPlugin(plugin, plugins...);
97  return res;
98  }
99 
107  HttpResource* set(String path, const HttpPathDelegate& callback);
108 
118  template <class... Tail>
119  HttpResource* set(const String& path, const HttpPathDelegate& callback, HttpResourcePlugin* plugin, Tail... plugins)
120  {
121  registerPlugin(plugin, plugins...);
122  auto res = set(path, callback);
123  res->addPlugin(plugin, plugins...);
124  return res;
125  }
126 
127 private:
128  void registerPlugin(HttpResourcePlugin* plugin)
129  {
130  loadedPlugins.add(plugin);
131  }
132 
133  template <class... Tail> void registerPlugin(HttpResourcePlugin* plugin, Tail... plugins)
134  {
135  registerPlugin(plugin);
136  registerPlugin(plugins...);
137  }
138 
139  HttpResourcePlugin::OwnedList loadedPlugins;
140 };
#define RESOURCE_PATH_DEFAULT
Identifies the default resource path.
Definition: HttpResourceTree.h:20
Definition: Delegate.h:20
Encapsulates an incoming or outgoing request.
Definition: HttpRequest.h:37
Base plugin class. Implementations should be based on either HttpPreFilter or HttpPostFilter
Definition: HttpResourcePlugin.h:24
Class to map URL paths to classes which handle them.
Definition: HttpResourceTree.h:27
HttpResource * setDefault(const HttpResourceDelegate &onRequestComplete)
Set the default resource handler, identified by "*" wildcard.
Definition: HttpResourceTree.h:41
HttpResource * set(const String &path, const HttpResourceDelegate &onRequestComplete)
Set a callback to handle the given path.
HttpResource * setDefault(HttpResource *resource)
Set the default resource handler.
Definition: HttpResourceTree.h:32
HttpResource * getDefault()
Get the current default resource handler, if any.
Definition: HttpResourceTree.h:55
HttpResource * setDefault(const HttpPathDelegate &callback)
Set the default resource handler, identified by "*" wildcard.
Definition: HttpResourceTree.h:47
void set(const K &key, V *value)
Set a key value.
Definition: ObjectMap.h:209
HttpResource * set(const String &path, const HttpPathDelegate &callback, HttpResourcePlugin *plugin, Tail... plugins)
Add a new path resource with callback and one or more plugins.
Definition: HttpResourceTree.h:119
HttpResource * set(const String &path, const HttpResourceDelegate &onRequestComplete, HttpResourcePlugin *plugin, Tail... plugins)
Set a callback to handle the given path, with one or more plugins.
Definition: HttpResourceTree.h:91
HttpResource * set(String path, const HttpPathDelegate &callback)
Add a new path resource with a callback.
HttpResource * set(const String &path, HttpResource *resource, HttpResourcePlugin *plugin, Tail... plugins)
Definition: HttpResourceTree.h:63
Instances of this class are registered with an HttpServer for a specific URL.
Definition: HttpResource.h:34
void addPlugin(HttpResourcePlugin *plugin)
Represents either an incoming or outgoing response to a HTTP request.
Definition: HttpResponse.h:26
bool add(ObjectType *object)
Definition: LinkedObjectList.h:134
Implementation of a HashMap for owned objects, i.e. anything created with new().
Definition: ObjectMap.h:49
HttpResource * find(const String &key) const
Find the value for a given key, if it exists.
Definition: ObjectMap.h:225
void set(const K &key, V *value)
Set a key value.
Definition: ObjectMap.h:209
The String class.
Definition: WString.h:137