// Write a number of bytes if we can, returning true if we wrote anything
bool Webserver::WriteBytes()
{
	Network *net = platform->GetNetwork();
	uint8_t i;
	for (i = 0; i < 50 && writing && net->CanWrite(); )
	{
		++i;
		if(jsonPointer >= 0)
		{
		  if(jsonResponse[jsonPointer])
		  {
			  net->Write(jsonResponse[jsonPointer++]);
		  }
		  else
		  {
			jsonPointer = -1;
			jsonResponse[0] = 0;
			CloseClient();
			break;
		  }
		} else
		{
		  char b;
		  if(fileBeingSent->Read(b))
		  {
			  net->Write(b);
		  }
		  else
		  {
			fileBeingSent->Close();
			CloseClient();
			break;
		  }
		}
	}
	return i != 0;
}
void Webserver::SendFile(const char* nameOfFileToSend)
{
  char sLen[SHORT_STRING_LENGTH];
  bool zip = false;
    
  if(StringStartsWith(nameOfFileToSend, KO_START))
    GetJsonResponse(&nameOfFileToSend[KO_FIRST]);
    
  if(jsonPointer < 0)
  {
    fileBeingSent = platform->GetFileStore(platform->GetWebDir(), nameOfFileToSend, false);
    if(fileBeingSent == NULL)
    {
      nameOfFileToSend = FOUR04_FILE;
      fileBeingSent = platform->GetFileStore(platform->GetWebDir(), nameOfFileToSend, false);
    }
    writing = (fileBeingSent != NULL);
  } 
  
  Network *net = platform->GetNetwork();
  net->Write("HTTP/1.1 200 OK\n");
  net->Write("Content-Type: ");
  
  if(StringEndsWith(nameOfFileToSend, ".png"))
	  net->Write("image/png\n");
  else if(StringEndsWith(nameOfFileToSend, ".ico"))
	  net->Write("image/x-icon\n");
  else if (jsonPointer >= 0)
	  net->Write("application/json\n");
  else if(StringEndsWith(nameOfFileToSend, ".js"))
	  net->Write("application/javascript\n");
  else if(StringEndsWith(nameOfFileToSend, ".css"))
	  net->Write("text/css\n");
  else if(StringEndsWith(nameOfFileToSend, ".htm") || StringEndsWith(nameOfFileToSend, ".html"))
	  net->Write("text/html\n");
  else if(StringEndsWith(nameOfFileToSend, ".zip"))
  {
	  net->Write("application/zip\n");
	  zip = true;
  } else
	  net->Write("application/octet-stream\n");

  if (jsonPointer >=0)
  {
	net->Write("Content-Length: ");
    snprintf(sLen, SHORT_STRING_LENGTH, "%d", strlen(jsonResponse));
    net->Write(sLen);
    net->Write("\n");
  }
    
  if(zip)
  {
	net->Write("Content-Encoding: gzip\n");
	net->Write("Content-Length: ");
    snprintf(sLen, SHORT_STRING_LENGTH, "%llu", fileBeingSent->Length());
    net->Write(sLen);
    net->Write("\n");
  }
    
  net->Write("Connection: close\n");
  net->Write('\n');
}