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:
15:371fbad587ed
Parent:
13:d3571c244759
Child:
16:16289b6ec82a
--- a/HTTPServer.h	Fri Jun 09 13:26:14 2017 +0000
+++ b/HTTPServer.h	Fri Jun 23 11:34:54 2017 +0000
@@ -4,7 +4,16 @@
 #define HTTP_SERVER_H
 
 #ifdef _DEBUG_ALL
-#define _DEBUG_HTTP_SERVER_H
+#include <stdio.h>
+#include <stdarg.h>
+static inline void http_server_debug_print(const char *format, ...) {
+    va_list args;
+    va_start(args, format);
+    vfprintf(stderr, format, args);
+    va_end(args);
+}
+#else
+static inline void http_server_debug_print(const char *format, ...) {}
 #endif
 
 #include <string>
@@ -17,10 +26,6 @@
 #include "rtos.h"
 #include "mbed.h"
 
-#define THREAD_MAX 5
-Thread *threads[THREAD_MAX];
-Thread *xthread;
-
 struct handlersComp { //Used to order handlers in the right way
     bool operator() (const string& handler1, const string& handler2) const {
         //The first handler is longer than the second one
@@ -74,9 +79,7 @@
         len++;
     }
     *p = 0;
-#ifdef _DEBUG_HTTP_SERVER_H
-    printf("Parsing request : %s\r\n", req);
-#endif
+    http_server_debug_print("Parsing request : %s\r\n", req);
     ret = sscanf(req, "%s %s HTTP/%*d.%*d", c_meth, c_path);
     if(ret !=2)        return false;
     *meth = string(c_meth);
@@ -89,60 +92,40 @@
     string path;
     string meth;
     HTTP_METH methCode;
-#ifdef _DEBUG_HTTP_SERVER_H
-    printf("Dispatching req\r\n");
-#endif
+    http_server_debug_print("Dispatching req\r\n");
     if( !getRequest(client,&path, &meth ) ) {
-#ifdef _DEBUG_HTTP_SERVER_H
-        printf("dispatchRequest Invalid request\r\n");
-#endif
+        http_server_debug_print("dispatchRequest Invalid request\r\n");
         return; //Invalid request
     }
     if( !meth.compare("GET") ) {
-#ifdef _DEBUG_HTTP_SERVER_H
-        printf("dispatchRequest HTTP_GET\r\n");
-#endif
+        http_server_debug_print("dispatchRequest HTTP_GET\r\n");
         methCode = HTTP_GET;
     } else if( !meth.compare("POST") ) {
-#ifdef _DEBUG_HTTP_SERVER_H
-        printf("dispatchRequest HTTP_POST\r\n");
-#endif
+        http_server_debug_print("dispatchRequest HTTP_POST\r\n");
         methCode = HTTP_POST;
     } else if( !meth.compare("HEAD") ) {
-#ifdef _DEBUG_HTTP_SERVER_H
-        printf("dispatchRequest HTTP_HEAD\r\n");
-#endif
+        http_server_debug_print("dispatchRequest HTTP_HEAD\r\n");
         methCode = HTTP_HEAD;
     } else {
-#ifdef _DEBUG_HTTP_SERVER_H
-        printf("dispatchRequest() Parse error\r\n");
-#endif
+        http_server_debug_print("dispatchRequest() Parse error\r\n");
         return;
     }
-#ifdef _DEBUG_HTTP_SERVER_H
-    printf("Looking for a handler\r\n");
-#endif
+    http_server_debug_print("Looking for a handler\r\n");
     map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocket*), handlersComp >::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
+        http_server_debug_print("Checking %s...\r\n", (*it).first.c_str());
         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
+            http_server_debug_print("Found (%s)\r\n", (*it).first.c_str());
             // Found!
             break;  // for
         }
     }
     if((it == m_lpHandlers.end()) && !(m_lpHandlers.empty())) {
-#ifdef _DEBUG_HTTP_SERVER_H
-        printf("Using default handler\r\n");
-#endif
+        http_server_debug_print("Using default handler\r\n");
         it = m_lpHandlers.end();
         it--; //Get the last element
         if( ! (((*it).first.length() == 0) || !(*it).first.compare("/")) ) //This is not the default handler
@@ -150,14 +133,10 @@
         root_len = 0;
     }
     if(it == m_lpHandlers.end()) {
-#ifdef _DEBUG_HTTP_SERVER_H
-        printf("No handler found\r\n");
-#endif
+        http_server_debug_print("No handler found\r\n");
         return;
     }
