Trim spaces from network server query

Spaces  left in  the  CAT network  server  cause incorrect  servername
lookups, particularly just spaces which override the default values.

Merged from wsjtx-1.4.




git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@4469 ab8295b8-cf94-4d9e-aec4-7959e3be5d79
This commit is contained in:
Bill Somerville 2014-10-04 01:13:26 +00:00
parent 8d3c86c399
commit 42302834f2
2 changed files with 47 additions and 44 deletions

View File

@ -6,11 +6,13 @@
#include <QString> #include <QString>
std::tuple<QHostAddress, quint16> std::tuple<QHostAddress, quint16>
network_server_lookup (QString const& query network_server_lookup (QString query
, quint16 default_service_port , quint16 default_service_port
, QHostAddress default_host_address , QHostAddress default_host_address
, QAbstractSocket::NetworkLayerProtocol required_protocol) , QAbstractSocket::NetworkLayerProtocol required_protocol)
{ {
query = query.trimmed ();
QHostAddress host_address {default_host_address}; QHostAddress host_address {default_host_address};
quint16 service_port {default_service_port}; quint16 service_port {default_service_port};
@ -20,63 +22,64 @@ network_server_lookup (QString const& query
int port_colon_index {-1}; int port_colon_index {-1};
if ('[' == query[0]) if ('[' == query[0])
{ {
// assume IPv6 combined address/port syntax [<address>]:<port> // assume IPv6 combined address/port syntax [<address>]:<port>
auto close_bracket_index = query.lastIndexOf (']'); auto close_bracket_index = query.lastIndexOf (']');
host_name = query.mid (1, close_bracket_index - 1); host_name = query.mid (1, close_bracket_index - 1);
port_colon_index = query.indexOf (':', close_bracket_index); port_colon_index = query.indexOf (':', close_bracket_index);
} }
else else
{ {
port_colon_index = query.lastIndexOf (':'); port_colon_index = query.lastIndexOf (':');
host_name = query.left (port_colon_index); host_name = query.left (port_colon_index);
} }
host_name = host_name.trimmed ();
if (port_colon_index >= 0) if (port_colon_index >= 0)
{ {
bool ok; bool ok;
service_port = query.mid (port_colon_index + 1).toUShort (&ok); service_port = query.mid (port_colon_index + 1).trimmed ().toUShort (&ok);
if (!ok) if (!ok)
{ {
throw std::runtime_error {"network server lookup error: invalid port"}; throw std::runtime_error {"network server lookup error: invalid port"};
} }
} }
} }
if (!host_name.isEmpty ()) if (!host_name.isEmpty ())
{ {
auto host_info = QHostInfo::fromName (host_name); auto host_info = QHostInfo::fromName (host_name);
if (host_info.addresses ().isEmpty ()) if (host_info.addresses ().isEmpty ())
{ {
throw std::runtime_error {"network server lookup error: host name lookup failed"}; throw std::runtime_error {"network server lookup error: host name lookup failed"};
} }
bool found {false}; bool found {false};
for (int i {0}; i < host_info.addresses ().size () && !found; ++i) for (int i {0}; i < host_info.addresses ().size () && !found; ++i)
{ {
host_address = host_info.addresses ().at (i); host_address = host_info.addresses ().at (i);
switch (required_protocol) switch (required_protocol)
{ {
case QAbstractSocket::IPv4Protocol: case QAbstractSocket::IPv4Protocol:
case QAbstractSocket::IPv6Protocol: case QAbstractSocket::IPv6Protocol:
if (required_protocol != host_address.protocol ()) if (required_protocol != host_address.protocol ())
{ {
break; break;
} }
// drop through // drop through
case QAbstractSocket::AnyIPProtocol: case QAbstractSocket::AnyIPProtocol:
found = true; found = true;
break; break;
default: default:
throw std::runtime_error {"network server lookup error: invalid required protocol"}; throw std::runtime_error {"network server lookup error: invalid required protocol"};
} }
} }
if (!found) if (!found)
{ {
throw std::runtime_error {"network server lookup error: no suitable host address found"}; throw std::runtime_error {"network server lookup error: no suitable host address found"};
} }
} }
return std::make_tuple (host_address, service_port); return std::make_tuple (host_address, service_port);

View File

@ -30,7 +30,7 @@ class QString;
// returned in the first member of the result tuple. // returned in the first member of the result tuple.
// //
std::tuple<QHostAddress, quint16> std::tuple<QHostAddress, quint16>
network_server_lookup (QString const& query network_server_lookup (QString query
, quint16 default_service_port , quint16 default_service_port
, QHostAddress default_host_address = QHostAddress::LocalHost , QHostAddress default_host_address = QHostAddress::LocalHost
, QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::AnyIPProtocol); , QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::AnyIPProtocol);