summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-03-03 12:33:05 +0100
committerJon Bergli Heier <snakebite@jvnv.net>2011-03-03 12:33:05 +0100
commit3568ebefab593ba03f3d874d5b2e130aec4f1a89 (patch)
treedaa5491b4a0c10be9d0a96a0bbb6c9a6a6af340c
parentdd62185d31b2967a98a1fa58bf869db46ab8284b (diff)
Fixed content-type and range handling.
-rw-r--r--encoder.h3
-rw-r--r--encoders/lame_encoder.cpp4
-rw-r--r--encoders/lame_encoder.h1
-rw-r--r--encoders/vorbis_encoder.cpp4
-rw-r--r--encoders/vorbis_encoder.h1
-rw-r--r--http_connection.cpp7
-rw-r--r--music.cpp51
7 files changed, 25 insertions, 46 deletions
diff --git a/encoder.h b/encoder.h
index 6d7a0c1..cdaaaaf 100644
--- a/encoder.h
+++ b/encoder.h
@@ -22,6 +22,9 @@ class Encoder {
//! Read into provided buffer.
virtual std::size_t read(char* buf, std::size_t buf_size) = 0;
+
+ //! MIME type of output.
+ virtual std::string get_mime_type() = 0;
};
#endif
diff --git a/encoders/lame_encoder.cpp b/encoders/lame_encoder.cpp
index f3816bf..28749aa 100644
--- a/encoders/lame_encoder.cpp
+++ b/encoders/lame_encoder.cpp
@@ -28,3 +28,7 @@ std::size_t EncoderLame::read(char* buf, std::size_t buf_size) {
return size;
}
+
+std::string EncoderLame::get_mime_type() {
+ return "audio/mpeg";
+}
diff --git a/encoders/lame_encoder.h b/encoders/lame_encoder.h
index 79bc944..717e337 100644
--- a/encoders/lame_encoder.h
+++ b/encoders/lame_encoder.h
@@ -17,6 +17,7 @@ class EncoderLame : public Encoder {
~EncoderLame();
virtual std::size_t read(char* buf, std::size_t buf_size);
+ virtual std::string get_mime_type();
};
#endif
diff --git a/encoders/vorbis_encoder.cpp b/encoders/vorbis_encoder.cpp
index 3704123..4449345 100644
--- a/encoders/vorbis_encoder.cpp
+++ b/encoders/vorbis_encoder.cpp
@@ -113,3 +113,7 @@ std::size_t VorbisEncoder::read(char* buf, std::size_t buf_size) {
return written;
}
+
+std::string VorbisEncoder::get_mime_type() {
+ return "application/ogg";
+}
diff --git a/encoders/vorbis_encoder.h b/encoders/vorbis_encoder.h
index cba8c21..5206ace 100644
--- a/encoders/vorbis_encoder.h
+++ b/encoders/vorbis_encoder.h
@@ -26,6 +26,7 @@ class VorbisEncoder : public Encoder {
virtual ~VorbisEncoder ();
virtual std::size_t read(char* buf, std::size_t buf_size);
+ virtual std::string get_mime_type();
};
#endif
diff --git a/http_connection.cpp b/http_connection.cpp
index daba10b..61928d3 100644
--- a/http_connection.cpp
+++ b/http_connection.cpp
@@ -82,7 +82,10 @@ void HTTP::Connection::send_file(const fs::path& filename) {
std::string::iterator it = std::find(range_str.begin(), range_str.end(), '-');
std::size_t begin = boost::lexical_cast<int>(std::string(range_str.begin(), it));
- std::size_t end = boost::lexical_cast<int>(std::string(it + 1, range_str.end())) + 1;
+ std::size_t end = length;
+ if(++it != range_str.end()) {
+ end = boost::lexical_cast<int>(std::string(it, range_str.end()));
+ }
// Is the range past the end of the file?
if(end > length) {
@@ -90,7 +93,7 @@ void HTTP::Connection::send_file(const fs::path& filename) {
}
// Do the file contain requested range?
- if(end && end < begin || begin > length) {
+ if((end && end < begin) || begin > length) {
send_error(416);
}
diff --git a/music.cpp b/music.cpp
index 8a28965..65e3735 100644
--- a/music.cpp
+++ b/music.cpp
@@ -169,20 +169,8 @@ MusicDirectory::MusicDirectory(const fs::path root) {
}
void MusicTrack::render(HTTP::Connection::p req) {
- req->add_header("Content-Type", "audio/mpeg");
fs::path file_path = path;
-
- int skip = 0;
- int range = 0;
-
- if(req->headers.count("Range")) {
- if(req->headers["Range"] == "bytes=0-1024") {
- range = 1025;
- } else {
- skip = 1025;
- }
- }
-
+
if(req->args.count("decoder") && req->args.count("encoder")) {
Decoder::p decoder = Decoder::get(req->args["decoder"], path.string());
Encoder::p encoder = Encoder::get(req->args["encoder"], decoder);
@@ -191,37 +179,12 @@ void MusicTrack::render(HTTP::Connection::p req) {
file_path = ec.get_path();
if(!fs::exists(file_path))
ec.create_cache();
- // TODO: Make an encoder-to-istream adapter to get rid of this:
- char data[0x10000];
- std::streamsize size = 1;
-
- if(skip) {
- size = encoder->read(data, skip);
- }
-
- if(range) {
- size = encoder->read(data, range);
- if(size > 0) {
- req->send_data(data, size);
- }
- return;
- }
-
- while(size) {
- size = encoder->read(data, 0x10000);
- if(size > 0)
- req->send_data(data, size);
- }
- }
-
- req->send_file(path);
- /*
- fs::ifstream is(file_path, std::ios::in | std::ios::binary);
- is.seekg(0, std::ios::end);
- req->add_header("content-length", boost::str(boost::format("%d") % is.tellg()));
- is.seekg(0, std::ios::beg);
+ req->add_header("Content-Type", encoder->get_mime_type());
+ } else {
+ // TODO: Send correct Content-Type.
+ req->add_header("Content-Type", "application/octet-stream");
+ }
- req->send_data(is);
- */
+ req->send_file(file_path);
}