基于boost URL库的编解码

URL 基于boost URL库的编解码:

#include <boost/algorithm/string.hpp>
#include <boost/url.hpp>
#include <iostream>

namespace url {
/// <summary>
/// url路径编码,可带参数一起编码
/// </summary>
/// <param name="orgurl">未编码的url</param>
/// <returns>已经编码的url</returns>
std::string encode(const std::string& orgurl)
{
    auto path = orgurl;
    std::string params;
    auto paramsPos = orgurl.find("?");
    if (paramsPos != std::string::npos) {
        path = orgurl.substr(0, paramsPos);
        params = orgurl.substr(paramsPos + 1);
    }
    std::string prefix;
    auto pathStart = path.find("://");
    if (pathStart != std::string::npos) {
        prefix = path.substr(0, pathStart + 3);
        path = path.substr(pathStart + 3);
    }

    {
        std::vector<std::string> substr;
        boost::split(substr, path, boost::is_any_of("/"));

        path.resize(path.size() * 3);
        path.clear();
        for (const auto& s : substr) {
            path += boost::urls::encode(s, boost::urls::unreserved_chars) + "/";
        }
        if (!path.empty()) {
            path.resize(path.size() - 1);
        }
    }

    // params
    if (!params.empty()) {
        std::vector<std::string> substr;
        boost::split(substr, params, boost::is_any_of("&"));

        params.resize(params.size() * 3);
        params.clear();
        for (const auto& s : substr) {
            if (!s.empty()) {
                auto eqpos = s.find('=');
                if (eqpos != std::string::npos) {
                    auto key = s.substr(0, eqpos);
                    auto value = s.substr(eqpos + 1);
                    params += boost::urls::encode(key, boost::urls::unreserved_chars)
                        + "="
                        + boost::urls::encode(value, boost::urls::unreserved_chars)
                        + "&";
                }
            }
        }

        if (!params.empty()) {
            params.resize(params.size() - 1);
        }
    }

    return prefix + ((!params.empty()) ? (path + "?" + params) : path);
}

/// <summary>
/// 已经编码的url进行解码
/// </summary>
/// <param name="enurl">已经编码的url</param>
/// <returns>解码后的url</returns>
std::string decode(const std::string& enurl)
{
    boost::urls::pct_string_view ur1(enurl);
    std::string buf(ur1.decoded_size(), '\0');
    ur1.decode({}, boost::urls::string_token::assign_to(buf));
    return buf;
}
} // namespace url


namespace urls = boost::urls;

int main(int argc, char** argv)
{
    std::string ourl = "https://chart.googleapis.com/chart?cht=qr&chl=https://www.boost.org/doc/libs/1_82_0/libs/url/doc/html/url/examples/qr_code.html&chs=100x100";
    std::string enurl = url::encode(ourl);
    std::string deurl = url::decode(enurl);
    std::cout << "org url: " << ourl << "\nenc url: " << enurl << "\ndec url: " << deurl << std::endl;
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值