1 /*
2  * Copyright (c) 2022 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 CURLADP_H
17 #define CURLADP_H
18 
19 #include <mutex>
20 #include <vector>
21 
22 #include "curl/curl.h"
23 #include "curl/easy.h"
24 #include "i_upload_task.h"
25 #include "timer.h"
26 #include "upload/upload_common.h"
27 #include "upload_config.h"
28 
29 namespace OHOS::Request::Upload {
30 class CUrlAdp : public std::enable_shared_from_this<CUrlAdp> {
31 public:
32     CUrlAdp(std::vector<FileData> &fileArray, std::shared_ptr<UploadConfig> &config);
33     virtual ~CUrlAdp();
34     uint32_t DoUpload(std::shared_ptr<IUploadTask> task);
35     bool Remove();
IsReadAbort()36     bool IsReadAbort()
37     {
38         return isReadAbort_;
39     }
40 
41 protected:
42     bool ClearCurlResource();
43     void SplitHttpMessage(const std::string &stmp, FileData *&fData);
44     static int ProgressCallback(
45         void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
46     static size_t HeaderCallback(char *buffer, size_t size, size_t nitems, void *userdata);
47     static size_t ReadCallback(char *buffer, size_t size, size_t nitems, void *arg);
48     static void NotifyAPI5(FileData *fData, std::string &headers);
49     static bool CheckCUrlAdp(FileData *fData);
50 
51 private:
52     int CheckUploadStatus(CURLM *curlMulti);
53     bool MultiAddHandle(CURLM *curlMulti, std::vector<CURL *> &curlArray);
54     int32_t UploadOneFile();
55     bool IsSuccess(const uint32_t count, const uint32_t size);
56     void SetCurlOpt(CURL *curl);
57     void SetHeadData(CURL *curl);
58     void SetHttpPut(CURL *curl);
59     void SetMimePost(CURL *curl);
60     void SetSslOpt(CURL *curl);
61     void SetConnectionOpt(CURL *curl);
62     void SetNetworkOpt(CURL *curl);
63     void SetCallbackOpt(CURL *curl);
64     void SetBehaviorOpt(CURL *curl);
65     std::string ReadCertification();
66     void CurlGlobalInit();
67     void CurlGlobalCleanup();
68     void StartTimer();
69     void StopTimer();
70 
71 private:
72     uint32_t timerId_;
73     std::shared_ptr<IUploadTask> uploadTask_;
74     std::vector<FileData> &fileDatas_;
75     FileData mfileData_;
76     std::shared_ptr<UploadConfig> config_;
77     static constexpr int32_t HTTP_SUCCESS = 200;
78     std::mutex mutex_;
79     std::mutex curlMutex_;
80     std::mutex globalMutex_;
81     bool isCurlGlobalInit_;
82     CURLM *curlMulti_;
83     std::vector<CURL *> curlArray_;
84     bool isReadAbort_;
85     Utils::Timer timer_;
86 
87     static constexpr int TRANS_TIMEOUT_MS = 300 * 1000;
88     static constexpr int READFILE_TIMEOUT_MS = 30 * 1000;
89     static constexpr int TIMEOUTTYPE = 1;
90     static constexpr int FILE_UPLOAD_INTERVAL = 1000;
91     static constexpr int COLLECT_DO_FLAG = 1;
92     static constexpr int COLLECT_END_FLAG = 2;
93 };
94 } // namespace OHOS::Request::Upload
95 #endif