Example #1
0
	websocketpp::http::status_code::value ApiRouter::handleHttpRequest(const string& aRequestPath,
		const websocketpp::http::parser::request& aRequest, json& output_, json& error_,
		bool aIsSecure, const string& aIp) noexcept {

		SessionPtr session = nullptr;
		auto token = aRequest.get_header("Authorization");
		if (token != websocketpp::http::empty_header) {
			session = WebServerManager::getInstance()->getUserManager().getSession(token);
		}

		auto& requestBody = aRequest.get_body();
		dcdebug("Received HTTP request: %s\n", aRequest.get_body().c_str());
		try {
			ApiRequest apiRequest(aRequestPath, aRequest.get_method(), output_, error_);
			apiRequest.validate();

			apiRequest.parseHttpRequestJson(requestBody);
			apiRequest.setSession(session);

			return handleRequest(apiRequest, aIsSecure, nullptr, aIp);
		} catch (const std::exception& e) {
			error_ = { "message", "Parsing failed: " + string(e.what()) };
			return websocketpp::http::status_code::bad_request;
		}

		return websocketpp::http::status_code::ok;
	}
Example #2
0
	websocketpp::http::status_code::value FileServer::handlePostRequest(const websocketpp::http::parser::request& aRequest,
		std::string& output_, StringPairList& headers_, const SessionPtr& aSession) noexcept {

		const auto& requestPath = aRequest.get_uri();
		if (requestPath == "/temp") {
			if (!aSession || !aSession->getUser()->hasPermission(Access::FILESYSTEM_EDIT)) {
				output_ = "Not authorized";
				return websocketpp::http::status_code::unauthorized;
			}

			const auto fileName = Util::toString(Util::rand());
			const auto filePath = Util::getTempPath() + fileName;

			try {
				File file(filePath, File::WRITE, File::TRUNCATE | File::CREATE, File::BUFFER_SEQUENTIAL);
				file.write(aRequest.get_body());
			} catch (const FileException& e) {
				output_ = "Failed to write the file: " + e.getError();
				return websocketpp::http::status_code::internal_server_error;
			}

			{
				WLock l(cs);
				tempFiles.emplace(fileName, filePath);
			}

			headers_.emplace_back("Location", fileName);
			return websocketpp::http::status_code::created;
		}

		output_ = "Requested resource was not found";
		return websocketpp::http::status_code::not_found;
	}