http::http_request http_client_impl::_build_get_request(http::method method, http::uri_builder request_uri, const ::utility::string_t& accept) const
{
    http::http_request msg(method);

    if (!accept.empty())
        msg.headers().add(U("Accept"), accept);

    msg.set_request_uri(request_uri.to_uri());

    return msg;
}
Exemplo n.º 2
0
http::http_request http_client_impl::_build_get_request(http::method method, http::uri_builder request_uri, const ::utility::string_t& accept) const
{
    http::http_request msg(method);

    if (!accept.empty())
        msg.headers().add(U("Accept"), accept);

    msg.headers().add(U("Authorization"), U("Basic YWRtaW5Ab2F1dGgzLmNjc2N0cC5uZXQ6UGEkJHcwcmQ="));
    msg.set_request_uri(request_uri.to_uri());

    return msg;
}
Exemplo n.º 3
0
bool is_relative_path(const ::utility::string_t& _root_url, const ::utility::string_t& _path)
{
	if (_root_url.empty() || _path.empty())
	{
		return false;
	}

	::utility::string_t root_url = _root_url;
	::utility::string_t path = _path;

	if (root_url.length() > path.length())
	{
		return true;
	}

	std::transform(root_url.begin(), root_url.end(), root_url.begin(), ::tolower);
	std::transform(path.begin(), path.end(), path.begin(), ::tolower);

	size_t index = path.find(root_url);

	return path.find(root_url) != 0 ? true : false;
}
Exemplo n.º 4
0
http::http_request http_client_impl::_build_request(http::method method, http::uri_builder request_uri, const ::utility::string_t& accept, json::value object) const
{
    http::http_request msg(method);

    if (!accept.empty())
        msg.headers().add(U("Accept"), accept);

    msg.set_request_uri(request_uri.to_uri());

    if (method == http::methods::POST || method == http::methods::PUT || method == http::methods::PATCH)
    {
        if(object.is_null() || !object.is_object())
            throw std::invalid_argument("POST, PUT, and PATCH requests require a payload");

        msg.set_body(object);
    }

    return msg;
}
void http_asserts::assert_test_request_equals(
        const test_request *const p_request,
        const ::http::method &mtd,
        const utility::string_t &path,
        const utility::string_t &content_type)
{
    VERIFY_ARE_EQUAL(mtd, p_request->m_method);
    VERIFY_ARE_EQUAL(path, p_request->m_path);

    // verify that content-type key exists in the header and the value matches the one provided
    auto iter = p_request->m_headers.find(U("Content-Type"));
    if(content_type.empty())
    {
        VERIFY_ARE_EQUAL(iter, p_request->m_headers.end());
    }
    else
    {
        VERIFY_IS_TRUE(iter != p_request->m_headers.end());
        VERIFY_ARE_EQUAL(iter->second.find(content_type), 0);
    }
}
Exemplo n.º 6
0
::utility::string_t strip_string(const ::utility::string_t& escaped)
{
    ::utility::string_t::size_type first = 0;
    ::utility::string_t::size_type size = escaped.size();

	if (escaped.empty())
	{
		return escaped;
	}

    if (escaped[0] == U('"'))
	{
        first += 1;
	}

    if (escaped[size - 1] == U('"'))
	{
        size -= 1;
	}

    return escaped.substr(first, size - first);
}
Exemplo n.º 7
0
void header_test_impl(const uri &address, const utility::string_t &headerName, const utility::string_t &headerValue, const utility::string_t &expectedHeaderValue = U(""))
{
    test_websocket_server server;
    websocket_client_config config;
    utility::string_t expectedValue = headerValue;
    if (!expectedHeaderValue.empty())
    {
        expectedValue = expectedHeaderValue;
    }
    config.headers().add(headerName, headerValue);
    websocket_client client(config);

    server.set_http_handler([&](test_http_request request)
    {
        test_http_response resp;
        if (request->get_header_val(utility::conversions::to_utf8string(headerName)).compare(utility::conversions::to_utf8string(expectedValue)) == 0)
            resp.set_status_code(200); // Handshake request will be completed only if header match succeeds.
        else
            resp.set_status_code(400); // Else fail the handshake, websocket client connect will fail in this case.
        return resp;
    });
    client.connect(address).wait();
    client.close().wait();
}
Exemplo n.º 8
0
std::vector<unsigned char> _from_base64(const utility::string_t& input)
{
    std::vector<unsigned char> result;

    if ( input.empty() ) 
        return result;

    size_t padding = 0;

    // Validation
    {
        auto size = input.size();

        if ( (size % 4) != 0 )
        {
            throw std::runtime_error("length of base64 string is not an even multiple of 4");
        }

        for (auto iter = input.begin(); iter != input.end(); ++iter,--size)
        {
            auto ch = *iter;
            if ( ch < 0 || _base64_dectbl[ch] == 255 )
            {
                throw std::runtime_error("invalid character found in base64 string");
            }
            if ( _base64_dectbl[ch] == 254 )
            {
                padding++;
                // padding only at the end
                if ( size > 2 || (size == 2 && _base64_dectbl[*(iter+1)] != 254) )
                {
                    throw std::runtime_error("invalid padding character found in base64 string");
                }
            }
        }
    }


    auto size = input.size();
    const char_t* ptr = &input[0];

    auto outsz = (size / 4)*3;
    outsz -= padding;

    result.resize(outsz);

    size_t idx = 0;
    for (; size > 4; ++idx )
    {
        unsigned char target[3];
        memset(target, 0, sizeof(target));
        _triple_byte* record = reinterpret_cast<_triple_byte*>(target);

        unsigned char val0 = _base64_dectbl[ptr[0]];
        unsigned char val1 = _base64_dectbl[ptr[1]];
        unsigned char val2 = _base64_dectbl[ptr[2]];
        unsigned char val3 = _base64_dectbl[ptr[3]];

        record->_0   = val0;
        record->_1_1 = val1 >> 4;
        result[idx] = target[0];

        record->_1_2 = val1 & 0xF;
        record->_2_1 = val2 >> 2;
        result[++idx] = target[1];

        record->_2_2 = val2 & 0x3;
        record->_3   = val3 & 0x3F;
        result[++idx] = target[2];

        ptr += 4;
        size -= 4;
    }

    // Handle the last four bytes separately, to avoid having the conditional statements
    // in all the iterations (a performance issue).

    { 
        unsigned char target[3];
        memset(target, 0, sizeof(target));
        _triple_byte* record = reinterpret_cast<_triple_byte*>(target);

        unsigned char val0 = _base64_dectbl[ptr[0]];
        unsigned char val1 = _base64_dectbl[ptr[1]];
        unsigned char val2 = _base64_dectbl[ptr[2]];
        unsigned char val3 = _base64_dectbl[ptr[3]];

        record->_0   = val0;
        record->_1_1 = val1 >> 4;
        result[idx] = target[0];

        record->_1_2 = val1 & 0xF;
        if ( val2 != 254 )
        {
            record->_2_1 = val2 >> 2;
            result[++idx] = target[1];
        }
        else
        {
            // There shouldn't be any information (ones) in the unused bits,
            if ( record->_1_2 != 0 )