• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "net_http_ffi.h"
16 #include "net_http_request_context.h"
17 #include "net_http_cache_proxy.h"
18 #include "net_http_client_exec.h"
19 #include "cj_lambda.h"
20 
21 #include <vector>
22 
23 using namespace OHOS::FFI;
24 namespace OHOS::NetStack::Http {
25 
SetUnknowError(RetDataCString & ret)26 void SetUnknowError(RetDataCString &ret)
27 {
28     ret.code = static_cast<int32_t>(HttpErrorCode::HTTP_UNKNOWN_OTHER_ERROR);
29     ret.data = MallocCString("Unknown Other Error.");
30 }
31 
32 EXTERN_C_START
CJ_CreateHttpResponseCache(uint32_t cacheSize)33     int32_t CJ_CreateHttpResponseCache(uint32_t cacheSize)
34     {
35         CacheProxy::RunCacheWithSize(cacheSize);
36         return 0;
37     }
38 
CJ_HttpResponseCacheFlush()39     int32_t CJ_HttpResponseCacheFlush()
40     {
41         CacheProxy::FlushCache();
42         return 0;
43     }
44 
CJ_HttpResponseCacheDelete()45     int32_t CJ_HttpResponseCacheDelete()
46     {
47         CacheProxy::StopCacheAndDelete();
48         return 0;
49     }
50 
CJ_CreateHttp()51     int64_t CJ_CreateHttp()
52     {
53         auto request = FFI::FFIData::Create<HttpRequestProxy>();
54         if (!request) {
55             return ERR_INVALID_INSTANCE_CODE;
56         }
57         return request->GetID();
58     }
59 
CJ_DestroyRequest(int64_t id)60     void CJ_DestroyRequest(int64_t id)
61     {
62         auto req = FFIData::GetData<HttpRequestProxy>(id);
63         if (req == nullptr) {
64             NETSTACK_LOGE("Failed to get HttpRequestProxy.");
65             return;
66         }
67         req->Destroy();
68         FFI::FFIData::Release(id);
69         return;
70     }
71 
CJ_SendRequest(int64_t id,char * url,CHttpRequestOptions * opt,bool isInStream,void (* callback)(CHttpResponse))72     RetDataCString CJ_SendRequest(int64_t id, char* url,
73         CHttpRequestOptions* opt, bool isInStream, void (*callback)(CHttpResponse))
74     {
75         NETSTACK_LOGI("request start");
76         RetDataCString ret = { .code = 0, .data = nullptr};
77         auto req = FFIData::GetData<HttpRequestProxy>(id);
78         if (req == nullptr || req->isDestroyed) {
79             // destroyed
80             SetUnknowError(ret);
81             return ret;
82         }
83         auto context = req->Request(std::string(url), opt, isInStream);
84         if (context == nullptr) {
85             // curl initialize failed
86             SetUnknowError(ret);
87             return ret;
88         }
89         if (isInStream) {
90             context->streamingCallback = req->callbacks;
91         }
92 
93         context->respCallback = CJLambda::Create(callback);
94 
95         if (context->IsPermissionDenied() || !context->IsParseOK()) {
96             ret.code = context->GetErrorCode();
97             ret.data = MallocCString(context->GetErrorMessage());
98             delete context;
99             return ret;
100         }
101 
102         return ret;
103     }
104 
CJ_OnHeadersReceive(int64_t id,bool once,void (* callback)(CArrString))105     void CJ_OnHeadersReceive(int64_t id, bool once, void (*callback)(CArrString))
106     {
107         auto req = FFIData::GetData<HttpRequestProxy>(id);
108         if (req == nullptr) {
109             NETSTACK_LOGE("Failed to get HttpRequestProxy.");
110             return;
111         }
112         if (once) {
113             req->callbacks->headersReceiveOnce.push_back(CJLambda::Create(callback));
114         } else {
115             req->callbacks->headersReceive.push_back(CJLambda::Create(callback));
116         }
117     }
118 
CJ_OffHeadersReceive(int64_t id)119     void CJ_OffHeadersReceive(int64_t id)
120     {
121         auto req = FFIData::GetData<HttpRequestProxy>(id);
122         if (req == nullptr) {
123             NETSTACK_LOGE("Failed to get HttpRequestProxy.");
124             return;
125         }
126         req->callbacks->headersReceiveOnce.clear();
127         req->callbacks->headersReceive.clear();
128     }
129 
CJ_OnDataReceive(int64_t id,void (* callback)(CArrUI8))130     void CJ_OnDataReceive(int64_t id, void (*callback)(CArrUI8))
131     {
132         auto req = FFIData::GetData<HttpRequestProxy>(id);
133         if (req == nullptr) {
134             NETSTACK_LOGE("Failed to get HttpRequestProxy.");
135             return;
136         }
137         req->callbacks->dataReceive.push_back(CJLambda::Create(callback));
138     }
139 
CJ_OffDataReceive(int64_t id)140     void CJ_OffDataReceive(int64_t id)
141     {
142         auto req = FFIData::GetData<HttpRequestProxy>(id);
143         if (req == nullptr) {
144             NETSTACK_LOGE("Failed to get HttpRequestProxy.");
145             return;
146         }
147         req->callbacks->dataReceive.clear();
148     }
149 
CJ_OnDataEnd(int64_t id,void (* callback)())150     void CJ_OnDataEnd(int64_t id, void (*callback)())
151     {
152         auto req = FFIData::GetData<HttpRequestProxy>(id);
153         if (req == nullptr) {
154             NETSTACK_LOGE("Failed to get HttpRequestProxy.");
155             return;
156         }
157         req->callbacks->dataEnd.push_back(CJLambda::Create(callback));
158     }
159 
CJ_OffDataEnd(int64_t id)160     void CJ_OffDataEnd(int64_t id)
161     {
162         auto req = FFIData::GetData<HttpRequestProxy>(id);
163         if (req == nullptr) {
164             NETSTACK_LOGE("Failed to get HttpRequestProxy.");
165             return;
166         }
167         req->callbacks->dataEnd.clear();
168     }
169 
CJ_OnDataReceiveProgress(int64_t id,void (* callback)(CDataReceiveProgressInfo))170     void CJ_OnDataReceiveProgress(int64_t id, void (*callback)(CDataReceiveProgressInfo))
171     {
172         auto req = FFIData::GetData<HttpRequestProxy>(id);
173         if (req == nullptr) {
174             NETSTACK_LOGE("Failed to get HttpRequestProxy.");
175             return;
176         }
177         req->callbacks->dataReceiveProgress.push_back(CJLambda::Create(callback));
178     }
179 
CJ_OffDataReceiveProgress(int64_t id)180     void CJ_OffDataReceiveProgress(int64_t id)
181     {
182         auto req = FFIData::GetData<HttpRequestProxy>(id);
183         if (req == nullptr) {
184             NETSTACK_LOGE("Failed to get HttpRequestProxy.");
185             return;
186         }
187         req->callbacks->dataReceiveProgress.clear();
188     }
189 
CJ_OnDataSendProgress(int64_t id,void (* callback)(CDataSendProgressInfo))190     void CJ_OnDataSendProgress(int64_t id, void (*callback)(CDataSendProgressInfo))
191     {
192         auto req = FFIData::GetData<HttpRequestProxy>(id);
193         if (req == nullptr) {
194             NETSTACK_LOGE("Failed to get HttpRequestProxy.");
195             return;
196         }
197         req->callbacks->dataSendProgress.push_back(CJLambda::Create(callback));
198     }
199 
CJ_OffDataSendProgress(int64_t id)200     void CJ_OffDataSendProgress(int64_t id)
201     {
202         auto req = FFIData::GetData<HttpRequestProxy>(id);
203         if (req == nullptr) {
204             NETSTACK_LOGE("Failed to get HttpRequestProxy.");
205             return;
206         }
207         req->callbacks->dataSendProgress.clear();
208     }
209 EXTERN_C_END
210 }
211 
212