brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame^] | 1 | #include <string.h> |
| 2 | |
| 3 | #include "aos/aos_core.h" |
| 4 | #include "aos/atom_code/output/HTTPServer.h" |
| 5 | #include "aos/atom_code/output/evhttp_ctemplate_emitter.h" |
| 6 | #include "ctemplate/template.h" |
| 7 | |
| 8 | #include "frc971/constants.h" |
| 9 | |
| 10 | RegisterTemplateFilename(ROBOT_HTML, "robot.html.tpl"); |
| 11 | |
| 12 | namespace frc971 { |
| 13 | |
| 14 | const char *const kPath = "/home/driver/robot_code/bin/"; |
| 15 | //const char *const kPath = "/home/brians/Desktop/git_frc971/2012/trunk/src/frc971/output"; |
| 16 | |
| 17 | class CameraServer : public aos::http::HTTPServer { |
| 18 | public: |
| 19 | CameraServer() : HTTPServer(kPath, 8080), buf_(NULL) { |
| 20 | AddPage<CameraServer>("/robot.html", &CameraServer::RobotHTML, this); |
| 21 | } |
| 22 | |
| 23 | private: |
| 24 | evbuffer *buf_; |
| 25 | bool Setup(evhttp_request *request, const char *content_type) { |
| 26 | if (evhttp_add_header(evhttp_request_get_output_headers(request), |
| 27 | "Content-Type", content_type) == -1) { |
| 28 | LOG(WARNING, "adding Content-Type failed\n"); |
| 29 | evhttp_send_error(request, HTTP_INTERNAL, NULL); |
| 30 | return false; |
| 31 | } |
| 32 | if (buf_ == NULL) buf_ = evbuffer_new(); |
| 33 | if (buf_ == NULL) { |
| 34 | LOG(WARNING, "evbuffer_new() failed\n"); |
| 35 | evhttp_send_error(request, HTTP_INTERNAL, NULL); |
| 36 | return false; |
| 37 | } |
| 38 | return true; |
| 39 | } |
| 40 | void RobotHTML(evhttp_request *request) { |
| 41 | if (!Setup(request, "text/html")) return; |
| 42 | |
| 43 | ctemplate::TemplateDictionary dict(ROBOT_HTML); |
| 44 | const char *host = evhttp_find_header( |
| 45 | evhttp_request_get_input_headers(request), "Host"); |
| 46 | if (host == NULL) { |
| 47 | evhttp_send_error(request, HTTP_BADREQUEST, "no Host header"); |
| 48 | return; |
| 49 | } |
| 50 | const char *separator = strchrnul(host, ':'); |
| 51 | size_t length = separator - host; |
| 52 | // Don't include the last ':' (or the terminating '\0') or anything else |
| 53 | // after it. |
| 54 | dict.SetValue("HOST", ctemplate::TemplateString(host, length)); |
| 55 | |
| 56 | int center; |
| 57 | if (!constants::camera_center(¢er)) { |
| 58 | evhttp_send_error(request, HTTP_INTERNAL, NULL); |
| 59 | return; |
| 60 | } |
| 61 | dict.SetIntValue("CENTER", center); |
| 62 | |
| 63 | aos::http::EvhttpCtemplateEmitter emitter(buf_); |
| 64 | if (!ctemplate::ExpandTemplate(ROBOT_HTML, ctemplate::STRIP_WHITESPACE, |
| 65 | &dict, &emitter)) { |
| 66 | LOG(ERROR, "expanding the template failed\n"); |
| 67 | evhttp_send_error(request, HTTP_INTERNAL, NULL); |
| 68 | return; |
| 69 | } |
| 70 | if (emitter.error()) { |
| 71 | evhttp_send_error(request, HTTP_INTERNAL, NULL); |
| 72 | return; |
| 73 | } |
| 74 | evhttp_send_reply(request, HTTP_OK, NULL, buf_); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | } // namespace frc971 |
| 79 | |
| 80 | AOS_RUN_NRT(frc971::CameraServer) |
| 81 | |