HttpServer.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  * HttpServer.h
8  *
9  * Modified: 2017 - Slavey Karadzhov <slav@attachix.com>
10  *
11  ****/
12 
19 #pragma once
20 
21 #include "TcpServer.h"
22 #include "WString.h"
23 #include "Http/HttpResourceTree.h"
25 #include "Http/HttpBodyParser.h"
26 
27 typedef struct {
28  uint16_t maxActiveConnections = 10;
29  uint16_t keepAliveSeconds = 0;
30  int minHeapSize = -1;
31  bool useDefaultBodyParsers = 1;
32  bool closeOnContentError =
33  true;
35 
36 class HttpServer : public TcpServer
37 {
38 public:
40  {
41  settings.keepAliveSeconds = 2;
42  configure(settings);
43  }
44 
45  HttpServer(const HttpServerSettings& settings)
46  {
47  configure(settings);
48  }
49 
53  void configure(const HttpServerSettings& settings);
54 
64  void setBodyParser(const String& contentType, HttpBodyParserDelegate parser)
65  {
66  bodyParsers[contentType] = parser;
67  }
68 
75  {
76  bodyParsers[ContentType::toString(mimeType)] = parser;
77  }
78 
80  void addPath(String path, const HttpPathDelegate& callback) SMING_DEPRECATED
81  {
82  paths.set(path, callback);
83  }
84 
86  void addPath(const String& path, const HttpResourceDelegate& onRequestComplete) SMING_DEPRECATED
87  {
88  paths.set(path, onRequestComplete);
89  }
90 
92  void addPath(const String& path, HttpResource* resource) SMING_DEPRECATED
93  {
94  paths.set(path, resource);
95  }
96 
99  {
100  paths.setDefault(callback);
101  }
102 
105  {
106  paths.setDefault(resource);
107  }
108 
109 public:
112 
113 protected:
114  TcpConnection* createClient(tcp_pcb* clientTcp) override;
115 
116 private:
117  HttpServerSettings settings;
118  BodyParsers bodyParsers;
119 };
120 
uint16_t keepAliveSeconds
default seconds to keep the connection alive before closing it
Definition: HttpServer.h:29
HttpServer()
Definition: HttpServer.h:39
HttpServer(const HttpServerSettings &settings)
Definition: HttpServer.h:45
void setBodyParser(const String &contentType, HttpBodyParserDelegate parser)
Allows content-type specific parsing of the body based on content-type.
Definition: HttpServer.h:64
void addPath(String path, const HttpPathDelegate &callback)
Definition: HttpServer.h:80
void addPath(const String &path, HttpResource *resource)
Definition: HttpServer.h:92
void configure(const HttpServerSettings &settings)
Allows changing the server configuration.
Definition: TcpConnection.h:39
void set(const String &path, const HttpResourceDelegate &onRequestComplete)
Set a callback to handle the given path.
Definition: HttpResourceTree.h:66
The String class.
Definition: WString.h:136
TcpConnection * createClient(tcp_pcb *clientTcp) override
Definition: HttpResource.h:30
#define SMING_DEPRECATED
Definition: sming_attr.h:30
Definition: HttpServer.h:27
String toString(enum MimeType m)
Get textual representation for a MIME type.
Definition: TcpServer.h:28
Definition: HttpServer.h:36
void setDefaultHandler(const HttpPathDelegate &callback)
Definition: HttpServer.h:98
Class to map URL paths to classes which handle them.
Definition: HttpResourceTree.h:24
HttpResourceTree paths
Maps paths to resources which deal with incoming requests.
Definition: HttpServer.h:111
void setDefaultResource(HttpResource *resource)
Definition: HttpServer.h:104
void setBodyParser(MimeType mimeType, HttpBodyParserDelegate parser)
Allows content-type specific parsing of the body based on content-type.
Definition: HttpServer.h:74
void setDefault(HttpResource *resource)
Set the default resource handler.
Definition: HttpResourceTree.h:30
MimeType
Definition: WebConstants.h:54
void addPath(const String &path, const HttpResourceDelegate &onRequestComplete)
Definition: HttpServer.h:86