|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r56361 - in sandbox/SOC/2007/cgi/branches/pickmeup: boost/cgi boost/cgi/cgi boost/cgi/detail boost/cgi/fcgi libs/cgi/build/msvc/9.0/Boost.CGI libs/cgi/example/fcgi/echo libs/cgi/example/fcgi/file_browser
From: lists.drrngrvy_at_[hidden]
Date: 2009-09-22 18:12:44
Author: drrngrvy
Date: 2009-09-22 18:12:43 EDT (Tue, 22 Sep 2009)
New Revision: 56361
URL: http://svn.boost.org/trac/boost/changeset/56361
Log:
Bug fixes and refactoring to FastCGI client's write_some().
Support for boost::hash.
Binary files modified:
sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/build/msvc/9.0/Boost.CGI/Boost.CGI.suo
Text files modified:
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/basic_request.hpp | 37 ++++++++++
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/cgi/request_service.hpp | 1
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/cgi_service_impl_base.hpp | 3
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/push_options.hpp | 13 +--
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/fcgi/client.hpp | 132 +++++++++++++++++----------------------
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/fcgi/request_service.hpp | 6 +
sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/fcgi/specification.hpp | 67 ++++++++++++++++++++
sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/build/msvc/9.0/Boost.CGI/Boost.CGI.sln | 6 +
sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/fcgi/echo/main.cpp | 1
sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/fcgi/file_browser/main.cpp | 29 +++++---
10 files changed, 195 insertions(+), 100 deletions(-)
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/basic_request.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/basic_request.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/basic_request.hpp 2009-09-22 18:12:43 EDT (Tue, 22 Sep 2009)
@@ -20,6 +20,7 @@
#include <boost/assert.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/noncopyable.hpp>
+#include <boost/functional/hash.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/system/error_code.hpp>
#include <boost/asio/basic_io_object.hpp>
@@ -46,6 +47,15 @@
namespace cgi {
namespace common {
+ /// Get a hashed interpretation of the request.
+ /**
+ * You cannot consider this completely unique to each
+ * request, but it should be quite useful anyway.
+ * You can use this for logging or tracking, for example.
+ */
+ template<typename T, typename S>
+ std::size_t hash_value(basic_request<T,S> const& req);
+
/// The basic_request class, primary entry point to the library
/**
* Note: By default, synchronous protocols (ie. cgi) auto-load AND parse
@@ -79,12 +89,12 @@
basic_request<
RequestService, ProtocolService
, Allocator
- > type;
+ > self_type;
typedef ::cgi::common::map map_type;
typedef RequestService service_type;
typedef typename service_type::protocol_type protocol_type;
typedef ProtocolService protocol_service_type;
- typedef boost::shared_ptr<type> pointer;
+ typedef boost::shared_ptr<self_type> pointer;
typedef typename RequestService::implementation_type implementation_type;
typedef typename implementation_type::char_type char_type;
typedef typename implementation_type::string_type string_type;
@@ -155,6 +165,11 @@
return (*impl)[varname.c_str()];
}
+ mapped_type const& operator[](key_type const& varname) const {
+ BOOST_ASSERT(impl);
+ return (*impl)[varname.c_str()];
+ }
+
operator map_type&() { BOOST_ASSERT(impl); return *impl; }
bool operator!() const { return !impl; }
@@ -248,7 +263,7 @@
static pointer create(protocol_service_type& ps)
{
- return pointer(new type(ps));
+ return pointer(new self_type(ps));
}
void set_protocol_service(protocol_service_type& ps)
@@ -598,12 +613,28 @@
return this->service.get_role(this->implementation);
}
+ std::size_t hash()
+ {
+ return boost::hash<self_type>()(*this);
+ }
+
void set_status(common::http::status_code const& status)
{
this->service.set_status(this->implementation, status);
}
};
+ template<typename T, typename S>
+ std::size_t hash_value(basic_request<T,S> const& req)
+ {
+ boost::hash<typename basic_request<T,S>::string_type> hasher;
+ return hasher(req.env["REMOTE_ADDR"] + ":"
+ + req.env["REMOTE_PORT"] + ":"
+ + req.env["HTTP_USER_AGENT"] + ":"
+ + req.env["REQUEST_METHOD"] + ":"
+ + req.env["REQUEST_URI"]);
+ }
+
} // namespace common
} // namespace cgi
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/cgi/request_service.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/cgi/request_service.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/cgi/request_service.hpp 2009-09-22 18:12:43 EDT (Tue, 22 Sep 2009)
@@ -28,7 +28,6 @@
: public cgi_service_impl_base<cgi_request_impl>
{
public:
- typedef cgi_request_impl impl_type;
typedef common::tags::cgi protocol_type;
cgi_request_service()
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/cgi_service_impl_base.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/cgi_service_impl_base.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/cgi_service_impl_base.hpp 2009-09-22 18:12:43 EDT (Tue, 22 Sep 2009)
@@ -63,7 +63,8 @@
client_type client_;
- // The number of characters left to read (ie. "content_length - bytes_read")
+ // The number of characters left to read (ie. "content_length -
+ // bytes_read")
std::size_t characters_left_;
boost::scoped_ptr<form_parser_type> fp_;
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/push_options.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/push_options.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/detail/push_options.hpp 2009-09-22 18:12:43 EDT (Tue, 22 Sep 2009)
@@ -9,14 +9,13 @@
//#ifdef _MSC_VER
-//#pragma warning (disable:)
// You may want to remove these.
-#define _CRT_SECURE_NO_DEPRECATE 1
-#define _SCL_SECURE_NO_WARNINGS 1
-#define _CRT_SECURE_NO_WARNINGS 1
-//#pragma warning (disable:4996)
-
-//#endif
+#if defined (BOOST_WINDOWS)
+# define _CRT_SECURE_NO_DEPRECATE 1
+# define _SCL_SECURE_NO_WARNINGS 1
+# define _CRT_SECURE_NO_WARNINGS 1
+# define NOMINMAX
+#endif // defined (BOOST_WINDOWS)
#if !defined(BOOST_CGI_INLINE)
# if defined(BOOST_CGI_BUILD_LIB)
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/fcgi/client.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/fcgi/client.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/fcgi/client.hpp 2009-09-22 18:12:43 EDT (Tue, 22 Sep 2009)
@@ -28,6 +28,10 @@
#include "boost/cgi/detail/protocol_traits.hpp"
#include "boost/cgi/connections/shareable_tcp_socket.hpp"
+#undef min
+#undef max
+#include <algorithm>
+
namespace cgi {
namespace common {
@@ -64,6 +68,10 @@
: request_id_(-1)
, status_(none_)
, keep_connection_(false)
+ , total_sent_bytes_(0)
+ , total_sent_packets_(0)
+ , header_()
+ , outbuf_()
{
}
@@ -72,6 +80,10 @@
: request_id_(-1)
, status_(none_)
, keep_connection_(false)
+ , total_sent_bytes_(0)
+ , total_sent_packets_(0)
+ , header_()
+ , outbuf_()
{
}
@@ -114,37 +126,20 @@
{
if (status_ == closed_) return ec;
- std::vector<boost::asio::const_buffer> bufs;
-
+ outbuf_.clear();
+ header_.reset(spec_detail::END_REQUEST, request_id_, 8);
// Write an EndRequest packet to the server.
- out_header_[0] = static_cast<unsigned char>(1); // FastCGI version
- out_header_[1] = static_cast<unsigned char>(3); // END_REQUEST
- out_header_[2] = static_cast<unsigned char>(request_id_ >> 8) & 0xff;
- out_header_[3] = static_cast<unsigned char>(request_id_) & 0xff;
- out_header_[4] = static_cast<unsigned char>(8 >> 8) & 0xff;
- out_header_[5] = static_cast<unsigned char>(8) & 0xff;
- out_header_[6] = static_cast<unsigned char>(0);
- out_header_[7] = 0;
//BOOST_ASSERT(role_ == fcgi::spec_detail::RESPONDER
// && "Only supports Responder role for now (**FIXME**)");
- header_buffer_type end_request_body =
- {{
- static_cast<unsigned char>(app_status >> 24) & 0xff
- , static_cast<unsigned char>(app_status >> 16) & 0xff
- , static_cast<unsigned char>(app_status >> 8) & 0xff
- , static_cast<unsigned char>(app_status >> 0) & 0xff
- , static_cast<unsigned char>(fcgi::spec_detail::REQUEST_COMPLETE)
- , static_cast<unsigned char>(0)
- , static_cast<unsigned char>(0)
- , static_cast<unsigned char>(0)
- }};
+ fcgi::spec::end_request_body body(
+ app_status, fcgi::spec_detail::REQUEST_COMPLETE);
- bufs.push_back(buffer(out_header_));
- bufs.push_back(buffer(end_request_body));
+ outbuf_.push_back(header_.data());
+ outbuf_.push_back(body.data());
- write(*connection_, bufs, boost::asio::transfer_all(), ec);
+ write(*connection_, outbuf_, boost::asio::transfer_all(), ec);
if (!ec && !keep_connection_)
connection_->close();
@@ -185,8 +180,8 @@
typename ConstBufferSequence::const_iterator iter = buf.begin();
typename ConstBufferSequence::const_iterator end = buf.end();
- std::vector<boost::asio::const_buffer> bufs;
- bufs.push_back(boost::asio::buffer(out_header_));
+ outbuf_.clear();
+ outbuf_.push_back(boost::asio::buffer(header_.data()));
int total_buffer_size(0);
for(; iter != end; ++iter)
@@ -194,30 +189,22 @@
boost::asio::const_buffer buffer(*iter);
std::size_t new_buf_size( boost::asio::buffer_size(*iter) );
std::cerr<< "-" << new_buf_size << "-";
- // only write a maximum of 65535 bytes
+ // Only write a maximum of 65535 bytes.
if (total_buffer_size + new_buf_size
> static_cast<std::size_t>(fcgi::spec::max_packet_size::value))
{
- if (new_buf_size > static_cast<std::size_t>(
- fcgi::spec::max_packet_size::value))
+ // If the send buffer is empty, extract a chunk of the
+ // new buffer to send. If there is already some data
+ // ready to send, don't add any more data to the pack.
+ if (total_buffer_size == 0)
{
- std::cerr<< "Buffer too big." << std::endl;
- total_buffer_size = 65000;
- bufs.push_back(boost::asio::buffer(*iter, 65000));
+ total_buffer_size
+ = std::min<std::size_t>(new_buf_size,65500);
+ std::cerr<< "Oversized buffer: " << total_buffer_size
+ << " / " << new_buf_size << " bytes sent\n";
+ outbuf_.push_back(
+ boost::asio::buffer(*iter, total_buffer_size));
break;
- /*
- std::size_t bytes_left(new_buf_size);
- std::size_t bufsz(0);
- while (bytes_left > 0)
- {
- if (bytes_left > fcgi::spec::max_packet_size::value)
- bufsz = fcgi::spec::max_packet_size::value;
- else
- bufsz = bytes_left;
- bufs.push_back(boost::asio::const_buffer(buffer, bufsz));
- bytes_left -= bufsz;
- }
- */
}
else
break;
@@ -225,39 +212,29 @@
else
{
total_buffer_size += new_buf_size;
- bufs.push_back(*iter);
+ outbuf_.push_back(*iter);
}
}
- //std::cerr<< '\n';
- fcgi::spec::stdout_header header(request_id_, total_buffer_size);
- bufs[0] = header.data();
- out_header_[0] = static_cast<unsigned char>(1);
- out_header_[1] = static_cast<unsigned char>(6);
- out_header_[2] = static_cast<unsigned char>(request_id_ >> 8) & 0xff;
- out_header_[3] = static_cast<unsigned char>(request_id_) & 0xff;
- out_header_[4] = static_cast<unsigned char>(total_buffer_size >> 8) & 0xff;
- out_header_[5] = static_cast<unsigned char>(total_buffer_size) & 0xff;
- out_header_[6] = static_cast<unsigned char>(0);
- out_header_[7] = 0;
+ header_.reset(spec_detail::STDOUT, request_id_, total_buffer_size);
- //bool empty = bufs.empty();
- std::size_t size = bufs.size();
- typedef std::vector<boost::asio::const_buffer>::const_iterator
- iter_t;
- for (iter_t iter(bufs.begin()), end(bufs.end()); iter != end; ++iter)
- {
- size = boost::asio::buffer_size(*iter);
- std::string str (
- boost::asio::buffer_cast<const char*>(*iter)
- , boost::asio::buffer_size(*iter) );
- }
-
std::size_t bytes_transferred
- = boost::asio::write(*connection_, bufs, boost::asio::transfer_all(), ec);
+ = boost::asio::write(*connection_, outbuf_
+ , boost::asio::transfer_all(), ec);
- //std::cerr<< "Transferred " << bytes_transferred << " bytes (total: " << total_buffer_size << ")." << std::endl;
- if (!ec && 0 != (total_buffer_size + fcgi::spec::header_length::value
- - bytes_transferred))
+ std::cerr<< "Transferred " << bytes_transferred
+ << " / " << total_buffer_size << " bytes (running total: "
+ << total_sent_bytes_ << "; "
+ << total_sent_packets_ << " packets).\n";
+ if (ec)
+ std::cerr<< "Error " << ec << ": " << ec.message() << '\n';
+
+ total_sent_bytes_ += bytes_transferred;
+ total_sent_packets_ += 1;
+ // Now remove the protocol overhead from the caller, who
+ // doesn't care about them.
+ bytes_transferred -= fcgi::spec::header_length::value;
+ // Check everything was written ok.
+ if (!ec && bytes_transferred != total_buffer_size)
ec = ::cgi::error::couldnt_write_complete_packet;
return bytes_transferred;
@@ -332,9 +309,16 @@
client_status status_;
std::size_t bytes_left_;
connection_ptr connection_;
+
+ boost::uint64_t total_sent_bytes_;
+ boost::uint64_t total_sent_packets_;
/// Buffer used to check the header of each packet.
- header_buffer_type out_header_;
+ //header_buffer_type out_header_;
+ fcgi::spec::header header_;
+
+ /// Output buffer.
+ std::vector<boost::asio::const_buffer> outbuf_;
bool keep_connection_;
role_type role_;
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/fcgi/request_service.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/fcgi/request_service.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/fcgi/request_service.hpp 2009-09-22 18:12:43 EDT (Tue, 22 Sep 2009)
@@ -44,6 +44,8 @@
typedef ::cgi::fcgi::client client_type;
typedef client_type::connection_type connection_type;
typedef client_type::header_buffer_type header_buffer_type;
+ typedef spec_detail::Header header_type;
+
typedef detail::protocol_traits<
protocol_type
>::protocol_service_type protocol_service_type;
@@ -77,6 +79,7 @@
bool all_done_;
header_buffer_type header_buf_;
+ header_type header_;
typedef detail::form_parser form_parser_type;
boost::scoped_ptr<form_parser_type> fp_;
@@ -129,8 +132,7 @@
void construct(implementation_type& impl)
{
- // **FIXME** move this to the client class
- impl.client_.set_connection(//new implementation_type::connection_type(this->io_service()));
+ impl.client_.set_connection(
implementation_type::connection_type::create(this->io_service())
);
}
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/fcgi/specification.hpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/fcgi/specification.hpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/boost/cgi/fcgi/specification.hpp 2009-09-22 18:12:43 EDT (Tue, 22 Sep 2009)
@@ -95,7 +95,8 @@
} impl;
public:
- typedef boost::asio::const_buffers_1 const_buffers_type;
+ typedef boost::asio::const_buffers_1 const_buffers_type;
+ typedef boost::asio::mutable_buffers_1 mutable_buffers_type;
Header()
{
@@ -108,6 +109,13 @@
reset(t, id, len);
}
+ mutable_buffers_type data()
+ {
+ return boost::asio::buffer(
+ static_cast<void*>(&impl)
+ , sizeof(impl));
+ }
+
const_buffers_type data() const
{
return boost::asio::buffer(
@@ -210,6 +218,8 @@
} impl;
public:
+ typedef boost::asio::const_buffers_1 const_buffers_type;
+
EndRequestBody() {}
EndRequestBody( boost::uint64_t appStatus
@@ -229,6 +239,13 @@
memset(impl.reserved_, 0, sizeof(impl.reserved_));
}
+
+ const_buffers_type data() const
+ {
+ return boost::asio::buffer(
+ static_cast<const void*>(&impl)
+ , sizeof(impl));
+ }
};
class EndRequestRecord
@@ -407,6 +424,9 @@
}
}
};
+
+ typedef spec_detail::Header header;
+ typedef spec_detail::EndRequestBody end_request_body;
struct begin_request
: boost::mpl::int_<1>
@@ -458,10 +478,55 @@
struct stdout_header
: spec_detail::Header
{
+ explicit stdout_header()
+ : Header(spec_detail::STDOUT,0,0)
+ {
+ }
explicit stdout_header(int request_id, int content_len)
: Header(spec_detail::STDOUT, request_id, content_len)
{
}
+ void reset(int request_id, int content_len)
+ {
+ spec_detail::Header::reset(
+ spec_detail::STDOUT
+ , request_id
+ , content_len);
+ }
+ };
+
+ struct end_request
+ : header
+ , end_request_body
+ {
+ explicit end_request
+ (
+ int request_id = 0
+ , boost::uint64_t app_status = 0
+ , spec_detail::status_types proc_status
+ = spec_detail::REQUEST_COMPLETE
+ )
+ : header(spec_detail::END_REQUEST, request_id
+ , sizeof(end_request_body))
+ , end_request_body(app_status, proc_status)
+ {
+ }
+
+ void reset
+ (
+ int request_id
+ , boost::uint64_t app_status = 0
+ , spec_detail::status_types proc_status
+ = spec_detail::REQUEST_COMPLETE
+ )
+ {
+ header::reset(
+ spec_detail::END_REQUEST
+ , request_id
+ , sizeof(end_request_body));
+ end_request_body::reset(
+ app_status, proc_status);
+ }
};
class BeginRequestBody
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/build/msvc/9.0/Boost.CGI/Boost.CGI.sln
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/build/msvc/9.0/Boost.CGI/Boost.CGI.sln (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/build/msvc/9.0/Boost.CGI/Boost.CGI.sln 2009-09-22 18:12:43 EDT (Tue, 22 Sep 2009)
@@ -33,6 +33,8 @@
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cgi_file_browser", "cgi_file_browser\cgi_file_browser.vcproj", "{FFF275FA-323A-4699-9D04-A5C9BDD6048B}"
EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cgi_file_browser_debug", "cgi_file_browser_debug\cgi_file_browser_debug.vcproj", "{E266C18C-F482-459C-9805-EBAF5C68E50A}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
@@ -102,6 +104,10 @@
{FFF275FA-323A-4699-9D04-A5C9BDD6048B}.Debug|Win32.Build.0 = Debug|Win32
{FFF275FA-323A-4699-9D04-A5C9BDD6048B}.Release|Win32.ActiveCfg = Release|Win32
{FFF275FA-323A-4699-9D04-A5C9BDD6048B}.Release|Win32.Build.0 = Release|Win32
+ {E266C18C-F482-459C-9805-EBAF5C68E50A}.Debug|Win32.ActiveCfg = Debug|Win32
+ {E266C18C-F482-459C-9805-EBAF5C68E50A}.Debug|Win32.Build.0 = Debug|Win32
+ {E266C18C-F482-459C-9805-EBAF5C68E50A}.Release|Win32.ActiveCfg = Release|Win32
+ {E266C18C-F482-459C-9805-EBAF5C68E50A}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/build/msvc/9.0/Boost.CGI/Boost.CGI.suo
==============================================================================
Binary files. No diff available.
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/fcgi/echo/main.cpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/fcgi/echo/main.cpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/fcgi/echo/main.cpp 2009-09-22 18:12:43 EDT (Tue, 22 Sep 2009)
@@ -115,6 +115,7 @@
"<head>"
"<body>"
"Request ID = " << req.id() << "<br />"
+ "Request Hash = " << req.hash() << "<br />"
"Process ID = " << process_id() << "<br />"
"<form method=post>" // enctype=\"multipart/form-data\">"
"<input type=text name=name value='"
Modified: sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/fcgi/file_browser/main.cpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/fcgi/file_browser/main.cpp (original)
+++ sandbox/SOC/2007/cgi/branches/pickmeup/libs/cgi/example/fcgi/file_browser/main.cpp 2009-09-22 18:12:43 EDT (Tue, 22 Sep 2009)
@@ -17,7 +17,6 @@
#include <boost/algorithm/string/find.hpp>
///////////////////////////////////////////////////////////
#include "boost/cgi/fcgi.hpp"
-#include "boost/cgi/utility/commit.hpp"
using std::cerr;
using std::endl;
@@ -105,6 +104,9 @@
if (filetype == "avi")
return "video/x-msvideo";
else
+ if (filetype == "wmv")
+ return "video/x-ms-wmv";
+ else
/// Rich media files.
if (filetype == "pdf")
return "application/pdf";
@@ -146,7 +148,8 @@
void show_file(Response& resp, Client& client, fs::path const& file)
{
if (!fs::exists(file))
- resp<< "File not found.";
+ resp<< content_type("text/plain")
+ << "File not found.";
else
{
boost::uintmax_t size (fs::file_size(file));
@@ -159,36 +162,40 @@
std::string mime_type (get_mime_type(file));
if (!mime_type.empty())
{
- cerr<< "MIME-type: " << mime_type << endl;
+ cerr<< "MIME-type: " << mime_type << '\n';
+ cerr<< "File size: " << size << '\n';
std::string ctype (content_type(mime_type).content + "\r\n\r\n");
- //write(client, boost::asio::buffer(ctype));
+ write(client, boost::asio::buffer(clen));
+ write(client, boost::asio::buffer(ctype));
/// Open the file and read it as binary data.
ifstream ifs (file.string().c_str(), std::ios::binary);
if (ifs.is_open())
{
resp<< content_type(mime_type);
//resp.flush(client);
- boost::uintmax_t bufsize = 100;
+ boost::uintmax_t bufsize = 1000;
boost::uintmax_t read_bytes;
- char buf[100];
+ char buf[1000];
ifs.seekg(0, std::ios::beg);
while (!ifs.eof() && size > 0)
{
ifs.read(buf, size < bufsize ? size : bufsize);
read_bytes = ifs.gcount();
size -= read_bytes;
+ cerr<< "Read " << read_bytes << " bytes from the file.\n";
//if (resp.content_length() + read_bytes >= 65000)
// resp.flush(client);
- resp.write(buf, read_bytes);
- //write(client, boost::asio::buffer(buf, read_bytes));
+ //resp.write(buf, read_bytes);
+ write(client, boost::asio::buffer(buf, read_bytes));
//resp.flush(client);
}
- resp.send(client);
- cerr<< "Content-length: " << resp.content_length() << endl;
+ //resp.send(client);
+ //cerr<< "Content-length: " << resp.content_length() << '\n';
}
}
else
- resp<< "File type not allowed.";
+ resp<< content_type("text/plain")
+ << "File type not allowed.";
}
}
}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk