virtual bool handleEvent(ServerHandlingEvent eventCode, MongooseConnection &connection, const MongooseRequest &request, MongooseResponse &response) { bool res = false; if (eventCode == MG_NEW_REQUEST) { if (request.getUri() == string("/info")) { handleInfo(request, response); res = true; } } return res; }
const string generateInfoContent(const MongooseRequest &request) { string result; result = "<h1>Sample Info Page</h1>"; result += "<br />Request URI: " + request.getUri(); result += "<br />Your IP: " + ipToString(request.getRemoteIp()); time_t tim; time(&tim); result += "<br />Current date & time: " + toString(ctime(&tim)); result += "<br /><br /><a href=\"/\">Index page</a>"; return result; }
const string search(const MongooseRequest& request) { string result; result = "<h1>Sample Info Page</h1>"; result += "<br />Request URI: " + request.getUri(); vector<uint64_t> matches; get_matches(request, matches); //result += "<br />Seq is: " + seq; result += "<br />Found " + to_string(matches.size()) + " matches:"; for ( uint i = 0; i < matches.size(); i++ ) { result += "<br />" + to_string(matches[i]); } return result; }
bool HttpServer::handleEvent(ServerHandlingEvent eventCode, MongooseConnection &connection, const MongooseRequest &request, MongooseResponse &response) { bool res = false; switch (eventCode) { case MG_NEW_REQUEST: // check that we want to handle this. if (request.getUri().substr(0,strlen("/racka3/")) == "/racka3/") { res = handleNewRequest(eventCode,connection,request,response); } break; default: break; } return res; }
bool HttpServer::handleNewRequest(ServerHandlingEvent eventCode, MongooseConnection &connection, const MongooseRequest &request, MongooseResponse &response) { bool res = false; cJSON* json = 0; string uri = request.getUri(); bool handled = false; // if this is a post, get the post data if (request.getRequestMethod() == "POST") { std::string buffer; char c; while (connection.read(&c,1)) { buffer.push_back(c); } json = cJSON_Parse(buffer.c_str()); // if we couldnt parse the json? make empty json if (!json) { json = cJSON_CreateObject(); } } else { // make an empty json object json = cJSON_CreateObject(); } // now act depending on the path if (uri == string("/racka3/getallplugins")) // <-- rack manipulation { _pluginHost->getAvailablePlugins(json); } else if (uri == string("/racka3/addplugin")) { _pluginHost->addPlugin(json); } else if (uri == string("/racka3/removeplugin")) { _pluginHost->removePlugin(json); } else if (uri == string("/racka3/moveplugin")) { _pluginHost->movePlugin(json); } else if (uri == string("/racka3/setparamvalue")) { _pluginHost->setPluginParam(json); } else if (uri == string("/racka3/getpluginchain")) { _pluginHost->getPluginChain(json); } else if (uri == string("/racka3/storePluginPreset")) { // <-- plugin presets _pluginHost->storePluginPreset(json); } else if (uri == string("/racka3/deletePluginPreset")) { _pluginHost->deletePluginPreset(json); } else if (uri == string("/racka3/storeRackPreset")) { // <-- rack presets _pluginHost->storeRackPreset(json); } else if (uri == string("/racka3/deleteRackPreset")) { _pluginHost->deleteRackPreset(json); } else if (uri == string("/racka3/loadRackPreset")) { _pluginHost->loadRackPreset(json); } else if (uri == string("/racka3/listRackPresets")) { _pluginHost->listRackPresets(json); } else if (uri == string("/racka3/listDevices")) { // <-- soundcard configuration _soundInterface->listDevices(json); } else if (uri == string("/racka3/setDevice")) { if (_pluginHost->getPluginChainSize()) cJSON_AddStringToObject(json,"error","cannot change sound settings unless rack is empty"); else _soundInterface->init(json); } else if (uri == string("/racka3/getCurrentDevice")) { _soundInterface->getCurrent(json); } // did we make any json? if (json->child) { // output the json char * content = cJSON_Print(json); response.setStatus(200); response.setContentType("application/json"); response.addContent(content); free(content); } else { // no json: error code for you. response.setStatus(404); } response.setCacheDisabled(); response.setConnectionAlive(false); response.write(); // clean up cJSON_Delete(json); return res; }
virtual bool handleEvent(ServerHandlingEvent eventCode, MongooseConnection& connection, const MongooseRequest& request, MongooseResponse& response) { bool res = false; //gets set to true if successful string result, content_type; if (eventCode == MG_NEW_REQUEST) { string uri = request.getUri(); //returns 0 if they match if ( ! uri.compare(0, 4, "/api") ) { cerr << "API URI: " << uri << endl; //prefix is api so we know it will be json content_type = "application/json"; try { if ( uri == string("/api/search") ) { result = search_json(request); res = true; } else if ( uri == string("/api/id") ) { result = id_json(request); res = true; } else if ( uri == string("/api/off_targets") ) { result = find_off_targets(request); res = true; } else if ( uri == string("/api/off_targets_by_seq") ) { result = off_targets_by_seq(request); res = true; } else { cerr << "Couldn't find api URI " << uri << endl; } } catch (const runtime_error& error) { string e = error.what(); result = json_error( e ); res = true; //we need this or we return 404 } if ( res ) handle_jsonp(request, result); } else { cerr << "URI: " << uri << endl; content_type = "text/html"; try { if ( uri == string("/search") ) { result = search(request); } else { cerr << "Couldn't find URI " << uri << endl; } } catch (const runtime_error& error) { result = "<h1>" + string(error.what()) + "</h1>"; } } } if ( res ) { process_response(request, response, content_type, result); last_accessed.set(); //update last accessed to now } return res; }