Skip to content

Commit

Permalink
Fix WinHttpHandler send function.
Browse files Browse the repository at this point in the history
Shutdown is called after receive is completed, instead of before.
  • Loading branch information
Jojo-1000 committed Jul 15, 2021
1 parent 4dc165b commit 474783b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
22 changes: 14 additions & 8 deletions src/BaseHttpHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,20 @@ std::string BaseHttpHandler::sendHTTPRequest(const std::string& method, const st
request.append("HTTP/1.0"); // HTTP-Version
request.append("\r\n"); // Ending
// Entities
request.append("Content-Type:"); // entity-header
request.append(" "); // Separation
request.append(contentType); // media-type
request.append("\r\n"); // Entity ending
request.append("Content-Length:"); // entity-header
request.append(" "); // Separation
request.append(std::to_string(body.size())); // length
request.append("\r\n\r\n"); // Entity ending & Request-Line ending
if (!contentType.empty())
{
request.append("Content-Type:"); // entity-header
request.append(" "); // Separation
request.append(contentType); // media-type
request.append("\r\n"); // Entity ending
}
if (!body.empty())
{
request.append("Content-Length:"); // entity-header
request.append(" "); // Separation
request.append(std::to_string(body.size())); // length
request.append("\r\n\r\n"); // Entity ending & Request-Line ending
}
request.append(body); // message-body
request.append("\r\n\r\n"); // Ending

Expand Down
23 changes: 11 additions & 12 deletions src/WinHttpHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace
class AddrInfoFreer
{
public:
explicit AddrInfoFreer(addrinfo* p) : p(p) {}
explicit AddrInfoFreer(addrinfo* p) : p(p) { }
~AddrInfoFreer() { freeaddrinfo(p); }

private:
Expand All @@ -48,7 +48,7 @@ class AddrInfoFreer
class SocketCloser
{
public:
explicit SocketCloser(SOCKET s) : s(s) {}
explicit SocketCloser(SOCKET s) : s(s) { }
~SocketCloser() { closesocket(s); }

private:
Expand Down Expand Up @@ -135,15 +135,6 @@ std::string WinHttpHandler::send(const std::string& msg, const std::string& adr,
throw(std::system_error(err, std::system_category(), "WinHttpHandler: send failed"));
}

// shutdown the connection for sending since no more data will be sent
// the client can still use the ConnectSocket for receiving data
if (shutdown(connect_socket, SD_SEND) == SOCKET_ERROR)
{
int err = WSAGetLastError();
std::cerr << "WinHttpHandler: shutdown failed: " << err << std::endl;
throw(std::system_error(err, std::system_category(), "WinHttpHandler: shutdown failed"));
}

const int recvbuflen = 128;
char recvbuf[recvbuflen];

Expand All @@ -170,6 +161,14 @@ std::string WinHttpHandler::send(const std::string& msg, const std::string& adr,
}
} while (res > 0);

// shutdown the connection
if (shutdown(connect_socket, SD_BOTH) == SOCKET_ERROR)
{
int err = WSAGetLastError();
std::cerr << "WinHttpHandler: shutdown failed: " << err << std::endl;
throw(std::system_error(err, std::system_category(), "WinHttpHandler: shutdown failed"));
}

return response;
}

Expand Down Expand Up @@ -250,7 +249,7 @@ std::vector<std::string> WinHttpHandler::sendMulticast(
}

// shutdown the connection for sending since no more data will be sent
// the client can still use the ConnectSocket for receiving data
// the client can still use the ConnectSocket for receiving data (no issue here because this is a UDP socket)
if (shutdown(connect_socket, SD_SEND) == SOCKET_ERROR)
{
int err = WSAGetLastError();
Expand Down

0 comments on commit 474783b

Please sign in to comment.