Webserver only w/o any other functions, single thread. Running on STM32F013+W5500

Dependencies:   NTPClient W5500Interface Watchdog device_configuration eeprom_flash mbed-rpc-nucleo mbed-rtos mbed

Fork of F103-Serial-to-Ethernet by Chau Vo

Committer:
olympux
Date:
Fri Aug 19 20:17:00 2016 +0000
Revision:
47:d92d2c5b8073
Parent:
40:c966abbe2d62
forked to create a new repo for webserver on F103+W5500. No other functions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
olympux 40:c966abbe2d62 1 #include "HTTPServer.h"
olympux 40:c966abbe2d62 2
olympux 40:c966abbe2d62 3 #define INVALID_FORMATTER "No valid formatter specified"
olympux 40:c966abbe2d62 4
olympux 40:c966abbe2d62 5 bool cmp(char* a, char* b)
olympux 40:c966abbe2d62 6 {
olympux 40:c966abbe2d62 7 return strcmp(a,b) < 0;
olympux 40:c966abbe2d62 8 }
olympux 40:c966abbe2d62 9
olympux 40:c966abbe2d62 10 HTTPServer::HTTPServer(Formatter *f):
olympux 40:c966abbe2d62 11 socket(),
olympux 40:c966abbe2d62 12 handlers(&cmp),
olympux 40:c966abbe2d62 13 formatter(f),
olympux 40:c966abbe2d62 14 reply(),
olympux 40:c966abbe2d62 15 command()
olympux 40:c966abbe2d62 16 {
olympux 40:c966abbe2d62 17 }
olympux 40:c966abbe2d62 18
olympux 40:c966abbe2d62 19 HTTPServer::~HTTPServer()
olympux 40:c966abbe2d62 20 {
olympux 40:c966abbe2d62 21 for(std::map<char*, RequestHandler*>::iterator itor = handlers.begin();
olympux 40:c966abbe2d62 22 itor != handlers.end();
olympux 40:c966abbe2d62 23 ++itor)
olympux 40:c966abbe2d62 24 delete itor->second;
olympux 40:c966abbe2d62 25
olympux 40:c966abbe2d62 26 if(formatter)
olympux 40:c966abbe2d62 27 delete formatter;
olympux 40:c966abbe2d62 28 }
olympux 40:c966abbe2d62 29
olympux 40:c966abbe2d62 30 bool HTTPServer::init(int port)
olympux 40:c966abbe2d62 31 {
olympux 40:c966abbe2d62 32 socket.set_blocking(true);
olympux 40:c966abbe2d62 33 if(socket.bind(port))
olympux 40:c966abbe2d62 34 {
olympux 40:c966abbe2d62 35 printf("Could not bind on port %d.\n", port);
olympux 40:c966abbe2d62 36 return false;
olympux 40:c966abbe2d62 37 }
olympux 40:c966abbe2d62 38
olympux 40:c966abbe2d62 39 if(socket.listen())
olympux 40:c966abbe2d62 40 {
olympux 40:c966abbe2d62 41 printf("Could not listen %d\n", port);
olympux 40:c966abbe2d62 42 return false;
olympux 40:c966abbe2d62 43 }
olympux 40:c966abbe2d62 44
olympux 40:c966abbe2d62 45 return true;
olympux 40:c966abbe2d62 46 }
olympux 40:c966abbe2d62 47
olympux 40:c966abbe2d62 48 void HTTPServer::run()
olympux 40:c966abbe2d62 49 {
olympux 40:c966abbe2d62 50 TCPSocketConnection c;
olympux 40:c966abbe2d62 51 while(true)
olympux 40:c966abbe2d62 52 {
olympux 40:c966abbe2d62 53 while(socket.accept(c));
olympux 40:c966abbe2d62 54 c.set_blocking(false, 1000);
olympux 40:c966abbe2d62 55 while(c.is_connected())
olympux 40:c966abbe2d62 56 {
olympux 40:c966abbe2d62 57 char buffer[512];
olympux 40:c966abbe2d62 58 int n = c.receive_all(buffer, sizeof(buffer)-1);
olympux 40:c966abbe2d62 59 if(n == 0)
olympux 40:c966abbe2d62 60 {
olympux 40:c966abbe2d62 61 c.close();
olympux 40:c966abbe2d62 62 break;
olympux 40:c966abbe2d62 63 }
olympux 40:c966abbe2d62 64 else if(n != -1)
olympux 40:c966abbe2d62 65 {
olympux 40:c966abbe2d62 66 printf("Received data\n");
olympux 40:c966abbe2d62 67 buffer[n] = '\0';
olympux 40:c966abbe2d62 68 handle_request(buffer);
olympux 40:c966abbe2d62 69 if(formatter != NULL)
olympux 40:c966abbe2d62 70 {
olympux 40:c966abbe2d62 71 printf("Sending data...");
olympux 40:c966abbe2d62 72 char *page = formatter->get_page(reply);
olympux 40:c966abbe2d62 73 do
olympux 40:c966abbe2d62 74 {
olympux 40:c966abbe2d62 75 c.send(page, strlen(page)+1);
olympux 40:c966abbe2d62 76 page = formatter->get_page(reply);
olympux 40:c966abbe2d62 77 }while(strlen(page)>0);
olympux 40:c966abbe2d62 78 printf("done\n");
olympux 40:c966abbe2d62 79 }
olympux 40:c966abbe2d62 80 else
olympux 40:c966abbe2d62 81 c.send(INVALID_FORMATTER, strlen(INVALID_FORMATTER)+1);
olympux 40:c966abbe2d62 82 }
olympux 40:c966abbe2d62 83 else
olympux 40:c966abbe2d62 84 printf("Error while receiving data\n");
olympux 40:c966abbe2d62 85 }
olympux 40:c966abbe2d62 86 }
olympux 40:c966abbe2d62 87 }
olympux 40:c966abbe2d62 88
olympux 40:c966abbe2d62 89 void HTTPServer::handle_request(char *buffer)
olympux 40:c966abbe2d62 90 {
olympux 40:c966abbe2d62 91 char *request_type = strtok(buffer, " ");
olympux 40:c966abbe2d62 92 char *request = strtok(NULL, " ");
olympux 40:c966abbe2d62 93
olympux 40:c966abbe2d62 94 reply[0] = '\0';
olympux 40:c966abbe2d62 95 if(!strcmp(request, "/"))
olympux 40:c966abbe2d62 96 return;
olympux 40:c966abbe2d62 97
olympux 40:c966abbe2d62 98 if(!command.decode(request))
olympux 40:c966abbe2d62 99 {
olympux 40:c966abbe2d62 100 strcat(reply, "Malformed request");
olympux 40:c966abbe2d62 101 return;
olympux 40:c966abbe2d62 102 }
olympux 40:c966abbe2d62 103
olympux 40:c966abbe2d62 104 std::map<char*, RequestHandler*>::iterator itor = handlers.find(request_type);
olympux 40:c966abbe2d62 105 if(itor == handlers.end())
olympux 40:c966abbe2d62 106 {
olympux 40:c966abbe2d62 107 strcat(reply, "No request handler found for this type of request.");
olympux 40:c966abbe2d62 108 return;
olympux 40:c966abbe2d62 109 }
olympux 40:c966abbe2d62 110 if(itor->second != NULL)
olympux 40:c966abbe2d62 111 itor->second->handle(command, reply);
olympux 40:c966abbe2d62 112 else
olympux 40:c966abbe2d62 113 strcat(reply, "Invalid request handler");
olympux 40:c966abbe2d62 114 }
olympux 40:c966abbe2d62 115
olympux 40:c966abbe2d62 116 void HTTPServer::add_request_handler(char *name, RequestHandler* handler)
olympux 40:c966abbe2d62 117 {
olympux 40:c966abbe2d62 118 handlers[name] = handler;
olympux 40:c966abbe2d62 119 }
olympux 40:c966abbe2d62 120