| 195 | } |
| 196 | |
| 197 | static cc_result HttpConnection_Open(struct HttpConnection* conn, const struct HttpUrl* url) { |
| 198 | cc_string host, port; |
| 199 | cc_uint16 portNum; |
| 200 | cc_result res; |
| 201 | cc_sockaddr addrs[SOCKET_MAX_ADDRS]; |
| 202 | char addrBuf[32]; |
| 203 | cc_string addrStr; |
| 204 | int i, numValidAddrs; |
| 205 | |
| 206 | ExtractHostPort(url, &host, &port); |
| 207 | if (!Convert_ParseUInt16(&port, &portNum)) { |
| 208 | portNum = url->https ? 443 : 80; |
| 209 | } |
| 210 | |
| 211 | conn->socket = -1; |
| 212 | conn->sslCtx = NULL; |
| 213 | |
| 214 | if ((res = Socket_ParseAddress(&host, portNum, addrs, &numValidAddrs))) return res; |
| 215 | res = ERR_INVALID_ARGUMENT; /* in case 0 valid addresses */ |
| 216 | |
| 217 | /* TODO: Connect in parallel instead of serial, but that's a lot of work */ |
| 218 | for (i = 0; i < numValidAddrs; i++) |
| 219 | { |
| 220 | res = Socket_Create(&conn->socket, &addrs[i], false); |
| 221 | if (res) { HttpConnection_Close(conn); continue; } |
| 222 | |
| 223 | String_InitArray(addrStr, addrBuf); |
| 224 | if (SockAddr_ToString(&addrs[i], &addrStr)) Platform_Log1(" Connecting to %s..", &addrStr); |
| 225 | |
| 226 | res = Socket_Connect(conn->socket, &addrs[i]); |
| 227 | if (res) { HttpConnection_Close(conn); continue; } |
| 228 | |
| 229 | break; /* Successful connection */ |
| 230 | } |
| 231 | if (res) return res; |
| 232 | |
| 233 | conn->valid = true; |
| 234 | if (!url->https) return 0; |
| 235 | return SSL_Init(conn->socket, &host, &conn->sslCtx); |
| 236 | } |
| 237 | |
| 238 | static cc_result WriteAllToSocket(cc_socket socket, const cc_uint8* data, cc_uint32 count) { |
| 239 | cc_uint32 sent; |
no test coverage detected