User | Revision | Line number | New contents of line |
slowness |
0:58c3d014a4e7
|
1
|
|
slowness |
0:58c3d014a4e7
|
2
|
/*
|
slowness |
0:58c3d014a4e7
|
3
|
Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
|
slowness |
0:58c3d014a4e7
|
4
|
|
slowness |
0:58c3d014a4e7
|
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
slowness |
0:58c3d014a4e7
|
6
|
of this software and associated documentation files (the "Software"), to deal
|
slowness |
0:58c3d014a4e7
|
7
|
in the Software without restriction, including without limitation the rights
|
slowness |
0:58c3d014a4e7
|
8
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
slowness |
0:58c3d014a4e7
|
9
|
copies of the Software, and to permit persons to whom the Software is
|
slowness |
0:58c3d014a4e7
|
10
|
furnished to do so, subject to the following conditions:
|
slowness |
0:58c3d014a4e7
|
11
|
|
slowness |
0:58c3d014a4e7
|
12
|
The above copyright notice and this permission notice shall be included in
|
slowness |
0:58c3d014a4e7
|
13
|
all copies or substantial portions of the Software.
|
slowness |
0:58c3d014a4e7
|
14
|
|
slowness |
0:58c3d014a4e7
|
15
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
slowness |
0:58c3d014a4e7
|
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
slowness |
0:58c3d014a4e7
|
17
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
slowness |
0:58c3d014a4e7
|
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
slowness |
0:58c3d014a4e7
|
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
slowness |
0:58c3d014a4e7
|
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
slowness |
0:58c3d014a4e7
|
21
|
THE SOFTWARE.
|
slowness |
0:58c3d014a4e7
|
22
|
*/
|
slowness |
0:58c3d014a4e7
|
23
|
|
slowness |
0:58c3d014a4e7
|
24
|
/**
|
slowness |
0:58c3d014a4e7
|
25
|
HTTP Request Handler header file.
|
slowness |
0:58c3d014a4e7
|
26
|
*/
|
slowness |
0:58c3d014a4e7
|
27
|
|
slowness |
0:58c3d014a4e7
|
28
|
#ifndef HTTP_REQUEST_HANDLER_H
|
slowness |
0:58c3d014a4e7
|
29
|
#define HTTP_REQUEST_HANDLER_H
|
slowness |
0:58c3d014a4e7
|
30
|
|
slowness |
0:58c3d014a4e7
|
31
|
#include "api/TCPSocket.h"
|
slowness |
0:58c3d014a4e7
|
32
|
//#include "HTTPServer.h"
|
slowness |
0:58c3d014a4e7
|
33
|
|
slowness |
0:58c3d014a4e7
|
34
|
#include "mbed.h"
|
slowness |
0:58c3d014a4e7
|
35
|
#include "core/netservice.h"
|
slowness |
0:58c3d014a4e7
|
36
|
|
slowness |
0:58c3d014a4e7
|
37
|
#include <string>
|
slowness |
0:58c3d014a4e7
|
38
|
using std::string;
|
slowness |
0:58c3d014a4e7
|
39
|
|
slowness |
0:58c3d014a4e7
|
40
|
#include <map>
|
slowness |
0:58c3d014a4e7
|
41
|
using std::map;
|
slowness |
0:58c3d014a4e7
|
42
|
|
slowness |
0:58c3d014a4e7
|
43
|
///HTTP Server's generic request handler
|
slowness |
0:58c3d014a4e7
|
44
|
class HTTPRequestHandler : public NetService
|
slowness |
0:58c3d014a4e7
|
45
|
{
|
slowness |
0:58c3d014a4e7
|
46
|
public:
|
slowness |
0:58c3d014a4e7
|
47
|
///Instantiated by the HTTP Server
|
slowness |
0:58c3d014a4e7
|
48
|
HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket);
|
slowness |
0:58c3d014a4e7
|
49
|
virtual ~HTTPRequestHandler();
|
slowness |
0:58c3d014a4e7
|
50
|
|
slowness |
0:58c3d014a4e7
|
51
|
//protected:
|
slowness |
0:58c3d014a4e7
|
52
|
virtual void doGet() = 0;
|
slowness |
0:58c3d014a4e7
|
53
|
virtual void doPost() = 0;
|
slowness |
0:58c3d014a4e7
|
54
|
virtual void doHead() = 0;
|
slowness |
0:58c3d014a4e7
|
55
|
|
slowness |
0:58c3d014a4e7
|
56
|
virtual void onReadable() = 0; //Data has been read
|
slowness |
0:58c3d014a4e7
|
57
|
virtual void onWriteable() = 0; //Data has been written & buf is free
|
slowness |
0:58c3d014a4e7
|
58
|
virtual void onTimeout(); //Connection has timed out
|
slowness |
0:58c3d014a4e7
|
59
|
virtual void onClose() = 0; //Connection is closing
|
slowness |
0:58c3d014a4e7
|
60
|
|
slowness |
0:58c3d014a4e7
|
61
|
virtual void close(); //Close socket and destroy data
|
slowness |
0:58c3d014a4e7
|
62
|
|
slowness |
0:58c3d014a4e7
|
63
|
protected:
|
slowness |
0:58c3d014a4e7
|
64
|
map<string, string>& reqHeaders() /*const*/;
|
slowness |
0:58c3d014a4e7
|
65
|
string& path() /*const*/;
|
slowness |
0:58c3d014a4e7
|
66
|
int dataLen() const;
|
slowness |
0:58c3d014a4e7
|
67
|
int readData(char* buf, int len);
|
slowness |
0:58c3d014a4e7
|
68
|
string& rootPath() /*const*/;
|
slowness |
0:58c3d014a4e7
|
69
|
|
slowness |
0:58c3d014a4e7
|
70
|
void setErrCode(int errc);
|
slowness |
0:58c3d014a4e7
|
71
|
void setContentLen(int len);
|
slowness |
0:58c3d014a4e7
|
72
|
|
slowness |
0:58c3d014a4e7
|
73
|
map<string, string>& respHeaders();
|
slowness |
0:58c3d014a4e7
|
74
|
int writeData(const char* buf, int len);
|
slowness |
0:58c3d014a4e7
|
75
|
|
slowness |
0:58c3d014a4e7
|
76
|
void setTimeout(int ms);
|
slowness |
0:58c3d014a4e7
|
77
|
void resetTimeout();
|
slowness |
0:58c3d014a4e7
|
78
|
|
slowness |
0:58c3d014a4e7
|
79
|
private:
|
slowness |
0:58c3d014a4e7
|
80
|
void readHeaders(); //Called at instanciation
|
slowness |
0:58c3d014a4e7
|
81
|
void writeHeaders(); //Called at the first writeData call
|
slowness |
0:58c3d014a4e7
|
82
|
void onTCPSocketEvent(TCPSocketEvent e);
|
slowness |
0:58c3d014a4e7
|
83
|
|
slowness |
0:58c3d014a4e7
|
84
|
TCPSocket* m_pTCPSocket;
|
slowness |
0:58c3d014a4e7
|
85
|
map<string, string> m_reqHeaders;
|
slowness |
0:58c3d014a4e7
|
86
|
map<string, string> m_respHeaders;
|
slowness |
0:58c3d014a4e7
|
87
|
string m_rootPath;
|
slowness |
0:58c3d014a4e7
|
88
|
string m_path;
|
slowness |
0:58c3d014a4e7
|
89
|
int m_errc; //Response code
|
slowness |
0:58c3d014a4e7
|
90
|
|
slowness |
0:58c3d014a4e7
|
91
|
Timeout m_watchdog;
|
slowness |
0:58c3d014a4e7
|
92
|
int m_timeout;
|
slowness |
0:58c3d014a4e7
|
93
|
|
slowness |
0:58c3d014a4e7
|
94
|
bool m_closed;
|
slowness |
0:58c3d014a4e7
|
95
|
bool m_headersSent;
|
slowness |
0:58c3d014a4e7
|
96
|
|
slowness |
0:58c3d014a4e7
|
97
|
int readLine(char* str, int maxLen);
|
slowness |
0:58c3d014a4e7
|
98
|
|
slowness |
0:58c3d014a4e7
|
99
|
};
|
slowness |
0:58c3d014a4e7
|
100
|
|
slowness |
0:58c3d014a4e7
|
101
|
#endif
|