]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/curlclient.php
fix user-agent for curlclient
[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 post($url, $headers=null)
92     {
93     }
94
95     function setup($ch)
96     {
97         curl_setopt_array($ch,
98                           array(CURLOPT_USERAGENT => $this->userAgent(),
99                                 CURLOPT_HEADER => true,
100                                 CURLOPT_RETURNTRANSFER => true));
101     }
102
103     function userAgent()
104     {
105         $version = curl_version();
106         return parent::userAgent() . " CurlClient/".CURLCLIENT_VERSION . " cURL/" . $version['version'];
107     }
108
109     function parseResults($results)
110     {
111         $resp = new HTTPResponse();
112
113         $lines = explode("\r\n", $results);
114
115         if (preg_match("#^HTTP/1.[01] (\d\d\d) .+$#", $lines[0], $match)) {
116             $resp->code = $match[1];
117         } else {
118             throw Exception("Bad format: initial line is not HTTP status line");
119         }
120
121         $lastk = null;
122
123         for ($i = 1; $i < count($lines); $i++) {
124             $l =& $lines[$i];
125             if (mb_strlen($l) == 0) {
126                 $resp->body = implode("\r\n", array_slice($lines, $i + 1));
127                 break;
128             }
129             if (preg_match("#^(\S+):\s+(.*)$#", $l, $match)) {
130                 $k = $match[1];
131                 $v = $match[2];
132
133                 if (array_key_exists($k, $resp->headers)) {
134                     if (is_array($resp->headers[$k])) {
135                         $resp->headers[$k][] = $v;
136                     } else {
137                         $resp->headers[$k] = array($resp->headers[$k], $v);
138                     }
139                 } else {
140                     $resp->headers[$k] = $v;
141                 }
142                 $lastk = $k;
143             } else if (preg_match("#^\s+(.*)$#", $l, $match)) {
144                 // continuation line
145                 if (is_null($lastk)) {
146                     throw Exception("Bad format: initial whitespace in headers");
147                 }
148                 $h =& $resp->headers[$lastk];
149                 if (is_array($h)) {
150                     $n = count($h);
151                     $h[$n-1] .= $match[1];
152                 } else {
153                     $h .= $match[1];
154                 }
155             }
156         }
157
158         return $resp;
159     }
160 }