1 /*
2 * Copyright (C) 2021-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 #include "http_request_options.h"
17
18 #include "constant.h"
19 #include "netstack_common_utils.h"
20
21 namespace OHOS::NetStack {
HttpRequestOptions()22 HttpRequestOptions::HttpRequestOptions()
23 : method_(HttpConstant::HTTP_METHOD_GET),
24 readTimeout_(HttpConstant::DEFAULT_READ_TIMEOUT),
25 connectTimeout_(HttpConstant::DEFAULT_CONNECT_TIMEOUT),
26 ifModifiedSince_(HttpConstant::DEFAULT_IF_MODIFIED_SINCE),
27 fixedLengthStreamingMode_(HttpConstant::DEFAULT_FIXED_LENGTH_STREAMING_MODE)
28 {
29 header_[CommonUtils::ToLower(HttpConstant::HTTP_CONTENT_TYPE)] = HttpConstant::HTTP_CONTENT_TYPE_JSON; // default
30 }
31
SetUrl(const std::string & url)32 void HttpRequestOptions::SetUrl(const std::string &url)
33 {
34 url_ = url;
35 }
36
SetMethod(const std::string & method)37 void HttpRequestOptions::SetMethod(const std::string &method)
38 {
39 method_ = method;
40 }
41
SetBody(const void * data,size_t length)42 void HttpRequestOptions::SetBody(const void *data, size_t length)
43 {
44 body_.append(static_cast<const char *>(data), length);
45 }
46
SetHeader(const std::string & key,const std::string & val)47 void HttpRequestOptions::SetHeader(const std::string &key, const std::string &val)
48 {
49 header_[key] = val;
50 }
51
SetReadTimeout(uint32_t readTimeout)52 void HttpRequestOptions::SetReadTimeout(uint32_t readTimeout)
53 {
54 readTimeout_ = readTimeout;
55 }
56
SetConnectTimeout(uint32_t connectTimeout)57 void HttpRequestOptions::SetConnectTimeout(uint32_t connectTimeout)
58 {
59 connectTimeout_ = connectTimeout;
60 }
61
SetIfModifiedSince(uint32_t ifModifiedSince)62 void HttpRequestOptions::SetIfModifiedSince(uint32_t ifModifiedSince)
63 {
64 ifModifiedSince_ = ifModifiedSince;
65 }
66
SetFixedLengthStreamingMode(int32_t fixedLengthStreamingMode)67 void HttpRequestOptions::SetFixedLengthStreamingMode(int32_t fixedLengthStreamingMode)
68 {
69 fixedLengthStreamingMode_ = fixedLengthStreamingMode;
70 }
71
GetUrl() const72 const std::string &HttpRequestOptions::GetUrl() const
73 {
74 return url_;
75 }
76
GetMethod() const77 const std::string &HttpRequestOptions::GetMethod() const
78 {
79 return method_;
80 }
81
GetBody() const82 const std::string &HttpRequestOptions::GetBody() const
83 {
84 return body_;
85 }
86
GetHeader() const87 const std::map<std::string, std::string> &HttpRequestOptions::GetHeader() const
88 {
89 return header_;
90 }
91
GetReadTimeout() const92 uint32_t HttpRequestOptions::GetReadTimeout() const
93 {
94 return readTimeout_;
95 }
96
GetConnectTimeout() const97 uint32_t HttpRequestOptions::GetConnectTimeout() const
98 {
99 return connectTimeout_;
100 }
101
GetIfModifiedSince() const102 uint32_t HttpRequestOptions::GetIfModifiedSince() const
103 {
104 return ifModifiedSince_;
105 }
106
GetFixedLengthStreamingMode() const107 int32_t HttpRequestOptions::GetFixedLengthStreamingMode() const
108 {
109 return fixedLengthStreamingMode_;
110 }
111 } // namespace OHOS::NetStack