Update to version 2
This commit is contained in:
77
libs/gempa/caps/url.cpp
Normal file
77
libs/gempa/caps/url.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
/***************************************************************************
|
||||
* Copyright (C) 2021 by gempa GmbH *
|
||||
* *
|
||||
* All Rights Reserved. *
|
||||
* *
|
||||
* NOTICE: All information contained herein is, and remains *
|
||||
* the property of gempa GmbH and its suppliers, if any. The intellectual *
|
||||
* and technical concepts contained herein are proprietary to gempa GmbH *
|
||||
* and its suppliers. *
|
||||
* Dissemination of this information or reproduction of this material *
|
||||
* is strictly forbidden unless prior written permission is obtained *
|
||||
* from gempa GmbH. *
|
||||
***************************************************************************/
|
||||
|
||||
|
||||
#include "url.h"
|
||||
#include "utils.h"
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace {
|
||||
|
||||
const string CAPS_PROTOCOL = "caps";
|
||||
const string CAPS_SSL_PROTOCOL = "capss";
|
||||
|
||||
}
|
||||
|
||||
namespace Gempa {
|
||||
namespace CAPS {
|
||||
|
||||
Url::Url()
|
||||
: port(0) {}
|
||||
|
||||
bool Url::parse(const string &address, uint16_t defaultPort) {
|
||||
string addr = trim(address);
|
||||
|
||||
// step 1: protocol
|
||||
size_t pos = addr.find("://");
|
||||
if ( pos == string::npos ) {
|
||||
protocol = CAPS_PROTOCOL;
|
||||
}
|
||||
else {
|
||||
protocol = addr.substr(0, pos);
|
||||
addr = addr.substr(pos + 3);
|
||||
}
|
||||
|
||||
if ( !boost::iequals(protocol, CAPS_PROTOCOL) &&
|
||||
!boost::iequals(protocol, CAPS_SSL_PROTOCOL) ) {
|
||||
errorString = "Unsupported protocol: %" + protocol;
|
||||
return false;
|
||||
}
|
||||
|
||||
// step 2: user:pass
|
||||
vector<string> toks;
|
||||
boost::split(toks, addr, boost::is_any_of("@"), boost::token_compress_on);
|
||||
if ( toks.size() >= 2 ) {
|
||||
string login = toks[0];
|
||||
addr = toks[1];
|
||||
boost::split(toks, login, boost::is_any_of(":"), boost::token_compress_on);
|
||||
user = toks.size() > 0 ? toks[0] : "";
|
||||
password = toks.size() > 1 ? toks[1] : "";
|
||||
}
|
||||
|
||||
// step 3: address
|
||||
if ( !splitAddress(host, port, addr, defaultPort) ) {
|
||||
errorString = "Wrong address format, expected "
|
||||
"[[caps|capss]://][user:pass@]host[:port]";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user