OSDN Git Service

[new feature] Generation Gap
[httpagent/http_agent.git] / lib / Core / Http / AgentTemplate.class.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
4 // | PHP Version 5                                                        |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 2010 Takahiro Ooishi                                   |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 3.0 of the PHP license,       |
9 // | that is bundled with this package in the file LICENSE, and is        |
10 // | available through the world-wide-web at the following url:           |
11 // | http://www.php.net/license/3_0.txt.                                  |
12 // | If you did not receive a copy of the PHP license and are unable to   |
13 // | obtain it through the world-wide-web, please send a note to          |
14 // | license@php.net so we can mail you a copy immediately.               |
15 // +----------------------------------------------------------------------+
16 // | Author: Takahiro Ooishi <taka0125.biz@gmail.com>                     |
17 // +----------------------------------------------------------------------+
18 //
19 // $Id$
20
21 require_once 'Http/Error.class.php';
22
23 abstract class Core_Http_AgentTemplate
24 {
25     protected $error;
26
27     protected $options;
28     protected $timeout;
29
30     const DefaultTimeout = 300;
31
32     abstract function send($url, $request);
33     abstract protected function open();
34     abstract protected function close();
35     abstract protected function setOptions($options);
36     abstract function setOption($name, $option);
37     abstract function setBasicAuthenticationAccount($user, $password);    
38  
39     function __construct($options = array(), $timeout = self::DefaultTimeout)
40     {
41         $this->setOptions($options);
42         $this->setTimeout($timeout);
43         $this->open();
44         $this->error = new Http_Error();
45     }
46
47     function __destruct()
48     {
49         $this->close();
50     }
51
52     function options()
53     {
54         return $this->options;
55     }
56
57     function setTimeout($timeout)
58     {
59         if (!is_numeric($timeout)) throw new ValidationException($timeout, 'Invalid Timeout.');
60         $timeout = intval($timeout);
61         if ($timeout < 1) throw new ValidationException($timeout, 'Invalid Timeout.');
62         $this->timeout = $timeout;
63     }
64
65     function isError()
66     {
67         return $this->error->isError();
68     }
69
70     function getError()
71     {
72         return $this->error->get();
73     }
74
75     protected function clearError()
76     {
77         return $this->error->clear();
78     }
79 }