Uri Parse

#include <string>
#include <algorithm>    // find


struct Uri
{
public:
std::wstring QueryString, Path, Protocol, Host, Port;


static Uri Parse(const std::wstring &uri)
{
Uri result;


typedef std::wstring::const_iterator iterator_t;


if (uri.length() == 0)
return result;


iterator_t uriEnd = uri.end();


// get query start
iterator_t queryStart = std::find(uri.begin(), uriEnd, L'?');


// protocol
iterator_t protocolStart = uri.begin();
iterator_t protocolEnd = std::find(protocolStart, uriEnd, L':');            //"://");


if (protocolEnd != uriEnd)
{
std::wstring prot = &*(protocolEnd);
if ((prot.length() > 3) && (prot.substr(0, 3) == L"://"))
{
result.Protocol = std::wstring(protocolStart, protocolEnd);
protocolEnd += 3;   //      ://
}
else
protocolEnd = uri.begin();  // no protocol
}
else
protocolEnd = uri.begin();  // no protocol


// host
iterator_t hostStart = protocolEnd;
iterator_t pathStart = std::find(hostStart, uriEnd, L'/');  // get pathStart


iterator_t hostEnd = std::find(protocolEnd,
(pathStart != uriEnd) ? pathStart : queryStart,
L':');  // check for port


result.Host = std::wstring(hostStart, hostEnd);


// port
if ((hostEnd != uriEnd) && ((&*(hostEnd))[0] == L':'))  // we have a port
{
hostEnd++;
iterator_t portEnd = (pathStart != uriEnd) ? pathStart : queryStart;
result.Port = std::wstring(hostEnd, portEnd);
}


// path
if (pathStart != uriEnd)
result.Path = std::wstring(pathStart, queryStart);


// query
if (queryStart != uriEnd)
result.QueryString = std::wstring(queryStart, uri.end());


return result;


}   // Parse
};  // uri
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值