diff --git a/components/common/cpp/include/ftl/uri.hpp b/components/common/cpp/include/ftl/uri.hpp index 24123f168102de184130bb5f1391349b393d877b..71e3bf456082f73a3ec98b397a217beaf8cdb41a 100644 --- a/components/common/cpp/include/ftl/uri.hpp +++ b/components/common/cpp/include/ftl/uri.hpp @@ -44,6 +44,8 @@ namespace ftl { const std::string &getFragment() const { return m_frag; } std::string getQuery() const; const std::string &getBaseURI() const { return m_base; }; + bool hasUserInfo() const; + const std::string &getUserInfo() const; /** * Get the URI without query parameters, and limit path to length N. @@ -74,6 +76,7 @@ namespace ftl { std::string m_path; std::string m_frag; std::string m_base; + std::string m_userinfo; std::vector<std::string> m_pathseg; int m_port; scheme_t m_proto; diff --git a/components/common/cpp/src/uri.cpp b/components/common/cpp/src/uri.cpp index 6884720d1e17cd5a222b56699ce754892ef9cd59..fa059a0a8f8c1c021268dced551518e5d87fe0b5 100644 --- a/components/common/cpp/src/uri.cpp +++ b/components/common/cpp/src/uri.cpp @@ -15,27 +15,28 @@ using ftl::uri_t; using std::string; URI::URI(uri_t puri) { - _parse(puri); + _parse(puri); } URI::URI(const std::string &puri) { - _parse(puri.c_str()); + _parse(puri.c_str()); } URI::URI(const URI &c) { - m_valid = c.m_valid; - m_host = c.m_host; - m_port = c.m_port; - m_proto = c.m_proto; - m_path = c.m_path; - m_pathseg = c.m_pathseg; - m_qmap = c.m_qmap; - m_base = c.m_base; + m_valid = c.m_valid; + m_host = c.m_host; + m_port = c.m_port; + m_proto = c.m_proto; + m_path = c.m_path; + m_pathseg = c.m_pathseg; + m_qmap = c.m_qmap; + m_base = c.m_base; + m_userinfo = c.m_userinfo; m_frag = c.m_frag; } void URI::_parse(uri_t puri) { - UriUriA uri; + UriUriA uri; std::string suri = puri; @@ -56,83 +57,98 @@ void URI::_parse(uri_t puri) { } #ifdef HAVE_URIPARSESINGLE - const char *errpos; - if (uriParseSingleUriA(&uri, puri, &errpos) != URI_SUCCESS) { + const char *errpos; + if (uriParseSingleUriA(&uri, puri, &errpos) != URI_SUCCESS) { #else - UriParserStateA uris; - uris.uri = &uri; - if (uriParseUriA(&uris, suri.c_str()) != URI_SUCCESS) { + UriParserStateA uris; + uris.uri = &uri; + if (uriParseUriA(&uris, suri.c_str()) != URI_SUCCESS) { #endif - m_valid = false; - m_host = "none"; - m_port = -1; - m_proto = SCHEME_NONE; - m_path = ""; + m_valid = false; + m_host = "none"; + m_port = -1; + m_proto = SCHEME_NONE; + m_path = ""; m_frag = ""; - } else { - m_host = std::string(uri.hostText.first, uri.hostText.afterLast - uri.hostText.first); - - std::string prototext = std::string(uri.scheme.first, uri.scheme.afterLast - uri.scheme.first); - if (prototext == "tcp") m_proto = SCHEME_TCP; - else if (prototext == "udp") m_proto = SCHEME_UDP; - else if (prototext == "ftl") m_proto = SCHEME_FTL; - else if (prototext == "http") m_proto = SCHEME_HTTP; - else if (prototext == "ws") m_proto = SCHEME_WS; - else if (prototext == "ipc") m_proto = SCHEME_IPC; + } else { + m_host = std::string(uri.hostText.first, uri.hostText.afterLast - uri.hostText.first); + + std::string prototext = std::string(uri.scheme.first, uri.scheme.afterLast - uri.scheme.first); + if (prototext == "tcp") m_proto = SCHEME_TCP; + else if (prototext == "udp") m_proto = SCHEME_UDP; + else if (prototext == "ftl") m_proto = SCHEME_FTL; + else if (prototext == "http") m_proto = SCHEME_HTTP; + else if (prototext == "ws") m_proto = SCHEME_WS; + else if (prototext == "ipc") m_proto = SCHEME_IPC; else if (prototext == "device") m_proto = SCHEME_DEVICE; else if (prototext == "file") m_proto = SCHEME_FILE; - else m_proto = SCHEME_OTHER; - m_protostr = prototext; - - std::string porttext = std::string(uri.portText.first, uri.portText.afterLast - uri.portText.first); - m_port = atoi(porttext.c_str()); - - for (auto h=uri.pathHead; h!=NULL; h=h->next) { - auto pstr = std::string( - h->text.first, h->text.afterLast - h->text.first); - - m_path += "/"; - m_path += pstr; - m_pathseg.push_back(pstr); - } - - //string query = std::string(uri.query.first, uri.query.afterLast - uri.query.first); - if (uri.query.afterLast - uri.query.first > 0) { - UriQueryListA *queryList; - int itemCount; - if (uriDissectQueryMallocA(&queryList, &itemCount, uri.query.first, - uri.query.afterLast) != URI_SUCCESS) { - // Failure - } - - UriQueryListA *item = queryList; - while (item) { - m_qmap[item->key] = item->value; - item = item->next; - } - - uriFreeQueryListA(queryList); - } - - uriFreeUriMembersA(&uri); + else m_proto = SCHEME_OTHER; + m_protostr = prototext; + + std::string porttext = std::string(uri.portText.first, uri.portText.afterLast - uri.portText.first); + m_port = atoi(porttext.c_str()); + m_userinfo = std::string(uri.userInfo.first, uri.userInfo.afterLast - uri.userInfo.first); + + for (auto h=uri.pathHead; h!=NULL; h=h->next) { + auto pstr = std::string( + h->text.first, h->text.afterLast - h->text.first); + + m_path += "/"; + m_path += pstr; + m_pathseg.push_back(pstr); + } + + //string query = std::string(uri.query.first, uri.query.afterLast - uri.query.first); + if (uri.query.afterLast - uri.query.first > 0) { + UriQueryListA *queryList; + int itemCount; + if (uriDissectQueryMallocA(&queryList, &itemCount, uri.query.first, + uri.query.afterLast) != URI_SUCCESS) { + // Failure + } + + UriQueryListA *item = queryList; + while (item) { + m_qmap[item->key] = item->value; + item = item->next; + } + uriFreeQueryListA(queryList); + } + + uriFreeUriMembersA(&uri); auto fraglast = (uri.query.first != NULL) ? uri.query.first : uri.fragment.afterLast; if (uri.fragment.first != NULL && fraglast - uri.fragment.first > 0) { m_frag = std::string(uri.fragment.first, fraglast - uri.fragment.first); } - m_valid = m_proto != SCHEME_NONE && (m_host.size() > 0 || m_path.size() > 0); - - if (m_valid) { - if (m_qmap.size() > 0) m_base = std::string(uri.scheme.first, uri.query.first - uri.scheme.first - 1); - else if (uri.fragment.first != NULL) m_base = std::string(uri.scheme.first, uri.fragment.first - uri.scheme.first - 1); - else m_base = std::string(uri.scheme.first); - } - } + m_valid = m_proto != SCHEME_NONE && (m_host.size() > 0 || m_path.size() > 0); + + if (m_valid) { + // remove userinfo from base uri + const char *start = uri.scheme.first; + if (m_userinfo != "") { + m_base = std::string(start, uri.userInfo.first - start); + start = uri.userInfo.afterLast + 1; + } + else { + m_base = std::string(""); + } + if (m_qmap.size() > 0) { + m_base += std::string(start, uri.query.first - start - 1); + } + else if (uri.fragment.first != NULL) { + m_base += std::string(start, uri.fragment.first - start - 1); + } + else { + m_base += std::string(start); + } + } + } } string URI::to_string() const { - return (m_qmap.size() > 0) ? m_base + "?" + getQuery() : m_base; + return (m_qmap.size() > 0) ? m_base + "?" + getQuery() : m_base; } string URI::getPathSegment(int n) const { @@ -142,42 +158,42 @@ string URI::getPathSegment(int n) const { } string URI::getBaseURI(int n) { - if (n >= (int)m_pathseg.size()) return m_base; - if (n >= 0) { - string r = m_protostr + string("://") + m_host + ((m_port != 0) ? string(":") + std::to_string(m_port) : ""); - for (int i=0; i<n; i++) { + if (n >= (int)m_pathseg.size()) return m_base; + if (n >= 0) { + string r = m_protostr + string("://") + m_host + ((m_port != 0) ? string(":") + std::to_string(m_port) : ""); + for (int i=0; i<n; i++) { r += "/"; - r += getPathSegment(i); - } - - return r; - } else if (m_pathseg.size()+n >= 0) { - string r = m_protostr + string("://") + m_host + ((m_port != 0) ? string(":") + std::to_string(m_port) : ""); - size_t N = m_pathseg.size()+n; - for (size_t i=0; i<N; i++) { + r += getPathSegment(i); + } + + return r; + } else if (m_pathseg.size()+n >= 0) { + string r = m_protostr + string("://") + m_host + ((m_port != 0) ? string(":") + std::to_string(m_port) : ""); + size_t N = m_pathseg.size()+n; + for (size_t i=0; i<N; i++) { r += "/"; - r += getPathSegment(i); - } + r += getPathSegment(i); + } - return r; - } else return ""; + return r; + } else return ""; } string URI::getQuery() const { - string q; - for (auto x : m_qmap) { - if (q.length() > 0) q += "&"; - q += x.first + "=" + x.second; - } - return q; + string q; + for (auto x : m_qmap) { + if (q.length() > 0) q += "&"; + q += x.first + "=" + x.second; + } + return q; }; void URI::setAttribute(const string &key, const string &value) { - m_qmap[key] = value; + m_qmap[key] = value; } void URI::setAttribute(const string &key, int value) { - m_qmap[key] = std::to_string(value); + m_qmap[key] = std::to_string(value); } void URI::to_json(nlohmann::json &json) { @@ -202,3 +218,11 @@ void URI::to_json(nlohmann::json &json) { } } } + +bool URI::hasUserInfo() const { + return m_userinfo != ""; +} + +const std::string &URI::getUserInfo() const { + return m_userinfo; +} diff --git a/components/net/cpp/include/ftl/net/ws_internal.hpp b/components/net/cpp/include/ftl/net/ws_internal.hpp index 29fa3ff68e1c79771b870a7cca3172e3303a58e5..fb457578ecd1e6a4f0bb5e35819eb161acdbe837 100644 --- a/components/net/cpp/include/ftl/net/ws_internal.hpp +++ b/components/net/cpp/include/ftl/net/ws_internal.hpp @@ -32,6 +32,10 @@ struct wsheader_type { uint8_t masking_key[4]; }; +struct ws_options { + std::string userinfo = ""; +}; + /** * Websocket dispatch parser. Given a raw socket buffer and its length, this * function parses the websocket header and if valid and containing enough data @@ -49,7 +53,7 @@ int ws_parse(msgpack::unpacker &buf, wsheader_type &ws); */ int ws_prepare(wsheader_type::opcode_type, bool useMask, size_t len, char *buffer, size_t maxlen); -bool ws_connect(SOCKET sockfd, const ftl::URI &uri); +bool ws_connect(SOCKET sockfd, const ftl::URI &uri, const ws_options &options=ws_options()); }; }; diff --git a/components/net/cpp/src/ws_internal.cpp b/components/net/cpp/src/ws_internal.cpp index db9393d2a5350f07c69b5ce514f59f6ccdfe68b5..780a54c65475653c5aef922f04e41dca5b2579b2 100644 --- a/components/net/cpp/src/ws_internal.cpp +++ b/components/net/cpp/src/ws_internal.cpp @@ -6,7 +6,10 @@ #include <loguru.hpp> #include <cstring> + #include <ftl/net/ws_internal.hpp> +#include <ftl/utility/base64.hpp> + #include <memory> @@ -184,7 +187,7 @@ int ftl::net::ws_prepare(wsheader_type::opcode_type op, bool useMask, size_t len return (int)header_size; } -bool ftl::net::ws_connect(SOCKET sockfd, const URI &uri) { +bool ftl::net::ws_connect(SOCKET sockfd, const URI &uri, const ws_options &options) { string http = ""; int status; int i; @@ -196,6 +199,12 @@ bool ftl::net::ws_connect(SOCKET sockfd, const URI &uri) { } else { http += "Host: "+uri.getHost()+":"+std::to_string(uri.getPort())+"\r\n"; } + if (uri.hasUserInfo()) { + //https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization + http += "Authorization: Basic "; + http += base64_encode(uri.getUserInfo()) + "\r\n"; + } + http += "Upgrade: websocket\r\n"; http += "Connection: Upgrade\r\n"; http += "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n";