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.
Dependents: httpserversample SIMPLE_WSS
HTTP_SERVER.cpp
00001 #include "HTTP_SERVER.h" 00002 #include "string" 00003 #ifndef DEBUG 00004 //#define DEBUG 00005 #endif 00006 00007 namespace HTTP_SERVER 00008 { 00009 void DEBUG_PRINT_LINE(const char* arg_line) 00010 { 00011 #ifdef DEBUG 00012 printf("(HTTP_SERVER) ") 00013 printf(arg_line); 00014 printf("\r\n"); 00015 #endif 00016 } 00017 template<typename T> 00018 void DEBUG_PRINT_LINE(const char* arg_line, T arg_t) 00019 { 00020 #ifdef DEBUG 00021 printf("(HTTP_SERVER) "); 00022 printf(arg_line, arg_t); 00023 printf("\r\n"); 00024 #endif 00025 } 00026 template<typename T1, typename T2> 00027 void DEBUG_PRINT_LINE(const char* arg_line, T1 arg_t1, T2 arg_t2) 00028 { 00029 #ifdef DEBUG 00030 printf("(HTTP_SERVER) "); 00031 printf(arg_line, arg_t1, arg_t2); 00032 printf("\r\n"); 00033 #endif 00034 } 00035 } 00036 using namespace HTTP_SERVER; 00037 00038 HttpServer::HttpServer() 00039 { 00040 keep_alive = (false); 00041 listening_flag = (false); 00042 req_buf[0] = '\0'; 00043 } 00044 00045 HttpServer::~HttpServer() 00046 { 00047 } 00048 00049 bool HttpServer::init() 00050 { 00051 00052 // Ethernet Initialization 00053 if(eth.init()) { 00054 printf("(HTTP_SERVER) Error!@EthernetInterface::init()\r\n"); 00055 return false; 00056 } 00057 // Ethernet Connecting setup 00058 if(eth.connect()) { 00059 printf("(HTTP_SERVER) Error!@EthernetInterface::connect()\r\n"); 00060 return false; 00061 } else { 00062 printf("(HTTP_SERVER) IP Address is %s\r\n", eth.getIPAddress()); 00063 } 00064 // TCP Socket setup 00065 // To open Server-side PORT 00066 if(tcpsvr.bind(TCP_PORT)< 0) { 00067 printf("(HTTP_SERVER) Error!@TCPSocketServer::bind()\r\n"); 00068 return false; 00069 } else { 00070 printf("(HTTP_SERVER) TCP Server has bounden!\r\n"); 00071 } 00072 // Server start listening Request from a web browser. 00073 if(tcpsvr.listen(1) < 0) { 00074 printf("(HTTP_SERVER) tcp server listen failed.\r\n"); 00075 return false; 00076 } else { 00077 listening_flag = true; 00078 printf("(HTTP_SERVER) tcp server is listening...\r\n"); 00079 } 00080 00081 return true; 00082 } 00083 00084 bool HttpServer::run() 00085 { 00086 DigitalOut led1(LED1); 00087 DigitalOut led2(LED1); 00088 00089 while (listening_flag) { 00090 led1 = true; 00091 // blocking mode (never timeout) 00092 // waiting client connection 00093 printf("(HTTP_SERVER) waiting connection\r\n"); 00094 if(tcpsvr.accept(tcpcon) < 0) { 00095 printf("(HTTP_SERVER) failed to accept connection.\r\n"); 00096 return -1; 00097 } else { 00098 printf("(HTTP_SERVER) connection success!\r\nIP: %s\r\n",tcpcon.get_address()); 00099 led2 = true; 00100 } 00101 // When conected 00102 while(tcpcon.is_connected()) { 00103 printf("(HTTP_SERVER) connected\r\n"); 00104 00105 char buffer[1024] = {0}; 00106 char* httpmethod = NULL; 00107 char* filepath = NULL; 00108 char* http_ver = NULL; 00109 char* header_field_name = NULL; 00110 char* header_field_val = NULL; 00111 00112 // 00113 // Request Analysis 00114 // 00115 DEBUG_PRINT_LINE("DEBUG MODE"); 00116 switch(tcpcon.receive(buffer, 1023)) { 00117 case 0: 00118 DEBUG_PRINT_LINE("recieved buffer is empty."); 00119 msger.setStatusLine(400, "No Request"); 00120 if(msger.setHeaderField("Connection", "Close"))DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger"); 00121 httpmethod = NULL; 00122 filepath = NULL; 00123 http_ver = NULL; 00124 break; 00125 case -1: 00126 DEBUG_PRINT_LINE("failed to read data from client."); 00127 msger.setStatusLine(500, "Internal Server Error"); 00128 if(msger.setHeaderField("Connection", "Close"))DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger"); 00129 httpmethod = NULL; 00130 filepath = NULL; 00131 http_ver = NULL; 00132 break; 00133 default: 00134 DEBUG_PRINT_LINE("Recieved Data: %d",strlen(buffer)); 00135 DEBUG_PRINT_LINE("-->\r\n"); 00136 DEBUG_PRINT_LINE("%.*s[End of Request]",strlen(buffer),buffer); 00137 // get HTTP method, File path, HTTP version 00138 httpmethod = strtok(buffer," "); 00139 filepath = strtok(NULL, " "); 00140 http_ver = strtok(NULL, "\r\n"); 00141 DEBUG_PRINT_LINE("httpmethod: %s", httpmethod); 00142 DEBUG_PRINT_LINE("file path: %s", filepath); 00143 DEBUG_PRINT_LINE("http ver : %s", http_ver); 00144 break; 00145 } 00146 00147 // 00148 // Response 00149 // 00150 if (strcmp(httpmethod,"GET") == 0 ) { 00151 DEBUG_PRINT_LINE("GET request incomming."); 00152 00153 // file calibration 00154 DEBUG_PRINT_LINE("file opening"); 00155 fhandl.open(filepath,"rb"); 00156 if(fhandl.arrival()) { 00157 msger.setStatusLine(200, "OK"); 00158 if(msger.setHeaderField("Content-Length", fhandl.getFileSize())) DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger"); 00159 if(msger.setHeaderField("Connection", "keep-alive")) DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger"); 00160 } else { 00161 if(msger.setStatusLine(404, "NOT FOUND")) DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger"); 00162 if(msger.setHeaderField("Connection", "Close")) DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger"); 00163 DEBUG_PRINT_LINE("NOT FOUND"); 00164 } 00165 if( !strcmp(fhandl.getSuffix(), "htm" ) || 00166 !strcmp(fhandl.getSuffix(), "HTM" ) || 00167 !strcmp(fhandl.getSuffix(), "html") || 00168 !strcmp(fhandl.getSuffix(), "HTML")){ 00169 if(msger.setHeaderField("Content-Type", "text/html")) DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger"); 00170 } else if( !strcmp(fhandl.getSuffix(), "js" )){ 00171 if(msger.setHeaderField("Content-Type", "text/javascript")) DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger"); 00172 } else if ( !strcmp(fhandl.getSuffix(), "ico" )){ 00173 if(msger.setHeaderField("Content-Type", "image/png")) DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger"); 00174 } else if ( !strcmp(fhandl.getSuffix(), "png" ) || 00175 !strcmp(fhandl.getSuffix(), "PNG" )){ 00176 if(msger.setHeaderField("Content-Type", "image/png")) DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger"); 00177 } else if ( !strcmp(fhandl.getSuffix(), "jpg" ) || 00178 !strcmp(fhandl.getSuffix(), "JPG" )){ 00179 if(msger.setHeaderField("Content-Type", "image/jpg")) DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger"); 00180 } else { 00181 msger.setStatusLine(406, "not acceptable"); 00182 } 00183 00184 // Connection timeout field 00185 if(msger.setHeaderField("Keep-Alive", "timeouit=15")) DEBUG_PRINT_LINE("buffer over flow @ ResponseMessenger"); 00186 00187 // send response 00188 msger.sendHTTPResponse(tcpcon, fhandl); 00189 00190 //file close 00191 if( fhandl.close()== 0) 00192 DEBUG_PRINT_LINE("file has closed"); 00193 else if(EOF) 00194 DEBUG_PRINT_LINE("failed to close the file"); 00195 00196 msger.resetHeader(); 00197 DEBUG_PRINT_LINE("echo back done."); 00198 } 00199 if (httpmethod == NULL) { 00200 msger.sendHTTPResponse(tcpcon); 00201 msger.resetHeader(); 00202 DEBUG_PRINT_LINE("echo back done."); 00203 } 00204 printf("(HTTP_SERVER) Response to Request has done\r\n"); 00205 // 00206 // 00207 // 00208 } 00209 printf("(HTTP_SERVER) close connection.\r\ntcp server is listening...\r\n"); 00210 tcpcon.close(); 00211 led2 = false; 00212 } 00213 tcpsvr.close(); 00214 listening_flag = false; 00215 led1 = false; 00216 return 0; 00217 }
Generated on Sun Jul 17 2022 06:54:47 by
1.7.2