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
16#ifndef HTTP_CONSTANT_H
17#define HTTP_CONSTANT_H
18
19#include <cstddef>
20#include <cstdint>
21
22#include "curl/curl.h"
23
24namespace OHOS::NetStack::Http {
25constexpr const uint32_t MAX_LIMIT = 100 * 1024 * 1024;
26constexpr const uint32_t DEFAULT_MAX_LIMIT = 5 * 1024 * 1024;
27constexpr const uint32_t DEFAULT_READ_TIMEOUT = 60000;
28constexpr const uint32_t DEFAULT_CONNECT_TIMEOUT = 60000;
29constexpr const size_t MAX_JSON_PARSE_SIZE = 65536;
30constexpr const uint32_t MIN_PRIORITY = 1;
31constexpr const uint32_t MAX_PRIORITY = 1000;
32constexpr const int64_t MIN_RESUM_NUMBER = 1;
33constexpr const int64_t MAX_RESUM_NUMBER = 4294967296;
34constexpr const size_t MAP_TUPLE_SIZE = 2;
35
36constexpr const char *HTTP_METHOD_GET = "GET";
37constexpr const char *HTTP_METHOD_HEAD = "HEAD";
38constexpr const char *HTTP_METHOD_OPTIONS = "OPTIONS";
39constexpr const char *HTTP_METHOD_TRACE = "TRACE";
40constexpr const char *HTTP_METHOD_DELETE = "DELETE";
41constexpr const char *HTTP_METHOD_POST = "POST";
42constexpr const char *HTTP_METHOD_PUT = "PUT";
43constexpr const char *HTTP_METHOD_CONNECT = "CONNECT";
44constexpr const char *HTTP_LINE_SEPARATOR = "\r\n";
45constexpr const char *HTTP_HEADER_SEPARATOR = ":";
46constexpr const char *RESPONSE_KEY_RESULT = "result";
47constexpr const char *RESPONSE_KEY_RESPONSE_CODE = "responseCode";
48constexpr const char *RESPONSE_KEY_HEADER = "header";
49constexpr const char *RESPONSE_KEY_COOKIES = "cookies";
50constexpr const char *RESPONSE_KEY_RESULT_TYPE = "resultType";
51constexpr const char *REQUEST_TIME = "requestTime";
52constexpr const char *RESPONSE_TIME = "responseTime";
53constexpr const char *HTTP_HEADER_BLANK_SEPARATOR = ";";
54constexpr const char *HTTP_RESPONSE_HEADER_SEPARATOR = "\r\n\r\n";
55constexpr const char *HTTP_URL_PARAM_START = "?";
56constexpr const char *HTTP_URL_PARAM_SEPARATOR = "&";
57constexpr const char *HTTP_URL_NAME_VALUE_SEPARATOR = "=";
58constexpr const char *HTTP_PROXY_EXCLUSIONS_SEPARATOR = ",";
59// cache constant
60constexpr const int DECIMAL = 10;
61constexpr const char *SPLIT = ", ";
62constexpr const char EQUAL = '=';
63
64constexpr const char *HTTP_DEFAULT_USER_AGENT = "libcurl-agent/1.0";
65constexpr const char *RESPONSE_KEY_SET_COOKIE = "set-cookie";
66constexpr const char *NO_CACHE = "no-cache";
67constexpr const char *NO_STORE = "no-store";
68constexpr const char *NO_TRANSFORM = "no-transform";
69constexpr const char *ONLY_IF_CACHED = "only-if-cached";
70constexpr const char *MAX_AGE = "max-age";
71constexpr const char *MAX_STALE = "max-stale";
72constexpr const char *MIN_FRESH = "min-fresh";
73constexpr const char *CACHE_CONTROL = "cache-control";
74constexpr const char *IF_MODIFIED_SINCE = "if-modified-since";
75constexpr const char *IF_NONE_MATCH = "if-none-match";
76constexpr const char *MUST_REVALIDATE = "must-revalidate";
77constexpr const char *PUBLIC = "public";
78constexpr const char *PRIVATE = "private";
79constexpr const char *PROXY_REVALIDATE = "proxy-revalidate";
80constexpr const char *S_MAXAGE = "s-maxage";
81constexpr const char *EXPIRES = "expires";
82constexpr const char *LAST_MODIFIED = "last-modified";
83constexpr const char *ETAG = "etag";
84constexpr const char *AGE = "age";
85constexpr const char *DATE = "date";
86constexpr const int INVALID_TIME = -1;
87constexpr const char *HTTP_PREPARE_CA_PATH = "/etc/security/certificates";
88
89constexpr const char *HTTP_CONTENT_TYPE = "content-type";
90constexpr const char *HTTP_CONTENT_TYPE_URL_ENCODE = "application/x-www-form-urlencoded";
91constexpr const char *HTTP_CONTENT_TYPE_JSON = "application/json";
92constexpr const char *HTTP_CONTENT_TYPE_OCTET_STREAM = "application/octet-stream";
93constexpr const char *HTTP_CONTENT_TYPE_IMAGE = "image";
94constexpr const char *HTTP_CONTENT_TYPE_MULTIPART = "multipart/form-data";
95
96// events
97constexpr const char *ON_HEADER_RECEIVE = "headerReceive";
98constexpr const char *ON_DATA_RECEIVE = "dataReceive";
99constexpr const char *ON_DATA_END = "dataEnd";
100constexpr const char *ON_DATA_RECEIVE_PROGRESS = "dataReceiveProgress";
101constexpr const char *ON_HEADERS_RECEIVE = "headersReceive";
102
103
104constexpr const char *RESPONSE_PERFORMANCE_TIMING = "performanceTiming";
105constexpr const char *RESPONSE_DNS_TIMING = "dnsTiming";
106constexpr const char *RESPONSE_TCP_TIMING = "tcpTiming";
107constexpr const char *RESPONSE_TLS_TIMING = "tlsTiming";
108constexpr const char *RESPONSE_FIRST_SEND_TIMING = "firstSendTiming";
109constexpr const char *RESPONSE_FIRST_RECEIVE_TIMING = "firstReceiveTiming";
110constexpr const char *RESPONSE_TOTAL_FINISH_TIMING = "totalFinishTiming";
111constexpr const char *RESPONSE_REDIRECT_TIMING = "redirectTiming";
112constexpr const char *RESPONSE_HEADER_TIMING = "responseHeaderTiming";
113constexpr const char *RESPONSE_BODY_TIMING = "responseBodyTiming";
114constexpr const char *RESPONSE_TOTAL_TIMING = "totalTiming";
115
116enum class ResponseCode {
117    OK = 200,
118    CREATED,
119    ACCEPTED,
120    NOT_AUTHORITATIVE,
121    NO_CONTENT,
122    RESET,
123    PARTIAL,
124    MULT_CHOICE = 300,
125    MOVED_PERM,
126    MOVED_TEMP,
127    SEE_OTHER,
128    NOT_MODIFIED,
129    USE_PROXY,
130    BAD_REQUEST = 400,
131    UNAUTHORIZED,
132    PAYMENT_REQUIRED,
133    FORBIDDEN,
134    NOT_FOUND,
135    BAD_METHOD,
136    NOT_ACCEPTABLE,
137    PROXY_AUTH,
138    CLIENT_TIMEOUT,
139    CONFLICT,
140    GONE,
141    LENGTH_REQUIRED,
142    PRECON_FAILED,
143    ENTITY_TOO_LARGE,
144    REQ_TOO_LONG,
145    UNSUPPORTED_TYPE,
146    INTERNAL_ERROR = 500,
147    NOT_IMPLEMENTED,
148    BAD_GATEWAY,
149    UNAVAILABLE,
150    GATEWAY_TIMEOUT,
151    VERSION,
152};
153
154enum class HttpErrorCode {
155    HTTP_ERROR_CODE_BASE = 2300000,
156    HTTP_UNSUPPORTED_PROTOCOL = HTTP_ERROR_CODE_BASE + CURLE_UNSUPPORTED_PROTOCOL,
157    HTTP_URL_MALFORMAT = HTTP_ERROR_CODE_BASE + CURLE_URL_MALFORMAT,
158    HTTP_COULDNT_RESOLVE_PROXY = HTTP_ERROR_CODE_BASE + CURLE_COULDNT_RESOLVE_PROXY,
159    HTTP_COULDNT_RESOLVE_HOST = HTTP_ERROR_CODE_BASE + CURLE_COULDNT_RESOLVE_HOST,
160    HTTP_COULDNT_CONNECT = HTTP_ERROR_CODE_BASE + CURLE_COULDNT_CONNECT,
161    HTTP_WEIRD_SERVER_REPLY = HTTP_ERROR_CODE_BASE + CURLE_WEIRD_SERVER_REPLY,
162    HTTP_REMOTE_ACCESS_DENIED = HTTP_ERROR_CODE_BASE + CURLE_REMOTE_ACCESS_DENIED,
163    HTTP_HTTP2_ERROR = HTTP_ERROR_CODE_BASE + CURLE_HTTP2,
164    HTTP_PARTIAL_FILE = HTTP_ERROR_CODE_BASE + CURLE_PARTIAL_FILE,
165    HTTP_WRITE_ERROR = HTTP_ERROR_CODE_BASE + CURLE_WRITE_ERROR,
166    HTTP_UPLOAD_FAILED = HTTP_ERROR_CODE_BASE + CURLE_UPLOAD_FAILED,
167    HTTP_READ_ERROR = HTTP_ERROR_CODE_BASE + CURLE_READ_ERROR,
168    HTTP_OUT_OF_MEMORY = HTTP_ERROR_CODE_BASE + CURLE_OUT_OF_MEMORY,
169    HTTP_OPERATION_TIMEDOUT = HTTP_ERROR_CODE_BASE + CURLE_OPERATION_TIMEDOUT,
170    HTTP_TOO_MANY_REDIRECTS = HTTP_ERROR_CODE_BASE + CURLE_TOO_MANY_REDIRECTS,
171    HTTP_GOT_NOTHING = HTTP_ERROR_CODE_BASE + CURLE_GOT_NOTHING,
172    HTTP_SEND_ERROR = HTTP_ERROR_CODE_BASE + CURLE_SEND_ERROR,
173    HTTP_RECV_ERROR = HTTP_ERROR_CODE_BASE + CURLE_RECV_ERROR,
174    HTTP_SSL_CERTPROBLEM = HTTP_ERROR_CODE_BASE + CURLE_SSL_CERTPROBLEM,
175    HTTP_SSL_CIPHER = HTTP_ERROR_CODE_BASE + CURLE_SSL_CIPHER,
176    HTTP_PEER_FAILED_VERIFICATION = HTTP_ERROR_CODE_BASE + CURLE_PEER_FAILED_VERIFICATION,
177    HTTP_BAD_CONTENT_ENCODING = HTTP_ERROR_CODE_BASE + CURLE_BAD_CONTENT_ENCODING,
178    HTTP_FILESIZE_EXCEEDED = HTTP_ERROR_CODE_BASE + CURLE_FILESIZE_EXCEEDED,
179    HTTP_REMOTE_DISK_FULL = HTTP_ERROR_CODE_BASE + CURLE_REMOTE_DISK_FULL,
180    HTTP_REMOTE_FILE_EXISTS = HTTP_ERROR_CODE_BASE + CURLE_REMOTE_FILE_EXISTS,
181    HTTP_SSL_CACERT_BADFILE = HTTP_ERROR_CODE_BASE + CURLE_SSL_CACERT_BADFILE,
182    HTTP_REMOTE_FILE_NOT_FOUND = HTTP_ERROR_CODE_BASE + CURLE_REMOTE_FILE_NOT_FOUND,
183    HTTP_AUTH_ERROR = HTTP_ERROR_CODE_BASE + CURLE_AUTH_ERROR,
184    HTTP_UNKNOWN_OTHER_ERROR = 2300999
185};
186
187enum class HttpDataType {
188    /**
189     * The returned type is string.
190     */
191    STRING,
192    /**
193     * The returned type is Object.
194     */
195    OBJECT = 1,
196    /**
197     * The returned type is ArrayBuffer.
198     */
199    ARRAY_BUFFER = 2,
200    /**
201     * The returned type is not set.
202     */
203    NO_DATA_TYPE = 3,
204};
205} // namespace OHOS::NetStack::Http
206
207#endif /* HTTP_CONSTANT_H */
208