![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Add ADC
Dependencies: Adafruit_GFX MbedJSONValue_v102 WIZnetInterface mbed
Fork of WIZwiki-REST-io_v101 by
HTTPServer.cpp@4:0b5a199e91f3, 2016-05-31 (annotated)
- Committer:
- joon874
- Date:
- Tue May 31 10:31:30 2016 +0000
- Revision:
- 4:0b5a199e91f3
- Parent:
- 2:ca36ecca24c2
WIZwiki-REST-io ver.2 ; Add POST Method
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joon874 | 0:5886f525a4ad | 1 | #include <string.h> |
joon874 | 0:5886f525a4ad | 2 | #include "HTTPServer.h" |
joon874 | 0:5886f525a4ad | 3 | #include "MbedJSONValue.h" |
joon874 | 0:5886f525a4ad | 4 | |
joon874 | 0:5886f525a4ad | 5 | extern MbedJSONValue WIZwikiREST; |
joon874 | 0:5886f525a4ad | 6 | |
joon874 | 0:5886f525a4ad | 7 | bool cmp(char* a, char* b) |
joon874 | 0:5886f525a4ad | 8 | { |
joon874 | 0:5886f525a4ad | 9 | return strcmp(a,b) < 0; |
joon874 | 0:5886f525a4ad | 10 | } |
joon874 | 0:5886f525a4ad | 11 | |
joon874 | 0:5886f525a4ad | 12 | HTTPServer::HTTPServer(): |
joon874 | 0:5886f525a4ad | 13 | socket(), |
joon874 | 0:5886f525a4ad | 14 | handlers(&cmp) |
joon874 | 0:5886f525a4ad | 15 | { |
joon874 | 0:5886f525a4ad | 16 | } |
joon874 | 0:5886f525a4ad | 17 | |
joon874 | 0:5886f525a4ad | 18 | HTTPServer::~HTTPServer() |
joon874 | 0:5886f525a4ad | 19 | { |
joon874 | 0:5886f525a4ad | 20 | for(std::map<char*, RequestHandler*>::iterator itor = handlers.begin(); |
joon874 | 0:5886f525a4ad | 21 | itor != handlers.end(); |
joon874 | 0:5886f525a4ad | 22 | ++itor) |
joon874 | 0:5886f525a4ad | 23 | delete itor->second; |
joon874 | 0:5886f525a4ad | 24 | } |
joon874 | 0:5886f525a4ad | 25 | |
joon874 | 0:5886f525a4ad | 26 | bool HTTPServer::init(int port) |
joon874 | 0:5886f525a4ad | 27 | { |
joon874 | 0:5886f525a4ad | 28 | socket.set_blocking(true); |
joon874 | 0:5886f525a4ad | 29 | if(socket.bind(port)) |
joon874 | 0:5886f525a4ad | 30 | { |
joon874 | 0:5886f525a4ad | 31 | printf("Could not bind on port %d.\n", port); |
joon874 | 0:5886f525a4ad | 32 | return false; |
joon874 | 0:5886f525a4ad | 33 | } |
joon874 | 0:5886f525a4ad | 34 | |
joon874 | 0:5886f525a4ad | 35 | if(socket.listen()) |
joon874 | 0:5886f525a4ad | 36 | { |
joon874 | 0:5886f525a4ad | 37 | printf("Could not listen %d\n", port); |
joon874 | 0:5886f525a4ad | 38 | return false; |
joon874 | 0:5886f525a4ad | 39 | } |
joon874 | 0:5886f525a4ad | 40 | |
joon874 | 0:5886f525a4ad | 41 | return true; |
joon874 | 0:5886f525a4ad | 42 | } |
joon874 | 0:5886f525a4ad | 43 | |
joon874 | 0:5886f525a4ad | 44 | void HTTPServer::run() |
joon874 | 0:5886f525a4ad | 45 | { |
joon874 | 0:5886f525a4ad | 46 | TCPSocketConnection c; |
joon874 | 0:5886f525a4ad | 47 | while(true) |
joon874 | 0:5886f525a4ad | 48 | { |
joon874 | 0:5886f525a4ad | 49 | while(socket.accept(c)); |
joon874 | 0:5886f525a4ad | 50 | c.set_blocking(false, 1000); |
joon874 | 0:5886f525a4ad | 51 | while(c.is_connected()) |
joon874 | 0:5886f525a4ad | 52 | { |
joon874 | 0:5886f525a4ad | 53 | |
joon874 | 0:5886f525a4ad | 54 | int n = c.receive_all(HTTPBUF, sizeof(HTTPBUF)-1); |
joon874 | 0:5886f525a4ad | 55 | |
joon874 | 0:5886f525a4ad | 56 | if(n == 0) |
joon874 | 0:5886f525a4ad | 57 | { |
joon874 | 0:5886f525a4ad | 58 | c.close(); |
joon874 | 0:5886f525a4ad | 59 | break; |
joon874 | 0:5886f525a4ad | 60 | } |
joon874 | 0:5886f525a4ad | 61 | else if(n != -1) |
joon874 | 0:5886f525a4ad | 62 | { |
joon874 | 2:ca36ecca24c2 | 63 | #ifdef DEBUG_HTTP |
joon874 | 0:5886f525a4ad | 64 | printf("Received data : %d\r\n",n); |
joon874 | 2:ca36ecca24c2 | 65 | #endif |
joon874 | 0:5886f525a4ad | 66 | |
joon874 | 0:5886f525a4ad | 67 | HTTPBUF[n] = '\0'; |
joon874 | 0:5886f525a4ad | 68 | if(handle_request(HTTPBUF) == HTTP_SUCCESS) |
joon874 | 0:5886f525a4ad | 69 | { |
joon874 | 0:5886f525a4ad | 70 | c.send(rest_result, strlen(rest_result)); |
joon874 | 0:5886f525a4ad | 71 | //c.send((char*)rest_result.c_str(), 159); |
joon874 | 0:5886f525a4ad | 72 | } |
joon874 | 0:5886f525a4ad | 73 | else |
joon874 | 0:5886f525a4ad | 74 | { |
joon874 | 0:5886f525a4ad | 75 | //printf("send fail : %s\r\n",(char*)rest_result.c_str()); |
joon874 | 0:5886f525a4ad | 76 | c.send(rest_result, strlen(rest_result)); |
joon874 | 0:5886f525a4ad | 77 | } |
joon874 | 0:5886f525a4ad | 78 | } |
joon874 | 0:5886f525a4ad | 79 | else |
joon874 | 0:5886f525a4ad | 80 | printf("Error while receiving data\n"); |
joon874 | 0:5886f525a4ad | 81 | |
joon874 | 0:5886f525a4ad | 82 | } |
joon874 | 0:5886f525a4ad | 83 | } |
joon874 | 0:5886f525a4ad | 84 | } |
joon874 | 0:5886f525a4ad | 85 | |
joon874 | 0:5886f525a4ad | 86 | |
joon874 | 0:5886f525a4ad | 87 | HTTP_RESULT HTTPServer::handle_request(char *buffer) |
joon874 | 0:5886f525a4ad | 88 | { |
joon874 | 0:5886f525a4ad | 89 | char *request_type; |
joon874 | 0:5886f525a4ad | 90 | char *request; |
joon874 | 0:5886f525a4ad | 91 | |
joon874 | 0:5886f525a4ad | 92 | //{"Name":"WIZwiki-REST-01","Network":{"IP":"192.168.100.100","SN":"255.255.255.0","GW":"192.168.100.1"},"User":{"Name":"Lawrence","ID":"law","PSWD":"law1234"}} 159 |
joon874 | 0:5886f525a4ad | 93 | if(buffer) |
joon874 | 0:5886f525a4ad | 94 | { |
joon874 | 2:ca36ecca24c2 | 95 | #ifdef DEBUG_HTTP |
joon874 | 0:5886f525a4ad | 96 | // buffer check |
joon874 | 0:5886f525a4ad | 97 | printf("*******************************************************\r\n"); |
joon874 | 0:5886f525a4ad | 98 | printf("buffer=%s\r\n",buffer); |
joon874 | 0:5886f525a4ad | 99 | printf("*******************************************************\r\n"); |
joon874 | 2:ca36ecca24c2 | 100 | #endif |
joon874 | 0:5886f525a4ad | 101 | // type parsing |
joon874 | 0:5886f525a4ad | 102 | request_type = strtok(buffer," \r\n"); |
joon874 | 2:ca36ecca24c2 | 103 | #ifdef DEBUG_HTTP |
joon874 | 0:5886f525a4ad | 104 | printf("Type = %s\r\n", request_type); |
joon874 | 2:ca36ecca24c2 | 105 | #endif |
joon874 | 0:5886f525a4ad | 106 | |
joon874 | 4:0b5a199e91f3 | 107 | if(request_type) |
joon874 | 0:5886f525a4ad | 108 | { |
joon874 | 4:0b5a199e91f3 | 109 | request = strtok(NULL, " \r\n"); // corrested " " -> " /" : /Name -> Name |
joon874 | 0:5886f525a4ad | 110 | if(request) |
joon874 | 0:5886f525a4ad | 111 | { |
joon874 | 2:ca36ecca24c2 | 112 | #ifdef DEBUG_HTTP |
joon874 | 0:5886f525a4ad | 113 | printf("URI = %s\r\n", request); |
joon874 | 2:ca36ecca24c2 | 114 | #endif |
joon874 | 4:0b5a199e91f3 | 115 | } |
joon874 | 4:0b5a199e91f3 | 116 | else |
joon874 | 4:0b5a199e91f3 | 117 | { |
joon874 | 4:0b5a199e91f3 | 118 | strcpy(rest_result, "Invaild URI"); |
joon874 | 2:ca36ecca24c2 | 119 | #ifdef DEBUG_HTTP |
joon874 | 4:0b5a199e91f3 | 120 | printf("%s\r\n",rest_result); |
joon874 | 2:ca36ecca24c2 | 121 | #endif |
joon874 | 4:0b5a199e91f3 | 122 | return HTTP_INVALID_URI; |
joon874 | 0:5886f525a4ad | 123 | } |
joon874 | 0:5886f525a4ad | 124 | } |
joon874 | 0:5886f525a4ad | 125 | } |
joon874 | 0:5886f525a4ad | 126 | |
joon874 | 0:5886f525a4ad | 127 | std::map<char*, RequestHandler*>::iterator itor = handlers.find(request_type); |
joon874 | 0:5886f525a4ad | 128 | if(itor == handlers.end()) |
joon874 | 0:5886f525a4ad | 129 | { |
joon874 | 0:5886f525a4ad | 130 | strcpy(rest_result, "No request handler found for this type of request."); |
joon874 | 0:5886f525a4ad | 131 | return HTTP_INVALID_HANDLE; |
joon874 | 0:5886f525a4ad | 132 | } |
joon874 | 0:5886f525a4ad | 133 | //if(itor != NULL) |
joon874 | 0:5886f525a4ad | 134 | //itor->handle(request, rest_result.c_str()); |
joon874 | 0:5886f525a4ad | 135 | if(itor->second != NULL) |
joon874 | 4:0b5a199e91f3 | 136 | { |
joon874 | 4:0b5a199e91f3 | 137 | char* request_data = 0; |
joon874 | 4:0b5a199e91f3 | 138 | if(!strcmp(request_type,"POST")) |
joon874 | 4:0b5a199e91f3 | 139 | { |
joon874 | 4:0b5a199e91f3 | 140 | request_data = strstr(request+strlen(request)+1, "\r\n\r\n"); |
joon874 | 4:0b5a199e91f3 | 141 | #ifdef DEBUG_HTTP |
joon874 | 4:0b5a199e91f3 | 142 | printf("POST:request_data=%s\r\n",request_data+4); |
joon874 | 4:0b5a199e91f3 | 143 | #endif |
joon874 | 4:0b5a199e91f3 | 144 | } |
joon874 | 4:0b5a199e91f3 | 145 | itor->second->handle(request, request_data, rest_result); |
joon874 | 4:0b5a199e91f3 | 146 | } |
joon874 | 0:5886f525a4ad | 147 | else |
joon874 | 0:5886f525a4ad | 148 | { |
joon874 | 0:5886f525a4ad | 149 | strcpy(rest_result, "Invalid request handler"); |
joon874 | 0:5886f525a4ad | 150 | return HTTP_INVALID_HANDLE; |
joon874 | 0:5886f525a4ad | 151 | } |
joon874 | 0:5886f525a4ad | 152 | return HTTP_SUCCESS; |
joon874 | 0:5886f525a4ad | 153 | } |
joon874 | 0:5886f525a4ad | 154 | |
joon874 | 0:5886f525a4ad | 155 | void HTTPServer::add_request_handler(char *name, RequestHandler* handler) |
joon874 | 0:5886f525a4ad | 156 | { |
joon874 | 0:5886f525a4ad | 157 | handlers[name] = handler; |
joon874 | 0:5886f525a4ad | 158 | } |
joon874 | 0:5886f525a4ad | 159 |