Differences Between: [Versions 400 and 401]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace enrol_lti\local\ltiadvantage\lib; 18 19 use Packback\Lti1p3\Interfaces\IHttpClient; 20 use Packback\Lti1p3\Interfaces\IHttpException; 21 use Packback\Lti1p3\Interfaces\IHttpResponse; 22 23 /** 24 * An implementation of IHTTPClient delegating to a curl object, for use with the lib/lti1p3 library code. 25 * 26 * @package enrol_lti 27 * @copyright 2022 Jake Dallimore <jrhdallimore@gmail.com> 28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 29 */ 30 class http_client implements IHttpClient { 31 32 /** @var \curl a curl client instance. */ 33 private $curlclient; 34 35 /** 36 * Constructor. 37 * 38 * @param \curl $curlclient a curl client instance. 39 */ 40 public function __construct(\curl $curlclient) { 41 $this->curlclient = $curlclient; 42 } 43 44 /** 45 * Make an HTTP request to the given URL. 46 * 47 * @param string $method the HTTP method to use. 48 * @param string $url the URL to send the request to. 49 * @param array $options an array of request options, mainly used to set headers and body. 50 * @return IHttpResponse the response 51 * @throws \Exception if the curl client encounters any errors making the request. 52 * @throws IHttpException if the response contains a 400-level or 500-level status code. 53 */ 54 public function request(string $method, string $url, array $options): IHttpResponse { 55 $this->curlclient->resetHeader(); 56 $this->curlclient->resetopt(); 57 if (isset($options['headers'])) { 58 $headers = $options['headers']; 59 array_walk( 60 $headers, 61 function(&$val, $key) { 62 $val = "$key: $val"; 63 } 64 ); 65 $this->curlclient->setHeader($headers); 66 } 67 if ($method == 'POST') { 68 $res = $this->curlclient->post($url, $options['body'] ?? null, ['CURLOPT_HEADER' => 1]); 69 } else if ($method == 'GET') { 70 $res = $this->curlclient->get($url, [], ['CURLOPT_HEADER' => 1]); 71 } else { 72 throw new \Exception('Sorry, that HTTP method is not supported yet.'); 73 } 74 75 $info = $this->curlclient->get_info(); 76 if (!$this->curlclient->get_errno() && !$this->curlclient->error) { 77 // No errors, so format the response. 78 $headersize = $info['header_size']; 79 $resheaders = substr($res, 0, $headersize); 80 $resbody = substr($res, $headersize); 81 $headerlines = array_filter(explode("\r\n", $resheaders)); 82 $parsedresponseheaders = [ 83 'httpstatus' => array_shift($headerlines) 84 ]; 85 foreach ($headerlines as $headerline) { 86 $headerbits = explode(':', $headerline, 2); 87 if (count($headerbits) == 2) { 88 // Only parse headers having colon separation. 89 $parsedresponseheaders[$headerbits[0]] = $headerbits[1]; 90 } 91 } 92 $response = new http_response(['headers' => $parsedresponseheaders, 'body' => $resbody], intval($info['http_code'])); 93 if ($response->getStatusCode() >= 400) { 94 throw new http_exception($response, "An HTTP error status was received: '{$response->getHeaders()['httpstatus']}'"); 95 } 96 return $response; 97 } 98 // The curl client experienced errors, so report that. 99 throw new \Exception("There was a cURL error when making the request: errno: {$this->curlclient->get_errno()}, 100 error: {$this->curlclient->error}."); 101 } 102 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body