diff --git a/Externals/SFML/include/SFML/Config.hpp b/Externals/SFML/include/SFML/Config.hpp index 91d052f8dc..84ec60f9fe 100644 --- a/Externals/SFML/include/SFML/Config.hpp +++ b/Externals/SFML/include/SFML/Config.hpp @@ -49,7 +49,7 @@ // MacOS #define SFML_SYSTEM_MACOS -#elif defined(__FreeBSD__) +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) // FreeBSD #define SFML_SYSTEM_FREEBSD diff --git a/Externals/SFML/include/SFML/Network/SocketTCP.hpp b/Externals/SFML/include/SFML/Network/SocketTCP.hpp index 964686a590..8929dc9597 100644 --- a/Externals/SFML/include/SFML/Network/SocketTCP.hpp +++ b/Externals/SFML/include/SFML/Network/SocketTCP.hpp @@ -214,8 +214,10 @@ private : // Member data //////////////////////////////////////////////////////////// SocketHelper::SocketType mySocket; ///< Socket descriptor - std::vector myPendingPacket; ///< Data of the current pending packet, if any (in non-blocking mode) - Int32 myPendingPacketSize; ///< Size of the current pending packet, if any (in non-blocking mode) + Uint32 myPendingHeader; ///< Data of the current pending packet header, if any + Uint32 myPendingHeaderSize; ///< Size of the current pending packet header, if any + std::vector myPendingPacket; ///< Data of the current pending packet, if any + Int32 myPendingPacketSize; ///< Size of the current pending packet, if any bool myIsBlocking; ///< Is the socket blocking or non-blocking ? }; diff --git a/Externals/SFML/include/SFML/Network/SocketUDP.hpp b/Externals/SFML/include/SFML/Network/SocketUDP.hpp index 37df3f7dd6..7cb6b19d30 100644 --- a/Externals/SFML/include/SFML/Network/SocketUDP.hpp +++ b/Externals/SFML/include/SFML/Network/SocketUDP.hpp @@ -215,8 +215,10 @@ private : //////////////////////////////////////////////////////////// SocketHelper::SocketType mySocket; ///< Socket identifier unsigned short myPort; ///< Port to which the socket is bound - std::vector myPendingPacket; ///< Data of the current pending packet, if any (in non-blocking mode) - Int32 myPendingPacketSize; ///< Size of the current pending packet, if any (in non-blocking mode) + Uint32 myPendingHeader; ///< Data of the current pending packet header, if any + Uint32 myPendingHeaderSize; ///< Size of the current pending packet header, if any + std::vector myPendingPacket; ///< Data of the current pending packet, if any + Int32 myPendingPacketSize; ///< Size of the current pending packet, if any bool myIsBlocking; ///< Is the socket blocking or non-blocking ? }; diff --git a/Externals/SFML/src/SFML/Network/Ftp.cpp b/Externals/SFML/src/SFML/Network/Ftp.cpp index 9dff2b7f4a..8e722c6ad7 100644 --- a/Externals/SFML/src/SFML/Network/Ftp.cpp +++ b/Externals/SFML/src/SFML/Network/Ftp.cpp @@ -380,7 +380,8 @@ Ftp::Response Ftp::Download(const std::string& DistantFile, const std::string& D std::ofstream File((Path + Filename).c_str(), std::ios_base::binary); if (!File) return Response(Response::InvalidFile); - File.write(&FileData[0], static_cast(FileData.size())); + if (!FileData.empty()) + File.write(&FileData[0], static_cast(FileData.size())); } } } @@ -402,7 +403,8 @@ Ftp::Response Ftp::Upload(const std::string& LocalFile, const std::string& DestP std::size_t Length = File.tellg(); File.seekg(0, std::ios::beg); std::vector FileData(Length); - File.read(&FileData[0], static_cast(Length)); + if (Length > 0) + File.read(&FileData[0], static_cast(Length)); // Extract the filename from the file path std::string Filename = LocalFile; @@ -700,7 +702,8 @@ void Ftp::DataChannel::Receive(std::vector& Data) void Ftp::DataChannel::Send(const std::vector& Data) { // Send data - myDataSocket.Send(&Data[0], Data.size()); + if (!Data.empty()) + myDataSocket.Send(&Data[0], Data.size()); // Close the data socket myDataSocket.Close(); diff --git a/Externals/SFML/src/SFML/Network/Http.cpp b/Externals/SFML/src/SFML/Network/Http.cpp index 2f4c140e8d..201634058f 100644 --- a/Externals/SFML/src/SFML/Network/Http.cpp +++ b/Externals/SFML/src/SFML/Network/Http.cpp @@ -27,6 +27,8 @@ //////////////////////////////////////////////////////////// #include #include +#include +#include #include @@ -51,14 +53,12 @@ namespace sf //////////////////////////////////////////////////////////// /// Default constructor //////////////////////////////////////////////////////////// -Http::Request::Request(Method RequestMethod, const std::string& URI, const std::string& Body) : -myMethod (RequestMethod), -myURI (URI), -myMajorVersion(1), -myMinorVersion(0), -myBody (Body) +Http::Request::Request(Method RequestMethod, const std::string& URI, const std::string& Body) { - + SetMethod(RequestMethod); + SetURI(URI); + SetHttpVersion(1, 0); + SetBody(Body); } @@ -180,7 +180,7 @@ myMinorVersion(0) //////////////////////////////////////////////////////////// const std::string& Http::Response::GetField(const std::string& Field) const { - FieldTable::const_iterator It = myFields.find(Field); + FieldTable::const_iterator It = myFields.find(ToLower(Field)); if (It != myFields.end()) { return It->second; @@ -297,8 +297,7 @@ void Http::Response::FromString(const std::string& Data) // Finally extract the body myBody.clear(); - while (std::getline(In, Line)) - myBody += Line + "\n"; + std::copy(std::istreambuf_iterator(In), std::istreambuf_iterator(), std::back_inserter(myBody)); } @@ -386,6 +385,14 @@ Http::Response Http::SendRequest(const Http::Request& Req, float Timeout) Out << ToSend.myBody.size(); ToSend.SetField("Content-Length", Out.str()); } + if ((ToSend.myMethod == Request::Post) && !ToSend.HasField("Content-Type")) + { + ToSend.SetField("Content-Type", "application/x-www-form-urlencoded"); + } + if ((ToSend.myMajorVersion * 10 + ToSend.myMinorVersion >= 11) && !ToSend.HasField("Connection")) + { + ToSend.SetField("Connection", "close"); + } // Prepare the response Response Received; diff --git a/Externals/SFML/src/SFML/Network/SocketTCP.cpp b/Externals/SFML/src/SFML/Network/SocketTCP.cpp index e4a4aa7f20..d4318555ac 100644 --- a/Externals/SFML/src/SFML/Network/SocketTCP.cpp +++ b/Externals/SFML/src/SFML/Network/SocketTCP.cpp @@ -135,18 +135,18 @@ Socket::Status SocketTCP::Connect(unsigned short Port, const IPAddress& HostAddr if (select(static_cast(mySocket + 1), NULL, &Selector, NULL, &Time) > 0) { // At this point the connection may have been either accepted or refused. - // To know whether it's a success or a failure, we try to retrieve the name of the connected peer + // To know whether it's a success or a failure, we try to retrieve the name of the connected peer SocketHelper::LengthType Size = sizeof(SockAddr); - if (getpeername(mySocket, reinterpret_cast(&SockAddr), &Size) != -1) - { - // Connection accepted - Status = Socket::Done; - } - else - { - // Connection failed - Status = SocketHelper::GetErrorStatus(); - } + if (getpeername(mySocket, reinterpret_cast(&SockAddr), &Size) != -1) + { + // Connection accepted + Status = Socket::Done; + } + else + { + // Connection failed + Status = SocketHelper::GetErrorStatus(); + } } else { @@ -344,11 +344,20 @@ Socket::Status SocketTCP::Receive(Packet& PacketToReceive) std::size_t Received = 0; if (myPendingPacketSize < 0) { - Socket::Status Status = Receive(reinterpret_cast(&PacketSize), sizeof(PacketSize), Received); - if (Status != Socket::Done) - return Status; + // Loop until we've received the entire size of the packet + // (even a 4 bytes variable may be received in more than one call) + while (myPendingHeaderSize < sizeof(myPendingHeader)) + { + char* Data = reinterpret_cast(&myPendingHeader) + myPendingHeaderSize; + Socket::Status Status = Receive(Data, sizeof(myPendingHeader) - myPendingHeaderSize, Received); + myPendingHeaderSize += Received; - PacketSize = ntohl(PacketSize); + if (Status != Socket::Done) + return Status; + } + + PacketSize = ntohl(myPendingHeader); + myPendingHeaderSize = 0; } else { @@ -472,6 +481,7 @@ void SocketTCP::Create(SocketHelper::SocketType Descriptor) myIsBlocking = true; // Reset the pending packet + myPendingHeaderSize = 0; myPendingPacket.clear(); myPendingPacketSize = -1; diff --git a/Externals/SFML/src/SFML/Network/SocketUDP.cpp b/Externals/SFML/src/SFML/Network/SocketUDP.cpp index 822e167fc5..ec46c65864 100644 --- a/Externals/SFML/src/SFML/Network/SocketUDP.cpp +++ b/Externals/SFML/src/SFML/Network/SocketUDP.cpp @@ -244,20 +244,25 @@ Socket::Status SocketUDP::Send(Packet& PacketToSend, const IPAddress& Address, u //////////////////////////////////////////////////////////// Socket::Status SocketUDP::Receive(Packet& PacketToReceive, IPAddress& Address, unsigned short& Port) { - // This is not safe at all, as data can be lost, duplicated, or arrive in a different order. - // So if a packet is split into more than one chunk, nobody knows what could happen... - // Conclusion : we shouldn't use packets with UDP, unless we build a more complex protocol on top of it. - // We start by getting the size of the incoming packet Uint32 PacketSize = 0; std::size_t Received = 0; if (myPendingPacketSize < 0) { - Socket::Status Status = Receive(reinterpret_cast(&PacketSize), sizeof(PacketSize), Received, Address, Port); - if (Status != Socket::Done) - return Status; + // Loop until we've received the entire size of the packet + // (even a 4 bytes variable may be received in more than one call) + while (myPendingHeaderSize < sizeof(myPendingHeader)) + { + char* Data = reinterpret_cast(&myPendingHeader) + myPendingHeaderSize; + Socket::Status Status = Receive(Data, sizeof(myPendingHeader) - myPendingHeaderSize, Received, Address, Port); + myPendingHeaderSize += Received; - PacketSize = ntohl(PacketSize); + if (Status != Socket::Done) + return Status; + } + + PacketSize = ntohl(myPendingHeader); + myPendingHeaderSize = 0; } else { @@ -265,9 +270,6 @@ Socket::Status SocketUDP::Receive(Packet& PacketToReceive, IPAddress& Address, u PacketSize = myPendingPacketSize; } - // Clear the user packet - PacketToReceive.Clear(); - // Use another address instance for receiving the packet data ; // chunks of data coming from a different sender will be discarded (and lost...) IPAddress Sender; @@ -402,6 +404,7 @@ void SocketUDP::Create(SocketHelper::SocketType Descriptor) myPort = 0; // Reset the pending packet + myPendingHeaderSize = 0; myPendingPacket.clear(); myPendingPacketSize = -1;