-#ifdef _DEBUG_HTTP_SERVER_H
-    printf("Handler found.\r\n");
-#endif
+    http_server_debug_print("Handler found.\r\n");
     HTTPRequestHandler* pHdlr = (*it).second((*it).first.c_str(), path.c_str() + root_len, client);
     //****  client = NULL; //We don't own it anymore
     switch(methCode) {
@@ -172,43 +151,39 @@
             break;
     }
     delete pHdlr;
-#ifdef _DEBUG_HTTP_SERVER_H
-    printf("(dispatcherRequest)return\r\n");
-#endif
+    http_server_debug_print("(dispatcherRequest)return\r\n");
     return ;
 }
 
-void HTTPServerChild (TCPSocket* client)
+#define THREAD_MAX 1
+
+#if (THREAD_MAX > 1) 
+static Thread *threads[THREAD_MAX];
+static Thread *xthread;
+
+static void HTTPServerChild (TCPSocket* client)
 {
-#ifdef _DEBUG_HTTP_SERVER_H
-    printf("HTTPServerChiled Start......\r\n");
-#endif
+    http_server_debug_print("HTTPServerChiled Start......\r\n");
 
     for (;;) {
-#ifdef _DEBUG_HTTP_SERVER_H
-        printf("(HTTPServer.h<HTTPServerChild>)Connection\r\n");
-#endif
+        http_server_debug_print("(HTTPServer.h<HTTPServerChild>)Connection\r\n");
         dispatchRequest(client);
-#ifdef _DEBUG_HTTP_SERVER_H
-        printf("(HTTPServer.h<HTTPServerChild>)Close\r\n");
-#endif
+        http_server_debug_print("(HTTPServer.h<HTTPServerChild>)Close\r\n");
         client->close();
         Thread::signal_wait(1);
     }
 }
 
-void HTTPServerCloser (TCPSocket *client)
+static void HTTPServerCloser (TCPSocket *client)
 {
     for (;;) {
         client->close();
-#ifdef _DEBUG_HTTP_SERVER_H
-        printf("Close\r\n");
-#endif
+        http_server_debug_print("Close\r\n");
         Thread::signal_wait(1);
     }
 }
 
-void HTTPServerStart(NetworkInterface *net, int port = 80)
+void HTTPServerStart(NetworkInterface *net, int port = 80, osPriority priority = osPriorityNormal)
 {
     int i, t = 0;
     TCPSocket clients[THREAD_MAX];
@@ -222,26 +197,22 @@
     TCPServer server(net);
     server.bind(port);
     server.listen();
-#ifdef _DEBUG_HTTP_SERVER_H
-    printf("Wait for new connection...\r\n");
-#endif
+    http_server_debug_print("Wait for new connection...\r\n");
 
     for (;;) {
-#ifdef _DEBUG_HTTP_SERVER_H
-        printf("**Start Loop** \r\n");
-#endif
+        http_server_debug_print("**Start Loop** \r\n");
         if (t >= 0) {
             if(server.accept(&clients[t])==0) {
                 // fork child process
                 if (threads[t]) {
                     threads[t]->signal_set(1);
                 } else {
-                    threads[t] = new Thread(osPriorityNormal, 1024 * 3);
+                    threads[t] = new Thread(priority, 1024 * 3);
                     threads[t]->start(callback(HTTPServerChild, &clients[t]));
                 }
-#ifdef _DEBUG_HTTP_SERVER_H
-                printf("Forked %d\r\n", t);
-#endif
+                http_server_debug_print("Forked %d\r\n", t);
+            } else {
+                Thread::wait(10);
             }
         } else {
             if(server.accept(&xclient)==0) {
@@ -249,12 +220,12 @@
                 if (xthread) {
                     xthread->signal_set(1);
                 } else {
-                    xthread = new Thread;
+                    xthread = new Thread(priority);
                     xthread->start(callback(HTTPServerCloser, &xclient));
                 }
-#ifdef _DEBUG_HTTP_SERVER_H
-                printf("Connection full\r\n");
-#endif
+                http_server_debug_print("Connection full\r\n");
+            } else {
+                Thread::wait(10);
             }
         }
 
@@ -267,6 +238,31 @@
         }
     }
 }
+
+#else  // THREAD_MAX == 1
+
+void HTTPServerStart(NetworkInterface *net, int port = 80, osPriority priority = osPriorityNormal)
+{
+    TCPSocket client;
+
+    (void)priority;
+
+    TCPServer server(net);
+    server.bind(port);
+    server.listen();
+    http_server_debug_print("Wait for new connection...\r\n");
+
+    while (1) {
+        http_server_debug_print("**Start Loop** \r\n");
+        if (server.accept(&client) == 0) {
+            dispatchRequest(&client);
+            client.close();
+        }
+    }
+}
+
+#endif
+
 #include "Handler/RPCHandler.h"
 #include "Handler/FSHandler.h"
 #include "Handler/SimpleHandler.h"