1 | <?php
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Zend Framework
|
---|
5 | *
|
---|
6 | * LICENSE
|
---|
7 | *
|
---|
8 | * This source file is subject to the new BSD license that is bundled
|
---|
9 | * with this package in the file LICENSE.txt.
|
---|
10 | * It is also available through the world-wide-web at this URL:
|
---|
11 | * http://framework.zend.com/license/new-bsd
|
---|
12 | * If you did not receive a copy of the license and are unable to
|
---|
13 | * obtain it through the world-wide-web, please send an email
|
---|
14 | * to license@zend.com so we can send you a copy immediately.
|
---|
15 | *
|
---|
16 | * @category Zend
|
---|
17 | * @package Zend_Gdata
|
---|
18 | * @subpackage App
|
---|
19 | * @version $Id: Socket.php 8064 2008-02-16 10:58:39Z thomas $
|
---|
20 | * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
21 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
22 | */
|
---|
23 |
|
---|
24 | require_once 'Zend/Http/Client/Adapter/Socket.php';
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Overrides the traditional socket-based adapter class for Zend_Http_Client to
|
---|
28 | * enable logging of requests. All requests are logged to a location specified
|
---|
29 | * in the config as $config['logfile']. Requests and responses are logged after
|
---|
30 | * they are sent and received/processed, thus an error could prevent logging.
|
---|
31 | *
|
---|
32 | * @category Zend
|
---|
33 | * @package Zend_Gdata
|
---|
34 | * @subpackage App
|
---|
35 | * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
36 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
37 | */
|
---|
38 | class Zend_Gdata_App_LoggingHttpClientAdapterSocket extends Zend_Http_Client_Adapter_Socket
|
---|
39 | {
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * The file handle for writing logs
|
---|
43 | *
|
---|
44 | * @var resource|null
|
---|
45 | */
|
---|
46 | protected $log_handle = null;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Log the given message to the log file. The log file is configured
|
---|
50 | * as the config param 'logfile'. This method opens the file for
|
---|
51 | * writing if necessary.
|
---|
52 | *
|
---|
53 | * @param string $message The message to log
|
---|
54 | */
|
---|
55 | protected function log($message)
|
---|
56 | {
|
---|
57 | if ($this->log_handle == null) {
|
---|
58 | $this->log_handle = fopen($this->config['logfile'], 'a');
|
---|
59 | }
|
---|
60 | fwrite($this->log_handle, $message);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Connect to the remote server
|
---|
65 | *
|
---|
66 | * @param string $host
|
---|
67 | * @param int $port
|
---|
68 | * @param boolean $secure
|
---|
69 | * @param int $timeout
|
---|
70 | */
|
---|
71 | public function connect($host, $port = 80, $secure = false)
|
---|
72 | {
|
---|
73 | $this->log("Connecting to: ${host}:${port}");
|
---|
74 | return parent::connect($host, $port, $secure);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Send request to the remote server
|
---|
79 | *
|
---|
80 | * @param string $method
|
---|
81 | * @param Zend_Uri_Http $uri
|
---|
82 | * @param string $http_ver
|
---|
83 | * @param array $headers
|
---|
84 | * @param string $body
|
---|
85 | * @return string Request as string
|
---|
86 | */
|
---|
87 | public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
|
---|
88 | {
|
---|
89 | $request = parent::write($method, $uri, $http_ver, $headers, $body);
|
---|
90 | $this->log("\n\n" . $request);
|
---|
91 | return $request;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Read response from server
|
---|
96 | *
|
---|
97 | * @return string
|
---|
98 | */
|
---|
99 | public function read()
|
---|
100 | {
|
---|
101 | $response = parent::read();
|
---|
102 | $this->log("${response}\n\n");
|
---|
103 | return $response;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Close the connection to the server
|
---|
108 | *
|
---|
109 | */
|
---|
110 | public function close()
|
---|
111 | {
|
---|
112 | $this->log("Closing socket\n\n");
|
---|
113 | parent::close();
|
---|
114 | }
|
---|
115 |
|
---|
116 | }
|
---|