Simple HTTP example

Dependencies:   LWIPInterface NetworkSocketAPI events funcptr mbed-rtos mbed

Committer:
Christopher Haster
Date:
Thu Apr 21 18:36:07 2016 -0500
Revision:
0:9dd004b6a8b7
Child:
1:ca2041d3a4a6
Initial commit of simple HTTP server

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 0:9dd004b6a8b7 1 /*
Christopher Haster 0:9dd004b6a8b7 2 * Dumb HTTP server
Christopher Haster 0:9dd004b6a8b7 3 */
Christopher Haster 0:9dd004b6a8b7 4 #ifndef SIMPLE_HTTP_H
Christopher Haster 0:9dd004b6a8b7 5 #define SIMPLE_HTTP_H
Christopher Haster 0:9dd004b6a8b7 6
Christopher Haster 0:9dd004b6a8b7 7 #include "FuncPtr.h"
Christopher Haster 0:9dd004b6a8b7 8 #include "Event.h"
Christopher Haster 0:9dd004b6a8b7 9 #include "TCPServer.h"
Christopher Haster 0:9dd004b6a8b7 10 #include <map>
Christopher Haster 0:9dd004b6a8b7 11 #include <string>
Christopher Haster 0:9dd004b6a8b7 12
Christopher Haster 0:9dd004b6a8b7 13 /** Simple HTTP server
Christopher Haster 0:9dd004b6a8b7 14 */
Christopher Haster 0:9dd004b6a8b7 15 class SimpleHTTP {
Christopher Haster 0:9dd004b6a8b7 16 public:
Christopher Haster 0:9dd004b6a8b7 17 /** SimpleHTTP server lifetime
Christopher Haster 0:9dd004b6a8b7 18 */
Christopher Haster 0:9dd004b6a8b7 19 SimpleHTTP(EventQueue *queue, NetworkStack *stack);
Christopher Haster 0:9dd004b6a8b7 20 ~SimpleHTTP();
Christopher Haster 0:9dd004b6a8b7 21
Christopher Haster 0:9dd004b6a8b7 22 /** Start serving HTTP requests
Christopher Haster 0:9dd004b6a8b7 23 */
Christopher Haster 0:9dd004b6a8b7 24 void start();
Christopher Haster 0:9dd004b6a8b7 25
Christopher Haster 0:9dd004b6a8b7 26 /** Stop serving HTTP requests
Christopher Haster 0:9dd004b6a8b7 27 */
Christopher Haster 0:9dd004b6a8b7 28 void stop();
Christopher Haster 0:9dd004b6a8b7 29
Christopher Haster 0:9dd004b6a8b7 30 /** Attach on GET request
Christopher Haster 0:9dd004b6a8b7 31 */
Christopher Haster 0:9dd004b6a8b7 32 void get(const char *path, FuncPtr<int(char *, unsigned)> callback);
Christopher Haster 0:9dd004b6a8b7 33
Christopher Haster 0:9dd004b6a8b7 34 template <typename T, typename M>
Christopher Haster 0:9dd004b6a8b7 35 void get(const char *path, T *obj, M method) {
Christopher Haster 0:9dd004b6a8b7 36 get(path, FuncPtr<int(char *, unsigned)>(obj, method));
Christopher Haster 0:9dd004b6a8b7 37 }
Christopher Haster 0:9dd004b6a8b7 38
Christopher Haster 0:9dd004b6a8b7 39 /** Get stats
Christopher Haster 0:9dd004b6a8b7 40 */
Christopher Haster 0:9dd004b6a8b7 41 unsigned get_errors();
Christopher Haster 0:9dd004b6a8b7 42 unsigned get_accepts();
Christopher Haster 0:9dd004b6a8b7 43 unsigned get_sent();
Christopher Haster 0:9dd004b6a8b7 44 unsigned get_recv();
Christopher Haster 0:9dd004b6a8b7 45
Christopher Haster 0:9dd004b6a8b7 46 private:
Christopher Haster 0:9dd004b6a8b7 47 EventQueue *_queue;
Christopher Haster 0:9dd004b6a8b7 48 NetworkStack *_stack;
Christopher Haster 0:9dd004b6a8b7 49 TCPServer _server;
Christopher Haster 0:9dd004b6a8b7 50 std::map<std::string, FuncPtr<int(char *, unsigned)> > _get_cbs;
Christopher Haster 0:9dd004b6a8b7 51
Christopher Haster 0:9dd004b6a8b7 52 struct {
Christopher Haster 0:9dd004b6a8b7 53 unsigned errors;
Christopher Haster 0:9dd004b6a8b7 54 unsigned accepts;
Christopher Haster 0:9dd004b6a8b7 55 unsigned sent;
Christopher Haster 0:9dd004b6a8b7 56 unsigned recv;
Christopher Haster 0:9dd004b6a8b7 57 } _stats;
Christopher Haster 0:9dd004b6a8b7 58
Christopher Haster 0:9dd004b6a8b7 59 Event<void()> _net_event;
Christopher Haster 0:9dd004b6a8b7 60 int _handle(char *buffer);
Christopher Haster 0:9dd004b6a8b7 61 void _net_cb();
Christopher Haster 0:9dd004b6a8b7 62 };
Christopher Haster 0:9dd004b6a8b7 63
Christopher Haster 0:9dd004b6a8b7 64 #endif