Exemple #1
0
void StreamClient::setCanClose(bool close) {
	SI_LOG_DEBUG("Connection can close: %d", close);
	_canClose = close;
}
Exemple #2
0
bool HttpServer::methodGet(SocketClient &client) {
	std::string htmlBody;
	std::string docType;
	int docTypeSize = 0;
	bool exitRequest = false;

	// Parse what to get
	if (StringConverter::isRootFile(client.getMessage())) {
		const std::string HTML_MOVED = "<html>\r\n"                            \
		                               "<head>\r\n"                            \
		                               "<title>Page is moved</title>\r\n"      \
		                               "</head>\r\n"                           \
		                               "<body>\r\n"                            \
		                               "<h1>Moved</h1>\r\n"                    \
		                               "<p>This page is moved:\r\n"            \
		                               "<a href=\"%s:%d%s\">link</a>.</p>\r\n" \
		                               "</body>\r\n"                           \
		                               "</html>";
		StringConverter::addFormattedString(docType, HTML_MOVED.c_str(), _interface.getIPAddress().c_str(), _properties.getHttpPort(), "/index.html");
		docTypeSize = docType.size();

		getHtmlBodyWithContent(htmlBody, HTML_MOVED_PERMA, "/index.html", CONTENT_TYPE_XML, docTypeSize, 0);
	} else {
		std::string file;
		if (StringConverter::hasTransportParameters(client.getMessage())) {
			processStreamingRequest(client);
		} else if (StringConverter::getRequestedFile(client.getMessage(), file)) {
			// remove first '/'
			file.erase(0, 1);

			const std::string filePath = _properties.getWebPath() + "/" + file;
			if (file.compare("data.xml") == 0) {
				makeDataXML(docType);
				docTypeSize = docType.size();

				getHtmlBodyWithContent(htmlBody, HTML_OK, file, CONTENT_TYPE_XML, docTypeSize, 0);
			} else if (file.compare("streams.xml") == 0) {
				makeStreamsXML(docType);
				docTypeSize = docType.size();

				getHtmlBodyWithContent(htmlBody, HTML_OK, file, CONTENT_TYPE_XML, docTypeSize, 0);
			} else if (file.compare(_properties.getFileName()) == 0) {
				_properties.addToXML(docType);
				docTypeSize = docType.size();

				getHtmlBodyWithContent(htmlBody, HTML_OK, file, CONTENT_TYPE_XML, docTypeSize, 0);
#ifdef LIBDVBCSA
			} else if (file.compare(_streams.getDecrypt()->getFileName()) == 0) {
				_streams.getDecrypt()->addToXML(docType);
				docTypeSize = docType.size();

				getHtmlBodyWithContent(htmlBody, HTML_OK, file, CONTENT_TYPE_XML, docTypeSize, 0);
#endif
			} else if (file.compare("log.xml") == 0) {
				docType = make_log_xml();
				docTypeSize = docType.size();

				getHtmlBodyWithContent(htmlBody, HTML_OK, file, CONTENT_TYPE_XML, docTypeSize, 0);
			} else if (file.compare("STOP") == 0) {
				exitRequest = true;

				getHtmlBodyWithContent(htmlBody, HTML_NO_RESPONSE, "", CONTENT_TYPE_HTML, 0, 0);
			} else if ((docTypeSize = readFile(filePath.c_str(), docType))) {
				if (file.find(".xml") != std::string::npos) {
					// check if the request is the SAT>IP description xml then fill in the server version, UUID and tuner string
					if (docType.find("urn:ses-com:device") != std::string::npos) {
						SI_LOG_DEBUG("Client: %s requesed %s", client.getIPAddress().c_str(), file.c_str());
						// check did we get our desc.xml (we assume there are some %s in there)
						if (docType.find("%s") != std::string::npos) {
							docTypeSize -= 4 * 2; // minus 4x %s
							docTypeSize += _properties.getDeliverySystemString().size();
							docTypeSize += _properties.getUUID().size();
							docTypeSize += _properties.getSoftwareVersion().size();
							docTypeSize += _properties.getXSatipM3U().size();
							char *doc_desc_xml = new char[docTypeSize + 1];
							if (doc_desc_xml != nullptr) {
								snprintf(doc_desc_xml, docTypeSize + 1, docType.c_str(), _properties.getSoftwareVersion().c_str(),
								         _properties.getUUID().c_str(), _properties.getDeliverySystemString().c_str(),
								         _properties.getXSatipM3U().c_str());
								docType = doc_desc_xml;
								DELETE_ARRAY(doc_desc_xml);
							}
						}
					}
					getHtmlBodyWithContent(htmlBody, HTML_OK, file, CONTENT_TYPE_XML, docTypeSize, 0);

				} else if (file.find(".html") != std::string::npos) {
					getHtmlBodyWithContent(htmlBody, HTML_OK, file, CONTENT_TYPE_HTML, docTypeSize, 0);
				} else if (file.find(".js") != std::string::npos) {
					getHtmlBodyWithContent(htmlBody, HTML_OK, file, CONTENT_TYPE_JS, docTypeSize, 0);
				} else if (file.find(".css") != std::string::npos) {
					getHtmlBodyWithContent(htmlBody, HTML_OK, file, CONTENT_TYPE_CSS, docTypeSize, 0);
				} else if (file.find(".m3u") != std::string::npos) {
					getHtmlBodyWithContent(htmlBody, HTML_OK, file, CONTENT_TYPE_VIDEO, docTypeSize, 0);
				} else if ((file.find(".png") != std::string::npos) ||
				           (file.find(".ico") != std::string::npos)) {
					getHtmlBodyWithContent(htmlBody, HTML_OK, file, CONTENT_TYPE_PNG, docTypeSize, 0);
				} else {
					getHtmlBodyWithContent(htmlBody, HTML_OK, file, CONTENT_TYPE_HTML, docTypeSize, 0);
				}
			} else {
				file = _properties.getWebPath() + "/" + "404.html";
				docTypeSize = readFile(file.c_str(), docType);
				getHtmlBodyWithContent(htmlBody, HTML_NOT_FOUND, file, CONTENT_TYPE_HTML, docTypeSize, 0);
			}
		}
	}
	// send something?
	if (docType.size() > 0) {
//		SI_LOG_INFO("%s", htmlBody);
		// send 'htmlBody' to client
		if (send(client.getFD(), htmlBody.c_str(), htmlBody.size(), 0) == -1) {
			PERROR("send htmlBody");
			return false;
		}
		// send 'docType' to client
		if (send(client.getFD(), docType.c_str(), docTypeSize, 0) == -1) {
			PERROR("send docType");
			return false;
		}
		return true;
	}
	// check did we have stop request
	if (exitRequest) {
		_properties.setExitApplication();
	}
	return false;
}