Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MbedJSONValue WIZnetInterface mbed
HTTPServer.cpp
00001 #include <string.h> 00002 #include "HTTPServer.h" 00003 #include "MbedJSONValue.h" 00004 00005 extern MbedJSONValue WIZwikiREST; 00006 00007 bool cmp(char* a, char* b) 00008 { 00009 return strcmp(a,b) < 0; 00010 } 00011 00012 HTTPServer::HTTPServer(): 00013 socket(), 00014 handlers(&cmp) 00015 { 00016 } 00017 00018 HTTPServer::~HTTPServer() 00019 { 00020 for(std::map<char*, RequestHandler*>::iterator itor = handlers.begin(); 00021 itor != handlers.end(); 00022 ++itor) 00023 delete itor->second; 00024 } 00025 00026 bool HTTPServer::init(int port) 00027 { 00028 socket.set_blocking(true); 00029 if(socket.bind(port)) 00030 { 00031 printf("Could not bind on port %d.\n", port); 00032 return false; 00033 } 00034 00035 if(socket.listen()) 00036 { 00037 printf("Could not listen %d\n", port); 00038 return false; 00039 } 00040 00041 return true; 00042 } 00043 00044 void HTTPServer::run() 00045 { 00046 TCPSocketConnection c; 00047 while(true) 00048 { 00049 while(socket.accept(c)); 00050 c.set_blocking(false, 1000); 00051 while(c.is_connected()) 00052 { 00053 00054 int n = c.receive_all(HTTPBUF, sizeof(HTTPBUF)-1); 00055 00056 if(n == 0) 00057 { 00058 c.close(); 00059 break; 00060 } 00061 else if(n != -1) 00062 { 00063 printf("Received data : %d\r\n",n); 00064 00065 HTTPBUF[n] = '\0'; 00066 if(handle_request(HTTPBUF) == HTTP_SUCCESS) 00067 { 00068 c.send(rest_result, strlen(rest_result)); 00069 //c.send((char*)rest_result.c_str(), 159); 00070 } 00071 else 00072 { 00073 //printf("send fail : %s\r\n",(char*)rest_result.c_str()); 00074 c.send(rest_result, strlen(rest_result)); 00075 } 00076 } 00077 else 00078 printf("Error while receiving data\n"); 00079 00080 } 00081 } 00082 } 00083 00084 00085 HTTP_RESULT HTTPServer::handle_request(char *buffer) 00086 { 00087 char *request_type; 00088 char *request; 00089 00090 //{"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 00091 if(buffer) 00092 { 00093 // buffer check 00094 printf("*******************************************************\r\n"); 00095 printf("buffer=%s\r\n",buffer); 00096 printf("*******************************************************\r\n"); 00097 00098 // type parsing 00099 request_type = strtok(buffer," \r\n"); 00100 printf("Type = %s\r\n", request_type); 00101 00102 if(request_type) 00103 { 00104 request = strtok(NULL, " \r\n"); // corrested " " -> " /" : /Name -> Name 00105 if(request) 00106 { 00107 printf("URI = %s\r\n", request); 00108 } 00109 else 00110 { 00111 strcpy(rest_result, "Invaild URI"); 00112 printf("%s\r\n",rest_result); 00113 return HTTP_INVALID_URI; 00114 } 00115 } 00116 } 00117 00118 std::map<char*, RequestHandler*>::iterator itor = handlers.find(request_type); 00119 if(itor == handlers.end()) 00120 { 00121 strcpy(rest_result, "No request handler found for this type of request."); 00122 return HTTP_INVALID_HANDLE; 00123 } 00124 //if(itor != NULL) 00125 //itor->handle(request, rest_result.c_str()); 00126 if(itor->second != NULL) 00127 itor->second->handle(request, rest_result); 00128 else 00129 { 00130 strcpy(rest_result, "Invalid request handler"); 00131 return HTTP_INVALID_HANDLE; 00132 } 00133 return HTTP_SUCCESS; 00134 } 00135 00136 void HTTPServer::add_request_handler(char *name, RequestHandler* handler) 00137 { 00138 handlers[name] = handler; 00139 } 00140
Generated on Mon Jul 18 2022 15:28:46 by
1.7.2