http server library
Revision 0:e6957f354668, committed 2020-07-30
- Comitter:
- lucastanio
- Date:
- Thu Jul 30 17:32:57 2020 +0000
- Commit message:
- Http server library
Changed in this revision
http_server.cpp | Show annotated file Show diff for this revision Revisions of this file |
http_server.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r e6957f354668 http_server.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/http_server.cpp Thu Jul 30 17:32:57 2020 +0000 @@ -0,0 +1,156 @@ +/********************************************************************* +* +* Simple HTTP Server library +* +* Mauricio Martins Donatti +* mauricio.donatti@lnls.br +* +* Electronics Instrumentation Group - GIE +* Brazilian Synchrotron Light Laboratory (LNLS) +* Brazilian Center for Research in Energy and Materials (CNPEM) +* +* March 2020 +* +*******************************************************************/ + +#include "http_server.h" + +char c; //aux variable + +HttpServer::HttpServer() +{ + buffer[0] = '\0'; //constructor +} + +HttpServer::~HttpServer() +{ +} +bool HttpServer::init(EthernetInterface* eth,char *file,int file_size) +{ + HTTP_PRINTF("DEBUG MODE"); + + html_file = file; + html_size = file_size; + + if(socket.open(eth)==NSAPI_ERROR_OK) + HTTP_PRINTF("Socket Opened\n\r"); + else + HTTP_PRINTF("Error opening socket\n\r"); + + if(socket.bind(80)==NSAPI_ERROR_OK) + HTTP_PRINTF("Socket Bind Success\n\r"); + else + HTTP_PRINTF("Bind Error\n\r"); + + if(socket.listen(QUEUED_REQUESTS)==NSAPI_ERROR_OK) + HTTP_PRINTF("Socket Listen Success\n\r"); + else + HTTP_PRINTF("Listen Error\n\r"); + + return true; +} + +bool HttpServer::run() +{ + client = socket.accept(&status); + HTTP_PRINTF("After accept\n\r"); + if(status==NSAPI_ERROR_OK) + { + client->set_timeout(WEBSERVER_TIMEOUT_MS); + n_recv = client->recv(buffer,MAX_BUFFER_SIZE); + switch(n_recv) { + case 0: + HTTP_PRINTF("received buffer is empty."); + status_code = 400; + sprintf(reason_phrase,"No Request"); + httpmethod[0] = '\0'; + filepath[0] = '\0'; + http_ver[0] = '\0'; + break; + case -1: + HTTP_PRINTF("failed to read data from client."); + status_code = 500; + sprintf(reason_phrase,"Internal Server Error"); + httpmethod[0] = '\0'; + filepath[0] = '\0'; + http_ver[0] = '\0'; + break; + default: + HTTP_PRINTF("Received Data: %d",strlen(buffer)); + HTTP_PRINTF("-->\r\n"); + HTTP_PRINTF("%.*s[End of Request]",strlen(buffer),buffer); + // get HTTP method, File path, HTTP version + strcpy(httpmethod,strtok(buffer, " ")); + strcpy(filepath,strtok(NULL, " ")); + strcpy(http_ver,strtok(NULL, " ")); + HTTP_PRINTF("httpmethod: %s", httpmethod); + HTTP_PRINTF("file path: %s", filepath); + HTTP_PRINTF("http ver : %s", http_ver); + break; + } + + if (httpmethod[0] == '\0') { + buffer[MAX_BUFFER_SIZE - 1] = '\0'; + sprintf(buffer,"%s %d %s\r\nConnection: Close\r\n\r\n", http_ver, status_code, reason_phrase); + HTTP_PRINTF("echo back done."); + } + + // Response + if (strcmp(httpmethod,"GET") == 0 ) //GET request - always index.html stoed in index_html + { + HTTP_PRINTF("GET request incomming."); + HTTP_PRINTF("httpmethod: %s", httpmethod); + buffer[MAX_BUFFER_SIZE-1] = '\0'; + status_code = 200; + sprintf(reason_phrase,"OK"); + + 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); + client->send(buffer,strlen(buffer)); + client->send(html_file,html_size); + } + if (strcmp(httpmethod,"POST") == 0 ) //POST request - javascript request + { + HTTP_PRINTF("POST request incomming."); + status_code = 200; + sprintf(reason_phrase,"OK"); + + sprintf(buffer,"%s %d %s\r\nConnection: Close\r\n\r\n", http_ver, status_code, reason_phrase); + client->send(buffer,strlen(buffer)); + + if(strcmp(filepath,"/read_data")==0){ + 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"); + //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()); + client->send(buffer,strlen(buffer)); + HTTP_PRINTF("Response: %s",buffer); + /* + //Temporary: update current: + (*CH).current = (*CH).current+1e-12; + if((*CH).current > 10e-12) + (*CH).current = -10e-12; */ + } + + if(strncmp(filepath,"/range",strlen("/range"))==0) + { + sscanf(filepath,"/range=%c",&c); + if(c>='0'&& c<='4') + { + //(*CH).range = c - '0'; + //set_range((*CH).range); + //(*CH).new_data = 1; + HTTP_PRINTF("Changing range: %d",c - '0'); + } + } + + if(strcmp(filepath,"/device_name")==0) + { + sprintf(buffer,"%s",DEVICE_NAME); + client->send(buffer,strlen(buffer)); + } + } + client->close(); + } + else + HTTP_PRINTF("Accept Error: %d\n\r",status); + + return 0; +} \ No newline at end of file
diff -r 000000000000 -r e6957f354668 http_server.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/http_server.h Thu Jul 30 17:32:57 2020 +0000 @@ -0,0 +1,93 @@ +/********************************************************************* +* +* HTTP_SERVER.h +* +* Mauricio Martins Donatti +* mauricio.donatti@lnls.br +* +* Electronics Instrumentation Group - GIE +* Brazilian Synchrotron Light Laboratory (LNLS) +* Brazilian Center for Research in Energy and Materials (CNPEM) +* +* March 2020 +* +*******************************************************************/ + +#ifndef HTTP_SERVER_H +#define HTTP_SERVER_H + +#include "mbed.h" +#include "EthernetInterface.h" +#include <string> + +#define MAX_BUFFER_SIZE 1024 +#define WEBSERVERPORT 80 +#define QUEUED_REQUESTS 3 +#define DEVICE_NAME "LNLS_4CH_001\0" +#define WEBSERVER_TIMEOUT_MS 1500 + + +#define DEBUG + +#ifdef DEBUG + +extern Serial pc; +#define HTTP_PRINTF(fmt, ...) pc.printf("HTTP: " fmt "\r\n", ##__VA_ARGS__) + +#else + +#define HTTP_PRINTF(fmt, ...) __NOP() + +#endif //Config enabled DEBUG + + + +using namespace std; + +// HttpServer class +// +// This is the class to make a mbed a simple HTTP Server. +class HttpServer +{ +public: + HttpServer(); + ~HttpServer(); + + // HTTP SERVER Initialization. + // + // This function should be called first of all. + // @return result of init() as boolean. + // @retval TRUE SACCESS + // @retval FALSE + // + bool init(EthernetInterface* eth,char *file,int file_size); + + // Run the surver service while listening flag is true. + // + // @return state ending. + // @retval TRUE at error end. + // @retval FALSE at normal end. + // + bool run(); + + char *html_file; //Pointer to HTML file + int html_size; + + char buffer[MAX_BUFFER_SIZE]; //receive and transmit buffer + int n_recv; + int buf_len; + int status_code; //http status code + char reason_phrase[30]; //http reason phrase + + char httpmethod[20]; //http method + char filepath[40]; //file requested + char http_ver[20]; //http version + + // Handlers + TCPSocket socket; // TCP server + TCPSocket* client; // TCP server connection clerk + + nsapi_error_t status; +}; + +#endif \ No newline at end of file