]> git.sesse.net Git - cubemap/blobdiff - httpinput.cpp
Fix a socket leak in HTTPInput.
[cubemap] / httpinput.cpp
index 46f1358927312ba210cfa2f518e29991595d14ed..291b17f089578fe54b2a4ac33be06bfafc1742f4 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdint.h>
+#include <unistd.h>
 #include <assert.h>
 #include <arpa/inet.h>
 #include <sys/socket.h>
@@ -57,6 +58,18 @@ HTTPInput::HTTPInput(const InputProto &serialized)
        parse_url(url, &protocol, &host, &port, &path);  // Don't care if it fails.
 }
 
+void HTTPInput::close_socket()
+{
+       int ret;
+       do {
+               ret = close(sock);
+       } while (ret == -1 && errno == EINTR);
+
+       if (ret == -1) {
+               perror("close()");
+       }
+}
+
 InputProto HTTPInput::serialize() const
 {
        InputProto serialized;
@@ -101,6 +114,15 @@ int HTTPInput::lookup_and_connect(const string &host, const string &port)
                        return sock;
                }
 
+               do {
+                       err = close(sock);
+               } while (err == -1 && errno == EINTR);
+
+               if (err == -1) {
+                       perror("close");
+                       // Can still continue.
+               }
+
                ai = ai->ai_next;
        }