1 /* 2 * Copyright (c) 2021-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 COMMUNICATIONNETSTACK_REQUEST_CONTEXT_H 17 #define COMMUNICATIONNETSTACK_REQUEST_CONTEXT_H 18 19 #include <queue> 20 #include <mutex> 21 #include "curl/curl.h" 22 #include "base_context.h" 23 #include "http_request_options.h" 24 #include "http_response.h" 25 26 namespace OHOS::NetStack::Http { 27 struct DlBytes { DlBytesDlBytes28 DlBytes() : nLen(0), tLen(0) {}; DlBytesDlBytes29 DlBytes(curl_off_t nowLen, curl_off_t totalLen) 30 { 31 nLen = nowLen; 32 tLen = totalLen; 33 }; 34 ~DlBytes() = default; 35 curl_off_t nLen; 36 curl_off_t tLen; 37 }; 38 39 class RequestContext final : public BaseContext { 40 public: 41 RequestContext() = delete; 42 43 RequestContext(napi_env env, EventManager *manager); 44 45 ~RequestContext() override; 46 47 void ParseParams(napi_value *params, size_t paramsCount) override; 48 49 HttpRequestOptions options; 50 51 HttpResponse response; 52 53 [[nodiscard]] bool IsUsingCache() const; 54 55 void SetCurlHeaderList(struct curl_slist *curlHeaderList); 56 57 struct curl_slist *GetCurlHeaderList(); 58 59 void SetCacheResponse(const HttpResponse &cacheResponse); 60 61 void SetResponseByCache(); 62 63 [[nodiscard]] int32_t GetErrorCode() const override; 64 65 [[nodiscard]] std::string GetErrorMessage() const override; 66 67 void EnableRequestInStream(); 68 69 [[nodiscard]] bool IsRequestInStream() const; 70 71 void SetDlLen(curl_off_t nowLen, curl_off_t totalLen); 72 73 DlBytes GetDlLen(); 74 75 void PopDlLen(); 76 77 void SetTempData(const void *data, size_t size); 78 79 std::string GetTempData(); 80 81 void PopTempData(); 82 83 private: 84 bool usingCache_; 85 bool requestInStream_; 86 std::mutex dlLenLock_; 87 std::mutex tempDataLock_; 88 std::queue<std::string> tempData_; 89 HttpResponse cacheResponse_; 90 std::queue<DlBytes> dlBytes_; 91 struct curl_slist *curlHeaderList_; 92 93 bool CheckParamsType(napi_value *params, size_t paramsCount); 94 95 void ParseNumberOptions(napi_value optionsValue); 96 97 void ParseHeader(napi_value optionsValue); 98 99 bool ParseExtraData(napi_value optionsValue); 100 101 void ParseUsingHttpProxy(napi_value optionsValue); 102 103 void ParseCaPath(napi_value optionsValue); 104 105 bool GetRequestBody(napi_value extraData); 106 107 void UrlAndOptions(napi_value urlValue, napi_value optionsValue); 108 109 bool HandleMethodForGet(napi_value extraData); 110 }; 111 } // namespace OHOS::NetStack::Http 112 113 #endif /* COMMUNICATIONNETSTACK_REQUEST_CONTEXT_H */ 114