Mauricio Donatti / HTTP_SERVER

Fork of HTTP_SERVER by Akifumi Takahashi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTP_SERVER.cpp Source File

HTTP_SERVER.cpp

00001 #include "HTTP_SERVER.h"
00002 
00003 #ifndef DEBUG
00004 //#define DEBUG
00005 #endif
00006 
00007 //HTML file - with Javascript
00008 const char *index_html = "<html><head><title>DCM Heaters Driver</title><link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"http://cnpem.br/wp-content/uploads/2018/01/LNLS_Sirius-02-293x300.png\" />\n\
00009 <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js\"></script></head>\n\
00010 <script language=\"javascript\" type=\"text/javascript\">\n\
00011 function loop(){\n\
00012 $.post(\"read_data\", function(data){\n\
00013 if(data.length >0)\n\
00014 {\n\
00015 var str = data;\n\
00016 var res = str.split(\" \");\n\
00017 if(res.length == 97){\n\
00018 var table = '<div id=\"data_table\">'+\n\
00019 '<table width=\"100%\" border=\"1\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#E3EBFB\">'+\n\
00020 '<tr align=\"center\" valign=\"middle\">'+\n\
00021 '<td width=\"50\" height=\"40\"><strong>Channel</font></strong></td>'+\n\
00022 '<td width=\"96\" height=\"40\"><strong>Voltage (V)</font></strong></td>'+\n\
00023 '<td width=\"96\" height=\"40\"><strong>Current (A)</font></strong></td>'+\n\
00024 '<td width=\"96\" height=\"40\"><strong>Control (%)</font></strong></td>'+\n\
00025 '<td width=\"96\" height=\"40\"><strong>Current Limit (A)</font></strong></td>'+\n\
00026 '<td width=\"96\" height=\"40\"><strong>Failure</font></strong></td>'+\n\
00027 '<td width=\"96\" height=\"40\"><strong>Overload</font></strong></td>'+\n\
00028 '<td width=\"96\" height=\"40\"><strong>No Load</font></strong></td>'+\n\
00029 '<td width=\"96\" height=\"40\"><strong>Enable</font></strong></td></tr>';\n\
00030 for(var i=0;i<8;i++){\n\
00031 table = table + '<tr align=\"center\" valign=\"middle\">'+\n\
00032 '<td width=\"50\" height=\"31\"bgcolor=\"#FFFFFF\"><span style=\"font-weight:bold;\">'+(i+1)+'</span></td>'+\n\
00033 '<td width=\"96\" height=\"28\"bgcolor=\"#FFFFFF\"><span style=\"font-weight:bold;\">'+res[i*12]+'</span></td>'+\n\
00034 '<td width=\"96\" height=\"28\"bgcolor=\"#FFFFFF\"><span style=\"font-weight:bold;\">'+res[i*12+1]+'</span></td>'+\n\
00035 '<td width=\"96\" height=\"28\"bgcolor=\"#FFFFFF\"><span style=\"font-weight:bold;\">'+res[i*12+2]+'</span></td>'+\n\
00036 '<td width=\"96\" height=\"28\"bgcolor=\"#FFFFFF\"><span style=\"font-weight:bold;\">'+res[i*12+3]+'</span></td>'+\n\
00037 '<td width=\"96\" height=\"28\"bgcolor=\"'+res[i*12+4]+'\"><span style=\"font-weight:bold;\">'+res[i*12+5]+'</span></td>'+\n\
00038 '<td width=\"96\" height=\"28\"bgcolor=\"'+res[i*12+6]+'\"><span style=\"font-weight:bold;\">'+res[i*12+7]+'</span></td>'+\n\
00039 '<td width=\"96\" height=\"28\"bgcolor=\"'+res[i*12+8]+'\"><span style=\"font-weight:bold;\">'+res[i*12+9]+'</span></td>'+\n\
00040 '<td width=\"96\" height=\"28\"bgcolor=\"'+res[i*12+10]+'\"><span style=\"font-weight:bold;\">'+res[i*12+11]+'</span></td>'+\n\
00041 '</tr>';\n\
00042 }\n\
00043 table = table + '</table></div>';\n\
00044 document.getElementById(\"data_table\").innerHTML = table;\n\
00045 }}\n\
00046 });\n\
00047 setTimeout(function(){ loop() }, 2000);\n\
00048 }\n\
00049 </script>\n\
00050 <body onLoad=\"loop()\">\n\
00051 <table width=\"100%\"border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"font-weight:normal;\">\n\
00052 <tr>\n\
00053 <td width=\"25%\" height=\"142\" align=\"center\" valign=\"middle\" style=\"border-bottom: #000000 solid 1px;\">\n\
00054 <a href=\"http://www.lnls.cnpem.br/\"><img src=\"http://lnls.cnpem.br/wp-content/themes/lnls-v2/assets/images/logo-header.svg\" alt=\"LNLS\" width=\"305\" height=\"142\" /></a></td>\n\
00055 <td colspan=\"5\" height=\"142\" align=\"center\" scope=\"col\" style=\"border-bottom: #000000 solid 1px;\"><p><span class=\"style7\">DCM HEATERS DRIVER<br /><br/>\n\
00056 <strong>DIAGNOSTICS<br/></strong></span></p>\n\
00057 <p><span class=\"style7\"><strong><em> <a href=\"http://www.lnls.cnpem.br\">LNLS</a></em></strong></span></p></td>\n\
00058 <td width=\"25%\" height=\"142\" align=\"center\" style=\"border-bottom: #000000 solid 1px;\">\n\
00059 <a href=\"http://www.lnls.cnpem.br/grupos/gae/\"><img src=\"http://www.gae.agency/wp-content/uploads/2016/03/Logo_gae_hp_blue.png\" alt=\"GAE\" width=\"305\" height=\"99\" /></a></td>\n\
00060 </tr>\n\
00061 </table>\n<div id=\"data_table\"></div></body></html>\0";
00062 
00063 //table colors vector
00064 const char *color[] = {"#C8FFC8\0","#FF0000\0"};
00065 
00066 int index_html_len; //index with html length
00067 
00068 char buffer[MAX_BUFFER_SIZE];   //receive and transmit buffer
00069 char tmp_buffer[200];           //aux buffer
00070 int status_code;                //http status code
00071 char reason_phrase[30];         //http reason phrase
00072 
00073 char httpmethod[20];            //http method
00074 char filepath[20];              //file requested
00075 char http_ver[20];              //http version
00076 
00077 int idx_buffer;                 //index buffer
00078 
00079 //Debug functions
00080 namespace HTTP_SERVER
00081 {
00082 void DEBUG_PRINT_LINE(const char* arg_line)
00083 {
00084 #ifdef DEBUG
00085     printf("(HTTP_SERVER) ")
00086     printf(arg_line);
00087     printf("\r\n");
00088 #endif
00089 }
00090 template<typename T>
00091 void DEBUG_PRINT_LINE(const char* arg_line, T arg_t)
00092 {
00093 #ifdef DEBUG
00094     printf("(HTTP_SERVER) ");
00095     printf(arg_line, arg_t);
00096     printf("\r\n");
00097 #endif
00098 }
00099 template<typename T1, typename T2>
00100 void DEBUG_PRINT_LINE(const char* arg_line, T1 arg_t1, T2 arg_t2)
00101 {
00102 #ifdef DEBUG
00103     printf("(HTTP_SERVER) ");
00104     printf(arg_line, arg_t1, arg_t2);
00105     printf("\r\n");
00106 #endif
00107 }
00108 }
00109 using namespace HTTP_SERVER;
00110 
00111 HttpServer::HttpServer()
00112 {
00113     buffer[0] = '\0';   //constructor
00114 }
00115 
00116 HttpServer::~HttpServer()
00117 {
00118 }
00119 bool HttpServer::init()
00120 {
00121     //  TCP Socket setup
00122     //  To open Server-side PORT
00123     if(tcpsvr.bind(TCP_PORT)< 0) {
00124         return false;
00125     } 
00126     tcpsvr.set_blocking(true,1500); //set blocking socket
00127 
00128     //  Server start listening Request from a web browser.
00129     
00130     if(tcpsvr.listen(5) < 0) {
00131         return false;
00132     }
00133     
00134     index_html_len = strlen(index_html);    //calculate string length
00135 
00136     return true;
00137 }
00138 
00139 bool HttpServer::run(channel *CH)
00140 {            
00141     if(tcpsvr.accept(tcpcon) < 0) {
00142         //printf("(HTTP_SERVER) failed to accept connection.\r\n");
00143         return -1;
00144     }
00145     //  When conected
00146     while(tcpcon.is_connected()) 
00147     {
00148         tcpcon.set_blocking(false,100);
00149         //
00150         //  Request Analysis
00151         //
00152         
00153         DEBUG_PRINT_LINE("DEBUG MODE");
00154         switch(tcpcon.receive(buffer, 1023)) {
00155             case 0:
00156                 //DEBUG_PRINT_LINE("received buffer is empty.");
00157                 status_code = 400;
00158                 sprintf(reason_phrase,"No Request\0");
00159                 httpmethod[0]    = '\0';
00160                 filepath[0]      = '\0';
00161                 http_ver[0]      = '\0';
00162                 break;  
00163             case -1:
00164                 DEBUG_PRINT_LINE("failed to read data from client.");
00165                 status_code = 500;
00166                 sprintf(reason_phrase,"Internal Server Error\0");
00167                 httpmethod[0]    = '\0';
00168                 filepath[0]      = '\0';
00169                 http_ver[0]      = '\0';
00170                 break;
00171             default:
00172                 DEBUG_PRINT_LINE("Received Data: %d",strlen(buffer));
00173                 DEBUG_PRINT_LINE("-->\r\n");
00174                 DEBUG_PRINT_LINE("%.*s[End of Request]",strlen(buffer),buffer);
00175                 //  get HTTP method, File path, HTTP version
00176                 sprintf(httpmethod,strtok(buffer," "));
00177                 filepath[0]      = '\0';
00178                 sprintf(http_ver,"HTTP/1.1\0");
00179                 DEBUG_PRINT_LINE("httpmethod: %s", httpmethod);
00180                 DEBUG_PRINT_LINE("file path:  %s", filepath);
00181                 DEBUG_PRINT_LINE("http ver :  %s", http_ver);
00182                 break;
00183         }
00184         
00185         if (httpmethod[0] == '\0') {
00186             buffer[MAX_BUFFER_SIZE - 1] = '\0';
00187             sprintf(buffer,"%s %d %s\r\nConnection: Close\r\n\r\n\0", http_ver, status_code, reason_phrase);
00188             DEBUG_PRINT_LINE("echo back done.");
00189             break;
00190         }
00191         
00192         //  Response
00193         if (strcmp(httpmethod,"GET") == 0 ) //GET request - always index.html stoed in index_html
00194         {
00195             DEBUG_PRINT_LINE("GET request incomming.");
00196             
00197             buffer[MAX_BUFFER_SIZE-1] = '\0';
00198             status_code = 200;
00199             sprintf(reason_phrase,"OK\0");
00200 
00201             sprintf(buffer,"%s %d %s\r\nConnection: Close\r\nContent-Type: text/html\r\nKeep-Alive: timeout=15\r\n\r\n\0", http_ver, status_code, reason_phrase);
00202             tcpcon.send_all(buffer,strlen(buffer));
00203             tcpcon.send_all((char*)index_html,index_html_len);
00204             
00205             break;
00206         } 
00207         if (strcmp(httpmethod,"POST") == 0 ) //POST request - javascript request
00208         {
00209             DEBUG_PRINT_LINE("POST request incomming.");
00210             status_code = 200;
00211             sprintf(reason_phrase,"OK\0");
00212 
00213             sprintf(buffer,"%s %d %s\r\nConnection: Close\r\n\r\n\0", http_ver, status_code, reason_phrase);
00214 
00215             for(idx_buffer=0;idx_buffer<=7;idx_buffer++)
00216             {
00217                 sprintf(tmp_buffer,"%4.2f %3.2f %d %3.2f %s %d %s %d %s %d %s %d \0",CH[idx_buffer].voltage,CH[idx_buffer].current,CH[idx_buffer].control,CH[idx_buffer].limit,\
00218                     color[CH[idx_buffer].failure>0],CH[idx_buffer].failure,\
00219                     color[CH[idx_buffer].overload>ERROR_REP],CH[idx_buffer].overload>ERROR_REP,\
00220                     color[CH[idx_buffer].noload>ERROR_REP],CH[idx_buffer].noload>ERROR_REP,\
00221                     color[CH[idx_buffer].enable==0],CH[idx_buffer].enable);
00222                 strcat(buffer,tmp_buffer);
00223 
00224             }
00225             tcpcon.send_all(buffer,strlen(buffer));
00226             break;
00227             
00228 
00229         }      
00230     }
00231     tcpcon.close(); //always close the connection
00232     return 0;
00233 }