From 1d05994fe1d9488193f01b47e92dcf11920c8f02 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 28 Dec 2010 00:43:12 +0100 Subject: Misc changes in HTTP code. --- music.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'music.cpp') diff --git a/music.cpp b/music.cpp index 5ff9ec9..b36ba7b 100644 --- a/music.cpp +++ b/music.cpp @@ -2,6 +2,7 @@ #include #include +#include namespace music { @@ -40,14 +41,16 @@ MusicListing *find(std::string path) { }; // namespace music -void MusicDirectory::render(std::ostream& os) { +void MusicDirectory::render(HTTPResponse& res) { + res.add_header("content-type", "text/html"); + for(MusicDirectories::iterator it = directories.begin(); it != directories.end(); it++) { std::string rel_path = it->path.string().substr(music::root_directory->path.string().size()); - os << boost::format("%s
") % rel_path % rel_path; + res.write(boost::str(boost::format("%s
") % rel_path % rel_path)); } for(MusicTracks::iterator it = tracks.begin(); it != tracks.end(); it++) { std::string rel_path = it->path.string().substr(music::root_directory->path.string().size()); - os << boost::format("%s
") % rel_path % rel_path; + res.write(boost::str(boost::format("%s
") % rel_path % rel_path)); } } @@ -72,5 +75,17 @@ MusicDirectory::MusicDirectory(const fs::path root) { } } -void MusicTrack::render(std::ostream& os) { +void MusicTrack::render(HTTPResponse& res) { + res.add_header("content-type", "application/octet-stream"); + + fs::ifstream is(path, std::ios::binary | std::ios::in); + is.seekg(0, std::ios::end); + res.add_header("content-length", boost::str(boost::format("%d") % is.tellg())); + is.seekg(0, std::ios::beg); + + char data[0x1000]; + while(is.good()) { + is.read(data, 0x1000); + res.write(data, is.gcount()); + } } -- cgit v1.2.3