Server that executes RPC commands through HTTP.

Dependencies:   EthernetInterface mbed-rpc mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPServer.cpp Source File

HTTPServer.cpp

00001 #include "HTTPServer.h"
00002 
00003 #define INVALID_FORMATTER "No valid formatter specified"
00004 
00005 bool cmp(char* a, char* b)
00006 {
00007     return strcmp(a,b) < 0;
00008 }
00009 
00010 HTTPServer::HTTPServer(Formatter *f):
00011 socket(),
00012 handlers(&cmp),
00013 formatter(f),
00014 reply(),
00015 command()
00016 {
00017 }
00018 
00019 HTTPServer::~HTTPServer()
00020 {
00021     for(std::map<char*, RequestHandler*>::iterator itor = handlers.begin();
00022         itor != handlers.end();
00023         ++itor)
00024         delete itor->second;
00025     
00026     if(formatter)
00027         delete formatter;
00028 }
00029 
00030 bool HTTPServer::init(int port)
00031 {
00032     socket.set_blocking(true);
00033     if(socket.bind(port))
00034     {
00035         printf("Could not bind on port %d.\n", port);
00036         return false; 
00037     }
00038     
00039     if(socket.listen())
00040     {
00041         printf("Could not listen %d\n", port);
00042         return false;
00043     }
00044     
00045     return true;
00046 }
00047 
00048 void HTTPServer::run()
00049 {
00050     TCPSocketConnection c;
00051     while(true)
00052     {
00053         while(socket.accept(c));
00054         c.set_blocking(false, 1000);
00055         while(c.is_connected())
00056         {
00057             char buffer[512];
00058             int n = c.receive_all(buffer, sizeof(buffer)-1);
00059             if(n == 0)
00060             {
00061                 c.close();
00062                 break;
00063             }
00064             else if(n != -1)
00065             {
00066                 printf("Received data\n");
00067                 buffer[n] = '\0';
00068                 handle_request(buffer);
00069                 if(formatter != NULL)
00070                 {
00071                     printf("Sending data...");
00072                     char *page = formatter->get_page(reply);
00073                     do
00074                     {
00075                         c.send(page, strlen(page)+1);
00076                         page = formatter->get_page(reply);
00077                     }while(strlen(page)>0);
00078                     printf("done\n");
00079                 }
00080                 else
00081                     c.send(INVALID_FORMATTER, strlen(INVALID_FORMATTER)+1);
00082             }
00083             else
00084                 printf("Error while receiving data\n");
00085         }
00086     }
00087 }
00088 
00089 void HTTPServer::handle_request(char *buffer)
00090 {
00091     char *request_type = strtok(buffer, " ");
00092     char *request = strtok(NULL, " ");
00093 
00094     reply[0] = '\0';
00095     if(!strcmp(request, "/"))
00096         return;    
00097     
00098     if(!command.decode(request))
00099     {
00100         strcat(reply, "Malformed request");
00101         return;
00102     }
00103     
00104     std::map<char*, RequestHandler*>::iterator itor = handlers.find(request_type);
00105     if(itor == handlers.end())
00106     {
00107         strcat(reply, "No request handler found for this type of request.");
00108         return;
00109     }
00110     if(itor->second != NULL)
00111         itor->second->handle(command, reply);
00112     else
00113         strcat(reply, "Invalid request handler");
00114 }
00115 
00116 void HTTPServer::add_request_handler(char *name, RequestHandler* handler)
00117 {
00118     handlers[name] = handler;
00119 }
00120