friendev EtherDune TCP/IP library
/home/jander/temp/etherdune/HTTPServer.h
Go to the documentation of this file.
1 // EtherDune HTTP Server class
2 // Author: Javier Peletier <jm@friendev.com>
3 // Summary: Provides an easy way to build a web server
4 //
5 // Copyright (c) 2015 All Rights Reserved, http://friendev.com
6 //
7 // This source is subject to the GPLv2 license.
8 // Please see the License.txt file for more information.
9 // All other rights reserved.
10 //
11 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
12 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 // PARTICULAR PURPOSE.
15 
16 
45 
46 
47 #ifndef _HTTP_SERVER_H_
48 #define _HTTP_SERVER_H_
49 
50 #include <ACross.h>
51 #include "config.h"
52 #include "HTTPConstants.h"
53 #include "TCPSocket.h"
54 #include <FlowScanner.h>
55 
56 
57 class HTTPServer :public TCPSocket
58 {
59 private:
60 
61  static const uint8_t HTTP_SERVER_STAGE_INIT = 0;
62  static const uint8_t HTTP_SERVER_STAGE_METHOD = 1;
63  static const uint8_t HTTP_SERVER_STAGE_QUERY_STRING = 2;
64  static const uint8_t HTTP_SERVER_STAGE_HEADERS = 3;
65  static const uint8_t HTTP_SERVER_STAGE_BODY = 4;
66  static const uint8_t HTTP_SERVER_STAGE_RESPONSE = 5;
67  static const uint8_t HTTP_SERVER_STAGE_RESPONSE_BODY = 6;
68  static const uint8_t HTTP_SERVER_STAGE_RESPONSE_END = 7;
69 
71  union
72  {
73  struct
74  {
75  char queryString[HTTP_SERVER_QUERY_STRING_MAX_LENGTH + 1];
76  };
77  struct
78  {
79  char headerName[HTTP_SERVER_HEADER_NAME_MAX_LENGTH + 1];
80  char headerValue[HTTP_SERVER_HEADER_VALUE_MAX_LENGTH + 1];
81  };
82 
83  };
85 
86  uint8_t stage;
87  uint8_t crlfcount;
88  FlowPattern requestPattern;
89  FlowPattern headerPattern;
90  FlowScanner scanner;
91 
92  void onReceive(uint16_t len, const byte* data);
93  void onConnect();
94  void onConnectRequest();
95  void onClose();
96  void resetParser();
97 
98 protected:
99 
100 
101 
102 public:
103 
104  uint8_t httpMethod;
105 
107  uint16_t contentLength;
108 
110  HTTPServer();
111 
112  void listen(uint16_t port = 80);
113  void beginResponse(uint16_t statusCode, const String& message = "");
114  void beginResponse_P(uint16_t statusCode, PGM_P message);
115  void writeHeader(const String& headerName, const String& headerValue);
116  void writeHeader_P(PGM_P headerName, const String& headerValue);
117  void writeContentTypeHeader(const String& contentType);
118  void writeContentTypeHeader_P(PGM_P contentType);
119  void beginResponseBody();
120  void endResponse();
121 
122  virtual void onBodyReceived(uint16_t len, const byte* data);
123  virtual void onHeaderReceived(const char* headerName, const char* headerValue);
124  virtual void onBodyBegin();
125  virtual void onRequest(char* queryString);
126  virtual void onRequestEnd();
127 
128 };
129 
130 
131 #endif
#define HTTP_SERVER_HEADER_NAME_MAX_LENGTH
max buffer to hold a header name, e.g.
Definition: config.h:240
void writeHeader(const String &headerName, const String &headerValue)
Writes an HTTP header to the ongoing response stream.
Definition: HTTPServer.cpp:225
void beginResponse(uint16_t statusCode, const String &message="")
Sends out the response line, e.g.
Definition: HTTPServer.cpp:203
uint8_t httpMethod
Indicates what HTTP method was used in this request.
Definition: HTTPServer.h:104
uint16_t contentLength
captured length of the incoming request, in bytes.
Definition: HTTPServer.h:107
virtual void onBodyBegin()
Called when all HTTP headers have been received and the body of the request is about to arrive...
Definition: HTTPServer.cpp:49
void listen()
Starts listening on the local port indicated by the localPort property.
Definition: TCPSocket.cpp:91
void beginResponse_P(uint16_t statusCode, PGM_P message)
Sends out the response line, e.g.
Definition: HTTPServer.cpp:214
virtual void onHeaderReceived(const char *headerName, const char *headerValue)
Called once for each header in the request, as they arrive.
Definition: HTTPServer.cpp:43
void beginResponseBody()
Indicates to the client that all headers have been written and that the response body follows...
Definition: HTTPServer.cpp:266
void writeContentTypeHeader(const String &contentType)
Convenient function to write the Content-Type header.
Definition: HTTPServer.cpp:246
Implements the TCP protocol.
Definition: TCPSocket.h:45
void writeContentTypeHeader_P(PGM_P contentType)
Convenient function to write the Content-Type header.
Definition: HTTPServer.cpp:257
Provides an easy way to build a web server.
Definition: HTTPServer.h:57
virtual void onRequestEnd()
Called after all the body has been received
Definition: HTTPServer.cpp:59
virtual void onBodyReceived(uint16_t len, const byte *data)
Called once for each fragment of the body that is received.
Definition: HTTPServer.cpp:34
void endResponse()
Ends the connection and finalizes the response.
Definition: HTTPServer.cpp:275
virtual void onRequest(char *queryString)
Called immediately after the first line that contains the HTTP method and query string is received ...
Definition: HTTPServer.cpp:55
#define HTTP_SERVER_HEADER_VALUE_MAX_LENGTH
max buffer to hold a header value, e.g.
Definition: config.h:241
void writeHeader_P(PGM_P headerName, const String &headerValue)
Writes an HTTP header to the ongoing response stream.
Definition: HTTPServer.cpp:236
#define HTTP_SERVER_QUERY_STRING_MAX_LENGTH
max buffer to hold the entire query string, e.g.
Definition: config.h:242