From: Dean M. B. <mik...@gm...> - 2011-02-01 14:00:35
|
On Tue, Feb 1, 2011 at 3:09 AM, Nelson, Erik - 2 <eri...@ba...> wrote: >> Dean Michael Berris wrote on Saturday, January 29, 2011 8:33 AM >> On Sat, Jan 29, 2011 at 7:08 AM, David Hite <dav...@gm...> wrote: >>> However, now I have the problem that response.content is a string. >>> How do I >>> send binary data, such as an image file? >>> >> >> The way I would do it is to somehow mmap the file, get the mmap'ed >> pointer, and copy the data into response.content directly. Binary data >> is held fine by std::string instances, you just have to reserve the >> appropriate amount of memory when you're doing that. >> > > This question will continue to come up. It's true that std::string *can* hold binary data. It's just not *natural*. The std::string interface is strongly influenced by null-terminated strings. Things that the interface implies (like string.c_str() or string(char*) constructor actually do what you'd think) are broken if you use std::string with binary data. > I know. ;) Except a lot of times though, the HTTP protocol largely deals with *text transfer*. Also it's not as simple as it sounds. See below. > The more natural interface (used by asio as well) is a buffer object which is just a tuple of the form {void*, size_t}, which is easily constructed from a string, vector<char>, array, or just about any container that the user has for the data. The user should *not* be forced to make a copy of their data in a std::string just to satisfy the network library. > And this interface is already available in 0.8.1, in the asynchronous server type. Basically now (thanks to Oleg Malashenko) you can do `write(boost::asio::const_buffers_1(ptr, size))` and it won't make a copy. The problem we have here in the synchronous server example is that the response object has to be created before we even pass it into the handler's operator()(...) overload. It also means that the data will have to be "accessible" until the internal write completion handler is invoked by Boost.Asio *after the handler's operator() overload is finished*. This means the data that you have to put in the response object that is passed into the synchronous server implementation, should out-live the scope of the handler's operator() overload. I see two possibilities moving forward here: 1. ) Break the interface of the server response type and require that data to be placed into the response object's body should be a variant of either a string or a boost::tuple<shared_ptr<void>, size_t> -- which I'm not even positive will work as I expect because what's in the shared_ptr<void> should be an object and not just an arbitrary pointer. This is going to make the server template's connection implementation a lot more tricky than it has to be. 2. ) Deprecate the synchronous server template in favor of the asynchronous server template. This will remove the simplicity of the approach provided for in the implementation. Yet another way is to put the burden of managing the memory of the data to be written out by the server, by providing an optional callback function when providing the content. So it would then be a variant between a string and a tuple<void*, size_t, function<void(void*)> >. All of these options makes the synchronous handler's implementation needlessly complex. Of course, any ideas and/or patches would be most welcome especially if makes things simple. :) -- Dean Michael Berris about.me/deanberris |