コード例 #1
0
ファイル: PageRequestHandler.C プロジェクト: oldoak/sandbox
void PageRequestHandler::handleRequest
(HTTPServerRequest& request, HTTPServerResponse& response)
{
    response.setChunkedTransferEncoding(true);
    response.setContentType("text/html");
    const char *message = "The quick brown fox jumps over the lazy dog.\n";
    for(int i {0}; i < 100; ++i)
    {
        response.sendBuffer(message, 44);
    }
}
コード例 #2
0
void CHTTPRequestHandler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
	response.setContentType("application/json");
	response.setChunkedTransferEncoding(true);
	if(m_buf == NULL)
		m_buf = new char[512];
	memset(m_buf, 0, 512);
	request.stream().getline(m_buf, 512, '\n');
	infof("%s, %d: Receive HTTP request[%s]", __FILE__, __LINE__, m_buf);
	JSON::Parser parser;
	Dynamic::Var var;
	try
	{
		var = parser.parse(m_buf);
	}
	catch(Exception& e)
	{
		JSON::Object::Ptr re = new JSON::Object;
		re->set(KEY_TYPE_STR, TYPE_RESPONSE_STR);
		re->set(KEY_RESULT_STR, RESULT_FAIL_STR);
		re->set(KEY_DETAIL_STR, "100");
		DynamicStruct ds_res = *re;
		response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length());
		infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str());
		re = NULL;
		return;
	}
	JSON::Object::Ptr obj = var.extract<JSON::Object::Ptr>();
	JSON::Object::Ptr res = new JSON::Object(*obj);
	if(!checkRequestFormat(obj, res))
	{
		DynamicStruct ds_res = *res;
		response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length());
		infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str());
		res = NULL;
		return;
	}
	DynamicStruct ds = *obj;
	std::string uuid = ds[KEY_PARAM_STR][REG_UUID_STR].toString();
	std::string action = ds[KEY_ACTION_STR].toString();
	std::string component = "";
	std::string method = "";
	bool ret = parseAction(action, component, method);
	if(!ret)
	{
		res->set(KEY_RESULT_STR, RESULT_FAIL_STR);
		res->set(KEY_DETAIL_STR, "105");
		DynamicStruct ds_res = *res;
		response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length());
		infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str());
		res = NULL;
		return;
	}
	if(component == COMPONENT_SERVER_STR)
	{
		if(method == SERVER_METHOD_CHECK)
		{
			res->set(KEY_RESULT_STR, RESULT_GOOD_STR);
			CDeviceManager* dev_mgr = CDeviceManager::instance();
			DeviceInfo* dev_info = dev_mgr->getDevice(uuid);
			DynamicStruct param;
			param[REG_UUID_STR] = uuid;
			if(dev_info != NULL)
			{
				param[REG_STATE_STR] = "online";
				param[REG_DEV_TYPE_STR] = dev_info->devType;
			}
			else
			{
				param[REG_STATE_STR] = "offline";
			}
			res->remove(KEY_PARAM_STR);
			res->set(KEY_PARAM_STR, param);
		}
		else
		{
			res->set(KEY_RESULT_STR, RESULT_FAIL_STR);
			res->set(KEY_DETAIL_STR, "106");
		}
		DynamicStruct ds_res = *res;
		infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str());
		response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length());
		res = NULL;
		return;
	}
	else if(component == COMPONENT_UPDATE_STR)
	{
		std::string detail = "";
		if(method == UPDATE_METHOD_CHECK)
		{
			CUpdateManager* update_manager = CUpdateManager::instance();
			JSON::Object::Ptr pParam = obj->getObject(KEY_PARAM_STR);
			if(update_manager->checkUpdate(pParam, detail))
			{
				res->set(KEY_RESULT_STR, RESULT_GOOD_STR);
				res->remove(KEY_PARAM_STR);
				res->set(KEY_PARAM_STR, pParam);
			}
			else
			{
				res->set(KEY_RESULT_STR, RESULT_FAIL_STR);
				res->set(KEY_DETAIL_STR, detail);
			}
		}
		else
		{
			res->set(KEY_RESULT_STR, RESULT_FAIL_STR);
			res->set(KEY_DETAIL_STR, "106");
		}
		DynamicStruct ds_res = *res;
		infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str());
		response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length());
		res = NULL;
		return;
	}
	RequestInfo* req = new RequestInfo((UInt64)this, uuid, 5*1000*1000, obj);
	CRegServer* reg_server = CRegServer::instance();
	//it will hang until response send back, or timeout
	reg_server->sendRequest(req);
	res = req->response;
	if(res.isNull())
	{
		warnf("%s, %d: Request timeout.", __FILE__, __LINE__);
		DynamicStruct result = *obj;
		result[KEY_RESULT_STR] = RESULT_FAIL_STR;
		result[KEY_DETAIL_STR] = "timeout";
		infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, result.toString().c_str());
		response.sendBuffer(result.toString().c_str(), result.toString().length());
	}
	else
	{
		DynamicStruct result = *res;
		infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, result.toString().c_str());
		response.sendBuffer(result.toString().c_str(), result.toString().length());
	}
	res = NULL;
	delete req;
}
コード例 #3
0
void DiskInfoRequestHandler::handleRequest(HTTPServerRequest& request,
		HTTPServerResponse& response) {

	Application& app = Application::instance();

	app.logger().information("Start processing request from: "+request.clientAddress().toString());

	Timestamp start;

	SharedPtr<HDDExplorer> explorer = hddExplorerCache.get(0);

	if(explorer.isNull()){
		explorer = new HDDExplorer;
		hddExplorerCache.add(0, explorer);
	}

	Object responseObject;

	if((*explorer).isSuccessfullyDiscovered() && (*explorer).getAvailableHDDs().size()>0){
		responseObject.set("found", true);

		vector<string> hdds = (*explorer).getAvailableHDDs();

		Array disks;

		for(vector<string>::iterator iter = hdds.begin(); iter!=hdds.end(); iter++){

			string hddPath = *iter;

			SharedPtr<DiskInfo> info = smartInfoCache.get(hddPath);

			if(info.isNull()){
				info = new DiskInfo(hddPath);
				smartInfoCache.add(hddPath, info);
			}

			Object jsonObject = (*info).toJSONObject();

			disks.add(jsonObject);

		}

		responseObject.set("disks", disks);
	} else {
		responseObject.set("found", false);
	}

	response.setContentType("application/javascript; charset=utf-8");

	HTMLForm form(request);

	string callbackFunc = "callback";

	if(form.has("callback") && !form.get("callback").empty()){
		callbackFunc = form.get("callback");
	}

	std::stringstream ostr;

	ostr<<callbackFunc<<"("; //send callback for JSONP
	responseObject.stringify(ostr);
	ostr<<")";

	string serialized = ostr.str();

	response.sendBuffer(serialized.c_str(), serialized.size());//connection keep-alive. We need to
	//know content-length

	Timestamp end;

	string secs = NumberFormatter::format((end-start)/1000)+" msecs";

	app.logger().information("Request from: "+request.clientAddress().toString()+
			" succesfully processed in "+secs);
}