MCPcopy Create free account
hub / github.com/ClassiCube/ClassiCube / HttpConnection_Open

Function HttpConnection_Open

src/Http_Worker.c:197–236  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

195}
196
197static 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
238static cc_result WriteAllToSocket(cc_socket socket, const cc_uint8* data, cc_uint32 count) {
239 cc_uint32 sent;

Callers 1

ConnectionPool_InsertFunction · 0.85

Calls 9

ExtractHostPortFunction · 0.85
Convert_ParseUInt16Function · 0.85
Socket_ParseAddressFunction · 0.85
HttpConnection_CloseFunction · 0.85
Platform_Log1Function · 0.85
SSL_InitFunction · 0.85
Socket_CreateFunction · 0.70
SockAddr_ToStringFunction · 0.70
Socket_ConnectFunction · 0.70

Tested by

no test coverage detected