1 /*
2  * Copyright (c) 2023-2023 Huawei Device 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 HISTREAMER_HTTP_CURL_CLIENT_H
17 #define HISTREAMER_HTTP_CURL_CLIENT_H
18 
19 #include <string>
20 #include <list>
21 #include "network/network_client.h"
22 #include "network/network_typs.h"
23 #include "curl/curl.h"
24 #include "osal/task/mutex.h"
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29 #include <iostream>
30 
31 namespace OHOS {
32 namespace Media {
33 namespace Plugins {
34 namespace HttpPlugin {
35 
36 std::string ToString(const std::list<std::string> &lists, char tab = ',');
37 std::string InsertCharBefore(std::string input, char from, char preChar, char nextChar);
38 std::string Trim(std::string str);
39 bool IsRegexValid(const std::string &regex);
40 std::string ReplaceCharacters(const std::string &input);
41 bool IsMatch(const std::string &str, const std::string &patternStr);
42 bool IsExcluded(const std::string &str, const std::string &exclusions, const std::string &split);
43 
44 class HttpCurlClient : public NetworkClient {
45 public:
46     HttpCurlClient(RxHeader headCallback, RxBody bodyCallback, void* userParam);
47 
48     ~HttpCurlClient() override;
49 
50     Status Init() override;
51 
52     Status Open(const std::string& url, const std::map<std::string, std::string>& httpHeader,
53                 int32_t timeoutMs) override;
54 
55     Status RequestData(long startPos, int len, const RequestInfo& requestInfo,
56         HandleResponseCbFunc completedCb) override;
57 
58     Status Close(bool isAsync) override;
59 
60     Status Deinit() override;
61     Status GetIp(std::string &ip) override;
62     void SetAppUid(int32_t appUid) override;
63 
64     struct SocketOwner {
65         uid_t uid;
66         gid_t gid;
67     };
68 private:
69     void InitCurlEnvironment(const std::string& url, int32_t timeoutMs);
70     void InitCurProxy(const std::string& url);
71     std::string UrlParse(const std::string& url) const;
72     void HttpHeaderParse(std::map<std::string, std::string> httpHeader);
73     static std::string ClearHeadTailSpace(std::string& str);
74     void CheckRequestRange(long startPos, int len);
75     Status SetIp();
76 
77 private:
78     RxHeader rxHeader_;
79     RxBody rxBody_;
80     void *userParam_;
81     CURL* easyHandle_ {nullptr};
82     mutable Mutex mutex_;
83     struct curl_slist* headerList_ {nullptr};
84     std::string ip_ {};
85     bool ipFlag_ {false};
86     bool isFirstRequest_ {true};
87     bool isFirstOpen_ {true};
88     volatile int32_t appUid_ {-1};
89 };
90 }
91 }
92 }
93 }
94 #endif