blob: b0d6b9326a89bddf994fa5091fca1bfeacff1015 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "aos/atom_code/output/HTTPServer.h"
2
3#include <inttypes.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <fcntl.h>
Brian Silvermanf665d692013-02-17 22:11:39 -08007#include <string.h>
brians343bc112013-02-10 01:53:46 +00008
9#include <memory>
10
11#include "event2/event.h"
12
13#include "aos/aos_core.h"
14#include "aos/common/scoped_fd.h"
15#include "aos/common/unique_malloc_ptr.h"
16
17namespace aos {
18namespace http {
19
20HTTPServer::HTTPServer(const char *directory, uint16_t port) :
21 directory_(directory), base_(event_base_new()), http_(evhttp_new(base_)) {
22 if (base_ == NULL) {
23 LOG(FATAL, "couldn't create an event_base\n");
24 }
25 if (http_ == NULL) {
26 LOG(FATAL, "couldn't create an evhttp\n");
27 }
28 if (evhttp_bind_socket(http_, "0.0.0.0", port) != 0) {
29 LOG(FATAL, "evhttp_bind_socket(%p, \"0.0.0.0\", %"PRIu16") failed\n",
30 http_, port);
31 }
32 evhttp_set_gencb(http_, StaticServeFile, this);
33}
34
35void HTTPServer::AddPage(const std::string &path,
36 void (*handler)(evhttp_request *, void *), void *data) {
37 switch (evhttp_set_cb(http_, path.c_str(), handler, data)) {
38 case 0:
39 LOG(DEBUG, "set callback handler for '%s'\n", path.c_str());
40 break;
41 case -1:
42 LOG(INFO, "changed callback handler for '%s'\n", path.c_str());
43 break;
44 default:
45 LOG(WARNING, "evhttp_set_cb(%p, %s, %p, %p) failed\n", http_, path.c_str(),
46 handler, data);
47 break;
48 }
49}
50
51void HTTPServer::AddStandardHeaders(evhttp_request *request) {
52 if (evhttp_add_header(evhttp_request_get_output_headers(request),
53 "Server", "aos::HTTPServer/0.0") == -1) {
54 LOG(WARNING, "adding Server header failed\n");
55 }
56}
57
58namespace {
59// All of these functions return false, NULL, or -1 if they fail (and send back
60// an error).
61
62// Returns the path of the file that is being requested.
63const char *GetPath(evhttp_request *request) {
64 // Docs are unclear whether this needs freeing, but it looks like it just
65 // returns an internal field of the request.
66 // Running valgrind with no freeing of uri or path doesn't report anything
67 // related to this code.
68 const evhttp_uri *uri = evhttp_request_get_evhttp_uri(request);
69 const char *path = evhttp_uri_get_path(uri);
70 if (path == NULL) {
71 evhttp_send_error(request, HTTP_BADREQUEST, "need a path");
72 return NULL;
73 }
74 if (strstr(path, "..") != NULL) {
75 evhttp_send_error(request, HTTP_NOTFOUND, "no .. allowed!!");
76 return NULL;
77 }
78 return path;
79}
80// Returns an fd open for reading for the file at "directory/path".
81int OpenFile(evhttp_request *request, const char *path,
82 const char *directory) {
83 char *temp;
84 if (asprintf(&temp, "%s/%s", directory, path) == -1) {
85 LOG(WARNING, "asprintf(%p, \"%%s/%%s\", %p, %p) failed with %d: %s\n",
86 &temp, directory, path, errno, strerror(errno));
87 evhttp_send_error(request, HTTP_INTERNAL, NULL);
88 return -1;
89 }
90 const unique_c_ptr<char> filename(temp);
91 ScopedFD file(open(filename.get(), O_RDONLY));
92 if (!file) {
93 if (errno == ENOENT) {
94 evhttp_send_error(request, HTTP_NOTFOUND, NULL);
95 return -1;
96 }
97 LOG(ERROR, "open('%s', 0) failed with %d: %s\n", filename.get(),
98 errno, strerror(errno));
99 evhttp_send_error(request, HTTP_INTERNAL, NULL);
100 return -1;
101 }
102 return file.release();
103}
104// Returns the size of the file specified by the given fd.
105off_t GetSize(int file) {
106 struct stat info;
107 if (fstat(file, &info) == -1) {
108 LOG(ERROR, "stat(%d, %p) failed with %d: %s\n", file, &info,
109 errno, strerror(errno));
110 return -1;
111 }
112 return info.st_size;
113}
114bool SendFileResponse(evhttp_request *request, int file_num) {
115 ScopedFD file(file_num);
116 const off_t size = GetSize(file.get());
117 if (size == -1) {
118 evhttp_send_error(request, HTTP_INTERNAL, NULL);
119 return false;
120 }
121 evbuffer *const buf = evhttp_request_get_output_buffer(request);
122 if (evbuffer_add_file(buf, file.get(), 0, size) == -1) {
123 LOG(WARNING, "evbuffer_add_file(%p, %d, 0, %jd) failed\n", buf,
124 file.get(), static_cast<intmax_t>(size));
125 evhttp_send_error(request, HTTP_INTERNAL, NULL);
126 return false;
127 } else {
128 // it succeeded, so evhttp takes ownership
129 file.release();
130 }
131 evhttp_send_reply(request, HTTP_OK, NULL, NULL);
132 return true;
133}
134
135} // namespace
136void HTTPServer::ServeFile(evhttp_request *request) {
137 AddStandardHeaders(request);
138
139 const char *path = GetPath(request);
140 if (path == NULL) return;
141
142 ScopedFD file(OpenFile(request, path, directory_));
143 if (!file) return;
144
145 if (!SendFileResponse(request, file.release())) return;
146}
147
148void HTTPServer::Run() {
149 event_base_dispatch(base_);
150 LOG(FATAL, "event_base_dispatch returned\n");
151}
152
153} // namespace http
154} // namespace aos