Esempio n. 1
0
// Helper function to convert message body without extracting.
static utility::string_t convert_body_to_string_t(const utility::string_t &content_type, concurrency::streams::istream instream)
{
    if (!instream)
    {
        // The instream is not yet set
        return utility::string_t();
    }

    concurrency::streams::streambuf<uint8_t>  streambuf = instream.streambuf();

    _ASSERTE((bool)streambuf);
    _ASSERTE(streambuf.is_open());
    _ASSERTE(streambuf.can_read());

    utility::string_t content, charset;
    parse_content_type_and_charset(content_type, content, charset);

    // Content-Type must have textual type.
    if(!is_content_type_textual(content) || streambuf.in_avail() == 0)
    {
        return utility::string_t();
    }

    std::vector<unsigned char> body((std::vector<unsigned char>::size_type)streambuf.in_avail());
    size_t copied = streambuf.scopy(&body[0], body.size());
    if (copied == 0 || copied == (size_t)-1) 
        return _XPLATSTR("");

    // Latin1
#ifdef _MS_WINDOWS
    if(_wcsicmp(charset.c_str(), charset_latin1.c_str()) == 0)
#else
    if(boost::iequals(charset, charset_latin1))
#endif
    {
        return to_string_t(latin1_to_utf16(convert_bytes_to_string(&body[0], body.size())));
    }

    // utf-8.
#ifdef _MS_WINDOWS
    else if(_wcsicmp(charset.c_str(), charset_utf8.c_str()) == 0)
#else
    else if(boost::iequals(charset, charset_utf8) )
#endif
    {
        return to_string_t(convert_utf8_to_utf16(&body[0], body.size()));
    }

    // utf-16.
#ifdef _MS_WINDOWS
    else if(_wcsicmp(charset.c_str(), charset_utf16.c_str()) == 0)
#else
    else if(boost::iequals(charset, charset_utf16) )
#endif
    {
        return to_string_t(convert_utf16_to_utf16(&body[0], body.size()));
    }

    // utf-16le
#ifdef _MS_WINDOWS
    else if(_wcsicmp(charset.c_str(), charset_utf16le.c_str()) == 0)
#else
    else if(boost::iequals(charset, charset_utf16le) )
#endif
    {
        return to_string_t(convert_bytes_to_wstring(&body[0], body.size()));
    }

    // utf-16be
#ifdef _MS_WINDOWS
    else if(_wcsicmp(charset.c_str(), charset_utf16be.c_str()) == 0)
#else
    else if(boost::iequals(charset, charset_utf16be) )
#endif
    {
        return to_string_t(convert_utf16be_to_utf16le(&body[0], body.size(), false));
    }
    
    else
    {
        return utility::string_t();
    }
}
Esempio n. 2
0
// Helper function to convert message body without extracting.
static utility::string_t convert_body_to_string_t(const utility::string_t& content_type,
                                                  concurrency::streams::istream instream)
{
    if (!instream)
    {
        // The instream is not yet set
        return utility::string_t();
    }

    concurrency::streams::streambuf<uint8_t> streambuf = instream.streambuf();

    _ASSERTE((bool)streambuf);
    _ASSERTE(streambuf.is_open());
    _ASSERTE(streambuf.can_read());

    utility::string_t content, charset;
    parse_content_type_and_charset(content_type, content, charset);

    // Content-Type must have textual type.
    if (!is_content_type_textual(content) || streambuf.in_avail() == 0)
    {
        return utility::string_t();
    }

    // Latin1
    if (utility::details::str_iequal(charset, charset_types::latin1))
    {
        std::string body;
        body.resize(streambuf.in_avail());
        if (streambuf.scopy((unsigned char*)&body[0], body.size()) == 0) return string_t();
        return to_string_t(latin1_to_utf16(std::move(body)));
    }

    // utf-8.
    else if (utility::details::str_iequal(charset, charset_types::utf8))
    {
        std::string body;
        body.resize(streambuf.in_avail());
        if (streambuf.scopy((unsigned char*)&body[0], body.size()) == 0) return string_t();
        return to_string_t(std::move(body));
    }

    // utf-16.
    else if (utility::details::str_iequal(charset, charset_types::utf16))
    {
        utf16string body;
        body.resize(streambuf.in_avail() / sizeof(utf16string::value_type));
        if (streambuf.scopy((unsigned char*)&body[0], body.size() * sizeof(utf16string::value_type)) == 0)
            return string_t();
        return convert_utf16_to_string_t(std::move(body));
    }

    // utf-16le
    else if (utility::details::str_iequal(charset, charset_types::utf16le))
    {
        utf16string body;
        body.resize(streambuf.in_avail() / sizeof(utf16string::value_type));
        if (streambuf.scopy((unsigned char*)&body[0], body.size() * sizeof(utf16string::value_type)) == 0)
            return string_t();
        return convert_utf16le_to_string_t(std::move(body), false);
    }

    // utf-16be
    else if (utility::details::str_iequal(charset, charset_types::utf16be))
    {
        utf16string body;
        body.resize(streambuf.in_avail() / sizeof(utf16string::value_type));
        if (streambuf.scopy((unsigned char*)&body[0], body.size() * sizeof(utf16string::value_type)) == 0)
            return string_t();
        return convert_utf16be_to_string_t(std::move(body), false);
    }

    else
    {
        return utility::string_t();
    }
}
size_t seek_read_and_compare(concurrency::streams::istream stream, std::vector<uint8_t> buffer_to_compare, utility::size64_t offset, size_t count, size_t expected_read_count)
{
    std::vector<uint8_t> buffer;
    buffer.resize(count);
    stream.seek(offset);
    auto read_count = stream.streambuf().getn(buffer.data(), count).get();
    CHECK_EQUAL(expected_read_count, read_count);
    CHECK_ARRAY_EQUAL(buffer_to_compare.data() + offset, buffer.data(), (int)read_count);
    return read_count;
}