From afcc07facd63033c61e197575484d2d5bdca2fc0 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 7 Dec 2023 13:31:24 -0500 Subject: [PATCH] Rough in a Uri class, use it to populate onvif_url if not filled in --- src/CMakeLists.txt | 1 + src/zm_monitor.cpp | 7 +++++++ src/zm_uri.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++ src/zm_uri.h | 31 ++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 src/zm_uri.cpp create mode 100644 src/zm_uri.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c8cb40cd2..9e0e3ba36 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -71,6 +71,7 @@ set(ZM_BIN_SRC_FILES zm_stream.cpp zm_swscale.cpp zm_time.cpp + zm_uri.cpp zm_user.cpp zm_utils.cpp zm_videostore.cpp diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index 4e35d29ef..0a03bdb2a 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -32,6 +32,7 @@ #include "zm_signal.h" #include "zm_time.h" #include "zm_utils.h" +#include "zm_uri.h" #include "zm_zone.h" @@ -531,6 +532,12 @@ void Monitor::Load(MYSQL_ROW dbrow, bool load_zones=true, Purpose p = QUERY) { /* "`ONVIF_URL`, `ONVIF_Username`, `ONVIF_Password`, `ONVIF_Options`, `ONVIF_Event_Listener`, `use_Amcrest_API`, " */ onvif_url = std::string(dbrow[col] ? dbrow[col] : ""); col++; + if (onvif_url.empty()) { + Uri path_uri(path); + if (!path_uri.Host.empty()) { + onvif_url = "http://"+path_uri.Host+"/onvif/device_service"; + } + } onvif_username = std::string(dbrow[col] ? dbrow[col] : ""); col++; onvif_password = std::string(dbrow[col] ? dbrow[col] : ""); col++; onvif_options = std::string(dbrow[col] ? dbrow[col] : ""); col++; diff --git a/src/zm_uri.cpp b/src/zm_uri.cpp new file mode 100644 index 000000000..930d1f75e --- /dev/null +++ b/src/zm_uri.cpp @@ -0,0 +1,51 @@ +#include // find + +#include "zm_uri.h" + +Uri::Uri(const std::string &uri) { + if (uri.empty()) return; + + typedef std::string::const_iterator iterator_t; + + iterator_t uriEnd = uri.end(); + + // get query start + iterator_t queryStart = std::find(uri.begin(), uriEnd, '?'); + + // protocol + iterator_t protocolStart = uri.begin(); + iterator_t protocolEnd = std::find(protocolStart, uriEnd, ':'); //"://"); + + if (protocolEnd != uriEnd) { + std::string prot = &*(protocolEnd); + if ((prot.length() > 3) && (prot.substr(0, 3) == "://")) { + Protocol = std::string(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, '/'); // get pathStart + + iterator_t hostEnd = std::find(protocolEnd, + (pathStart != uriEnd) ? pathStart : queryStart, + ':'); // check for port + + Host = std::string(hostStart, hostEnd); + + // port + if ((hostEnd != uriEnd) && ((&*(hostEnd))[0] == ':')) { + // we have a port + hostEnd++; + iterator_t portEnd = (pathStart != uriEnd) ? pathStart : queryStart; + Port = std::string(hostEnd, portEnd); + } + + if (pathStart != uriEnd) Path = std::string(pathStart, queryStart); + if (queryStart != uriEnd) QueryString = std::string(queryStart, uri.end()); +} diff --git a/src/zm_uri.h b/src/zm_uri.h new file mode 100644 index 000000000..41c427dad --- /dev/null +++ b/src/zm_uri.h @@ -0,0 +1,31 @@ +// +// ZoneMinder Uri Class +// Copyright (C) 2023 ZoneMinder Inc +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// + +#ifndef ZM_URI_H +#define ZM_URI_H + +#include + +class Uri { + public: + std::string QueryString, Path, Protocol, Host, Port; + + Uri(const std::string &uri); +}; +#endif