How to simply read a stream into a string?

Oct 17, 2014 at 3:35 PM
I'm 5 minutes into using the sdk and already bewildered at the redefinition of the standard things in the std:: namespaces like fstream. I imagine you have you reasons for doing so, but have you considered doing it completely? Case in point: I want to use a wstringstream but unfortunately after I read in the buffer with resp.body().read_to_end(os.streambuf()) it turns out that wstringstream does not have a str() method.

So, 2 question really:
  1. If you're duplicating STL interfaces (in itself a bad idea), why aren't you supporting them fully?
  2. How can I get the entire string from the buffer?
Oct 17, 2014 at 4:28 PM
Okay, now I tried stringstreambuf buf followed by resp.body().read_to_end(buf) and it doesn't work either.
Oct 17, 2014 at 4:35 PM
This should also work, but does not

http_client client(U("http://google.com"));
std::wstring s;

auto task = client.request(methods::GET)
.then([=](http_response resp)
{
return resp.extract_string();
}).then([&](const string& ss) {
s = ss;
});
task.wait();

std::wcout << s << std::endl;
Coordinator
Oct 17, 2014 at 5:02 PM
Hi nesteruk,

(1st and 2nd posts)
In Casablanca, we use a different stream interface that better supports the task model. If you want to use a standard stream as one of our streams, you can use interopstream.h to wrap your stream into a Casablanca stream (and then retrieve the data as you usually would). You can also wrap a standard container like std::vector using containerstream.h. See https://casablanca.codeplex.com/wikipage?title=Asynchronous%20Streams for more details on that.

If you simply want a string out, try using concurrency::streams::container_buffer<std::string> as the stream, then using the underlying string.

(3rd post)
What precisely is the problem you're encountering? As written, it shouldn't compile (since you're trying to catch the result of extract_string() with a std::string instead of a std::wstring). I assume that's just a mistranslation while copying it into the post.

Sincerely,
roschuma
Oct 17, 2014 at 5:42 PM
I'm now trying the following, which crashes outright while trying to read a zero memory location:

``` http_client client(U("http://activemesa.com"));

auto task = client.request(methods::GET)
.then([=](http_response resp)
{
if (resp.status_code() == status_codes::OK)
{
  auto body = resp.extract_string().get();
  std::wcout << body;
}
});
task.wait();```
Oct 17, 2014 at 5:46 PM
Image
Oct 17, 2014 at 5:53 PM
Okay, I can confirm that this is a bona fide bug because when I switched to MSVC, the bug went away.
Coordinator
Oct 17, 2014 at 6:08 PM
Could you please provide more details here? What do you mean by "Switched to MSVC"?

roschuma
Oct 17, 2014 at 6:09 PM
I switched to the Microsoft compiler in Visual Studio. I don't use MSVC since Intel parallel studio is so vastly superior.
Coordinator
Oct 17, 2014 at 6:15 PM
While we don't officially support Intel parallel studio, if you can trace this back down to a code bug in the library, we would be glad to fix it.

Let us know if you decide to pursue this,
roschuma
Oct 17, 2014 at 6:17 PM
I'm happy to help. What do I need to do?
Coordinator
Oct 17, 2014 at 6:23 PM
First, (and you may have already done this), you'll need to build casablanca from source. From there, you'll need to track down why that memcpy is accessing invalid memory. Most likely, it's because some object got deleted before it should have. If you can figure out what object is being deleted, (say, a std::wstring of some kind), you could try adding a breakpoint to the destructor for std::wstring and step through the code until you see the string being destroyed too early.

If that works, you should be able to find out why it's being destroyed. That cause is probably either a bug in our code or the intel compiler being overly aggressive about optimizations.

Other than those guidelines, I can't really give much more advice. Generally speaking, just use standard debugging techniques to figure out why what's happening is happening and why you expect something different to happen.

Sincerely,
roschuma
Coordinator
Oct 17, 2014 at 6:26 PM
Oh, also -- please work off the development branch. If you find an issue big enough to warrant a full code contribution, it will be easier to merge if you're working off of the development branch instead of master.