HttpServer Library for "mbed-os" which added a snapshot handler.

Dependents:   GR-PEACH-webcam GR-Boards_WebCamera GR-Boards_WebCamera GR-Boards_WebCamera

Fork of HttpServer_snapshot by Renesas

Revision:
4:1b6b021ee21d
Parent:
0:fdf9c2c5200f
Child:
6:d9e6379eefac
--- a/HTTPServer.h	Thu Feb 20 13:11:34 2014 +0000
+++ b/HTTPServer.h	Fri Feb 21 07:10:30 2014 +0000
@@ -1,6 +1,12 @@
+//#define _DEBUG_ALL
+
 #ifndef HTTP_SERVER_H
 #define HTTP_SERVER_H
 
+#ifdef _DEBUG_ALL
+#define _DEBUG_HTTP_SERVER_H
+#endif
+
 #include <string>
 using std::string;
 
@@ -40,9 +46,6 @@
 }
 
 void ListenThread(void const *args);
-//DigitalOut _led(LED4);
-//bool serverIsListened;
-//bool clientIsConnected;
 enum HTTP_METH {
     HTTP_GET,
     HTTP_POST,
@@ -76,8 +79,9 @@
         len++;
     }
     *p = 0;
-
+#ifdef _DEBUG_HTTP_SERVER_H
     printf("Parsing request : %s\r\n", req);
+#endif
     ret = sscanf(req, "%s %s HTTP/%*d.%*d", c_meth, c_path);
     if(ret !=2)        return false;
     *meth = string(c_meth);
@@ -90,45 +94,62 @@
     string path;
     string meth;
     HTTP_METH methCode;
-
+#ifdef _DEBUG_HTTP_SERVER_H
     printf("Dispatching req\r\n");
-
+#endif
     if( !getRequest(client,&path, &meth ) ) {
+#ifdef _DEBUG_HTTP_SERVER_H
         printf("dispatchRequest Invalid request\r\n");
+#endif
         //close();
         return; //Invalid request
     }
     if( !meth.compare("GET") ) {
+#ifdef _DEBUG_HTTP_SERVER_H
         printf("dispatchRequest HTTP_GET\r\n");
+#endif
         methCode = HTTP_GET;
     } else if( !meth.compare("POST") ) {
+#ifdef _DEBUG_HTTP_SERVER_H
         printf("dispatchRequest HTTP_POST\r\n");
+#endif
         methCode = HTTP_POST;
     } else if( !meth.compare("HEAD") ) {
+#ifdef _DEBUG_HTTP_SERVER_H
         printf("dispatchRequest HTTP_HEAD\r\n");
+#endif
         methCode = HTTP_HEAD;
     } else {
+#ifdef _DEBUG_HTTP_SERVER_H
         printf("dispatchRequest() Parse error\r\n");
+#endif
         //close(); //Parse error
         return;
     }
-    
+#ifdef _DEBUG_HTTP_SERVER_H
     printf("Looking for a handler\r\n");
+#endif
     map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocketConnection*) >::iterator it;
     int root_len = 0;
     for (it = m_lpHandlers.begin(); it != m_lpHandlers.end(); it++) {
+#ifdef _DEBUG_HTTP_SERVER_H
         printf("Checking %s...\r\n", (*it).first.c_str());
+#endif
         root_len = (*it).first.length();
         if ( root_len &&
                 !path.compare( 0, root_len, (*it).first ) &&
                 (path[root_len] == '/' || path[root_len] == '\0')) {
+#ifdef _DEBUG_HTTP_SERVER_H
             printf("Found (%s)\r\n", (*it).first.c_str());
+#endif
             // Found!
             break;  // for
         }
     }
     if((it == m_lpHandlers.end()) && !(m_lpHandlers.empty())) {
+#ifdef _DEBUG_HTTP_SERVER_H
         printf("Using default handler\r\n");
+#endif
         it = m_lpHandlers.end();
         it--; //Get the last element
         if( ! (((*it).first.length() == 0) || !(*it).first.compare("/")) ) //This is not the default handler
@@ -136,11 +157,14 @@
         root_len = 0;
     }
     if(it == m_lpHandlers.end()) {
+#ifdef _DEBUG_HTTP_SERVER_H
         printf("No handler found\r\n");
+#endif
         return;
     }
+#ifdef _DEBUG_HTTP_SERVER_H
     printf("Handler found.\r\n");
-   
+#endif
     HTTPRequestHandler* pHdlr = (*it).second((*it).first.c_str(), path.c_str() + root_len, client);
     //****  client = NULL; //We don't own it anymore
     switch(methCode) {
@@ -155,21 +179,29 @@
             break;
     }
     delete pHdlr;
-   // delete client;
-   // delete m_pTCPSocketConnection;
-   printf("(dispatcherRequest)return\r\n");
-   return ;
+    // delete client;
+    // delete m_pTCPSocketConnection;
+#ifdef _DEBUG_HTTP_SERVER_H
+    printf("(dispatcherRequest)return\r\n");
+#endif
+    return ;
 }
 
 void HTTPServerChild (void const *arg)
 {
+#ifdef _DEBUG_HTTP_SERVER_H
     printf("HTTPServerChiled Start......\r\n");
+#endif
     TCPSocketConnection* client = (TCPSocketConnection*)arg;
 
     for (;;) {
+#ifdef _DEBUG_HTTP_SERVER_H
         printf("(HTTPServer.h<HTTPServerChild>)Connection from %s\r\n", client->get_address());
+#endif
         dispatchRequest(client);
+#ifdef _DEBUG_HTTP_SERVER_H
         printf("(HTTPServer.h<HTTPServerChild>)Close %s\r\n", client->get_address());
+#endif
         client->close();
         client->reset_address();
         //delete client;
@@ -183,7 +215,9 @@
 
     for (;;) {
         client->close();
+#ifdef _DEBUG_HTTP_SERVER_H
         printf("Close %s\r\n", client->get_address());
+#endif
         Thread::signal_wait(1);
     }
 }
@@ -202,10 +236,14 @@
     TCPSocketServer server;
     server.bind(port);
     server.listen();
-   // server.set_blocking(false);
+    // server.set_blocking(false);
+#ifdef _DEBUG_HTTP_SERVER_H
     printf("Wait for new connection...\r\n");
+#endif
     for (;;) {
+#ifdef _DEBUG_HTTP_SERVER_H
         printf("**Start Loop** \r\n");
+#endif
         if (t >= 0) {
             if(server.accept(clients[t])==0) {
                 // fork child process
@@ -214,7 +252,9 @@
                 } else {
                     threads[t] = new Thread(HTTPServerChild, (void*)&clients[t]);
                 }
+#ifdef _DEBUG_HTTP_SERVER_H
                 printf("Forked %d\r\n", t);
+#endif
             }
         } else {
             if(server.accept(xclient)==0) {
@@ -224,7 +264,9 @@
                 } else {
                     xthread = new Thread(HTTPServerCloser, (void*)&xclient);
                 }
+#ifdef _DEBUG_HTTP_SERVER_H
                 printf("Connection full\r\n");
+#endif
             }
         }
 
@@ -234,7 +276,7 @@
                 if (t < 0) t = i; // next empty thread
             }
         }
-        Thread::wait(100);
+        // Thread::wait(100);
     }
 }
 #include "Handler/RPCHandler.h"