Mercurial > SimpleWebPresenter
comparison Http.inc.php @ 134:b6b4a58c7625
Using .inc.php rather than just .inc for include files.
| author | Tom Fredrik Blenning <bfg@bfgconsult.no> |
|---|---|
| date | Sun, 22 Jan 2023 19:22:00 +0100 |
| parents | Http.inc@51b53cd01080 |
| children |
comparison
equal
deleted
inserted
replaced
| 133:00255ca89459 | 134:b6b4a58c7625 |
|---|---|
| 1 <?php | |
| 2 include_once 'ScriptIncludeCache.inc.php'; | |
| 3 | |
| 4 /// @cond | |
| 5 $baseDir = dirname(__FILE__); | |
| 6 $cache = ScriptIncludeCache::instance(__FILE__); | |
| 7 /// @endcond | |
| 8 | |
| 9 /** | |
| 10 * Thrown if a request has gone bad | |
| 11 */ | |
| 12 class RequestException extends RuntimeException | |
| 13 { | |
| 14 private $_info; | |
| 15 | |
| 16 /** | |
| 17 * Constructs a RequestException | |
| 18 * | |
| 19 * @param $info an info array as defined in http_get, if a key with | |
| 20 * name error exist, this will be the error text for the | |
| 21 * RuntimeException | |
| 22 */ | |
| 23 function __construct($info) | |
| 24 { | |
| 25 $this->_info = $info; | |
| 26 if (array_key_exists('error', $info)) { | |
| 27 parent::__construct($info['error']); | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 /** | |
| 32 * Get the info object associated with this RequestException | |
| 33 * | |
| 34 * @return $info an info array as defined in http_get | |
| 35 */ | |
| 36 function info() | |
| 37 { | |
| 38 return $this->_info; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 /** | |
| 43 * Http contains a set of functions for retrieving http information | |
| 44 */ | |
| 45 class Http | |
| 46 { | |
| 47 /** | |
| 48 * Gets the content of a page | |
| 49 * | |
| 50 * This mimics the file_content with a uri parameter which is often | |
| 51 * disabled due to security reasons | |
| 52 * | |
| 53 * @param $uri The uri to fetch | |
| 54 */ | |
| 55 static function get_uri_contents($uri) | |
| 56 { | |
| 57 $crl = curl_init(); | |
| 58 $timeout = 5; | |
| 59 | |
| 60 curl_setopt ($crl, CURLOPT_URL, $uri); | |
| 61 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); | |
| 62 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); | |
| 63 $ret = curl_exec($crl); | |
| 64 curl_close($crl); | |
| 65 return $ret; | |
| 66 } | |
| 67 | |
| 68 /** | |
| 69 * Splits an http header response into an associative array | |
| 70 * | |
| 71 * @param $response The headers to parse | |
| 72 * @return an Associative array, the key '' refers to the HTTP status header | |
| 73 */ | |
| 74 static function headersToArray($response) | |
| 75 { | |
| 76 $headers = array(); | |
| 77 $response = trim($response); | |
| 78 $str = explode("\n", $response); | |
| 79 $headers[''] = trim($str[0]); | |
| 80 foreach($str as $kv) { | |
| 81 $p = strpos($kv, ":"); | |
| 82 if ($p) { | |
| 83 $key = substr($kv, 0, $p); | |
| 84 $value = trim(substr($kv, $p + 1)); | |
| 85 $headers[$key] = $value; | |
| 86 } | |
| 87 } | |
| 88 return $headers; | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * Queries a URL for headers | |
| 93 * | |
| 94 * @param $url the url to query | |
| 95 * @param $timeout float number of seconds to wait before timeout | |
| 96 * @return an associative array of all headers returned | |
| 97 */ | |
| 98 static function getHeaders($url, $timeout = 1) | |
| 99 { | |
| 100 //Workaround when getHeaders doesn't work | |
| 101 if (false) { | |
| 102 $response = http_head($url, array("timeout" => $timeout), $info); | |
| 103 if (array_key_exists('error', $info) && $info['error']) { | |
| 104 throw new RequestException($info); | |
| 105 } | |
| 106 return self::headersToArray($response); | |
| 107 } | |
| 108 elseif (true) { | |
| 109 $rp = get_headers($url); | |
| 110 $response=array('' => array_shift ($rp)); | |
| 111 foreach($rp as $kv) { | |
| 112 if (!str_contains($kv, ':')) { | |
| 113 if (!preg_match("/^HTTP\/\S+\s(\d+)/", $response[''], $re)) { | |
| 114 throw new Exception('Uninteligble header'); | |
| 115 } | |
| 116 //FIXME: We should do something with this, | |
| 117 //but now we just assume we have a redirect | |
| 118 $retCode=intval($re[1]); | |
| 119 $response=array('' => $kv); | |
| 120 } | |
| 121 else { | |
| 122 $tmp=array_map('trim', explode(':', $kv, 2)); | |
| 123 $response[$tmp[0]]=$tmp[1]; | |
| 124 } | |
| 125 } | |
| 126 return $response; | |
| 127 } | |
| 128 else { | |
| 129 return array('' => 'HTTP/1.1 200 OK'); | |
| 130 } | |
| 131 | |
| 132 } | |
| 133 | |
| 134 /** | |
| 135 * Performs a post to an URI for headers | |
| 136 * | |
| 137 * @param $uri the uri to query | |
| 138 * @param $params an associative array of params to post | |
| 139 * @param $timeout float number of seconds to wait before timeout | |
| 140 * @return an associative array of all headers returned | |
| 141 */ | |
| 142 static function postHeaders($uri, $params, $timeout = 1) | |
| 143 { | |
| 144 $crl = curl_init(); | |
| 145 | |
| 146 $descriptorspec = array( | |
| 147 0 => array("pipe", "r"), | |
| 148 1 => array("pipe", "w"), | |
| 149 //2 => array("file", "/tmp/error-output.txt", "a") | |
| 150 ); | |
| 151 | |
| 152 //We use tac, since it buffers, and we don't care about the output | |
| 153 //being reordered. | |
| 154 $process = proc_open('tac | tac', $descriptorspec, $pipes); | |
| 155 | |
| 156 curl_setopt ($crl, CURLOPT_URL, $uri); | |
| 157 curl_setopt ($crl, CURLOPT_WRITEHEADER, $pipes[0]); | |
| 158 curl_setopt ($crl, CURLOPT_NOBODY, true); | |
| 159 curl_setopt ($crl, CURLOPT_POST, true); | |
| 160 curl_setopt ($crl, CURLOPT_POSTFIELDS, $params); | |
| 161 | |
| 162 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); | |
| 163 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); | |
| 164 $ret = curl_exec($crl); | |
| 165 curl_close($crl); | |
| 166 | |
| 167 fclose($pipes[0]); | |
| 168 $buf = fread($pipes[1], 8192); | |
| 169 | |
| 170 return self::headersToArray($buf); | |
| 171 } | |
| 172 } | |
| 173 ?> |
