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 #ifndef COMMUNICATIONNETSTACK_CONSTANT_H 17 #define COMMUNICATIONNETSTACK_CONSTANT_H 18 19 #include <cstddef> 20 #include <cstdint> 21 22 #include "curl/curl.h" 23 24 namespace OHOS::NetStack { 25 enum class ResponseCode { 26 OK = 200, 27 CREATED, 28 ACCEPTED, 29 NOT_AUTHORITATIVE, 30 NO_CONTENT, 31 RESET, 32 PARTIAL, 33 MULT_CHOICE = 300, 34 MOVED_PERM, 35 MOVED_TEMP, 36 SEE_OTHER, 37 NOT_MODIFIED, 38 USE_PROXY, 39 BAD_REQUEST = 400, 40 UNAUTHORIZED, 41 PAYMENT_REQUIRED, 42 FORBIDDEN, 43 NOT_FOUND, 44 BAD_METHOD, 45 NOT_ACCEPTABLE, 46 PROXY_AUTH, 47 CLIENT_TIMEOUT, 48 CONFLICT, 49 GONE, 50 LENGTH_REQUIRED, 51 PRECON_FAILED, 52 ENTITY_TOO_LARGE, 53 REQ_TOO_LONG, 54 UNSUPPORTED_TYPE, 55 INTERNAL_ERROR = 500, 56 NOT_IMPLEMENTED, 57 BAD_GATEWAY, 58 UNAVAILABLE, 59 GATEWAY_TIMEOUT, 60 VERSION, 61 }; 62 63 enum HttpErrorCode { 64 HTTP_ERROR_CODE_BASE = 2300000, 65 HTTP_UNSUPPORTED_PROTOCOL = HTTP_ERROR_CODE_BASE + CURLE_UNSUPPORTED_PROTOCOL, 66 HTTP_URL_MALFORMAT = HTTP_ERROR_CODE_BASE + CURLE_URL_MALFORMAT, 67 HTTP_COULDNT_RESOLVE_PROXY = HTTP_ERROR_CODE_BASE + CURLE_COULDNT_RESOLVE_PROXY, 68 HTTP_COULDNT_RESOLVE_HOST = HTTP_ERROR_CODE_BASE + CURLE_COULDNT_RESOLVE_HOST, 69 HTTP_COULDNT_CONNECT = HTTP_ERROR_CODE_BASE + CURLE_COULDNT_CONNECT, 70 HTTP_WEIRD_SERVER_REPLY = HTTP_ERROR_CODE_BASE + CURLE_WEIRD_SERVER_REPLY, 71 HTTP_REMOTE_ACCESS_DENIED = HTTP_ERROR_CODE_BASE + CURLE_REMOTE_ACCESS_DENIED, 72 HTTP_HTTP2_ERROR = HTTP_ERROR_CODE_BASE + CURLE_HTTP2, 73 HTTP_PARTIAL_FILE = HTTP_ERROR_CODE_BASE + CURLE_PARTIAL_FILE, 74 HTTP_WRITE_ERROR = HTTP_ERROR_CODE_BASE + CURLE_WRITE_ERROR, 75 HTTP_UPLOAD_FAILED = HTTP_ERROR_CODE_BASE + CURLE_UPLOAD_FAILED, 76 HTTP_READ_ERROR = HTTP_ERROR_CODE_BASE + CURLE_READ_ERROR, 77 HTTP_OUT_OF_MEMORY = HTTP_ERROR_CODE_BASE + CURLE_OUT_OF_MEMORY, 78 HTTP_OPERATION_TIMEDOUT = HTTP_ERROR_CODE_BASE + CURLE_OPERATION_TIMEDOUT, 79 HTTP_TOO_MANY_REDIRECTS = HTTP_ERROR_CODE_BASE + CURLE_TOO_MANY_REDIRECTS, 80 HTTP_GOT_NOTHING = HTTP_ERROR_CODE_BASE + CURLE_GOT_NOTHING, 81 HTTP_SEND_ERROR = HTTP_ERROR_CODE_BASE + CURLE_SEND_ERROR, 82 HTTP_RECV_ERROR = HTTP_ERROR_CODE_BASE + CURLE_RECV_ERROR, 83 HTTP_SSL_CERTPROBLEM = HTTP_ERROR_CODE_BASE + CURLE_SSL_CERTPROBLEM, 84 HTTP_SSL_CIPHER = HTTP_ERROR_CODE_BASE + CURLE_SSL_CIPHER, 85 HTTP_PEER_FAILED_VERIFICATION = HTTP_ERROR_CODE_BASE + CURLE_PEER_FAILED_VERIFICATION, 86 HTTP_BAD_CONTENT_ENCODING = HTTP_ERROR_CODE_BASE + CURLE_BAD_CONTENT_ENCODING, 87 HTTP_FILESIZE_EXCEEDED = HTTP_ERROR_CODE_BASE + CURLE_FILESIZE_EXCEEDED, 88 HTTP_REMOTE_DISK_FULL = HTTP_ERROR_CODE_BASE + CURLE_REMOTE_DISK_FULL, 89 HTTP_REMOTE_FILE_EXISTS = HTTP_ERROR_CODE_BASE + CURLE_REMOTE_FILE_EXISTS, 90 HTTP_SSL_CACERT_BADFILE = HTTP_ERROR_CODE_BASE + CURLE_SSL_CACERT_BADFILE, 91 HTTP_REMOTE_FILE_NOT_FOUND = HTTP_ERROR_CODE_BASE + CURLE_REMOTE_FILE_NOT_FOUND, 92 HTTP_AUTH_ERROR = HTTP_ERROR_CODE_BASE + CURLE_AUTH_ERROR, 93 HTTP_UNKNOWN_OTHER_ERROR = 2300999 94 }; 95 96 enum class HttpDataType { 97 /** 98 * The returned type is string. 99 */ 100 STRING, 101 /** 102 * The returned type is Object. 103 */ 104 OBJECT = 1, 105 /** 106 * The returned type is ArrayBuffer. 107 */ 108 ARRAY_BUFFER = 2, 109 /** 110 * The returned type is not set. 111 */ 112 NO_DATA_TYPE = 3, 113 }; 114 115 class HttpConstant final { 116 public: 117 /* Http Method */ 118 static const char *const HTTP_METHOD_GET; 119 static const char *const HTTP_METHOD_HEAD; 120 static const char *const HTTP_METHOD_OPTIONS; 121 static const char *const HTTP_METHOD_TRACE; 122 static const char *const HTTP_METHOD_DELETE; 123 static const char *const HTTP_METHOD_POST; 124 static const char *const HTTP_METHOD_PUT; 125 static const char *const HTTP_METHOD_CONNECT; 126 127 /* default options */ 128 static const uint32_t DEFAULT_READ_TIMEOUT; 129 static const uint32_t DEFAULT_CONNECT_TIMEOUT; 130 131 static const size_t MAX_JSON_PARSE_SIZE; 132 133 /* options key */ 134 static const char *const PARAM_KEY_METHOD; 135 static const char *const PARAM_KEY_EXTRA_DATA; 136 static const char *const PARAM_KEY_HEADER; 137 static const char *const PARAM_KEY_READ_TIMEOUT; 138 static const char *const PARAM_KEY_CONNECT_TIMEOUT; 139 static const char *const PARAM_KEY_USING_PROTOCOL; 140 static const char *const PARAM_KEY_USING_CACHE; 141 static const char *const PARAM_KEY_EXPECT_DATA_TYPE; 142 static const char *const PARAM_KEY_PRIORITY; 143 144 static const char *const PARAM_KEY_USING_HTTP_PROXY; 145 146 static const char *const HTTP_PROXY_KEY_HOST; 147 static const char *const HTTP_PROXY_KEY_PORT; 148 static const char *const HTTP_PROXY_KEY_EXCLUSION_LIST; 149 static const char *const HTTP_PROXY_EXCLUSIONS_SEPARATOR; 150 151 static const char *const RESPONSE_KEY_RESULT; 152 static const char *const RESPONSE_KEY_RESPONSE_CODE; 153 static const char *const RESPONSE_KEY_HEADER; 154 static const char *const RESPONSE_KEY_COOKIES; 155 static const char *const RESPONSE_KEY_RESULT_TYPE; 156 157 static const char *const HTTP_URL_PARAM_START; 158 static const char *const HTTP_URL_PARAM_SEPARATOR; 159 static const char *const HTTP_URL_NAME_VALUE_SEPARATOR; 160 static const char *const HTTP_HEADER_SEPARATOR; 161 static const char *const HTTP_LINE_SEPARATOR; 162 163 static const char *const HTTP_DEFAULT_USER_AGENT; 164 static const char *const HTTP_DEFAULT_CA_PATH; 165 166 static const char *const HTTP_CONTENT_TYPE; 167 static const char *const HTTP_CONTENT_TYPE_URL_ENCODE; 168 static const char *const HTTP_CONTENT_TYPE_JSON; 169 static const char *const HTTP_CONTENT_TYPE_OCTET_STREAM; 170 static const char *const HTTP_CONTENT_TYPE_IMAGE; 171 172 static const char *const HTTP_CONTENT_ENCODING_GZIP; 173 174 static const char *const REQUEST_TIME; 175 static const char *const RESPONSE_TIME; 176 }; 177 } // namespace OHOS::NetStack 178 179 #endif /* COMMUNICATIONNETSTACK_CONSTANT_H */ 180