onion_connection_status Onion::render_to_response(::Onion::template_f fn, const ::Onion::Dict& context, ::Onion::Response &res) { ONION_DEBUG("Context: %s", context.toJSON().c_str()); onion_dict *d=onion_dict_dup( context.c_handler() ); fn(d, res.c_handler()); return OCS_PROCESSED; }
/* Like 'search_file' */ onion_connection_status OnionServer::getSettingKinectWrapped( Onion::Request &req, Onion::Response &res) { const char* path = onion_request_get_fullpath( req.c_handler() ); #ifdef VERBOSE printf("Request of %s.\n",path); #endif std::string filename("./html/"); filename.append(path); std::ifstream file(filename.c_str()); std::string line; if( file.is_open()){ /* Create header with mime type and charset information for several file extensions. * This is just a workaround. There should be an automatic mechanicm * in libonion. */ int periodPos = filename.find_last_of('.'); std::string extension = filename.substr(periodPos+1); std::string key("Content-Type"); std::string defaultType("text/html; charset: utf-8"); std::string mime = m_mimedict.get( extension , defaultType ) ; res.setHeader(key,mime); onion_response_write_headers(res.c_handler());// missing in cpp bindings? //res.writeHeaders();//this was added by me... try{ /* res.write("json_kinect = ", 14); const char* kinect = m_settingKinect.getConfig(true); size_t len = strlen( kinect ); res.write(kinect, (int) len ); res.write(";\n", 2 ); */ while (std::getline(file, line)) { res.write( line.c_str(), line.size() ); res.write("\n", 1 ); } }//catch ( const boost::iobase::failure &ex ) catch ( const std::exception & ex ){ std::cerr << "Can not read " << filename << std::endl; res.write( ErrReadFailed.c_str(), ErrReadFailed.size()); } }else{ res.write( ErrNotFound.c_str(), ErrNotFound.size()); } return OCS_PROCESSED; }
/* Replace some template variables and send b9creator_settings.js */ onion_connection_status OnionServer::getB9CreatorSettingsWrapped( Onion::Request &req, Onion::Response &res) { /* std::string key("ONION_JSON"); std::string conf(m_b9CreatorSettings.getConfig()); Onion::Dict d; d.add(key, conf, 0); return b9creator_settings_js_template(d.c_handler(), req.c_handler(), res.c_handler()); */ onion_dict *d=onion_dict_new();//will free'd in template call onion_dict_add( d, "ONION_JSON", m_b9CreatorSettings.getConfig(), 0); return b9creator_settings_js_template(d, req.c_handler(), res.c_handler()); }
/* Replace some template variables (filename of last config) call index_html_template */ onion_connection_status OnionServer::index_html( Onion::Request &req, Onion::Response &res) { /* Problem: This cause double free of mem because * onion_dict_free will called twice: in index_html_template and deconstructor. * Onion::Dict d; std::string key("LAST_SETTING_FILENAME"); d.add(key,m_b9CreatorSettings.m_configFilename,0); return index_html_template(d.c_handler(), req.c_handler(), res.c_handler() ); */ onion_dict *d=onion_dict_new();//will free'd in template call onion_dict_add( d, "LAST_SETTING_FILENAME", m_b9CreatorSettings.m_configFilename.c_str(), 0); return index_html_template(d, req.c_handler(), res.c_handler() ); }
/* Replace some template variables (filename of last config) call index_html_template */ onion_connection_status OnionServer::index_html( Onion::Request &req, Onion::Response &res) { /* Issue note: The following cause double free of mem because * onion_dict_free will called twice: in index_html_template and deconstructor. * Onion::Dict d; std::string key("LAST_SETTING_FILENAME"); d.add(key,m_settingKinect.m_configFilename,0); return index_html_template(d.c_handler(), req.c_handler(), res.c_handler() ); => Thus, use pointer, but do not free here. */ onion_dict *d=onion_dict_new();//will free'd in template call onion_dict_add( d, "LAST_SETTING_FILENAME", m_settingKinect.m_configFilename.c_str(), 0); return index_html_template(d, req.c_handler(), res.c_handler() ); }
onion_connection_status Onion::redirect(const std::string& url, ::Onion::Request& req, ::Onion::Response& res) { return onion_shortcut_redirect(url.c_str(), req.c_handler(), res.c_handler()); }
onion_connection_status extended_html_template(Onion::Request &req, Onion::Response &res){ extended_html_template(NULL,req.c_handler(), res.c_handler()); }
onion_connection_status extended_html_template(Onion::Dict &d, Onion::Request &req, Onion::Response &res){ extended_html_template(onion_dict_dup(d.c_handler()),req.c_handler(), res.c_handler()); }