]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/curlclient.php
f45c3c2f4a01c4bd7e88b88a20090e160e7bce18
[quix0rs-gnu-social.git] / lib / curlclient.php
1 n<?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Utility class for wrapping Curl
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  HTTP
23  * @package   Laconica
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 define(CURLCLIENT_VERSION, "0.1");
35
36 /**
37  * Wrapper for Curl
38  *
39  * Makes Curl HTTP client calls within our HTTPClient framework
40  *
41  * @category HTTP
42  * @package  Laconica
43  * @author   Evan Prodromou <evan@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://laconi.ca/
46  */
47
48 class CurlClient extends HTTPClient
49 {
50     function __construct()
51     {
52     }
53
54     function head($url, $headers=null)
55     {
56         $ch = curl_init($url);
57
58         $this->setup($ch);
59
60         curl_setopt_array($ch,
61                           array(CURLOPT_NOBODY => true));
62
63         if (!is_null($headers)) {
64             curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
65         }
66
67         $result = curl_exec($ch);
68
69         curl_close($ch);
70
71         return $this->parseResults($result);
72     }
73
74     function get($url, $headers=null)
75     {
76         $ch = curl_init($url);
77
78         $this->setup($ch);
79
80         if (!is_null($headers)) {
81             curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
82         }
83
84         $result = curl_exec($ch);
85
86         curl_close($ch);
87
88         return $this->parseResults($result);
89     }
90
91     function setup($ch)
92     {
93         curl_setopt_array($ch,
94                           array(CURLOPT_USERAGENT, $this->userAgent(),
95                                 CURLOPT_HEADER => true,
96                                 CURLOPT_RETURNTRANSFER => true));
97     }
98
99     function userAgent()
100     {
101         $version = curl_version();
102         return parent::userAgent() . " CurlClient/".CURLCLIENT_VERSION . " cURL/" . $version['version'];
103     }
104
105     function parseResults($results)
106     {
107         $resp = new HTTPResponse();
108
109         $lines = explode("\r\n", $results);
110
111         if (preg_match("#^HTTP/1.[01] (\d\d\d) .+$#", $lines[0], $match)) {
112             $resp->code = $match[1];
113         } else {
114             throw Exception("Bad format: initial line is not HTTP status line");
115         }
116
117         $lastk = null;
118
119         for ($i = 1; $i < count($lines); $i++) {
120             $l =& $lines[$i];
121             if (mb_strlen($l) == 0) {
122                 $resp->body = implode("\r\n", array_slice($lines, $i + 1));
123                 break;
124             }
125             if (preg_match("#^(\S+):\s+(.*)$#", $l, $match)) {
126                 $k = $match[1];
127                 $v = $match[2];
128
129                 if (array_key_exists($k, $resp->headers)) {
130                     if (is_array($resp->headers[$k])) {
131                         $resp->headers[$k][] = $v;
132                     } else {
133                         $resp->headers[$k] = array($resp->headers[$k], $v);
134                     }
135                 } else {
136                     $resp->headers[$k] = $v;
137                 }
138                 $lastk = $k;
139             } else if (preg_match("#^\s+(.*)$#", $l, $match)) {
140                 // continuation line
141                 if (is_null($lastk)) {
142                     throw Exception("Bad format: initial whitespace in headers");
143                 }
144                 $h =& $resp->headers[$lastk];
145                 if (is_array($h)) {
146                     $n = count($h);
147                     $h[$n-1] .= $match[1];
148                 } else {
149                     $h .= $match[1];
150                 }
151             }
152         }
153
154         return $resp;
155     }
156 }