blob: 35b16b4c3410718fc1cbf36248ec172659fe6257 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include <string.h>
2
Brian Silverman14fd0fb2014-01-14 21:42:01 -08003#include "aos/linux_code/output/HTTPServer.h"
4#include "aos/linux_code/output/evhttp_ctemplate_emitter.h"
5#include "aos/linux_code/output/ctemplate_cache.h"
brians343bc112013-02-10 01:53:46 +00006#include "ctemplate/template.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -08007#include "aos/linux_code/init.h"
Brian Silverman598800f2013-05-09 17:08:42 -07008#include "aos/common/logging/logging.h"
Brian Silverman14fd0fb2014-01-14 21:42:01 -08009#include "aos/linux_code/configuration.h"
brians343bc112013-02-10 01:53:46 +000010
11#include "frc971/constants.h"
12
13RegisterTemplateFilename(ROBOT_HTML, "robot.html.tpl");
14
15namespace frc971 {
16
brians343bc112013-02-10 01:53:46 +000017class CameraServer : public aos::http::HTTPServer {
18 public:
Brian Silverman66f079a2013-08-26 16:24:30 -070019 CameraServer() : HTTPServer(::aos::configuration::GetRootDirectory(), 8080),
brians2fdfc072013-02-26 05:35:15 +000020 buf_(NULL) {
brians343bc112013-02-10 01:53:46 +000021 AddPage<CameraServer>("/robot.html", &CameraServer::RobotHTML, this);
22 }
23
24 private:
25 evbuffer *buf_;
26 bool Setup(evhttp_request *request, const char *content_type) {
27 if (evhttp_add_header(evhttp_request_get_output_headers(request),
28 "Content-Type", content_type) == -1) {
29 LOG(WARNING, "adding Content-Type failed\n");
30 evhttp_send_error(request, HTTP_INTERNAL, NULL);
31 return false;
32 }
33 if (buf_ == NULL) buf_ = evbuffer_new();
34 if (buf_ == NULL) {
35 LOG(WARNING, "evbuffer_new() failed\n");
36 evhttp_send_error(request, HTTP_INTERNAL, NULL);
37 return false;
38 }
39 return true;
40 }
41 void RobotHTML(evhttp_request *request) {
42 if (!Setup(request, "text/html")) return;
43
44 ctemplate::TemplateDictionary dict(ROBOT_HTML);
45 const char *host = evhttp_find_header(
46 evhttp_request_get_input_headers(request), "Host");
47 if (host == NULL) {
48 evhttp_send_error(request, HTTP_BADREQUEST, "no Host header");
49 return;
50 }
51 const char *separator = strchrnul(host, ':');
52 size_t length = separator - host;
53 // Don't include the last ':' (or the terminating '\0') or anything else
54 // after it.
55 dict.SetValue("HOST", ctemplate::TemplateString(host, length));
56
Brian Silverman431500a2013-10-28 19:50:15 -070057 dict.SetIntValue("CENTER", constants::GetValues().camera_center);
brians343bc112013-02-10 01:53:46 +000058
59 aos::http::EvhttpCtemplateEmitter emitter(buf_);
brians2fdfc072013-02-26 05:35:15 +000060 if (!aos::http::get_template_cache()->
61 ExpandWithData(ROBOT_HTML, ctemplate::STRIP_WHITESPACE,
62 &dict, NULL, &emitter)) {
brians343bc112013-02-10 01:53:46 +000063 LOG(ERROR, "expanding the template failed\n");
64 evhttp_send_error(request, HTTP_INTERNAL, NULL);
65 return;
66 }
67 if (emitter.error()) {
68 evhttp_send_error(request, HTTP_INTERNAL, NULL);
69 return;
70 }
71 evhttp_send_reply(request, HTTP_OK, NULL, buf_);
72 }
73};
74
75} // namespace frc971
76
Brian Silverman598800f2013-05-09 17:08:42 -070077int main() {
78 ::aos::InitNRT();
79 ::frc971::CameraServer server;
80 server.Run();
81 ::aos::Cleanup();
82}