MCPcopy Create free account
hub / github.com/SFML/SFML / sendRequest

Method sendRequest

src/SFML/Network/Http.cpp:337–443  ·  view source on GitHub ↗

///////////////////////////////////////////////////////

Source from the content-addressed store, hash-verified

335
336////////////////////////////////////////////////////////////
337Http::Response Http::sendRequest(const Http::Request& request, Time timeout, bool verifyServer) const
338{
339 // First make sure that the request is valid -- add missing mandatory fields
340 Request toSend(request);
341 if (!toSend.hasField("From"))
342 {
343 toSend.setField("From", "user@sfml-dev.org");
344 }
345 if (!toSend.hasField("User-Agent"))
346 {
347 toSend.setField("User-Agent", "libsfml-network/3.x");
348 }
349 if (!toSend.hasField("Host"))
350 {
351 toSend.setField("Host", m_hostName);
352 }
353 if (!toSend.hasField("Content-Length"))
354 {
355 std::ostringstream out;
356 out << toSend.m_body.size();
357 toSend.setField("Content-Length", out.str());
358 }
359 if ((toSend.m_method == Request::Method::Post) && !toSend.hasField("Content-Type"))
360 {
361 toSend.setField("Content-Type", "application/x-www-form-urlencoded");
362 }
363 if ((toSend.m_majorVersion * 10 + toSend.m_minorVersion >= 11) && !toSend.hasField("Connection"))
364 {
365 toSend.setField("Connection", "close");
366 }
367
368 auto hosts = m_hosts;
369
370 if (m_addressType == IpAddress::Type::IpV4)
371 hosts.erase(std::remove_if(hosts.begin(), hosts.end(), [](const auto& host) { return host.isV6(); }), hosts.end());
372
373 if (m_addressType == IpAddress::Type::IpV6)
374 hosts.erase(std::remove_if(hosts.begin(), hosts.end(), [](const auto& host) { return host.isV4(); }), hosts.end());
375
376 // Prepare the response
377 Response received;
378
379 for (const auto& host : m_hosts)
380 {
381 if ((m_addressType.has_value()) && (host.getType() != m_addressType))
382 continue;
383
384 TcpSocket connection;
385
386 // Connect the socket to the host
387 if (connection.connect(host, m_port, timeout) == Socket::Status::Done)
388 {
389 if (m_https &&
390 (connection.setupTlsClient(m_hostName, verifyServer) != sf::TcpSocket::TlsStatus::HandshakeComplete))
391 continue;
392
393 // Convert the request to string and send it through the connected socket
394 const std::string requestStr = toSend.prepare();

Callers 3

Http.test.cppFile · 0.80
getPublicAddressMethod · 0.80
requestUrlFunction · 0.80

Calls 15

hasFieldMethod · 0.80
setFieldMethod · 0.80
eraseMethod · 0.80
isV6Method · 0.80
isV4Method · 0.80
getTypeMethod · 0.80
setupTlsClientMethod · 0.80
prepareMethod · 0.80
parseMethod · 0.80
beginMethod · 0.45
endMethod · 0.45
connectMethod · 0.45

Tested by

no test coverage detected