]> git.mxchange.org Git - mailer.git/blobdiff - inc/functions.php
Broken mask fixed (%%) and request dispatcher sendRawRequest() rewritten to timeout...
[mailer.git] / inc / functions.php
index f54d18693db3cc0ef999edbe1683b2e2665e991e..aa691935e1f452796adb75cae04fdce88fbf1e81 100644 (file)
@@ -1750,6 +1750,9 @@ function sendGetRequest ($script, $data = array()) {
        // Add script data
        $script .= $body;
 
+       // Remove trailed & to make it more conform
+       if (substr($script, -1, 1) == '&') $script = substr($script, 0, -1);
+
        // Generate GET request header
        $request  = 'GET /' . trim($script) . ' HTTP/1.1' . getConfig('HTTP_EOL');
        $request .= 'Host: ' . $host . getConfig('HTTP_EOL');
@@ -1764,7 +1767,7 @@ function sendGetRequest ($script, $data = array()) {
        $request .= 'Cache-Control: no-cache' . getConfig('HTTP_EOL');
        $request .= 'Content-Type: text/plain' . getConfig('HTTP_EOL');
        $request .= 'Content-Length: '.strlen($body) . getConfig('HTTP_EOL');
-       $request .= 'Connection: Close' . getConfig('HTTP_EOL') . getConfig('HTTP_EOL');
+       $request .= 'Connection: close' . getConfig('HTTP_EOL') . getConfig('HTTP_EOL');
 
        // Send the raw request
        $response = sendRawRequest($host, $request);
@@ -1795,7 +1798,7 @@ function sendPostRequest ($script, $postData) {
        $request .= 'User-Agent: ' . getConfig('TITLE') . '/' . getConfig('FULL_VERSION') . getConfig('HTTP_EOL');
        $request .= 'Cache-Control: no-cache' . getConfig('HTTP_EOL');
        $request .= 'Content-Type: application/x-www-form-urlencoded' . getConfig('HTTP_EOL');
-       $request .= 'Connection: Close' . getConfig('HTTP_EOL') . getConfig('HTTP_EOL');
+       $request .= 'Connection: close' . getConfig('HTTP_EOL') . getConfig('HTTP_EOL');
        $request .= $body;
 
        // Send the raw request
@@ -1826,17 +1829,22 @@ function sendRawRequest ($host, $request) {
        //* DEBUG: */ die("SCRIPT=" . $script.'<br />');
        if ($useProxy === true) {
                // Connect to host through proxy connection
-               $fp = @fsockopen(compileRawCode(getConfig('proxy_host')), bigintval(getConfig('proxy_port')), $errno, $errdesc, 30);
+               $fp = fsockopen(compileRawCode(getConfig('proxy_host')), bigintval(getConfig('proxy_port')), $errno, $errdesc, 30);
        } else {
                // Connect to host directly
-               $fp = @fsockopen($host, 80, $errno, $errdesc, 30);
+               $fp = fsockopen($host, 80, $errno, $errdesc, 30);
        }
 
        // Is there a link?
        if (!is_resource($fp)) {
                // Failed!
+               logDebugMessage(__FUNCTION__, __LINE__, $errdesc . ' (' . $errno . ')');
                return $response;
-       } // END - if
+       } elseif ((!stream_set_blocking($fp, 0)) || (!stream_set_timeout($fp, 1))) {
+               // Cannot set non-blocking mode or timeout
+               logDebugMessage(__FUNCTION__, __LINE__, socket_strerror(socket_last_error()));
+               return $response;
+       }
 
        // Do we use proxy?
        if ($useProxy === true) {
@@ -1856,7 +1864,7 @@ function sendRawRequest ($host, $request) {
                //* DEBUG: */ print('<strong>proxyTunnel=</strong><pre>' . $proxyTunnel.'</pre>');
 
                // Write request
-               fputs($fp, $proxyTunnel);
+               fwrite($fp, $proxyTunnel);
 
                // Got response?
                if (feof($fp)) {
@@ -1874,11 +1882,39 @@ function sendRawRequest ($host, $request) {
        } // END - if
 
        // Write request
-       fputs($fp, $request);
+       fwrite($fp, $request);
+
+       // Start counting
+       $start = microtime(true);
 
        // Read response
        while (!feof($fp)) {
-               $response[] = trim(fgets($fp, 1024));
+               // Get info from stream
+               $info = stream_get_meta_data($fp);
+
+               // Is it timed out? 15 seconds is a really patient...
+               if (($info['timed_out'] == true) || (microtime(true) - $start) > 15) {
+                       // Timeout
+                       logDebugMessage(__FUNCTION__, __LINE__, 'Timed out to get data from host ' . $host);
+
+                       // Abort here
+                       break;
+               } // END - if
+
+               // Get line from stream
+               $line = fgets($fp, 128);
+
+               // Ignore empty lines because of non-blocking mode
+               if (empty($line)) {
+                       // uslepp a little to avoid 100% CPU load
+                       usleep(10);
+
+                       // Skip this
+                       continue;
+               } // END - if
+
+               // Add it to response
+               $response[] = trim($line);
        } // END - while
 
        // Close socket
@@ -1902,8 +1938,11 @@ function sendRawRequest ($host, $request) {
 
        //* DEBUG: */ print('<strong>Response:</strong><pre>'.print_r($response, true).'</pre>');
 
-       // Proxy agent found?
-       if ((substr(strtolower($response[0]), 0, 11) == 'proxy-agent') && ($useProxy === true)) {
+       // Proxy agent found or something went wrong?
+       if (!isset($response[0])) {
+               // No response, maybe timeout
+               $response = array('', '', '');
+       } elseif ((substr(strtolower($response[0]), 0, 11) == 'proxy-agent') && ($useProxy === true)) {
                // Proxy header detected, so remove two lines
                array_shift($response);
                array_shift($response);
@@ -2836,7 +2875,7 @@ function changeDataInFile ($FQFN, $comment, $prefix, $suffix, $DATA, $seek=0) {
                                        } // END - if
 
                                        // Write to temp file
-                                       fputs($fp_tmp, $line);
+                                       fwrite($fp_tmp, $line);
                                } // END - while
 
                                // Close temp file