Adaptation of the HttpServer by user yueee_yt. This version has improved handling of the HTTP headers (**NOTE**: There are limitations with this implementation and it is not fully functional. Use it only as a starting point.)

Dependents:   DMSupport DMSupport DMSupport DMSupport

Fork of DM_HttpServer by EmbeddedArtists AB

Files at this revision

API Documentation at this revision

Comitter:
embeddedartists
Date:
Mon Nov 04 14:29:37 2019 +0000
Parent:
10:c1c8276af541
Commit message:
More updates related to mbed OS 5

Changed in this revision

HTTPRequestHandler.cpp Show annotated file Show diff for this revision Revisions of this file
HTTPServer.h Show annotated file Show diff for this revision Revisions of this file
diff -r c1c8276af541 -r 9dcff8cf906a HTTPRequestHandler.cpp
--- a/HTTPRequestHandler.cpp	Wed Oct 23 06:58:32 2019 +0000
+++ b/HTTPRequestHandler.cpp	Mon Nov 04 14:29:37 2019 +0000
@@ -127,6 +127,7 @@
         m_headersSent = true;
         writeHeaders();
     }
+
     return m_pTCPSocketConnection->send((char *)buf, len);
 }
 /**
diff -r c1c8276af541 -r 9dcff8cf906a HTTPServer.h
--- a/HTTPServer.h	Wed Oct 23 06:58:32 2019 +0000
+++ b/HTTPServer.h	Mon Nov 04 14:29:37 2019 +0000
@@ -38,7 +38,7 @@
     }
 };
 
-map< string, HTTPRequestHandler*(*)(const char*, const char* , TCPSocketConnection* ), handlersComp > m_lpHandlers;
+map< string, HTTPRequestHandler*(*)(const char*, const char* , TCPSocket* ), handlersComp > m_lpHandlers;
 template<typename T>
 void HTTPServerAddHandler(const char* path)  //Template decl in header
 {
@@ -52,7 +52,7 @@
     HTTP_HEAD
 };
 
-bool getRequest(TCPSocketConnection* client,string* path, string* meth)
+bool getRequest(TCPSocket* client,string* path, string* meth)
 {
     char req[128];
     char c_path[128];
@@ -63,7 +63,7 @@
     int ret;
     int len = 0;
     for(int i = 0; i < maxLen - 1; i++) {
-        ret = client->receive(p, 1);
+        ret = client->recv(p, 1);
         if(!ret) {
             break;
         }
@@ -89,7 +89,7 @@
     return true;
 }
 
-void dispatchRequest(TCPSocketConnection* client)
+void dispatchRequest(TCPSocket* client)
 {
     string path;
     string meth;
@@ -129,7 +129,7 @@
 #ifdef _DEBUG_HTTP_SERVER_H
     printf("Looking for a handler\r\n");
 #endif
-    map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocketConnection*) >::iterator it;
+    map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocket*) >::iterator it;
     int root_len = 0;
     for (it = m_lpHandlers.begin(); it != m_lpHandlers.end(); it++) {
 #ifdef _DEBUG_HTTP_SERVER_H
@@ -190,51 +190,71 @@
 void HTTPServerChild (void const *arg)
 {
 #ifdef _DEBUG_HTTP_SERVER_H
+    SocketAddress remoteAddress;
     printf("HTTPServerChiled Start......\r\n");
 #endif
-    TCPSocketConnection* client = (TCPSocketConnection*)arg;
+    TCPSocket* client = (TCPSocket*)arg;
 
     for (;;) {
 #ifdef _DEBUG_HTTP_SERVER_H
-        printf("(HTTPServer.h<HTTPServerChild>)Connection from %s\r\n", client->get_address());
+        client->getpeername(&remoteAddress);
+        printf("(HTTPServer.h<HTTPServerChild>)Connection from %s\r\n", remoteAddress.get_ip_address());
 #endif
         dispatchRequest(client);
 #ifdef _DEBUG_HTTP_SERVER_H
-        printf("(HTTPServer.h<HTTPServerChild>)Close %s\r\n", client->get_address());
+        client->getpeername(&remoteAddress);
+        printf("(HTTPServer.h<HTTPServerChild>)Close %s\r\n", remoteAddress.get_ip_address());
 #endif
         client->close();
-        client->reset_address();
+//        client->reset_address();
         //delete client;
-        Thread::signal_wait(1);
+        ThisThread::flags_wait_any(1);
     }
 }
 
 void HTTPServerCloser (void const *arg)
 {
-    TCPSocketConnection *client = (TCPSocketConnection*)arg;
+#ifdef _DEBUG_HTTP_SERVER_H
+    SocketAddress remoteAddress;
+#endif    
+    TCPSocket *client = (TCPSocket*)arg;
 
     for (;;) {
+#ifdef _DEBUG_HTTP_SERVER_H
+        client->getpeername(&remoteAddress);
+        printf("Close %s, %x\r\n", remoteAddress.get_ip_address(), client);
+#endif
         client->close();
-#ifdef _DEBUG_HTTP_SERVER_H
-        printf("Close %s\r\n", client->get_address());
-#endif
-        Thread::signal_wait(1);
+        ThisThread::flags_wait_any(1);
     }
 }
 
 void HTTPServerStart(int port = 80)
 {
     int i, t = 0;
-    TCPSocketConnection clients[THREAD_MAX];
-    TCPSocketConnection xclient;
+    nsapi_error_t ret;
+    TCPSocket* clients[THREAD_MAX];
+    TCPSocket* xclient;
 
     for (i = 0; i < THREAD_MAX; i ++) {
         threads[i] = NULL;
     }
     xthread = NULL;
 
-    TCPSocketServer server;
-    server.bind(port);
+    TCPSocket server;
+    
+    
+    ret = server.open(NetworkInterface::get_default_instance());
+    if (ret != NSAPI_ERROR_OK) {
+        printf("HTTPServer: open failed %d\n", ret);
+        return;
+    }    
+    
+    ret = server.bind(port);
+    if (ret != NSAPI_ERROR_OK) {
+        printf("HTTPServer: bind failed %d\n", ret);
+        return;
+    }
     server.listen();
     // server.set_blocking(false);
 #ifdef _DEBUG_HTTP_SERVER_H
@@ -245,24 +265,37 @@
         printf("**Start Loop** \r\n");
 #endif
         if (t >= 0) {
-            if(server.accept(clients[t])==0) {
+            
+            clients[t] = server.accept();
+            //if(server.accept(clients[t])==0) {
+            if (clients[t] != NULL) {
                 // fork child process
                 if (threads[t]) {
-                    threads[t]->signal_set(1);
+                    threads[t]->flags_set(1);
                 } else {
-                    threads[t] = new Thread(HTTPServerChild, (void*)&clients[t], osPriorityNormal, DEFAULT_STACK_SIZE*2);
+                    
+                    threads[t] = new Thread(osPriorityNormal, OS_STACK_SIZE*2);
+                    if (threads[t] != NULL) {
+                        
+                        threads[t]->start(callback(HTTPServerChild, (void*)clients[t]));
+                    } 
                 }
 #ifdef _DEBUG_HTTP_SERVER_H
                 printf("Forked %d\r\n", t);
 #endif
             }
         } else {
-            if(server.accept(xclient)==0) {
+            xclient = server.accept();
+            //if(server.accept(xclient)==0) {
+            if (xclient != NULL) {
                 // closer process
                 if (xthread) {
-                    xthread->signal_set(1);
+                    xthread->flags_set(1);
                 } else {
-                    xthread = new Thread(HTTPServerCloser, (void*)&xclient, osPriorityNormal, DEFAULT_STACK_SIZE*2);
+                    xthread = new Thread(osPriorityNormal, OS_STACK_SIZE*2);
+                    if (xthread != NULL) {
+                        xthread->start(callback(HTTPServerChild, (void*)xclient));
+                    }                     
                 }
 #ifdef _DEBUG_HTTP_SERVER_H
                 printf("Connection full\r\n");
@@ -272,7 +305,7 @@
 
         t = -1;
         for (i = 0; i < THREAD_MAX; i ++) {
-            if (threads[i] == NULL || threads[i]->get_state() == Thread::WaitingAnd) {
+            if (threads[i] == NULL || threads[i]->get_state() == Thread::WaitingThreadFlag) {
                 if (t < 0) t = i; // next empty thread
             }
         }