1 /*
2  * Copyright (c) 2023-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 #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_SSL_PINNEDPUBKEYNOTMATCH, "Specified pinned public key did not match"},
61     {HTTP_UNKNOWN_OTHER_ERROR, "Unknown Other Error"},
62 };
63 
GetErrorMessage() const64 const std::string &HttpClientError::GetErrorMessage() const
65 {
66     auto err = errorCode_;
67     if (HTTP_ERR_MAP.find(err) == HTTP_ERR_MAP.end()) {
68         err = HTTP_UNKNOWN_OTHER_ERROR;
69     }
70 
71     return HTTP_ERR_MAP.find(err)->second;
72 }
73 
SetErrorCode(HttpErrorCode code)74 void HttpClientError::SetErrorCode(HttpErrorCode code)
75 {
76     errorCode_ = code;
77 }
78 
GetErrorCode() const79 HttpErrorCode HttpClientError::GetErrorCode() const
80 {
81     return errorCode_;
82 }
83 
SetCURLResult(CURLcode result)84 void HttpClientError::SetCURLResult(CURLcode result)
85 {
86     HttpErrorCode err = HTTP_UNKNOWN_OTHER_ERROR;
87     if (result > CURLE_OK) {
88         if (HTTP_ERR_MAP.find(result + HTTP_ERROR_CODE_BASE) != HTTP_ERR_MAP.end()) {
89             err = static_cast<HttpErrorCode>(result + HTTP_ERROR_CODE_BASE);
90         }
91     } else {
92         err = HTTP_NONE_ERR;
93     }
94 
95     NETSTACK_LOGD("HttpClientError::SetCURLResult: result=%d, err=%d", result, err);
96     SetErrorCode(err);
97 }
98 } // namespace HttpClient
99 } // namespace NetStack
100 } // namespace OHOS