http server library

Dependents:   Socket-Server-TCP

Committer:
lucastanio
Date:
Thu Jul 30 17:32:57 2020 +0000
Revision:
0:e6957f354668
Http server library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lucastanio 0:e6957f354668 1 /*********************************************************************
lucastanio 0:e6957f354668 2 *
lucastanio 0:e6957f354668 3 * Simple HTTP Server library
lucastanio 0:e6957f354668 4 *
lucastanio 0:e6957f354668 5 * Mauricio Martins Donatti
lucastanio 0:e6957f354668 6 * mauricio.donatti@lnls.br
lucastanio 0:e6957f354668 7 *
lucastanio 0:e6957f354668 8 * Electronics Instrumentation Group - GIE
lucastanio 0:e6957f354668 9 * Brazilian Synchrotron Light Laboratory (LNLS)
lucastanio 0:e6957f354668 10 * Brazilian Center for Research in Energy and Materials (CNPEM)
lucastanio 0:e6957f354668 11 *
lucastanio 0:e6957f354668 12 * March 2020
lucastanio 0:e6957f354668 13 *
lucastanio 0:e6957f354668 14 *******************************************************************/
lucastanio 0:e6957f354668 15
lucastanio 0:e6957f354668 16 #include "http_server.h"
lucastanio 0:e6957f354668 17
lucastanio 0:e6957f354668 18 char c; //aux variable
lucastanio 0:e6957f354668 19
lucastanio 0:e6957f354668 20 HttpServer::HttpServer()
lucastanio 0:e6957f354668 21 {
lucastanio 0:e6957f354668 22 buffer[0] = '\0'; //constructor
lucastanio 0:e6957f354668 23 }
lucastanio 0:e6957f354668 24
lucastanio 0:e6957f354668 25 HttpServer::~HttpServer()
lucastanio 0:e6957f354668 26 {
lucastanio 0:e6957f354668 27 }
lucastanio 0:e6957f354668 28 bool HttpServer::init(EthernetInterface* eth,char *file,int file_size)
lucastanio 0:e6957f354668 29 {
lucastanio 0:e6957f354668 30 HTTP_PRINTF("DEBUG MODE");
lucastanio 0:e6957f354668 31
lucastanio 0:e6957f354668 32 html_file = file;
lucastanio 0:e6957f354668 33 html_size = file_size;
lucastanio 0:e6957f354668 34
lucastanio 0:e6957f354668 35 if(socket.open(eth)==NSAPI_ERROR_OK)
lucastanio 0:e6957f354668 36 HTTP_PRINTF("Socket Opened\n\r");
lucastanio 0:e6957f354668 37 else
lucastanio 0:e6957f354668 38 HTTP_PRINTF("Error opening socket\n\r");
lucastanio 0:e6957f354668 39
lucastanio 0:e6957f354668 40 if(socket.bind(80)==NSAPI_ERROR_OK)
lucastanio 0:e6957f354668 41 HTTP_PRINTF("Socket Bind Success\n\r");
lucastanio 0:e6957f354668 42 else
lucastanio 0:e6957f354668 43 HTTP_PRINTF("Bind Error\n\r");
lucastanio 0:e6957f354668 44
lucastanio 0:e6957f354668 45 if(socket.listen(QUEUED_REQUESTS)==NSAPI_ERROR_OK)
lucastanio 0:e6957f354668 46 HTTP_PRINTF("Socket Listen Success\n\r");
lucastanio 0:e6957f354668 47 else
lucastanio 0:e6957f354668 48 HTTP_PRINTF("Listen Error\n\r");
lucastanio 0:e6957f354668 49
lucastanio 0:e6957f354668 50 return true;
lucastanio 0:e6957f354668 51 }
lucastanio 0:e6957f354668 52
lucastanio 0:e6957f354668 53 bool HttpServer::run()
lucastanio 0:e6957f354668 54 {
lucastanio 0:e6957f354668 55 client = socket.accept(&status);
lucastanio 0:e6957f354668 56 HTTP_PRINTF("After accept\n\r");
lucastanio 0:e6957f354668 57 if(status==NSAPI_ERROR_OK)
lucastanio 0:e6957f354668 58 {
lucastanio 0:e6957f354668 59 client->set_timeout(WEBSERVER_TIMEOUT_MS);
lucastanio 0:e6957f354668 60 n_recv = client->recv(buffer,MAX_BUFFER_SIZE);
lucastanio 0:e6957f354668 61 switch(n_recv) {
lucastanio 0:e6957f354668 62 case 0:
lucastanio 0:e6957f354668 63 HTTP_PRINTF("received buffer is empty.");
lucastanio 0:e6957f354668 64 status_code = 400;
lucastanio 0:e6957f354668 65 sprintf(reason_phrase,"No Request");
lucastanio 0:e6957f354668 66 httpmethod[0] = '\0';
lucastanio 0:e6957f354668 67 filepath[0] = '\0';
lucastanio 0:e6957f354668 68 http_ver[0] = '\0';
lucastanio 0:e6957f354668 69 break;
lucastanio 0:e6957f354668 70 case -1:
lucastanio 0:e6957f354668 71 HTTP_PRINTF("failed to read data from client.");
lucastanio 0:e6957f354668 72 status_code = 500;
lucastanio 0:e6957f354668 73 sprintf(reason_phrase,"Internal Server Error");
lucastanio 0:e6957f354668 74 httpmethod[0] = '\0';
lucastanio 0:e6957f354668 75 filepath[0] = '\0';
lucastanio 0:e6957f354668 76 http_ver[0] = '\0';
lucastanio 0:e6957f354668 77 break;
lucastanio 0:e6957f354668 78 default:
lucastanio 0:e6957f354668 79 HTTP_PRINTF("Received Data: %d",strlen(buffer));
lucastanio 0:e6957f354668 80 HTTP_PRINTF("-->\r\n");
lucastanio 0:e6957f354668 81 HTTP_PRINTF("%.*s[End of Request]",strlen(buffer),buffer);
lucastanio 0:e6957f354668 82 // get HTTP method, File path, HTTP version
lucastanio 0:e6957f354668 83 strcpy(httpmethod,strtok(buffer, " "));
lucastanio 0:e6957f354668 84 strcpy(filepath,strtok(NULL, " "));
lucastanio 0:e6957f354668 85 strcpy(http_ver,strtok(NULL, " "));
lucastanio 0:e6957f354668 86 HTTP_PRINTF("httpmethod: %s", httpmethod);
lucastanio 0:e6957f354668 87 HTTP_PRINTF("file path: %s", filepath);
lucastanio 0:e6957f354668 88 HTTP_PRINTF("http ver : %s", http_ver);
lucastanio 0:e6957f354668 89 break;
lucastanio 0:e6957f354668 90 }
lucastanio 0:e6957f354668 91
lucastanio 0:e6957f354668 92 if (httpmethod[0] == '\0') {
lucastanio 0:e6957f354668 93 buffer[MAX_BUFFER_SIZE - 1] = '\0';
lucastanio 0:e6957f354668 94 sprintf(buffer,"%s %d %s\r\nConnection: Close\r\n\r\n", http_ver, status_code, reason_phrase);
lucastanio 0:e6957f354668 95 HTTP_PRINTF("echo back done.");
lucastanio 0:e6957f354668 96 }
lucastanio 0:e6957f354668 97
lucastanio 0:e6957f354668 98 // Response
lucastanio 0:e6957f354668 99 if (strcmp(httpmethod,"GET") == 0 ) //GET request - always index.html stoed in index_html
lucastanio 0:e6957f354668 100 {
lucastanio 0:e6957f354668 101 HTTP_PRINTF("GET request incomming.");
lucastanio 0:e6957f354668 102 HTTP_PRINTF("httpmethod: %s", httpmethod);
lucastanio 0:e6957f354668 103 buffer[MAX_BUFFER_SIZE-1] = '\0';
lucastanio 0:e6957f354668 104 status_code = 200;
lucastanio 0:e6957f354668 105 sprintf(reason_phrase,"OK");
lucastanio 0:e6957f354668 106
lucastanio 0:e6957f354668 107 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);
lucastanio 0:e6957f354668 108 client->send(buffer,strlen(buffer));
lucastanio 0:e6957f354668 109 client->send(html_file,html_size);
lucastanio 0:e6957f354668 110 }
lucastanio 0:e6957f354668 111 if (strcmp(httpmethod,"POST") == 0 ) //POST request - javascript request
lucastanio 0:e6957f354668 112 {
lucastanio 0:e6957f354668 113 HTTP_PRINTF("POST request incomming.");
lucastanio 0:e6957f354668 114 status_code = 200;
lucastanio 0:e6957f354668 115 sprintf(reason_phrase,"OK");
lucastanio 0:e6957f354668 116
lucastanio 0:e6957f354668 117 sprintf(buffer,"%s %d %s\r\nConnection: Close\r\n\r\n", http_ver, status_code, reason_phrase);
lucastanio 0:e6957f354668 118 client->send(buffer,strlen(buffer));
lucastanio 0:e6957f354668 119
lucastanio 0:e6957f354668 120 if(strcmp(filepath,"/read_data")==0){
lucastanio 0:e6957f354668 121 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");
lucastanio 0:e6957f354668 122 //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());
lucastanio 0:e6957f354668 123 client->send(buffer,strlen(buffer));
lucastanio 0:e6957f354668 124 HTTP_PRINTF("Response: %s",buffer);
lucastanio 0:e6957f354668 125 /*
lucastanio 0:e6957f354668 126 //Temporary: update current:
lucastanio 0:e6957f354668 127 (*CH).current = (*CH).current+1e-12;
lucastanio 0:e6957f354668 128 if((*CH).current > 10e-12)
lucastanio 0:e6957f354668 129 (*CH).current = -10e-12; */
lucastanio 0:e6957f354668 130 }
lucastanio 0:e6957f354668 131
lucastanio 0:e6957f354668 132 if(strncmp(filepath,"/range",strlen("/range"))==0)
lucastanio 0:e6957f354668 133 {
lucastanio 0:e6957f354668 134 sscanf(filepath,"/range=%c",&c);
lucastanio 0:e6957f354668 135 if(c>='0'&& c<='4')
lucastanio 0:e6957f354668 136 {
lucastanio 0:e6957f354668 137 //(*CH).range = c - '0';
lucastanio 0:e6957f354668 138 //set_range((*CH).range);
lucastanio 0:e6957f354668 139 //(*CH).new_data = 1;
lucastanio 0:e6957f354668 140 HTTP_PRINTF("Changing range: %d",c - '0');
lucastanio 0:e6957f354668 141 }
lucastanio 0:e6957f354668 142 }
lucastanio 0:e6957f354668 143
lucastanio 0:e6957f354668 144 if(strcmp(filepath,"/device_name")==0)
lucastanio 0:e6957f354668 145 {
lucastanio 0:e6957f354668 146 sprintf(buffer,"%s",DEVICE_NAME);
lucastanio 0:e6957f354668 147 client->send(buffer,strlen(buffer));
lucastanio 0:e6957f354668 148 }
lucastanio 0:e6957f354668 149 }
lucastanio 0:e6957f354668 150 client->close();
lucastanio 0:e6957f354668 151 }
lucastanio 0:e6957f354668 152 else
lucastanio 0:e6957f354668 153 HTTP_PRINTF("Accept Error: %d\n\r",status);
lucastanio 0:e6957f354668 154
lucastanio 0:e6957f354668 155 return 0;
lucastanio 0:e6957f354668 156 }