summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-08-04 21:54:38 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-08-04 21:54:38 +0200
commita0f327140c4068afd46b264d24f767c4dd3d0f40 (patch)
tree13c0ccf3e9a56d91fa61c0252d32168b90a78260
parentca1fc859f27560c7988f078cff89f40f1b6e2c0f (diff)
Implemented JSON list handler.HEADmaster
-rw-r--r--http_json.cpp34
-rw-r--r--http_json.h15
-rw-r--r--main.cpp4
3 files changed, 53 insertions, 0 deletions
diff --git a/http_json.cpp b/http_json.cpp
new file mode 100644
index 0000000..d769e5e
--- /dev/null
+++ b/http_json.cpp
@@ -0,0 +1,34 @@
+#include "http_json.h"
+#include "json.h"
+#include "music.h"
+
+void HTTP::JSON::operator()(Connection::p connection) {
+ std::string directory;
+ if(connection->args.find("directory") != connection->args.end()) {
+ directory = connection->args["directory"];
+ } else {
+ directory = "/";
+ }
+
+ ::JSON::Array results;
+
+ MusicDirectory::p dir = music::get_directory(directory);
+ for(MusicDirectory::PathListings::iterator it = dir->directories.begin(); it != dir->directories.end(); it++) {
+ ::JSON::Object obj;
+ obj["type"] = std::string("dir");
+ // relative path
+ obj["name"] = it->string().substr(music::root_directory.string().size());
+ results.push_back(obj);
+ }
+ for(MusicDirectory::PathListings::iterator it = dir->tracks.begin(); it != dir->tracks.end(); it++) {
+ ::JSON::Object obj;
+ obj["type"] = std::string("file");
+ obj["name"] = it->string().substr(music::root_directory.string().size());
+ obj["size"] = (int)fs::file_size(*it);
+ results.push_back(obj);
+ }
+
+ std::stringstream ss;
+ ss << results;
+ connection->send_data(ss);
+}
diff --git a/http_json.h b/http_json.h
new file mode 100644
index 0000000..14addef
--- /dev/null
+++ b/http_json.h
@@ -0,0 +1,15 @@
+#ifndef HTTP_JSON_H
+#define HTTP_JSON_H
+
+#include "http_connection.h"
+
+namespace HTTP {
+ //! JSON handler.
+ class JSON {
+ public:
+ //! Handle request.
+ void operator()(Connection::p connection);
+ };
+};
+
+#endif
diff --git a/main.cpp b/main.cpp
index 6fa7504..5d5af1d 100644
--- a/main.cpp
+++ b/main.cpp
@@ -6,6 +6,7 @@
#include "telnetd.h"
#include "http_static.h"
+#include "http_json.h"
#include <iostream>
#include <vector>
@@ -39,6 +40,9 @@ int main(int argc, char **argv) {
HTTP::Static static_files("static");
httpd.add_handler("static", static_files);
httpd.add_handler("", static_files);
+
+ HTTP::JSON http_json;
+ httpd.add_handler("list", http_json);
telnet::Server telnetd(io_service, tcp::endpoint(tcp::v6(), config::vm["audist.telnetd_port"].as<int>()));