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 
24 namespace OHOS::NetStack::Http {
25 constexpr const uint32_t MAX_LIMIT = 100 * 1024 * 1024;
26 constexpr const uint32_t DEFAULT_MAX_LIMIT = 5 * 1024 * 1024;
27 constexpr const uint32_t DEFAULT_READ_TIMEOUT = 60000;
28 constexpr const uint32_t DEFAULT_CONNECT_TIMEOUT = 60000;
29 constexpr const size_t MAX_JSON_PARSE_SIZE = 65536;
30 constexpr const uint32_t MIN_PRIORITY = 1;
31 constexpr const uint32_t MAX_PRIORITY = 1000;
32 constexpr const int64_t MIN_RESUM_NUMBER = 1;
33 constexpr const int64_t MAX_RESUM_NUMBER = 4294967296;
34 constexpr const size_t MAP_TUPLE_SIZE = 2;
35 
36 constexpr const char *HTTP_METHOD_GET = "GET";
37 constexpr const char *HTTP_METHOD_HEAD = "HEAD";
38 constexpr const char *HTTP_METHOD_OPTIONS = "OPTIONS";
39 constexpr const char *HTTP_METHOD_TRACE = "TRACE";
40 constexpr const char *HTTP_METHOD_DELETE = "DELETE";
41 constexpr const char *HTTP_METHOD_POST = "POST";
42 constexpr const char *HTTP_METHOD_PUT = "PUT";
43 constexpr const char *HTTP_METHOD_CONNECT = "CONNECT";
44 constexpr const char *HTTP_LINE_SEPARATOR = "\r\n";
45 constexpr const char *HTTP_HEADER_SEPARATOR = ":";
46 constexpr const char *RESPONSE_KEY_RESULT = "result";
47 constexpr const char *RESPONSE_KEY_RESPONSE_CODE = "responseCode";
48 constexpr const char *RESPONSE_KEY_HEADER = "header";
49 constexpr const char *RESPONSE_KEY_COOKIES = "cookies";
50 constexpr const char *RESPONSE_KEY_RESULT_TYPE = "resultType";
51 constexpr const char *REQUEST_TIME = "requestTime";
52 constexpr const char *RESPONSE_TIME = "responseTime";
53 constexpr const char *HTTP_HEADER_BLANK_SEPARATOR = ";";
54 constexpr const char *HTTP_RESPONSE_HEADER_SEPARATOR = "\r\n\r\n";
55 constexpr const char *HTTP_URL_PARAM_START = "?";
56 constexpr const char *HTTP_URL_PARAM_SEPARATOR = "&";
57 constexpr const char *HTTP_URL_NAME_VALUE_SEPARATOR = "=";
58 constexpr const char *HTTP_PROXY_EXCLUSIONS_SEPARATOR = ",";
59 // cache constant
60 constexpr const int DECIMAL = 10;
61 constexpr const char *SPLIT = ", ";
62 constexpr const char EQUAL = '=';
63 
64 constexpr const char *HTTP_DEFAULT_USER_AGENT = "libcurl-agent/1.0";
65 constexpr const char *RESPONSE_KEY_SET_COOKIE = "set-cookie";
66 constexpr const char *NO_CACHE = "no-cache";
67 constexpr const char *NO_STORE = "no-store";
68 constexpr const char *NO_TRANSFORM = "no-transform";
69 constexpr const char *ONLY_IF_CACHED = "only-if-cached";
70 constexpr const char *MAX_AGE = "max-age";
71 constexpr const char *MAX_STALE = "max-stale";
72 constexpr const char *MIN_FRESH = "min-fresh";
73 constexpr const char *CACHE_CONTROL = "cache-control";
74 constexpr const char *IF_MODIFIED_SINCE = "if-modified-since";
75 constexpr const char *IF_NONE_MATCH = "if-none-match";
76 constexpr const char *MUST_REVALIDATE = "must-revalidate";
77 constexpr const char *PUBLIC = "public";
78 constexpr const char *PRIVATE = "private";
79 constexpr const char *PROXY_REVALIDATE = "proxy-revalidate";
80 constexpr const char *S_MAXAGE = "s-maxage";
81 constexpr const char *EXPIRES = "expires";
82 constexpr const char *LAST_MODIFIED = "last-modified";
83 constexpr const char *ETAG = "etag";
84 constexpr const char *AGE = "age";
85 constexpr const char *DATE = "date";
86 constexpr const int INVALID_TIME = -1;
87 constexpr const char *HTTP_PREPARE_CA_PATH = "/etc/security/certificates";
88 
89 constexpr const char *HTTP_CONTENT_TYPE = "content-type";
90 constexpr const char *HTTP_CONTENT_TYPE_URL_ENCODE = "application/x-www-form-urlencoded";
91 constexpr const char *HTTP_CONTENT_TYPE_JSON = "application/json";
92 constexpr const char *HTTP_CONTENT_TYPE_OCTET_STREAM = "application/octet-stream";
93 constexpr const char *HTTP_CONTENT_TYPE_IMAGE = "image";
94 constexpr const char *HTTP_CONTENT_TYPE_MULTIPART = "multipart/form-data";
95 
96 // events
97 constexpr const char *ON_HEADER_RECEIVE = "headerReceive";
98 constexpr const char *ON_DATA_RECEIVE = "dataReceive";
99 constexpr const char *ON_DATA_END = "dataEnd";
100 constexpr const char *ON_DATA_RECEIVE_PROGRESS = "dataReceiveProgress";
101 constexpr const char *ON_HEADERS_RECEIVE = "headersReceive";
102 
103 
104 constexpr const char *RESPONSE_PERFORMANCE_TIMING = "performanceTiming";
105 constexpr const char *RESPONSE_DNS_TIMING = "dnsTiming";
106 constexpr const char *RESPONSE_TCP_TIMING = "tcpTiming";
107 constexpr const char *RESPONSE_TLS_TIMING = "tlsTiming";
108 constexpr const char *RESPONSE_FIRST_SEND_TIMING = "firstSendTiming";
109 constexpr const char *RESPONSE_FIRST_RECEIVE_TIMING = "firstReceiveTiming";
110 constexpr const char *RESPONSE_TOTAL_FINISH_TIMING = "totalFinishTiming";
111 constexpr const char *RESPONSE_REDIRECT_TIMING = "redirectTiming";
112 constexpr const char *RESPONSE_HEADER_TIMING = "responseHeaderTiming";
113 constexpr const char *RESPONSE_BODY_TIMING = "responseBodyTiming";
114 constexpr const char *RESPONSE_TOTAL_TIMING = "totalTiming";
115 
116 enum 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 
154 enum 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 
187 enum 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