From: Nelson, E. - 2 <eri...@ba...> - 2011-02-01 17:04:24
|
> From: Dean Michael Berris wrote on Tuesday, February 01, 2011 9:00 AM >> On Tue, Feb 1, 2011 at 3:09 AM, Nelson, Erik - 2 wrote: >>> Dean Michael Berris wrote on Saturday, January 29, 2011 8:33 AM >>> 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 fact that it's a common use case (I'd suggest that actually much more binary image data is transmitted than text) means that the library should easily accommodate text transfer. I think this goes back to an underlying assumption that you expressed some time ago- iirc, you believed that HTTP could *only* transmit text, and that binary data needed to be base64'd into text before transmission. That assumption may still be informing your design choices here, but the very suggestion of copying binary data into a string should (in my opinion) is a red flag that there's something wrong with the interface. Having an overload that makes std::string usage natural (like it is now) is good thing. Forcing someone to copy memory regions into a std::string is a bad thing. >> 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. My original point was only about the inelegance of using std::string. The point you're raising here is a different one, and all you're saying is that the lifetime of the payload needs to be roughly the same as the lifetime of the result object. That's not a terribly complicated- problem, and there are lots of solutions to it. It seems that one that's easy for the user is to just hand off ownership to the response, something like auto_ptr<MyObject> obj(new MyObject); response << body(obj); should be sufficient for POD types, and for non-POD types, maybe you'd need something like auto_ptr<vector<char> > obj(new vector<char>()); response << body(&(*obj)[0], obj->size(), obj); The response can delete it whenever it's done with doing whatever it does. That seems to me to be pretty easy and intuitive for the user. Erik ---------------------------------------------------------------------- This message w/attachments (message) is intended solely for the use of the intended recipient(s) and may contain information that is privileged, confidential or proprietary. If you are not an intended recipient, please notify the sender, and then please delete and destroy all copies and attachments, and be advised that any review or dissemination of, or the taking of any action in reliance on, the information contained in or attached to this message is prohibited. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Sender. Subject to applicable law, Sender may intercept, monitor, review and retain e-communications (EC) traveling through its networks/systems and may produce any such EC to regulators, law enforcement, in litigation and as required by law. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or free of errors or viruses. References to "Sender" are references to any subsidiary of Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this EC may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.bankofamerica.com/emaildisclaimer. By messaging with Sender you consent to the foregoing. |