Ejemplo n.º 1
0
HTTPInputStream::HTTPInputStream(const string & url) {
  host = "";
  port = 80;
  path = "/";
  sockfd = -1;
  contentLength = -1;
  numRead = 0;

  ParseURL(url);
  //***cout << "[host=" << host << ", port=" << port << ", path=" << path << "]" << endl;
  OpenConnection();
  //***cout << "[connection established]" << endl;
  SendRequest();
  ParseHTTPHeader();
}
Ejemplo n.º 2
0
void web::ProcessWebClients()
{
	// listen for incoming clients
	EthernetClient client = m_server->available();
	if (client)
	{
		bool bReset = false;
#ifdef ARDUINO
		FILE stream_file;
		FILE * pFile = &stream_file;
		setup_sendbuf();
		fdev_setup_stream(pFile, stream_putchar, NULL, _FDEV_SETUP_WRITE);
		stream_file.udata = &client;
#else
		FILE * pFile = fdopen(client.GetSocket(), "w");
#endif
		freeMemory();
		trace(F("Got a client\n"));
		//ShowSockStatus();
		KVPairs key_value_pairs;
		char sPage[35];

		if (!ParseHTTPHeader(client, &key_value_pairs, sPage, sizeof(sPage)))
		{
			trace(F("ERROR!\n"));
			ServeError(pFile);
		}
		else
		{

			trace(F("Page:%s\n"), sPage);
			//ShowSockStatus();

			if (strcmp(sPage, "bin/setSched") == 0)
			{
				if (SetSchedule(key_value_pairs))
				{
					if (GetRunSchedules())
						ReloadEvents();
					ServeHeader(pFile, 200, "OK", false);
				}
				else
					ServeError(pFile);
			}
			else if (strcmp(sPage, "bin/setZones") == 0)
			{
				if (SetZones(key_value_pairs))
				{
					ReloadEvents();
					ServeHeader(pFile, 200, "OK", false);
				}
				else
					ServeError(pFile);
			}
			else if (strcmp(sPage, "bin/delSched") == 0)
			{
				if (DeleteSchedule(key_value_pairs))
				{
					if (GetRunSchedules())
						ReloadEvents();
					ServeHeader(pFile, 200, "OK", false);
				}
				else
					ServeError(pFile);
			}
			else if (strcmp(sPage, "bin/setQSched") == 0)
			{
				if (SetQSched(key_value_pairs))
				{
					ServeHeader(pFile, 200, "OK", false);
				}
				else
					ServeError(pFile);
			}
			else if (strcmp(sPage, "bin/settings") == 0)
			{
				if (SetSettings(key_value_pairs))
				{
					ReloadEvents();
					ServeHeader(pFile, 200, "OK", false);
				}
				else
					ServeError(pFile);
			}
			else if (strcmp(sPage, "bin/manual") == 0)
			{
				if (ManualZone(key_value_pairs))
				{
					ServeHeader(pFile, 200, "OK", false);
				}
				else
					ServeError(pFile);
			}
			else if (strcmp(sPage, "bin/run") == 0)
			{
				if (RunSchedules(key_value_pairs))
				{
					ReloadEvents();
					ServeHeader(pFile, 200, "OK", false);
				}
				else
					ServeError(pFile);
			}
			else if (strcmp(sPage, "bin/factory") == 0)
			{
				ResetEEPROM();
				ReloadEvents();
				ServeHeader(pFile, 200, "OK", false);
			}
			else if (strcmp(sPage, "bin/reset") == 0)
			{
				ServeHeader(pFile, 200, "OK", false);
				bReset = true;
			}
			else if (strcmp(sPage, "json/schedules") == 0)
			{
				JSONSchedules(key_value_pairs, pFile);
			}
			else if (strcmp(sPage, "json/zones") == 0)
			{
				JSONZones(key_value_pairs, pFile);
			}
			else if (strcmp(sPage, "json/settings") == 0)
			{
				JSONSettings(key_value_pairs, pFile);
			}
			else if (strcmp(sPage, "json/state") == 0)
			{
				JSONState(key_value_pairs, pFile);
			}
			else if (strcmp(sPage, "json/schedule") == 0)
			{
				JSONSchedule(key_value_pairs, pFile);
			}
			else if (strcmp(sPage, "json/wcheck") == 0)
			{
				JSONwCheck(key_value_pairs, pFile);
			}
#ifdef LOGGING
			else if (strcmp(sPage, "json/logs") == 0)
			{
				JSONLogs(key_value_pairs, pFile);
			}
			else if (strcmp(sPage, "json/tlogs") == 0)
			{
				JSONtLogs(key_value_pairs, pFile);
			}
#endif
			else if (strcmp(sPage, "ShowSched") == 0)
			{
				freeMemory();
				ServeSchedPage(pFile);
			}
			else if (strcmp(sPage, "ShowZones") == 0)
			{
				freeMemory();
				ServeZonesPage(pFile);
			}
			else if (strcmp(sPage, "ShowEvent") == 0)
			{
				ServeEventPage(pFile);
			}
			else if (strcmp(sPage, "ReloadEvent") == 0)
			{
				ReloadEvents(true);
				ServeEventPage(pFile);
			}
			else
			{
				if (strlen(sPage) == 0)
					strcpy(sPage, "index.htm");
				// prepend path
				memmove(sPage + 5, sPage, sizeof(sPage) - 5);
				memcpy(sPage, "/web/", 5);
				sPage[sizeof(sPage)-1] = 0;
				trace(F("Serving Page: %s\n"), sPage);
				SdFile theFile;
				if (!theFile.open(sPage, O_READ))
					Serve404(pFile);
				else
				{
					if (theFile.isFile())
						ServeFile(pFile, sPage, theFile, client);
					else
						Serve404(pFile);
					theFile.close();
				}
			}
		}

#ifdef ARDUINO
		flush_sendbuf(client);
		// give the web browser time to receive the data
		delay(1);
#else
		fflush(pFile);
		fclose(pFile);
#endif
		// close the connection:
		client.stop();

		if (bReset)
			sysreset();
	}
}