brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include <string.h> |
| 2 | |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 3 | #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" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 6 | #include "ctemplate/template.h" |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 7 | #include "aos/linux_code/init.h" |
Brian Silverman | 598800f | 2013-05-09 17:08:42 -0700 | [diff] [blame] | 8 | #include "aos/common/logging/logging.h" |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 9 | #include "aos/linux_code/configuration.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | |
| 11 | #include "frc971/constants.h" |
| 12 | |
| 13 | RegisterTemplateFilename(ROBOT_HTML, "robot.html.tpl"); |
| 14 | |
| 15 | namespace frc971 { |
| 16 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 17 | class CameraServer : public aos::http::HTTPServer { |
| 18 | public: |
Brian Silverman | 66f079a | 2013-08-26 16:24:30 -0700 | [diff] [blame] | 19 | CameraServer() : HTTPServer(::aos::configuration::GetRootDirectory(), 8080), |
brians | 2fdfc07 | 2013-02-26 05:35:15 +0000 | [diff] [blame] | 20 | buf_(NULL) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 21 | 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 Silverman | 431500a | 2013-10-28 19:50:15 -0700 | [diff] [blame] | 57 | dict.SetIntValue("CENTER", constants::GetValues().camera_center); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 58 | |
| 59 | aos::http::EvhttpCtemplateEmitter emitter(buf_); |
brians | 2fdfc07 | 2013-02-26 05:35:15 +0000 | [diff] [blame] | 60 | if (!aos::http::get_template_cache()-> |
| 61 | ExpandWithData(ROBOT_HTML, ctemplate::STRIP_WHITESPACE, |
| 62 | &dict, NULL, &emitter)) { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 63 | 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 Silverman | 598800f | 2013-05-09 17:08:42 -0700 | [diff] [blame] | 77 | int main() { |
| 78 | ::aos::InitNRT(); |
| 79 | ::frc971::CameraServer server; |
| 80 | server.Run(); |
| 81 | ::aos::Cleanup(); |
| 82 | } |