Programme+libraires

Dependents:   Serveur_web_pt2 Serveur_web_pt2_V2_avec_index_html Serveur_web_pt2_index_commenter Serveur_web_pt2_index_commenter

Fork of HTTPServer by Donatien Garnier

Committer:
sedid
Date:
Tue Jul 03 12:00:10 2018 +0000
Revision:
7:f88b734870ab
Programme + librairies pour PT2 info2 du serveur web capteur par Sedid dupuis

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sedid 7:f88b734870ab 1 #ifndef MYOWNHANDLER_H
sedid 7:f88b734870ab 2 #define MYOWNHANDLER_H
sedid 7:f88b734870ab 3
sedid 7:f88b734870ab 4 #include "../HTTPRequestHandler.h"
sedid 7:f88b734870ab 5 float x,y;
sedid 7:f88b734870ab 6
sedid 7:f88b734870ab 7 class MyOwnHandler : public HTTPRequestHandler
sedid 7:f88b734870ab 8 {
sedid 7:f88b734870ab 9 public:
sedid 7:f88b734870ab 10 MyOwnHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket)
sedid 7:f88b734870ab 11 : HTTPRequestHandler(rootPath, path, pTCPSocket) {}
sedid 7:f88b734870ab 12 virtual ~MyOwnHandler() {}
sedid 7:f88b734870ab 13
sedid 7:f88b734870ab 14 //protected:
sedid 7:f88b734870ab 15 static inline HTTPRequestHandler* inst(const char* rootPath,
sedid 7:f88b734870ab 16 const char* path, TCPSocket* pTCPSocket)
sedid 7:f88b734870ab 17 { return new MyOwnHandler(rootPath, path, pTCPSocket); }
sedid 7:f88b734870ab 18
sedid 7:f88b734870ab 19 virtual void doGet();
sedid 7:f88b734870ab 20 virtual void doPost() {}
sedid 7:f88b734870ab 21 virtual void doHead() {}
sedid 7:f88b734870ab 22
sedid 7:f88b734870ab 23 virtual void onReadable() {}
sedid 7:f88b734870ab 24 virtual void onWriteable() { close(); } // Data has been written,
sedid 7:f88b734870ab 25 // we can close the connection
sedid 7:f88b734870ab 26 virtual void onClose() {}
sedid 7:f88b734870ab 27 };
sedid 7:f88b734870ab 28
sedid 7:f88b734870ab 29 void MyOwnHandler::doGet()
sedid 7:f88b734870ab 30 {
sedid 7:f88b734870ab 31 // To get request data, we use the method path()
sedid 7:f88b734870ab 32 // We assume that path() will be:
sedid 7:f88b734870ab 33 // /variable
sedid 7:f88b734870ab 34 // or /variable value
sedid 7:f88b734870ab 35 // Get variable string
sedid 7:f88b734870ab 36 int endVar = path().find_first_of('%', 1);
sedid 7:f88b734870ab 37 string variable = path().substr(1, endVar-1);
sedid 7:f88b734870ab 38 // Get value string
sedid 7:f88b734870ab 39 string value;
sedid 7:f88b734870ab 40 if (endVar!=string::npos) value = path().substr(endVar+3, string::npos);
sedid 7:f88b734870ab 41 // Get a pointer on the variable
sedid 7:f88b734870ab 42 // The type of the variable must be float
sedid 7:f88b734870ab 43 float *ptr = NULL;
sedid 7:f88b734870ab 44 if (variable.compare("x")==0) ptr=&x;
sedid 7:f88b734870ab 45 else if (variable.compare("y")==0) ptr=&y;
sedid 7:f88b734870ab 46 // else if (variable.compare("variable")==0) ptr=&variable;
sedid 7:f88b734870ab 47 // Get the float value and set the variable value
sedid 7:f88b734870ab 48 if ((ptr!=NULL)&&(!value.empty())) {
sedid 7:f88b734870ab 49 float v;
sedid 7:f88b734870ab 50 if (sscanf(value.c_str(), "%f", &v)!=EOF) *ptr=v;
sedid 7:f88b734870ab 51 }
sedid 7:f88b734870ab 52 // Server response will be the value of the variable
sedid 7:f88b734870ab 53 char resp[20];
sedid 7:f88b734870ab 54 if (ptr!=NULL) {
sedid 7:f88b734870ab 55 sprintf(resp, "%f", *ptr);
sedid 7:f88b734870ab 56 } else {
sedid 7:f88b734870ab 57 sprintf(resp, "Unknown variable");
sedid 7:f88b734870ab 58 }
sedid 7:f88b734870ab 59 setContentLen( strlen(resp) );
sedid 7:f88b734870ab 60 respHeaders()["Connection"] = "close";
sedid 7:f88b734870ab 61 writeData(resp, strlen(resp));
sedid 7:f88b734870ab 62 }
sedid 7:f88b734870ab 63 #endif