Network Services
Fork of W5500Interface_K22F by
HTTPD/HTTPD.h@15:14382459c8b7, 2017-06-15 (annotated)
- Committer:
- dgriffin65
- Date:
- Thu Jun 15 20:29:03 2017 +0000
- Revision:
- 15:14382459c8b7
Converted to a single library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dgriffin65 | 15:14382459c8b7 | 1 | /* Copyright (C) 2013 Hiroshi Suga, MIT License |
dgriffin65 | 15:14382459c8b7 | 2 | * |
dgriffin65 | 15:14382459c8b7 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
dgriffin65 | 15:14382459c8b7 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
dgriffin65 | 15:14382459c8b7 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
dgriffin65 | 15:14382459c8b7 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
dgriffin65 | 15:14382459c8b7 | 7 | * furnished to do so, subject to the following conditions: |
dgriffin65 | 15:14382459c8b7 | 8 | * |
dgriffin65 | 15:14382459c8b7 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
dgriffin65 | 15:14382459c8b7 | 10 | * substantial portions of the Software. |
dgriffin65 | 15:14382459c8b7 | 11 | * |
dgriffin65 | 15:14382459c8b7 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
dgriffin65 | 15:14382459c8b7 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
dgriffin65 | 15:14382459c8b7 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
dgriffin65 | 15:14382459c8b7 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
dgriffin65 | 15:14382459c8b7 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
dgriffin65 | 15:14382459c8b7 | 17 | */ |
dgriffin65 | 15:14382459c8b7 | 18 | |
dgriffin65 | 15:14382459c8b7 | 19 | #ifndef HTTPD_H |
dgriffin65 | 15:14382459c8b7 | 20 | #define HTTPD_H |
dgriffin65 | 15:14382459c8b7 | 21 | |
dgriffin65 | 15:14382459c8b7 | 22 | #include "mbed.h" |
dgriffin65 | 15:14382459c8b7 | 23 | #include "rtos.h" |
dgriffin65 | 15:14382459c8b7 | 24 | #include "NetworkStack.h" |
dgriffin65 | 15:14382459c8b7 | 25 | #include "EthInterface.h" |
dgriffin65 | 15:14382459c8b7 | 26 | #include "CBuffer.h" |
dgriffin65 | 15:14382459c8b7 | 27 | |
dgriffin65 | 15:14382459c8b7 | 28 | #define DEBUG |
dgriffin65 | 15:14382459c8b7 | 29 | |
dgriffin65 | 15:14382459c8b7 | 30 | #define HTTPD_PORT 80 |
dgriffin65 | 15:14382459c8b7 | 31 | #define HTTPD_MAX_CLIENTS 5 |
dgriffin65 | 15:14382459c8b7 | 32 | #define HTTPD_KEEPALIVE 10 |
dgriffin65 | 15:14382459c8b7 | 33 | #define HTTPD_TIMEOUT 15000 |
dgriffin65 | 15:14382459c8b7 | 34 | #define HTTPD_MAX_HANDLES 10 |
dgriffin65 | 15:14382459c8b7 | 35 | |
dgriffin65 | 15:14382459c8b7 | 36 | #define HTTPD_CMD_SIZE 100 |
dgriffin65 | 15:14382459c8b7 | 37 | #define HTTPD_BUF_SIZE 256 |
dgriffin65 | 15:14382459c8b7 | 38 | #define HTTPD_STACK_SIZE (1024 * 6) |
dgriffin65 | 15:14382459c8b7 | 39 | //#define HTTPD_ENABLE_CLOSER |
dgriffin65 | 15:14382459c8b7 | 40 | |
dgriffin65 | 15:14382459c8b7 | 41 | //Debug is disabled by default |
dgriffin65 | 15:14382459c8b7 | 42 | #if defined(DEBUG) and (!defined(TARGET_LPC11U24)) |
dgriffin65 | 15:14382459c8b7 | 43 | #define DBG(x, ...) std::printf("[DBG]" x "\r\n", ##__VA_ARGS__); |
dgriffin65 | 15:14382459c8b7 | 44 | #define WARN(x, ...) std::printf("[WARN]" x "\r\n", ##__VA_ARGS__); |
dgriffin65 | 15:14382459c8b7 | 45 | #define ERR(x, ...) std::printf("[ERR]" x "\r\n", ##__VA_ARGS__); |
dgriffin65 | 15:14382459c8b7 | 46 | #define INFO(x, ...) std::printf("[INFO]" x "\r\n", ##__VA_ARGS__); |
dgriffin65 | 15:14382459c8b7 | 47 | #else |
dgriffin65 | 15:14382459c8b7 | 48 | #define DBG(x, ...) |
dgriffin65 | 15:14382459c8b7 | 49 | #define WARN(x, ...) |
dgriffin65 | 15:14382459c8b7 | 50 | #define ERR(x, ...) |
dgriffin65 | 15:14382459c8b7 | 51 | #define INFO(x, ...) |
dgriffin65 | 15:14382459c8b7 | 52 | #endif |
dgriffin65 | 15:14382459c8b7 | 53 | |
dgriffin65 | 15:14382459c8b7 | 54 | |
dgriffin65 | 15:14382459c8b7 | 55 | class HTTPD { |
dgriffin65 | 15:14382459c8b7 | 56 | public: |
dgriffin65 | 15:14382459c8b7 | 57 | |
dgriffin65 | 15:14382459c8b7 | 58 | enum Request { |
dgriffin65 | 15:14382459c8b7 | 59 | REQ_HTTPGET, |
dgriffin65 | 15:14382459c8b7 | 60 | REQ_HTTPPOST, |
dgriffin65 | 15:14382459c8b7 | 61 | REQ_PUT, |
dgriffin65 | 15:14382459c8b7 | 62 | }; |
dgriffin65 | 15:14382459c8b7 | 63 | |
dgriffin65 | 15:14382459c8b7 | 64 | enum Mode { |
dgriffin65 | 15:14382459c8b7 | 65 | MODE_REQUEST, |
dgriffin65 | 15:14382459c8b7 | 66 | MODE_REQSTR, |
dgriffin65 | 15:14382459c8b7 | 67 | MODE_HEADER, |
dgriffin65 | 15:14382459c8b7 | 68 | MODE_BODY, |
dgriffin65 | 15:14382459c8b7 | 69 | MODE_ENTER, |
dgriffin65 | 15:14382459c8b7 | 70 | MODE_ERROR, |
dgriffin65 | 15:14382459c8b7 | 71 | MODE_WEBSOCKET, |
dgriffin65 | 15:14382459c8b7 | 72 | MODE_WEBSOCKET_MASK, |
dgriffin65 | 15:14382459c8b7 | 73 | MODE_WEBSOCKET_BODY, |
dgriffin65 | 15:14382459c8b7 | 74 | MODE_WEBSOCKET_ENTER, |
dgriffin65 | 15:14382459c8b7 | 75 | }; |
dgriffin65 | 15:14382459c8b7 | 76 | |
dgriffin65 | 15:14382459c8b7 | 77 | struct STATE { |
dgriffin65 | 15:14382459c8b7 | 78 | Thread *thread; |
dgriffin65 | 15:14382459c8b7 | 79 | TCPSocket *client; |
dgriffin65 | 15:14382459c8b7 | 80 | volatile Request req; |
dgriffin65 | 15:14382459c8b7 | 81 | volatile Mode mode; |
dgriffin65 | 15:14382459c8b7 | 82 | CircBuffer <char>*buf; |
dgriffin65 | 15:14382459c8b7 | 83 | char uri[HTTPD_CMD_SIZE]; |
dgriffin65 | 15:14382459c8b7 | 84 | char *filename; |
dgriffin65 | 15:14382459c8b7 | 85 | char *querystring; |
dgriffin65 | 15:14382459c8b7 | 86 | int enter; |
dgriffin65 | 15:14382459c8b7 | 87 | int length; |
dgriffin65 | 15:14382459c8b7 | 88 | int n; |
dgriffin65 | 15:14382459c8b7 | 89 | int keepalive; |
dgriffin65 | 15:14382459c8b7 | 90 | int websocket; |
dgriffin65 | 15:14382459c8b7 | 91 | char *websocket_key; |
dgriffin65 | 15:14382459c8b7 | 92 | int websocket_opcode; |
dgriffin65 | 15:14382459c8b7 | 93 | int websocket_flg; |
dgriffin65 | 15:14382459c8b7 | 94 | char websocket_mask[4]; |
dgriffin65 | 15:14382459c8b7 | 95 | int websocket_payload; |
dgriffin65 | 15:14382459c8b7 | 96 | int (*sendws)(int id, const char *buf, int len, const char *mask); |
dgriffin65 | 15:14382459c8b7 | 97 | }; |
dgriffin65 | 15:14382459c8b7 | 98 | |
dgriffin65 | 15:14382459c8b7 | 99 | HTTPD (); |
dgriffin65 | 15:14382459c8b7 | 100 | |
dgriffin65 | 15:14382459c8b7 | 101 | int start (NetworkStack *ns, int port = HTTPD_PORT); |
dgriffin65 | 15:14382459c8b7 | 102 | |
dgriffin65 | 15:14382459c8b7 | 103 | // --- HTTPD_req.cpp --- |
dgriffin65 | 15:14382459c8b7 | 104 | void httpdError (int id, int err); |
dgriffin65 | 15:14382459c8b7 | 105 | |
dgriffin65 | 15:14382459c8b7 | 106 | // --- HTTPD_ws.cpp --- |
dgriffin65 | 15:14382459c8b7 | 107 | static int sendWebsocket (int id, const char *buf, int len, const char *mask = NULL); |
dgriffin65 | 15:14382459c8b7 | 108 | |
dgriffin65 | 15:14382459c8b7 | 109 | // --- HTTPD_util.cpp --- |
dgriffin65 | 15:14382459c8b7 | 110 | char *getUri (int id); |
dgriffin65 | 15:14382459c8b7 | 111 | char *getFilename (int id); |
dgriffin65 | 15:14382459c8b7 | 112 | char *getQueryString (int id); |
dgriffin65 | 15:14382459c8b7 | 113 | |
dgriffin65 | 15:14382459c8b7 | 114 | TCPSocket *getClientSocket(int id) { |
dgriffin65 | 15:14382459c8b7 | 115 | if (id >= HTTPD_MAX_CLIENTS) return NULL; |
dgriffin65 | 15:14382459c8b7 | 116 | return _state[id].client; |
dgriffin65 | 15:14382459c8b7 | 117 | } |
dgriffin65 | 15:14382459c8b7 | 118 | int send (int id, const char *body, int len, const char *header = NULL); |
dgriffin65 | 15:14382459c8b7 | 119 | int sendstr (int id, const char *buf); |
dgriffin65 | 15:14382459c8b7 | 120 | int hprintf(int id, const char* format, ...); |
dgriffin65 | 15:14382459c8b7 | 121 | int receive (int id, char *buf, int len); |
dgriffin65 | 15:14382459c8b7 | 122 | int attach (const char *uri, const char *dir); |
dgriffin65 | 15:14382459c8b7 | 123 | int attach (const char *uri, void (*funcCgi)(int id)); |
dgriffin65 | 15:14382459c8b7 | 124 | int base64encode (const char *input, int length, char *output, int len); |
dgriffin65 | 15:14382459c8b7 | 125 | int urlencode (const char *str, char *buf, int len); |
dgriffin65 | 15:14382459c8b7 | 126 | int urldecode (const char *str, char *buf, int len); |
dgriffin65 | 15:14382459c8b7 | 127 | |
dgriffin65 | 15:14382459c8b7 | 128 | static HTTPD * getInstance() { |
dgriffin65 | 15:14382459c8b7 | 129 | return _inst; |
dgriffin65 | 15:14382459c8b7 | 130 | }; |
dgriffin65 | 15:14382459c8b7 | 131 | |
dgriffin65 | 15:14382459c8b7 | 132 | private : |
dgriffin65 | 15:14382459c8b7 | 133 | static HTTPD *_inst; |
dgriffin65 | 15:14382459c8b7 | 134 | Thread *_daemon; |
dgriffin65 | 15:14382459c8b7 | 135 | TCPServer _server; |
dgriffin65 | 15:14382459c8b7 | 136 | |
dgriffin65 | 15:14382459c8b7 | 137 | NetworkStack *m_ns; |
dgriffin65 | 15:14382459c8b7 | 138 | |
dgriffin65 | 15:14382459c8b7 | 139 | #ifdef HTTPD_ENABLE_CLOSER |
dgriffin65 | 15:14382459c8b7 | 140 | struct STATE _state[HTTPD_MAX_CLIENTS + 1]; |
dgriffin65 | 15:14382459c8b7 | 141 | #else |
dgriffin65 | 15:14382459c8b7 | 142 | struct STATE _state[HTTPD_MAX_CLIENTS]; |
dgriffin65 | 15:14382459c8b7 | 143 | #endif |
dgriffin65 | 15:14382459c8b7 | 144 | |
dgriffin65 | 15:14382459c8b7 | 145 | struct HANDLER { |
dgriffin65 | 15:14382459c8b7 | 146 | char *uri; |
dgriffin65 | 15:14382459c8b7 | 147 | char *dir; |
dgriffin65 | 15:14382459c8b7 | 148 | void (*funcCgi)(int id); |
dgriffin65 | 15:14382459c8b7 | 149 | } _handler[HTTPD_MAX_HANDLES]; |
dgriffin65 | 15:14382459c8b7 | 150 | |
dgriffin65 | 15:14382459c8b7 | 151 | int _handler_count; |
dgriffin65 | 15:14382459c8b7 | 152 | |
dgriffin65 | 15:14382459c8b7 | 153 | static void daemon (); |
dgriffin65 | 15:14382459c8b7 | 154 | static void child (void const *arg); |
dgriffin65 | 15:14382459c8b7 | 155 | static void closer (void const *arg); |
dgriffin65 | 15:14382459c8b7 | 156 | |
dgriffin65 | 15:14382459c8b7 | 157 | // --- HTTPD_req.cpp --- |
dgriffin65 | 15:14382459c8b7 | 158 | int httpdFile (int id, char *dir); |
dgriffin65 | 15:14382459c8b7 | 159 | void recvData (int id, char c); |
dgriffin65 | 15:14382459c8b7 | 160 | int parseRequest (int id); |
dgriffin65 | 15:14382459c8b7 | 161 | int parseHeader (int id); |
dgriffin65 | 15:14382459c8b7 | 162 | void reqContentLength (int id, const char *buf); |
dgriffin65 | 15:14382459c8b7 | 163 | void reqConnection (int id, const char *buf); |
dgriffin65 | 15:14382459c8b7 | 164 | void reqUpgrade (int id, const char *buf); |
dgriffin65 | 15:14382459c8b7 | 165 | void reqWebSocketVersion (int id, const char *buf); |
dgriffin65 | 15:14382459c8b7 | 166 | void reqWebSocketKey (int id, const char *buf); |
dgriffin65 | 15:14382459c8b7 | 167 | |
dgriffin65 | 15:14382459c8b7 | 168 | // --- HTTPD_ws.cpp --- |
dgriffin65 | 15:14382459c8b7 | 169 | void recvWS (int id, char c); |
dgriffin65 | 15:14382459c8b7 | 170 | int parseWebsocket (int id); |
dgriffin65 | 15:14382459c8b7 | 171 | int acceptWebsocket (int id); |
dgriffin65 | 15:14382459c8b7 | 172 | |
dgriffin65 | 15:14382459c8b7 | 173 | // --- HTTPD_util.cpp --- |
dgriffin65 | 15:14382459c8b7 | 174 | int getHandler (const char *uri); |
dgriffin65 | 15:14382459c8b7 | 175 | char *mimetype (char *file); |
dgriffin65 | 15:14382459c8b7 | 176 | int strnicmp (const char *p1, const char *p2, int n); |
dgriffin65 | 15:14382459c8b7 | 177 | int from_hex (int ch); |
dgriffin65 | 15:14382459c8b7 | 178 | int to_hex (int code); |
dgriffin65 | 15:14382459c8b7 | 179 | }; |
dgriffin65 | 15:14382459c8b7 | 180 | |
dgriffin65 | 15:14382459c8b7 | 181 | #endif |