void WSStreamerHandler::on_open(websocketpp::session_ptr client) { if (client->get_resource() != "/") { shared_ptr<ifstream> resource(new ifstream(client->get_resource().substr(1).c_str(), ios::binary)); if (resource->is_open()) { cout << "INFO: Client has connected and opened " + client->get_resource() << endl; // Check if it is a rtpdump file. If it is, fast foward past file header if (resource->good()) { streampos filestart = resource->tellg(); char* rtpdumphdr = new char[strlen(RTPPLAY_MAGIC)]; resource->read(rtpdumphdr, strlen(RTPPLAY_MAGIC)); if (resource->gcount() == strlen(RTPPLAY_MAGIC) && strncmp(rtpdumphdr, RTPPLAY_MAGIC, strlen(RTPPLAY_MAGIC)) == 0) { cout << "INFO: Requested file is an rtpdump file. Fast forwarding past file header" << endl; resource->ignore(sizeof(RD_hdr_t)); } else { // Reset file to beginning if we do not see rtpdump header resource->seekg(filestart); } delete[] rtpdumphdr; } WSSClientInfo clientInfo; clientInfo.resource = resource; connections.insert(pair<websocketpp::session_ptr, WSSClientInfo>(client, clientInfo)); } else { cerr << "ERROR: Client has connected but server is unable to access " + client->get_resource() << endl; client->send("ERROR: Failed to open resource"); } } }
void WSStreamerHandler::validate(websocketpp::session_ptr client) { // Check if requested resource exists if (client->get_resource() == "/") { cout << "INFO: Client is connecting without asking for a resource" << endl; } else { ifstream resource(client->get_resource().substr(1).c_str(), ios::binary); if (!resource.is_open()) { string err = "Request for unknown resource " + client->get_resource(); cerr << err << endl; throw(websocketpp::handshake_error(err, 404)); } else { cout << "INFO: Client request for " + client->get_resource() + " accepted" << endl; resource.close(); } } }
void data_server_handler::on_client_connect(websocketpp::session_ptr client){ static const boost::regex nonolith_domain("^https?://[[:w:]\\.-]*?nonolithlabs.com$"); static const boost::regex localhost_domain("^https?://localhost(:[[:d:]]+)?$"); const string origin = client->get_client_header("Origin"); if (!allowAnyOrigin && origin!="" && origin!="null" && !regex_match(origin, localhost_domain) && !regex_match(origin, nonolith_domain) ){ client->start_http(403, "Origin not allowed"); std::cerr << "Rejected client with unknown origin " << origin << std::endl; return; } const std::string resource = client->get_resource(); Url url(resource); UrlPath path(url, 1); try{ if (path.leaf()){ // "/" client->set_header("Location", redir_url); client->start_http(301); }else if (path.matches("rest")){ handleJSONRequest(path.sub(), client); }else if (path.matches("ws")){ client->start_websocket(); }else{ client->start_http(404, "Not found"); } }catch(std::exception& e){ JSONNode j; j.push_back(JSONNode("error", e.what())); std::cerr << "Exception while processing request: " << e.what() <<std::endl; respondJSON(client, j, 500); } }