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.
http_server.cpp
00001 /********************************************************************* 00002 * 00003 * Simple HTTP Server library 00004 * 00005 * Mauricio Martins Donatti 00006 * mauricio.donatti@lnls.br 00007 * 00008 * Electronics Instrumentation Group - GIE 00009 * Brazilian Synchrotron Light Laboratory (LNLS) 00010 * Brazilian Center for Research in Energy and Materials (CNPEM) 00011 * 00012 * March 2020 00013 * 00014 *******************************************************************/ 00015 00016 #include "http_server.h" 00017 00018 char c; //aux variable 00019 00020 HttpServer::HttpServer() 00021 { 00022 buffer[0] = '\0'; //constructor 00023 } 00024 00025 HttpServer::~HttpServer() 00026 { 00027 } 00028 bool HttpServer::init(EthernetInterface* eth,char *file,int file_size) 00029 { 00030 HTTP_PRINTF("DEBUG MODE"); 00031 00032 html_file = file; 00033 html_size = file_size; 00034 00035 if(socket.open(eth)==NSAPI_ERROR_OK) 00036 HTTP_PRINTF("Socket Opened\n\r"); 00037 else 00038 HTTP_PRINTF("Error opening socket\n\r"); 00039 00040 if(socket.bind(80)==NSAPI_ERROR_OK) 00041 HTTP_PRINTF("Socket Bind Success\n\r"); 00042 else 00043 HTTP_PRINTF("Bind Error\n\r"); 00044 00045 if(socket.listen(QUEUED_REQUESTS)==NSAPI_ERROR_OK) 00046 HTTP_PRINTF("Socket Listen Success\n\r"); 00047 else 00048 HTTP_PRINTF("Listen Error\n\r"); 00049 00050 return true; 00051 } 00052 00053 bool HttpServer::run() 00054 { 00055 client = socket.accept(&status); 00056 HTTP_PRINTF("After accept\n\r"); 00057 if(status==NSAPI_ERROR_OK) 00058 { 00059 client->set_timeout(WEBSERVER_TIMEOUT_MS); 00060 n_recv = client->recv(buffer,MAX_BUFFER_SIZE); 00061 switch(n_recv) { 00062 case 0: 00063 HTTP_PRINTF("received buffer is empty."); 00064 status_code = 400; 00065 sprintf(reason_phrase,"No Request"); 00066 httpmethod[0] = '\0'; 00067 filepath[0] = '\0'; 00068 http_ver[0] = '\0'; 00069 break; 00070 case -1: 00071 HTTP_PRINTF("failed to read data from client."); 00072 status_code = 500; 00073 sprintf(reason_phrase,"Internal Server Error"); 00074 httpmethod[0] = '\0'; 00075 filepath[0] = '\0'; 00076 http_ver[0] = '\0'; 00077 break; 00078 default: 00079 HTTP_PRINTF("Received Data: %d",strlen(buffer)); 00080 HTTP_PRINTF("-->\r\n"); 00081 HTTP_PRINTF("%.*s[End of Request]",strlen(buffer),buffer); 00082 // get HTTP method, File path, HTTP version 00083 strcpy(httpmethod,strtok(buffer, " ")); 00084 strcpy(filepath,strtok(NULL, " ")); 00085 strcpy(http_ver,strtok(NULL, " ")); 00086 HTTP_PRINTF("httpmethod: %s", httpmethod); 00087 HTTP_PRINTF("file path: %s", filepath); 00088 HTTP_PRINTF("http ver : %s", http_ver); 00089 break; 00090 } 00091 00092 if (httpmethod[0] == '\0') { 00093 buffer[MAX_BUFFER_SIZE - 1] = '\0'; 00094 sprintf(buffer,"%s %d %s\r\nConnection: Close\r\n\r\n", http_ver, status_code, reason_phrase); 00095 HTTP_PRINTF("echo back done."); 00096 } 00097 00098 // Response 00099 if (strcmp(httpmethod,"GET") == 0 ) //GET request - always index.html stoed in index_html 00100 { 00101 HTTP_PRINTF("GET request incomming."); 00102 HTTP_PRINTF("httpmethod: %s", httpmethod); 00103 buffer[MAX_BUFFER_SIZE-1] = '\0'; 00104 status_code = 200; 00105 sprintf(reason_phrase,"OK"); 00106 00107 sprintf(buffer,"%s %d %s\r\nConnection: Close\r\nContent-Type: text/html\r\nKeep-Alive: timeout=15\r\n\r\n", http_ver, status_code, reason_phrase); 00108 client->send(buffer,strlen(buffer)); 00109 client->send(html_file,html_size); 00110 } 00111 if (strcmp(httpmethod,"POST") == 0 ) //POST request - javascript request 00112 { 00113 HTTP_PRINTF("POST request incomming."); 00114 status_code = 200; 00115 sprintf(reason_phrase,"OK"); 00116 00117 sprintf(buffer,"%s %d %s\r\nConnection: Close\r\n\r\n", http_ver, status_code, reason_phrase); 00118 client->send(buffer,strlen(buffer)); 00119 00120 if(strcmp(filepath,"/read_data")==0){ 00121 sprintf(buffer,"0 1.25 2 200e-12 10e9 0 0 1.25 1 100e-12 10e9 0 0 1.25 0.5 50e-12 10e9 0 0 1.25 0.2 20e-12 10e9 0"); 00122 //sprintf(buffer,"%d %e %e %s %s\0",(*CH).range,(*CH).voltage,(*CH).current,full_scale[(*CH).range].c_str(),ranges[(*CH).range].c_str()); 00123 client->send(buffer,strlen(buffer)); 00124 HTTP_PRINTF("Response: %s",buffer); 00125 /* 00126 //Temporary: update current: 00127 (*CH).current = (*CH).current+1e-12; 00128 if((*CH).current > 10e-12) 00129 (*CH).current = -10e-12; */ 00130 } 00131 00132 if(strncmp(filepath,"/range",strlen("/range"))==0) 00133 { 00134 sscanf(filepath,"/range=%c",&c); 00135 if(c>='0'&& c<='4') 00136 { 00137 //(*CH).range = c - '0'; 00138 //set_range((*CH).range); 00139 //(*CH).new_data = 1; 00140 HTTP_PRINTF("Changing range: %d",c - '0'); 00141 } 00142 } 00143 00144 if(strcmp(filepath,"/device_name")==0) 00145 { 00146 sprintf(buffer,"%s",DEVICE_NAME); 00147 client->send(buffer,strlen(buffer)); 00148 } 00149 } 00150 client->close(); 00151 } 00152 else 00153 HTTP_PRINTF("Accept Error: %d\n\r",status); 00154 00155 return 0; 00156 }
Generated on Wed Jul 20 2022 22:28:41 by
1.7.2