Commit 474783bc46e4ae58ce64129a645e01cbc27458a9

Authored by Jojo-1000
Committed by Jan
1 parent 4dc165bf

Fix WinHttpHandler send function.

Shutdown is called after receive is completed, instead of before.
src/BaseHttpHandler.cpp
... ... @@ -56,14 +56,20 @@ std::string BaseHttpHandler::sendHTTPRequest(const std::string& method, const st
56 56 request.append("HTTP/1.0"); // HTTP-Version
57 57 request.append("\r\n"); // Ending
58 58 // Entities
59   - request.append("Content-Type:"); // entity-header
60   - request.append(" "); // Separation
61   - request.append(contentType); // media-type
62   - request.append("\r\n"); // Entity ending
63   - request.append("Content-Length:"); // entity-header
64   - request.append(" "); // Separation
65   - request.append(std::to_string(body.size())); // length
66   - request.append("\r\n\r\n"); // Entity ending & Request-Line ending
  59 + if (!contentType.empty())
  60 + {
  61 + request.append("Content-Type:"); // entity-header
  62 + request.append(" "); // Separation
  63 + request.append(contentType); // media-type
  64 + request.append("\r\n"); // Entity ending
  65 + }
  66 + if (!body.empty())
  67 + {
  68 + request.append("Content-Length:"); // entity-header
  69 + request.append(" "); // Separation
  70 + request.append(std::to_string(body.size())); // length
  71 + request.append("\r\n\r\n"); // Entity ending & Request-Line ending
  72 + }
67 73 request.append(body); // message-body
68 74 request.append("\r\n\r\n"); // Ending
69 75  
... ...
src/WinHttpHandler.cpp
... ... @@ -39,7 +39,7 @@ namespace
39 39 class AddrInfoFreer
40 40 {
41 41 public:
42   - explicit AddrInfoFreer(addrinfo* p) : p(p) {}
  42 + explicit AddrInfoFreer(addrinfo* p) : p(p) { }
43 43 ~AddrInfoFreer() { freeaddrinfo(p); }
44 44  
45 45 private:
... ... @@ -48,7 +48,7 @@ private:
48 48 class SocketCloser
49 49 {
50 50 public:
51   - explicit SocketCloser(SOCKET s) : s(s) {}
  51 + explicit SocketCloser(SOCKET s) : s(s) { }
52 52 ~SocketCloser() { closesocket(s); }
53 53  
54 54 private:
... ... @@ -135,15 +135,6 @@ std::string WinHttpHandler::send(const std::string& msg, const std::string& adr,
135 135 throw(std::system_error(err, std::system_category(), "WinHttpHandler: send failed"));
136 136 }
137 137  
138   - // shutdown the connection for sending since no more data will be sent
139   - // the client can still use the ConnectSocket for receiving data
140   - if (shutdown(connect_socket, SD_SEND) == SOCKET_ERROR)
141   - {
142   - int err = WSAGetLastError();
143   - std::cerr << "WinHttpHandler: shutdown failed: " << err << std::endl;
144   - throw(std::system_error(err, std::system_category(), "WinHttpHandler: shutdown failed"));
145   - }
146   -
147 138 const int recvbuflen = 128;
148 139 char recvbuf[recvbuflen];
149 140  
... ... @@ -170,6 +161,14 @@ std::string WinHttpHandler::send(const std::string&amp; msg, const std::string&amp; adr,
170 161 }
171 162 } while (res > 0);
172 163  
  164 + // shutdown the connection
  165 + if (shutdown(connect_socket, SD_BOTH) == SOCKET_ERROR)
  166 + {
  167 + int err = WSAGetLastError();
  168 + std::cerr << "WinHttpHandler: shutdown failed: " << err << std::endl;
  169 + throw(std::system_error(err, std::system_category(), "WinHttpHandler: shutdown failed"));
  170 + }
  171 +
173 172 return response;
174 173 }
175 174  
... ... @@ -250,7 +249,7 @@ std::vector&lt;std::string&gt; WinHttpHandler::sendMulticast(
250 249 }
251 250  
252 251 // shutdown the connection for sending since no more data will be sent
253   - // the client can still use the ConnectSocket for receiving data
  252 + // the client can still use the ConnectSocket for receiving data (no issue here because this is a UDP socket)
254 253 if (shutdown(connect_socket, SD_SEND) == SOCKET_ERROR)
255 254 {
256 255 int err = WSAGetLastError();
... ...