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