/** * Processes a HTTP response * * This extracts response code, headers, cookies and decodes body if it * was encoded in some way * * @access public * @param bool Whether to store response body in object property, set * this to false if downloading a LARGE file and using a Listener. * This is assumed to be true if body is gzip-encoded. * @param bool Whether the response can actually have a message-body. * Will be set to false for HEAD requests. * @throws PEAR_Error * @return mixed true on success, PEAR_Error in case of malformed response */ function process($saveBody = true, $canHaveBody = true) { do { $line = $this->_sock->readLine(); if (sscanf($line, 'HTTP/%s %s', $http_version, $returncode) != 2) { return PEAR::raiseError('Malformed response', HTTP_REQUEST_ERROR_RESPONSE); } else { $this->_protocol = 'HTTP/' . $http_version; $this->_code = intval($returncode); } while ('' !== ($header = $this->_sock->readLine())) { $this->_processHeader($header); } } while (100 == $this->_code); $this->_notify('gotHeaders', $this->_headers); // RFC 2616, section 4.4: // 1. Any response message which "MUST NOT" include a message-body ... // is always terminated by the first empty line after the header fields // 3. ... If a message is received with both a // Transfer-Encoding header field and a Content-Length header field, // the latter MUST be ignored. $canHaveBody = $canHaveBody && $this->_code >= 200 && $this->_code != 204 && $this->_code != 304; // If response body is present, read it and decode $chunked = isset($this->_headers['transfer-encoding']) && ('chunked' == $this->_headers['transfer-encoding']); $gzipped = isset($this->_headers['content-encoding']) && ('gzip' == $this->_headers['content-encoding']); $hasBody = false; if ($canHaveBody && ($chunked || !isset($this->_headers['content-length']) || 0 != $this->_headers['content-length'])) { if ($chunked || !isset($this->_headers['content-length'])) { $this->_toRead = null; } else { $this->_toRead = $this->_headers['content-length']; } while (!$this->_sock->eof() && (is_null($this->_toRead) || 0 < $this->_toRead)) { if ($chunked) { $data = $this->_readChunked(); } elseif (is_null($this->_toRead)) { $data = $this->_sock->read(4096); } else { $data = $this->_sock->read(min(4096, $this->_toRead)); $this->_toRead -= HTTP_REQUEST_MBSTRING? mb_strlen($data, 'iso-8859-1'): strlen($data); } if ('' == $data) { break; } else { $hasBody = true; if ($saveBody || $gzipped) { $this->_body .= $data; } $this->_notify($gzipped? 'gzTick': 'tick', $data); } } } if ($hasBody) { // Uncompress the body if needed if ($gzipped) { $body = $this->_decodeGzip($this->_body); if (PEAR::isError($body)) { return $body; } $this->_body = $body; $this->_notify('gotBody', $this->_body); } else { $this->_notify('gotBody'); } } return true; }
/** * @brief * @param string $data_file Saveするファイルのパス * @param string $url データのURL * @retval bool */ function fetchTgzFile($data_file, $url) { /* exec('which wget || which curl', $which, $status); $command = strpos($which[0], 'wget')!==false ? sprintf('%s -O %s %s', $which[0], $data_file, $_url) : sprintf('%s -o %s %s', $which[0], $data_file, $_url); exec($command, $result, $status); return $status === 0; */ if ($fp = fopen($data_file, 'w')) { $url = parse_url($url); if (isset($url['port']) && $url['port']) { $port = $url['port']; } else { $port = $url['scheme'] == 'https' ? '443' : '80'; } $path = isset($url['path']) && $url['path'] ? $url['path'] : '/'; $path .= isset($url['query']) && $url['query'] ? $url['query'] : ''; require_once 'Net/Socket.php'; $sock = new Net_Socket(); $connect = $sock->connect($url['host'], $port); if ($connect) { $sock->writeLine("GET {$path} HTTP/1.1"); $sock->writeLine("Host: " . $url['host']); $sock->writeLine("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (.NET CLR 3.5.30729)"); $sock->writeLine("Keep-Alive: 1000"); $sock->writeLine("Connection: keep-alive"); $sock->writeLine(""); $null_line = false; while (!$sock->eof()) { if ($null_line === false) { $_sock_d = $sock->readLine(); if ($_sock_d == "") { $null_line = true; } } if ($null_line === true) { $_sock_d = $sock->read(1024); fputs($fp, $_sock_d); } } $sock->disconnect(); } fclose($fp); return true; } return false; }