Ejemplo n.º 1
0
fawkes::WebReply *
ROSWebviewRequestProcessor::process_request(const fawkes::WebRequest *request)
{
  //logger->log_debug(__logcomp.c_str(), "Processing %s", url);

  fawkes_msgs::WebviewProcessRequest srv;
  srv.request.url = request->url();
  //srv.request.method = method;
  //srv.request.version = version;
  //srv.request.upload_data =
  //  std::vector<uint8_t>((uint8_t *)upload_data,
  //			 (uint8_t *)&upload_data[*upload_data_size]);

  if (! __srv_client.exists()) {
    return new WebErrorPageReply(WebReply::HTTP_GONE,
				 "Service %s is no longer available",
				 __srv_name.c_str());

  } else if (__srv_client.call(srv)) {
    if (srv.response.code == WebReply::HTTP_OK) {
      WebReply *r = NULL;
      if (srv.response.wrap_in_page) {
	WebPageReply *pr =
	  new WebPageReply(srv.response.title, srv.response.body);
	pr->set_html_header(srv.response.html_header);
	r = pr;
      } else {
	r = new StaticWebReply(WebReply::HTTP_OK, srv.response.body);
      }

      std::vector<std::string>::iterator h;
      for (h = srv.response.headers.begin(); h != srv.response.headers.end(); ++h)
      {
	try {
	  r->add_header(*h);
	} catch (Exception &e) {
	  // ignore
	}
      }

      return r;
    } else {
      return new WebErrorPageReply((WebReply::Code)srv.response.code,
				   "Execution of service %s failed: %s",
				   __srv_name.c_str(),
				   srv.response.error.c_str());
    }
  } else {
    return new WebErrorPageReply(WebReply::HTTP_INTERNAL_SERVER_ERROR,
				 "Execution of service %s failed",
				 __srv_name.c_str());
  }

  // should not happen...
  return NULL;
}
Ejemplo n.º 2
0
WebReply *
RRDWebRequestProcessor::process_request(const fawkes::WebRequest *request)
{
  if ( strncmp(__baseurl, request->url().c_str(), __baseurl_len) == 0 ) {
    // It is in our URL prefix range
    std::string subpath = request->url().substr(__baseurl_len);

    const RWLockVector<RRDGraphDefinition *> &graphs(__rrd_man->get_graphs());
    RWLockVector<RRDGraphDefinition *>::const_iterator g;

    ScopedRWLock(graphs.rwlock(), ScopedRWLock::LOCK_READ);

    if (subpath.find("/graph/") == 0) {
      std::string graph_name = subpath.substr(subpath.find_first_not_of("/", std::string("/graph/").length()));

      for (g = graphs.begin(); g != graphs.end(); ++g) {
	if (strcmp((*g)->get_name(), graph_name.c_str()) == 0) {
          try {
	    return new DynamicFileWebReply((*g)->get_filename());
	  } catch (Exception &e) {
            return new WebErrorPageReply(WebReply::HTTP_NOT_FOUND, e.what());
	  }
	}
      }
      return new WebErrorPageReply(WebReply::HTTP_NOT_FOUND, "Graph not found");
    } else {
      WebPageReply *r = new WebPageReply("RRD Graphs");
      r->set_html_header("  <link rel=\"stylesheet\" type=\"text/css\" "
			 "href=\"/static/css/rrdweb.css\" />\n");
      *r += "<h2>RRD Graphs</h2>\n";

      std::string subpath = request->url().substr(__baseurl_len);

      unsigned int i = 0;
      *r += "<table class=\"rrdgrid\">";
      for (g = graphs.begin(); g != graphs.end(); ++g) {
	if ((i % 2) == 0) *r += "  <tr>";
	r->append_body("<td class=\"%s\"><img src=\"/rrd/graph/%s\" /></td>",
		       ((i % 2) == 0) ? "left" : "right",
		       (*g)->get_name());
	if ((i++ % 2) == 1) *r += "  </tr>\n";
      }
      *r += "</table>";

      return r;
    }
  } else {
    return NULL;
  }
}
Ejemplo n.º 3
0
WebReply *
WebviewStartPageRequestProcessor::process_request(const char *url,
						  const char *method,
						  const char *version,
						  const char *upload_data,
						  size_t *upload_data_size,
						  void **session_data)
{
  if ( strncmp("/", url, 1) == 0 ) {

    WebPageReply *r = new WebPageReply("Fawkes", "<h1>Welcome to Fawkes.</h1>\n");

    std::list<CacheLogger::CacheEntry> & messages = __cache_logger->get_messages();
    std::list<CacheLogger::CacheEntry>::reverse_iterator i;

    *r += "<h2>Latest log messages</h2>\n";
    *r += "<table>\n";
    for (i = messages.rbegin(); i != messages.rend(); ++i) {
      CacheLogger::CacheEntry &e = *i;
      const char *color = NULL;
      switch (e.log_level) {
      case Logger::LL_DEBUG: color = "#888888"; break;
      case Logger::LL_WARN:  color = "orange";  break;
      case Logger::LL_ERROR: color = "red";     break;
      default: ;
      }
      if (color) {
	r->append_body("<tr><td>%s</td><td>%s</td><td><span style=\"color:%s\">%s</span></td></tr>\n",
		       e.timestr.c_str(), e.component.c_str(), color, e.message.c_str());
      } else {
	r->append_body("<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n",
		       e.timestr.c_str(), e.component.c_str(), e.message.c_str());
      }
    }
    *r += "</table>\n";

    return r;
  } else {
    return NULL;
  }
}
Ejemplo n.º 4
0
WebReply *
ClipsWebRequestProcessor::process_request(const fawkes::WebRequest *request)
{
  if ( strncmp(baseurl_, request->url().c_str(), baseurl_len_) == 0 ) {
    // It is in our URL prefix range
    std::string env_name = request->url().substr(baseurl_len_);
    std::string::size_type slash_pos = env_name.find("/", 1);
    std::string subpath;
    if (slash_pos != std::string::npos) {
      subpath  = env_name.substr(slash_pos);
      env_name = env_name.substr(1, slash_pos-1);
    } else if (env_name.length() > 0) {
      // remove lead slash
      env_name = env_name.substr(1);
    }

    std::map<std::string, LockPtr<CLIPS::Environment>> envs =
      clips_env_mgr_->environments();

    if (envs.find(env_name) == envs.end()) {
      if (envs.size() == 1) {
	// if there is only one just redirect
	return new WebRedirectReply(std::string(baseurl_) + "/" + envs.begin()->first);
      } else {
	WebPageReply *r = new WebPageReply("CLIPS - Environment not found");
	*r += "<h2>Environment " + env_name + " not found</h2>\n";
	if (! envs.empty()) {
	  *r += "<p>Choose on of the following existing environments:</p>\n";
	  *r += "<ul>\n";
	  for (auto env : envs) {
	    *r += std::string("<li><a href=\"") + baseurl_ + "/" +
	      env.first + "\">" + env.first + "</a></li>\n";
	  }
	  *r += "</ul>\n";
	} else {
	  *r += "<p>No environments have been registered.</p>\n";
	}
	return r;
      }
    }

    
    LockPtr<CLIPS::Environment> &clips = envs[env_name];

    if (subpath == "/assert") {
      MutexLocker lock(clips.objmutex_ptr());
      enable_error_log(clips);
      if (! request->post_value("index").empty()) {
	long int index = StringConversions::to_long(request->post_value("index"));
	retract_fact(clips, index);
      }

      clips->assert_fact(request->post_value("fact"));
      disable_error_log(clips);
      if (! errors_.empty()) {
	WebPageReply *r = new WebPageReply("CLIPS");
	*r += "<h2>CLIPS Fact Assertion</h2>\n";
	r->append_body("<p><span style=\"color:red\">Asserting '%s' failed:</span>\n<pre>",
		       request->post_value("fact").c_str());
	for (auto e : errors_) {
	  *r += e;
	}
	*r += "</pre></p>";
	r->append_body("<p><a href=\"%s\">Back</a></p>", baseurl_);

	r->append_body("<form action=\"%s/%s/assert\" method=\"post\">"
		       "<input type=\"hidden\" name=\"index\" value=\"%s\">"
		       "New fact: <input type=\"text\" name=\"fact\" value=\"%s\"/>"
		       "<input type=\"submit\" value=\"Assert\" />",
		       baseurl_, env_name.c_str(),
		       request->post_value("index").c_str(),
		       request->post_value("fact").c_str());

	return r;
      } else {
	return new WebRedirectReply(std::string(baseurl_) + "/" + env_name);
      }
    } else if (subpath.find("/retract") == 0) {
      std::string index_str = subpath.substr(9); // length of "/retract/"
      long int index = StringConversions::to_long(index_str);
      fawkes::MutexLocker lock(clips.objmutex_ptr());
      retract_fact(clips, index);
      return new WebRedirectReply(std::string(baseurl_) + "/" + env_name);
    }

    MutexLocker lock(clips.objmutex_ptr());

    WebPageReply *r = new WebPageReply("CLIPS");
    r->set_html_header("  <link type=\"text/css\" href=\"/static/css/jqtheme/"
		       "jquery-ui.custom.css\" rel=\"stylesheet\" />\n"
		       "  <script type=\"text/javascript\" src=\"/static/js/"
		       "jquery.min.js\"></script>\n"
		       "  <script type=\"text/javascript\" src=\"/static/js/"
		       "jquery-ui.custom.min.js\"></script>\n");

    *r +=
      "<style type=\"text/css\">\n"
      "  tr:hover { background-color: #eeeeee; }\n"
      "  :link:hover, :visited:hover { background-color: #bb0000; color: white; }\n"
      "  .envs { margin: 0px; padding: 0px; display: inline; }\n"
      "  .envs li { display: inline; padding-left: 8px; white-space: no-wrap; }\n"
      "</style>";

    if (envs.size() > 1) {
      *r += "Environments: <ul class=\"envs\">\n";
      for (auto env : envs) {
	*r += std::string("<li><a href=\"") + baseurl_ + "/" +
	  env.first + "\">" + env.first + "</a></li>\n";
      }
      *r += "</ul>\n";
    }


    *r += "<h2>CLIPS Facts</h2>\n";

    *r += "<table>";
    *r += "<tr><th>Index</th><th>Fact</th><th>Action</th></tr>\n";

    CLIPS::Fact::pointer fact = clips->get_facts();
    while (fact) {
      CLIPS::Template::pointer tmpl = fact->get_template();

      char tmp[16384];
      OpenStringDestination(clips->cobj(), (char *)"ProcPPForm", tmp, 16383);
      PrintFact(clips->cobj(), (char *)"ProcPPForm",
		(struct fact *)fact->cobj(), FALSE, FALSE);
      CloseStringDestination(clips->cobj(), (char *)"ProcPPForm");

      r->append_body("<tr><td>f-%li</td><td>%s</td>"
		     "<td><a href=\"%s/%s/retract/%li\">Retract</a></td>"
		     "</tr>\n",
		     fact->index(), tmp, baseurl_, env_name.c_str(), fact->index());

      std::string escaped = tmp;
      size_t pos = 0;
      while ((pos = escaped.find("\"", pos)) != std::string::npos) {
	escaped.replace(pos, 1, "&quot;");
      }

      fact = fact->next();
    }
    

    *r += "</table>";

    r->append_body("<p><form action=\"%s/%s/assert\" method=\"post\">"
		   "<input type=\"hidden\" name=\"index\" value=\"\">"
		   "New fact: <input type=\"text\" name=\"fact\" />"
		   "<input type=\"submit\" value=\"Assert\" /></form></p>",
		   baseurl_, env_name.c_str());

    return r;
  } else {
    return NULL;
  }
}