First step: AutoIP compiled in and working

Dependencies:   mbed

Revision:
1:4218cacaf696
Parent:
0:55a05330f8cc
--- a/services/http/server/impl/FSHandler.cpp	Fri Jun 18 09:11:35 2010 +0000
+++ b/services/http/server/impl/FSHandler.cpp	Fri Jun 18 15:54:21 2010 +0000
@@ -26,7 +26,9 @@
 //#define __DEBUG
 #include "dbg/dbg.h"
 
-#define CHUNK_SIZE 512//128
+#define CHUNK_SIZE 128
+
+#define DEFAULT_PAGE "/index.htm"
 
 FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket), m_err404(false)
 {}
@@ -48,9 +50,23 @@
 
 void FSHandler::doGet()
 {
-  DBG("\r\nIn FSHandler::doGet()\r\n");
+  DBG("\r\nIn FSHandler::doGet() - rootPath=%s, path=%s\r\n", rootPath().c_str(), path().c_str());
   //FIXME: Translate path to local/path
-  string filePath = m_lFsPath[rootPath()] + path();
+  string checkedRootPath = rootPath();
+  if(checkedRootPath.empty())
+    checkedRootPath="/";
+  string filePath = m_lFsPath[checkedRootPath];
+  if (path().size() > 1)
+  {
+    filePath += path();
+  }
+  else
+  {
+    filePath += DEFAULT_PAGE;
+  }
+  
+  DBG("Trying to open %s\n", filePath.c_str());
+
   m_fp = fopen(filePath.c_str(), "r"); //FIXME: if null, error 404
   
   if(!m_fp)
@@ -62,7 +78,7 @@
     respHeaders()["Content-Type"] = "text/html";
     respHeaders()["Connection"] = "close";
     writeData(msg,strlen(msg)); //Only send header
-    DBG("\r\nExit SimpleHandler::doGet() w Error 404\r\n");
+    DBG("\r\nExit FSHandler::doGet() w Error 404\r\n");
     return;
   }
     
@@ -102,14 +118,38 @@
   }
   
   static char rBuf[CHUNK_SIZE];
-  int len = fread(rBuf, 1, CHUNK_SIZE, m_fp);
-  if(len>0)
+  while(true)
   {
-    writeData(rBuf, len);
-  }
-  else
-  {
-    close(); //Data written, we can close the connection
+    int len = fread(rBuf, 1, CHUNK_SIZE, m_fp);
+    if(len>0)
+    {
+      int writtenLen = writeData(rBuf, len);
+      if(writtenLen < 0) //Socket error
+      {
+        DBG("FSHandler: Socket error %d\n", writtenLen);
+        if(writtenLen == TCPSOCKET_MEM)
+        {
+          fseek(m_fp, -len, SEEK_CUR);
+          return; //Wait for the queued TCP segments to be transmitted
+        }
+        else
+        {
+          //This is a critical error
+          close();
+          return; 
+        }
+      }
+      else if(writtenLen < len) //Short write, socket's buffer is full
+      {
+        fseek(m_fp, writtenLen - len, SEEK_CUR);
+        return;
+      }
+    }
+    else
+    {
+      close(); //Data written, we can close the connection
+      return;
+    }
   }
 }