summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2010-12-31 00:09:06 +0100
committerVegard Storheil Eriksen <zyp@jvnv.net>2010-12-31 00:09:06 +0100
commit9a5138ce981697e243fa0de2bc2668eaa80efb76 (patch)
tree9c177496360871ecb4751ef894762d43b31a5897
parentf1d6b1400f5192ef97f84ab4fd7ce83a60168dba (diff)
Hacked stuff to work again.
-rw-r--r--http_connection.cpp39
-rw-r--r--http_connection.h17
-rw-r--r--music.cpp30
-rw-r--r--music.h9
4 files changed, 51 insertions, 44 deletions
diff --git a/http_connection.cpp b/http_connection.cpp
index fb45aac..e682037 100644
--- a/http_connection.cpp
+++ b/http_connection.cpp
@@ -15,10 +15,28 @@ HTTP::Connection::Connection(boost::asio::io_service& io_service) : socket(io_se
void HTTP::Connection::handle_write(const boost::system::error_code& error, size_t bytes_transferred) {
}
+void HTTP::Connection::foo_handler(HTTP::Connection::p connection) {
+ std::cout << "Handling!" << std::endl;
+
+ HTTPResponse res(connection->socket);
+
+ MusicListing::p ml = music::get(connection->path);
+ if(ml) {
+ res.code = 200;
+ res.status = "OK";
+
+ ml->render(connection, res);
+ } else {
+ res.code = 404;
+ res.status = "Not Found";
+ }
+}
+
void HTTP::Connection::handle_read(const boost::system::error_code& error, size_t bytes_transferred) {
if(!parse_request(buf)) {
// Request parse error.
- return;
+ std::cout << "Request parse error." << std::endl;
+ //return;
}
std::cout << "Path: " << std::endl;
@@ -36,24 +54,7 @@ void HTTP::Connection::handle_read(const boost::system::error_code& error, size_
std::cout << " " << it->first << " = " << it->second << std::endl;
}
- return;
-
- std::istream is(&buf);
-
- HTTPRequest req(is);
-
- HTTPResponse res(socket);
-
- MusicListing::p ml = music::get(req.path);
- if(ml) {
- res.code = 200;
- res.status = "OK";
-
- ml->render(req, res);
- } else {
- res.code = 404;
- res.status = "Not Found";
- }
+ foo_handler(shared_from_this());
}
void print(char c) {
diff --git a/http_connection.h b/http_connection.h
index b6d6e27..8322721 100644
--- a/http_connection.h
+++ b/http_connection.h
@@ -20,6 +20,14 @@ namespace HTTP {
tcp::socket socket;
boost::asio::streambuf buf;
+ //! Parse request headers.
+ bool parse_request(boost::asio::streambuf& buf);
+
+ public:
+ typedef boost::shared_ptr<Connection> p;
+
+ void start();
+
//! Request method.
std::string method;
@@ -35,14 +43,7 @@ namespace HTTP {
//! Request headers.
std::map<std::string, std::string> headers;
-
- //! Parse request headers.
- bool parse_request(boost::asio::streambuf& buf);
-
- public:
- typedef boost::shared_ptr<Connection> p;
-
- void start();
+ static void foo_handler(Connection::p connection);
};
};
diff --git a/music.cpp b/music.cpp
index 1651d53..5292e66 100644
--- a/music.cpp
+++ b/music.cpp
@@ -25,15 +25,19 @@ void music::init(std::string root) {
}
}
-MusicListing::p music::get(const std::string path) {
+MusicListing::p music::get(const std::vector<std::string>& path) {
// prefix path with our root_directory
- fs::path p = root_directory / path;
-
- // don't allow requests with /../ in the path
- if(path.find("/../") != std::string::npos) {
- return MusicListing::p();
+ fs::path p = root_directory;
+
+ for(std::vector<std::string>::const_iterator it = path.begin(); it != path.end(); it++) {
+ // don't allow requests with /../ in the path
+ if(*it == "..") {
+ return MusicListing::p();
+ }
+
+ p /= *it;
}
-
+
if(fs::is_directory(p)) {
boost::shared_ptr<MusicListing> ml(new MusicDirectory(p));
return ml;
@@ -48,7 +52,7 @@ MusicListing::p music::get(const std::string path) {
std::vector<MusicListing::p> music::find_artist(const std::string artist) {
soci::session sql(config::vm["audist.database"].as<std::string>());
- soci::rowset<std::string> rs = (sql.prepare << "SELECT file_name FROM tracks WHERE artist_id IN (SELECT id FROM artists WHERE name ILIKE :name)",
+ soci::rowset<std::string> rs = (sql.prepare << "SELECT file_name FROM tracks WHERE artist_id IN (SELECT id FROM artists WHERE name LIKE :name)",
soci::use("%"+artist+"%"));
std::vector<MusicListing::p> results;
@@ -61,7 +65,7 @@ std::vector<MusicListing::p> music::find_artist(const std::string artist) {
return results;
}
-void MusicDirectory::render(HTTPRequest& req, HTTPResponse& res) {
+void MusicDirectory::render(HTTP::Connection::p req, HTTPResponse& res) {
res.add_header("content-type", "text/html");
for(PathListings::iterator it = directories.begin(); it != directories.end(); it++) {
@@ -93,7 +97,7 @@ MusicDirectory::MusicDirectory(const fs::path root) {
}
}
-void MusicTrack::render(HTTPRequest& req, HTTPResponse& res) {
+void MusicTrack::render(HTTP::Connection::p req, HTTPResponse& res) {
res.add_header("content-type", "application/octet-stream");
// tag test
@@ -102,9 +106,9 @@ void MusicTrack::render(HTTPRequest& req, HTTPResponse& res) {
fs::ifstream is(path, std::ios::binary | std::ios::in);
- if(req.query.find("decoder") != req.query.end() && req.query.find("encoder") != req.query.end()) {
- DecoderBase *d = decoder::get_decoder(req.query["decoder"]);
- EncoderBase *e = encoder::get_encoder(req.query["encoder"]);
+ if(req->args.count("decoder") && req->args.count("encoder")) {
+ DecoderBase *d = decoder::get_decoder(req->args["decoder"]);
+ EncoderBase *e = encoder::get_encoder(req->args["encoder"]);
Transcoder t(is, res, *d, *e);
t.run();
delete d;
diff --git a/music.h b/music.h
index effc32c..fe3ca4a 100644
--- a/music.h
+++ b/music.h
@@ -2,6 +2,7 @@
#define MUSIC_H
#include "http.h"
+#include "http_connection.h"
#include <boost/filesystem.hpp>
#include <vector>
@@ -13,13 +14,13 @@ class MusicListing {
public:
typedef boost::shared_ptr<MusicListing> p;
fs::path path;
- virtual void render(HTTPRequest& req, HTTPResponse& res) = 0;
+ virtual void render(HTTP::Connection::p req, HTTPResponse& res) = 0;
};
class MusicTrack : public MusicListing {
public:
MusicTrack(const fs::path path);
- virtual void render(HTTPRequest& req, HTTPResponse& res);
+ virtual void render(HTTP::Connection::p req, HTTPResponse& res);
};
class MusicDirectory : public MusicListing {
@@ -29,13 +30,13 @@ class MusicDirectory : public MusicListing {
PathListings tracks;
MusicDirectory(const fs::path root);
- virtual void render(HTTPRequest& req, HTTPResponse& res);
+ virtual void render(HTTP::Connection::p req, HTTPResponse& res);
};
namespace music {
extern fs::path root_directory;
void init(std::string root);
- MusicListing::p get(const std::string path);
+ MusicListing::p get(const std::vector<std::string>& path);
std::vector<MusicListing::p> find_artist(const std::string artist);
};