1 /* 2 * Copyright (C) 2021 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 OHOS_HTTPREQ_H 17 #define OHOS_HTTPREQ_H 18 19 #include <fcntl.h> 20 #include <unistd.h> 21 #include <netdb.h> 22 #include <arpa/inet.h> 23 #include <netinet/in.h> 24 #include <string> 25 #include <cerrno> 26 #include <cstdio> 27 #include <cstdlib> 28 #include <cstdarg> 29 #include <sys/socket.h> 30 #include <sys/types.h> 31 #include <condition_variable> 32 33 #define BUFSIZE 1024 34 #define URLSIZE 1024 35 #define INVALID_SOCKET (-1) 36 37 const int HTTP_HEADER_LENGTH = 7; 38 const int HTTPS_HEADER_LENGTH = 8; 39 const int DEFAULT_PORT = 80; 40 const int SEND_HTTP_DELAY_TIME = 1; 41 42 namespace OHOS { 43 namespace Wifi { 44 class HttpRequest { 45 public: 46 /** 47 * @Description : Constructs a new Http Request object. 48 * 49 */ 50 HttpRequest(); 51 52 /** 53 * @Description : Destroy the Http Request object. 54 * 55 */ 56 ~HttpRequest(); 57 58 /** 59 * @Description : HttpGet request. 60 * 61 * @param strUrl - HTTP request URL.[in] 62 * @param strResponse - HTTP request response.[in] 63 * @return int 64 */ 65 int HttpGet(const std::string &strUrl, std::string &strResponse); 66 67 /** 68 * @Description : HttpPost request 69 * 70 * @param strUrl - HTTP request URL.[in] 71 * @param strData - Data sent in a POST request.[in] 72 * @param strResponse - HTTP request response.[in] 73 * @return int 74 */ 75 int HttpPost(const std::string &strUrl, const std::string &strData, std::string &strResponse); 76 77 private: 78 /** 79 * @Description : Executes HTTP requests, GET or POST. 80 * 81 * @param strMethod - The Http request type.[in] 82 * @param strUrl - HTTP request URL.[in] 83 * @param strData - Data sent in a POST request.[in] 84 * @param strResponse - HTTP request response.[in] 85 * @return int 86 */ 87 int HttpRequestExec( 88 const std::string &strMethod, const std::string &strUrl, const std::string &strData, std::string &strResponse); 89 90 /** 91 * @Description : Constructing an HTTP Message Header 92 * 93 * @param strMethod - The Http request type.[in] 94 * @param strData - Data sent in a POST request.[in] 95 */ 96 void HttpHeadCreate(const std::string &strMethod, const std::string &strData); 97 98 /** 99 * @Description : Send an HTTP request and receive a response. 100 * 101 * @param iSockFd - a sign.[in] 102 * @return int 103 */ 104 int HttpDataTransmit(const int &iSockFd); 105 106 /** 107 * @Description : Http connection processing function. 108 * 109 * @param strResponse - HTTP request response.[in] 110 * @return int 111 */ 112 int HttpConnect(std::string &strResponse); 113 114 /** 115 * @Description : Obtain the port number from the HTTP request URL object 116 * 117 * @param strUrl - HTTP request URL.[in] 118 * @return none 119 */ 120 void GetPortFromUrl(); 121 122 /** 123 * @Description : Obtain the IP address from the HTTP request URL. 124 * 125 * @param strUrl - HTTP request URL.[in] 126 * @return int 127 */ 128 int GetIPFromUrl(); 129 130 /** 131 * @Description : Obtain the host address, website address, or IP address in 132 * dotted decimal notation from the HTTP request URL. 133 * 134 * @param strUrl - HTTP request URL.[in] 135 * @return int 136 */ 137 int GetHostAddrFromUrl(const std::string &strUrl); 138 139 /** 140 * @Description : Check whether SocketFd is writable and unreadable. 141 * 142 * @param iSockFd - a sign.[in] 143 * @return int 144 */ 145 int SocketFdCheck(const int &iSockFd) const; 146 147 private: 148 int mISocketFd; 149 int iPort; 150 std::string strHost; 151 std::string strIp; 152 std::string strRes; 153 std::string strParam; 154 std::string httpHead; 155 }; 156 struct HostData { 157 bool isIp = false; 158 std::string strIp; 159 std::string strIpOrDomain; 160 std::condition_variable waitTimeout; 161 }; 162 } 163 } /* namespace OHOS */ 164 #endif