From 3f3d6c0222793164ec8cf4fc5a8d62266a2aaf6c Mon Sep 17 00:00:00 2001
From: Thulinma <jaron@vietors.com>
Date: Tue, 18 Oct 2011 09:10:19 +0200
Subject: [PATCH] Fixes

---
 util/http_parser.cpp |  5 ++++-
 util/socket.cpp      | 31 +++++++++++++++++++++----------
 util/util.cpp        |  9 +++++++++
 3 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/util/http_parser.cpp b/util/http_parser.cpp
index f46e5a2b..06bc713c 100644
--- a/util/http_parser.cpp
+++ b/util/http_parser.cpp
@@ -178,9 +178,12 @@ bool HTTP::Parser::parse(){
     if (seenHeaders){
       if (length > 0){
         if (HTTPbuffer.length() >= length){
+          if ((method != "HTTP/1.0") && (method != "HTTP/1.1")){
+            body = HTTPbuffer.substr(0, length);
+            parseVars(body); //parse POST variables
+          }
           body = HTTPbuffer.substr(0, length);
           HTTPbuffer.erase(0, length);
-          parseVars(body); //parse POST variables
           return true;
         }else{
           return false;
diff --git a/util/socket.cpp b/util/socket.cpp
index 5463e8de..522db714 100644
--- a/util/socket.cpp
+++ b/util/socket.cpp
@@ -29,7 +29,7 @@ Socket::Connection::Connection(){
 
 /// Close connection. The internal socket is closed and then set to -1.
 void Socket::Connection::close(){
-  #if DEBUG >= 4
+  #if DEBUG >= 6
   fprintf(stderr, "Socket closed.\n");
   #endif
   shutdown(sock, SHUT_RDWR);
@@ -81,14 +81,24 @@ Socket::Connection::Connection(std::string address, bool nonblock){
 /// \param port String containing the port to connect to.
 /// \param nonblock Whether the socket should be nonblocking.
 Socket::Connection::Connection(std::string host, int port, bool nonblock){
-  struct addrinfo *result, *rp;
+  struct addrinfo *result, *rp, hints;
   Error = false;
   Blocking = false;
   std::stringstream ss;
   ss << port;
-  if (getaddrinfo(host.c_str(), ss.str().c_str(), 0, &result) != 0){
+
+  memset(&hints, 0, sizeof(struct addrinfo));
+  hints.ai_family = AF_UNSPEC;
+  hints.ai_socktype = SOCK_STREAM;
+  hints.ai_flags = AI_ADDRCONFIG;
+  hints.ai_protocol = 0;
+  hints.ai_canonname = NULL;
+  hints.ai_addr = NULL;
+  hints.ai_next = NULL;
+  int s = getaddrinfo(host.c_str(), ss.str().c_str(), &hints, &result);
+  if (s != 0){
     #if DEBUG >= 1
-    fprintf(stderr, "Could not connect to %s:%i! Error: %s\n", host.c_str(), port, strerror(errno));
+    fprintf(stderr, "Could not connect to %s:%i! Error: %s\n", host.c_str(), port, gai_strerror(s));
     #endif
     close();
     return;
@@ -96,10 +106,11 @@ Socket::Connection::Connection(std::string host, int port, bool nonblock){
 
   for (rp = result; rp != NULL; rp = rp->ai_next) {
     sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
-    if (sock == -1){continue;}
-    if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1){break;}
+    if (sock < 0){continue;}
+    if (connect(sock, rp->ai_addr, rp->ai_addrlen) == 0){break;}
     ::close(sock);
   }
+  freeaddrinfo(result);
 
   if (rp == 0){
     #if DEBUG >= 1
@@ -534,18 +545,18 @@ Socket::Connection Socket::Server::accept(bool nonblock){
   }else{
     if (addrinfo.sin6_family == AF_INET6){
       tmp.remotehost = inet_ntop(AF_INET6, &(addrinfo.sin6_addr), addrconv, INET6_ADDRSTRLEN);
-      #if DEBUG >= 4
+      #if DEBUG >= 6
       fprintf(stderr,"IPv6 addr: %s\n", tmp.remotehost.c_str());
       #endif
     }
     if (addrinfo.sin6_family == AF_INET){
       tmp.remotehost = inet_ntop(AF_INET, &(((sockaddr_in*)&addrinfo)->sin_addr), addrconv, INET6_ADDRSTRLEN);
-      #if DEBUG >= 4
+      #if DEBUG >= 6
       fprintf(stderr,"IPv4 addr: %s\n", tmp.remotehost.c_str());
       #endif
     }
     if (addrinfo.sin6_family == AF_UNIX){
-      #if DEBUG >= 4
+      #if DEBUG >= 6
       tmp.remotehost = ((sockaddr_un*)&addrinfo)->sun_path;
       fprintf(stderr,"Unix socket, no address\n");
       #endif
@@ -557,7 +568,7 @@ Socket::Connection Socket::Server::accept(bool nonblock){
 
 /// Close connection. The internal socket is closed and then set to -1.
 void Socket::Server::close(){
-  #if DEBUG >= 4
+  #if DEBUG >= 6
   fprintf(stderr, "ServerSocket closed.\n");
   #endif
   shutdown(sock, SHUT_RDWR);
diff --git a/util/util.cpp b/util/util.cpp
index 687dba58..06a1ace1 100644
--- a/util/util.cpp
+++ b/util/util.cpp
@@ -5,7 +5,12 @@
 #include <string.h>
 #include <sys/types.h>
 #include <signal.h>
+
+#ifdef __FreeBSD__
+#include <sys/wait.h>
+#else
 #include <wait.h>
+#endif
 #include <errno.h>
 #include <iostream>
 #include <sys/types.h>
@@ -52,6 +57,7 @@ void Util::Procs::childsig_handler(int signum){
   #if DEBUG >= 1
   if (isActive(pname)){
     std::cerr << "Process " << pname << " half-terminated." << std::endl;
+    Stop(pname);
   }else{
     std::cerr << "Process " << pname << " fully terminated." << std::endl;
   }
@@ -190,8 +196,11 @@ pid_t Util::Procs::Start(std::string name, std::string cmd, std::string cmd2){
 /// Stops the named process, if running.
 /// \arg name (Internal) name of process to stop
 void Util::Procs::Stop(std::string name){
+  int max = 5;
   while (isActive(name)){
     Stop(getPid(name));
+    max--;
+    if (max <= 0){return;}
   }
 }