more handlers

Dependents:   bandwidth-meter-net mbedRail24v

Fork of Tiny-HTTPD by ban4jp -

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPD.h Source File

HTTPD.h

00001 /* Copyright (C) 2013 Hiroshi Suga, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #ifndef HTTPD_H
00020 #define HTTPD_H
00021 
00022 #include "mbed.h"
00023 #include "TCPSocketServer.h"
00024 #include "CBuffer.h"
00025 
00026 //#define DEBUG
00027 
00028 #define HTTPD_PORT 80
00029 #define HTTPD_MAX_CLIENTS 1
00030 #define HTTPD_KEEPALIVE 0 // keep-alive off
00031 #define HTTPD_TIMEOUT 15000
00032 #define HTTPD_MAX_HANDLES 12
00033  
00034 #define HTTPD_CMD_SIZE 100
00035 #define HTTPD_BUF_SIZE 256
00036 #define HTTPD_STACK_SIZE (1024 * 6)
00037 //#define HTTPD_ENABLE_CLOSER
00038 
00039 //Debug is disabled by default
00040 #if defined(DEBUG) and (!defined(TARGET_LPC11U24))
00041 #define DBG(x, ...) std::printf("[DBG]" x "\r\n", ##__VA_ARGS__);
00042 #define WARN(x, ...) std::printf("[WARN]" x "\r\n", ##__VA_ARGS__);
00043 #define ERR(x, ...) std::printf("[ERR]" x "\r\n", ##__VA_ARGS__);
00044 #define INFO(x, ...) std::printf("[INFO]" x "\r\n", ##__VA_ARGS__);
00045 #else
00046 #define DBG(x, ...)
00047 #define WARN(x, ...)
00048 #define ERR(x, ...)
00049 #define INFO(x, ...)
00050 #endif
00051 
00052 const char* const server_name = "Server: Tiny-HTTPD(mbed)\r\n";
00053 
00054 class HTTPD {
00055 public:
00056 
00057     enum Request {
00058         REQ_HTTPGET,
00059         REQ_HTTPPOST,
00060         REQ_PUT,
00061     };
00062 
00063     enum Mode {
00064         MODE_REQUEST,
00065         MODE_REQSTR,
00066         MODE_HEADER,
00067         MODE_BODY,
00068         MODE_ENTER,
00069         MODE_ERROR,
00070         MODE_WEBSOCKET,
00071         MODE_WEBSOCKET_MASK,
00072         MODE_WEBSOCKET_BODY,
00073         MODE_WEBSOCKET_ENTER,
00074     };
00075 
00076     struct STATE {
00077         //Thread *thread;
00078         TCPSocketConnection *client;
00079         volatile Request req;
00080         volatile Mode mode;
00081         CircBuffer <char>*buf;
00082         char uri[HTTPD_CMD_SIZE];
00083         char *filename;
00084         char *querystring;
00085         int enter;
00086         int length;
00087         int n;
00088         int keepalive;
00089         int websocket;
00090         char *websocket_key;
00091         int websocket_opcode;
00092         int websocket_flg;
00093         char websocket_mask[4];
00094         int websocket_payload;
00095         int (*sendws)(int id, const char *buf, int len, const char *mask);
00096     };
00097 
00098     HTTPD ();
00099     
00100     int start (int port = HTTPD_PORT);
00101     void poll ();
00102 
00103     // --- HTTPD_req.cpp ---
00104     void httpdError (int id, int err);
00105 
00106     // --- HTTPD_ws.cpp ---
00107     static int sendWebsocket (int id, const char *buf, int len, const char *mask = NULL);
00108 
00109     // --- HTTPD_util.cpp ---
00110     char *getUri (int id);
00111     char *getFilename (int id);
00112     char *getQueryString (int id);
00113     int send (int id, const char *body, int len, const char *header = NULL);
00114     int receive (int id, char *buf, int len);
00115     int attach (const char *uri, const char *dir);
00116     int attach (const char *uri, void (*funcCgi)(int id));
00117     int base64encode (const char *input, int length, char *output, int len);
00118     int urlencode (const char *str, char *buf, int len);
00119     int urldecode (const char *str, char *buf, int len);
00120 
00121     static HTTPD * getInstance() {
00122         return _inst;
00123     };
00124 
00125 private :
00126     static HTTPD *_inst;
00127     //Thread *_daemon;
00128     TCPSocketServer _server;
00129 
00130 #ifdef HTTPD_ENABLE_CLOSER
00131     struct STATE _state[HTTPD_MAX_CLIENTS + 1];
00132 #else
00133     struct STATE _state[HTTPD_MAX_CLIENTS];
00134 #endif
00135 
00136     struct HANDLER {
00137         char *uri;
00138         char *dir;
00139         void (*funcCgi)(int id);
00140     } _handler[HTTPD_MAX_HANDLES];
00141 
00142     int _handler_count;
00143 
00144     static void daemon (void const *arg);
00145     static void child (void const *arg);
00146     static void closer (void const *arg);
00147 
00148     // --- HTTPD_req.cpp ---
00149     int httpdFile (int id, char *dir);
00150     void recvData (int id, char c);
00151     int parseRequest (int id);
00152     int parseHeader (int id);
00153     void reqContentLength (int id, const char *buf);
00154     void reqConnection (int id, const char *buf);
00155     void reqUpgrade (int id, const char *buf);
00156     void reqWebSocketVersion (int id, const char *buf);
00157     void reqWebSocketKey (int id, const char *buf);
00158 
00159     // --- HTTPD_ws.cpp ---
00160     void recvWS (int id, char c);
00161     int parseWebsocket (int id);
00162     int acceptWebsocket (int id);
00163 
00164     // --- HTTPD_util.cpp ---
00165     int getHandler (const char *uri);
00166     char *mimetype (char *file);
00167     int strnicmp (const char *p1, const char *p2, int n);
00168     int from_hex (int ch);
00169     int to_hex (int code);
00170 };
00171 
00172 #endif