• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include <iostream>
17 
18 #include "http_client_error.h"
19 #include "netstack_log.h"
20 
21 namespace OHOS {
22 namespace NetStack {
23 namespace HttpClient {
24 
25 static const std::map<int32_t, const std::string> HTTP_ERR_MAP = {
26     {HTTP_NONE_ERR, "No errors occurred"},
27     {HTTP_PERMISSION_DENIED_CODE, "Permission denied"},
28     {HTTP_PARSE_ERROR_CODE, "Parameter error"},
29     {HTTP_UNSUPPORTED_PROTOCOL, "Unsupported protocol"},
30     {HTTP_FAILED_INIT, "Failed to initialize"},
31     {HTTP_URL_MALFORMAT, "URL using bad/illegal format or missing URL"},
32     {HTTP_COULDNT_RESOLVE_PROXY, "Couldn't resolve proxy name"},
33     {HTTP_COULDNT_RESOLVE_HOST, "Couldn't resolve host name"},
34     {HTTP_COULDNT_CONNECT, "Couldn't connect to server"},
35     {HTTP_WEIRD_SERVER_REPLY, "Weird server reply"},
36     {HTTP_REMOTE_ACCESS_DENIED, "Access denied to remote resource"},
37     {HTTP_HTTP2_ERROR, "Error in the HTTP2 framing layer"},
38     {HTTP_PARTIAL_FILE, "Transferred a partial file"},
39     {HTTP_WRITE_ERROR, "Failed writing received data to disk/application"},
40     {HTTP_UPLOAD_FAILED, "Upload failed"},
41     {HTTP_READ_ERROR, "Failed to open/read local data from file/application"},
42     {HTTP_OUT_OF_MEMORY, "Out of memory"},
43     {HTTP_POST_ERROR, "Post error"},
44     {HTTP_OPERATION_TIMEDOUT, "Timeout was reached"},
45     {HTTP_TASK_CANCELED, "Task was canceled"},
46     {HTTP_TOO_MANY_REDIRECTS, "Number of redirects hit maximum amount"},
47     {HTTP_GOT_NOTHING, "Server returned nothing (no headers, no data)"},
48     {HTTP_SEND_ERROR, "Failed sending data to the peer"},
49     {HTTP_RECV_ERROR, "Failure when receiving data from the peer"},
50     {HTTP_SSL_CERTPROBLEM, "Problem with the local SSL certificate"},
51     {HTTP_SSL_CIPHER, "Couldn't use specified SSL cipher"},
52     {HTTP_PEER_FAILED_VERIFICATION, "SSL peer certificate or SSH remote key was not OK"},
53     {HTTP_BAD_CONTENT_ENCODING, "Unrecognized or bad HTTP Content or Transfer-Encoding"},
54     {HTTP_FILESIZE_EXCEEDED, "Maximum file size exceeded"},
55     {HTTP_REMOTE_DISK_FULL, "Disk full or allocation exceeded"},
56     {HTTP_REMOTE_FILE_EXISTS, "Remote file already exists"},
57     {HTTP_SSL_CACERT_BADFILE, "Problem with the SSL CA cert (path? access rights?)"},
58     {HTTP_REMOTE_FILE_NOT_FOUND, "Remote file not found"},
59     {HTTP_AUTH_ERROR, "An authentication function returned an error"},
60     {HTTP_UNKNOWN_OTHER_ERROR, "Unknown Other Error"},
61 };
62 
GetErrorMessage() const63 const std::string &HttpClientError::GetErrorMessage() const
64 {
65     auto err = errorCode_;
66     if (HTTP_ERR_MAP.find(err) == HTTP_ERR_MAP.end()) {
67         err = HTTP_UNKNOWN_OTHER_ERROR;
68     }
69 
70     return HTTP_ERR_MAP.find(err)->second;
71 }
72 
SetErrorCode(HttpErrorCode code)73 void HttpClientError::SetErrorCode(HttpErrorCode code)
74 {
75     errorCode_ = code;
76 }
77 
GetErrorCode() const78 HttpErrorCode HttpClientError::GetErrorCode() const
79 {
80     return errorCode_;
81 }
82 
SetCURLResult(CURLcode result)83 void HttpClientError::SetCURLResult(CURLcode result)
84 {
85     HttpErrorCode err = HTTP_UNKNOWN_OTHER_ERROR;
86     if (result > CURLE_OK) {
87         if (HTTP_ERR_MAP.find(result + HTTP_ERROR_CODE_BASE) != HTTP_ERR_MAP.end()) {
88             err = static_cast<HttpErrorCode>(result + HTTP_ERROR_CODE_BASE);
89         }
90     } else {
91         err = HTTP_NONE_ERR;
92     }
93 
94     NETSTACK_LOGD("HttpClientError::SetCURLResult: result=%d, err=%d", result, err);
95     SetErrorCode(err);
96 }
97 } // namespace HttpClient
98 } // namespace NetStack
99 } // namespace OHOS