Simple HTTP example

Dependencies:   LWIPInterface NetworkSocketAPI events funcptr mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SimpleHTTP.h Source File

SimpleHTTP.h

00001 /*
00002  * Dumb HTTP server
00003  */
00004 #ifndef SIMPLE_HTTP_H
00005 #define SIMPLE_HTTP_H
00006 
00007 #include "FuncPtr.h"
00008 #include "EventLoop.h"
00009 #include "Event.h"
00010 #include "TCPServer.h"
00011 #include <map>
00012 #include <string>
00013 
00014 /** Simple HTTP server
00015  */
00016 class SimpleHTTP {
00017 public:
00018     /** SimpleHTTP server lifetime
00019      */
00020     SimpleHTTP(NetworkStack *stack);
00021     ~SimpleHTTP();
00022 
00023     /** Start serving HTTP requests
00024      */
00025     void start();
00026 
00027     /** Stop serving HTTP requests
00028      */
00029     void stop();
00030 
00031     /** Attach on GET request
00032      */
00033     void get(const char *path, FuncPtr<int(char *, unsigned)> callback);
00034 
00035     template <typename T, typename M>
00036     void get(const char *path, T *obj, M method) {
00037         get(path, FuncPtr<int(char *, unsigned)>(obj, method));
00038     }
00039 
00040     /** Get stats
00041      */
00042     unsigned get_errors();
00043     unsigned get_accepts();
00044     unsigned get_sent();
00045     unsigned get_recv();
00046 
00047 private:
00048     EventLoop _loop;
00049     NetworkStack *_stack;
00050     TCPServer _server;
00051     std::map<std::string, FuncPtr<int(char *, unsigned)> > _get_cbs;
00052 
00053     struct {
00054         unsigned errors;
00055         unsigned accepts;
00056         unsigned sent;
00057         unsigned recv;
00058     } _stats;
00059 
00060     Event<void()> _net_event;
00061     int _handle(char *buffer);
00062     void _net_cb();
00063 };
00064 
00065 #endif