EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Revision:
2:a4f97773c90f
Parent:
1:abb442332fa8
Child:
3:95e0bc00a1bb
diff -r abb442332fa8 -r a4f97773c90f services/http/server/HTTPRequestDispatcher.cpp
--- a/services/http/server/HTTPRequestDispatcher.cpp	Mon Jun 14 10:33:54 2010 +0000
+++ b/services/http/server/HTTPRequestDispatcher.cpp	Fri Jun 18 09:22:54 2010 +0000
@@ -41,14 +41,13 @@
 
 void HTTPRequestDispatcher::dispatchRequest()
 {
-  string rootPath;
-  string subPath;
+  string path;
   string meth;
   HTTP_METH methCode;
   
-  DBG("\r\nDispatching req\r\n");
+  DBG("Dispatching req\r\n");
   
-  if( !getRequest(&rootPath, &subPath, &meth ) )
+  if( !getRequest(&path, &meth ) )
   {
     close();
     return; //Invalid request
@@ -72,23 +71,47 @@
     return;
   }
   
-  DBG("\r\nLooking for a handler\r\n");
+  DBG("Looking for a handler\r\n");
   
   map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocket*) >::iterator it;
-  it = m_pSvr->m_lpHandlers.find(rootPath); //We are friends so we can do that
-  if(it == m_pSvr->m_lpHandlers.end())
+//  it = m_pSvr->m_lpHandlers.find(rootPath); //We are friends so we can do that
+// NEW CODE START: 
+  int root_len = 0;
+  for (it = m_pSvr->m_lpHandlers.begin(); it != m_pSvr->m_lpHandlers.end(); it++)
   {
-    it = m_pSvr->m_lpHandlers.find(""); //Use default handler if it exists
+    DBG("Checking %s...\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'))
+    {
+      DBG("Found (%s)\n", (*it).first.c_str());
+	    // Found!
+	    break;	// for
+	  }
+  }
+// NEW CODE END
+  if((it == m_pSvr->m_lpHandlers.end()) && !(m_pSvr->m_lpHandlers.empty()))
+  {
+    DBG("Using default handler\n");
+    it = m_pSvr->m_lpHandlers.end();
+    it--; //Get the last element
+    if( ! (((*it).first.length() == 0) || (*it).first.compare("/")) ) //This is not the default handler
+      it = m_pSvr->m_lpHandlers.end();
+    root_len = 0;
   }
   if(it == m_pSvr->m_lpHandlers.end())
   {    
+    DBG("No handler found\n");
     close(); //No handler found
     return;
   }
   
-  DBG("\r\nHandler found.\r\n");
+  DBG("Handler found.\r\n");
   
-  HTTPRequestHandler* pHdlr = (*it).second(rootPath.c_str(), subPath.c_str(), m_pTCPSocket);
+//HTTPRequestHandler* pHdlr = (*it).second(rootPath.c_str(), subPath.c_str(), m_pTCPSocket);
+//NEW CODE 1 LINE:
+  HTTPRequestHandler* pHdlr = (*it).second((*it).first.c_str(), path.c_str() + root_len, m_pTCPSocket);
   m_pTCPSocket = NULL; //We don't own it anymore
   
   switch(methCode)
@@ -104,7 +127,7 @@
     break;
   }
   
-  DBG("\r\nReq handled (or being handled)\r\n");
+  DBG("Req handled (or being handled)\r\n");
   close();
 }
 
@@ -129,7 +152,7 @@
   close();
 }
 
-bool HTTPRequestDispatcher::getRequest(string* rootPath, string* subPath, string* meth)
+bool HTTPRequestDispatcher::getRequest(string* path, string* meth)
 {
   char req[128];
   char c_path[128];
@@ -162,40 +185,18 @@
   }
   *p = 0;
   
-  DBG("\r\nParsing request : %s\r\n", req);
+  DBG("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);
-  *subPath = string(c_path);
-  
-  c_path[0]= '/';
-  if(!strchr(c_path+1, '/'))
-  {
-    //Not found, so this is the root path
-    c_path[1]=0;
-    *rootPath = string(c_path); 
-  } 
-  else
-  {
-    ret = sscanf(req, "%*s /%[^/ ]/%*s HTTP/%*d.%*d", c_path+1);
-    if(ret !=1)
-    {
-      //This is the root path
-      DBG("Default path\n");
-    }
-    *rootPath = string(c_path); 
-    subPath->erase(0,rootPath->length());
-  }
-  
-  
-  DBG("\r\nParse OK :\r\nRoot Path: %s\r\nSub Path: %s\r\nMethod: %s\r\n", rootPath->c_str(), subPath->c_str(), meth->c_str());
-  
+// NEW CODE (old code removed):
+   *path = string(c_path);
   return true;
+}
 
-}
 
 
 void HTTPRequestDispatcher::onTCPSocketEvent(TCPSocketEvent e)