Esempio n. 1
0
void Webserver::SendJsonResponse(const char* command)
{
	Network *net = reprap.GetNetwork();
	RequestState *req = net->GetRequest(NULL);
	bool keepOpen = false;
	bool mayKeepOpen;
	if (numQualKeys == 0)
	{
		mayKeepOpen = GetJsonResponse(command, "", "", 0);
	}
	else
	{
		mayKeepOpen = GetJsonResponse(command, qualifiers[0].key, qualifiers[0].value, qualifiers[1].key - qualifiers[0].value - 1);
	}
	if (mayKeepOpen)
	{
		// Check that the browser wants to persist the connection too
		for (size_t i = 0; i < numHeaderKeys; ++i)
		{
			if (StringEquals(headers[i].key, "Connection"))
			{
// Comment out the following line to disable persistent connections
				keepOpen = StringEquals(headers[i].value, "keep-alive");
				break;
			}
		}
	}
	req->Write("HTTP/1.1 200 OK\n");
	req->Write("Content-Type: application/json\n");
	req->Printf("Content-Length: %u\n", strlen(jsonResponse));
	req->Printf("Connection: %s\n\n", keepOpen ? "keep-alive" : "close");
	req->Write(jsonResponse);
	net->SendAndClose(NULL, keepOpen);
}
Esempio n. 2
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');
}