1 /* 2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_SHARING_RTSP_URI_H 17 #define OHOS_SHARING_RTSP_URI_H 18 19 #include <iostream> 20 #include <regex> 21 #include <string> 22 23 #define RTSP_DEFAULT_PORT 554 24 namespace OHOS { 25 namespace Sharing { 26 class RtspUrl { 27 public: 28 RtspUrl() = default; RtspUrl(const std::string & url)29 explicit RtspUrl(const std::string &url) 30 { 31 Parse(url); 32 } 33 Parse(const std::string & url)34 bool Parse(const std::string &url) 35 { 36 if (url.empty()) { 37 return false; 38 } 39 40 std::regex match("^rtsp://(([a-zA-Z0-9]+):([a-zA-Z0-9]+)@)?([a-zA-Z0-9.-]+)(:([0-9]+))?"); 41 std::smatch sm; 42 if (!std::regex_search(url, sm, match)) { 43 return false; 44 } 45 if (sm.size() != 7) { // 7:fixed size 46 return false; 47 } 48 username_ = sm[2].str(); // 2:matching position 49 password_ = sm[3].str(); // 3:matching position 50 host_ = sm[4].str(); // 4:matching position 51 int32_t port = std::atoi(sm[6].str().c_str()); // 6:matching position 52 if (port > 0) { 53 port_ = port; 54 } 55 path_ = sm.suffix().str(); 56 if (username_.empty() && path_.find("?username") != std::string::npos) { 57 auto ui = path_.find("?username"); 58 auto pi = path_.find("&password"); 59 if (pi != std::string::npos) { 60 username_ = path_.substr(ui + 10, pi - ui - 10); // 10:matching position 61 password_ = path_.substr(pi + 10); // 10:matching position 62 } else { 63 username_ = path_.substr(ui + 10); // 10:matching position 64 } 65 path_ = path_.substr(0, path_.find('?')); 66 } 67 68 return true; 69 } 70 GetHost()71 std::string GetHost() const 72 { 73 return host_; 74 } 75 GetPath()76 std::string GetPath() const 77 { 78 return path_; 79 } 80 GetPort()81 uint16_t GetPort() const 82 { 83 return port_; 84 } 85 GetUsername()86 std::string GetUsername() const 87 { 88 return username_; 89 } 90 GetPassword()91 std::string GetPassword() const 92 { 93 return password_; 94 } 95 GetUrl()96 std::string GetUrl() const 97 { 98 return "rtsp://" + host_ + ':' + std::to_string(port_) + path_; 99 } 100 101 private: 102 uint16_t port_ = RTSP_DEFAULT_PORT; 103 104 std::string host_; 105 std::string path_; 106 std::string username_; 107 std::string password_; 108 }; 109 } // namespace Sharing 110 } // namespace OHOS 111 #endif // OHOS_SHARING_RTSP_URI_H 112