14c3a95e3SAndreas Gohr<?php 24c3a95e3SAndreas Gohr 34c3a95e3SAndreas Gohrnamespace dokuwiki\plugin\dw2pdf\src; 44c3a95e3SAndreas Gohr 54c3a95e3SAndreas Gohruse dokuwiki\HTTP\DokuHTTPClient; 64c3a95e3SAndreas Gohruse Mpdf\Http\ClientInterface; 74c3a95e3SAndreas Gohruse Mpdf\PsrHttpMessageShim\Response; 84c3a95e3SAndreas Gohruse Mpdf\PsrHttpMessageShim\Stream; 94c3a95e3SAndreas Gohruse Mpdf\PsrLogAwareTrait\PsrLogAwareTrait; 104c3a95e3SAndreas Gohruse Psr\Http\Message\RequestInterface; 114c3a95e3SAndreas Gohruse Psr\Log\LoggerAwareInterface; 124c3a95e3SAndreas Gohr 134c3a95e3SAndreas Gohr/** 144c3a95e3SAndreas Gohr * mPDF HTTP client adapter that routes requests through Dokuwiki's HTTP stack. 154c3a95e3SAndreas Gohr * 164c3a95e3SAndreas Gohr * Basically wraps a simple, naive PSR-7 implementation around DokuHTTPClient. 174c3a95e3SAndreas Gohr */ 184c3a95e3SAndreas Gohrclass HttpClient implements ClientInterface, LoggerAwareInterface 194c3a95e3SAndreas Gohr{ 204c3a95e3SAndreas Gohr use PsrLogAwareTrait; 214c3a95e3SAndreas Gohr 224c3a95e3SAndreas Gohr /** 234c3a95e3SAndreas Gohr * Send the HTTP request using Dokuwiki's HTTP client, falling back to media resolution when possible. 244c3a95e3SAndreas Gohr * 254c3a95e3SAndreas Gohr * @inheritDoc 264c3a95e3SAndreas Gohr */ 274c3a95e3SAndreas Gohr public function sendRequest(RequestInterface $request) 284c3a95e3SAndreas Gohr { 294c3a95e3SAndreas Gohr $uri = $request->getUri(); 304c3a95e3SAndreas Gohr 314c3a95e3SAndreas Gohr $url = (string)$uri; 324c3a95e3SAndreas Gohr 33*3c796bb4SAnna Dabrowska // standard Dokuwiki HTTP client for any remote content 344c3a95e3SAndreas Gohr $client = new DokuHTTPClient(); 354c3a95e3SAndreas Gohr $client->headers = $this->buildHeaders($request); 364c3a95e3SAndreas Gohr $client->referer = $request->getHeaderLine('Referer'); 374c3a95e3SAndreas Gohr if ($agent = $request->getHeaderLine('User-Agent')) { 384c3a95e3SAndreas Gohr $client->agent = $agent; 394c3a95e3SAndreas Gohr } 404c3a95e3SAndreas Gohr 414c3a95e3SAndreas Gohr $body = (string)$request->getBody(); 424c3a95e3SAndreas Gohr if ($request->getBody()->isSeekable()) { 434c3a95e3SAndreas Gohr $request->getBody()->rewind(); 444c3a95e3SAndreas Gohr } 454c3a95e3SAndreas Gohr 464c3a95e3SAndreas Gohr $method = strtoupper($request->getMethod()); 475340eaffSAndreas Gohr $client->sendRequest($url, $body, $method); 484c3a95e3SAndreas Gohr 494c3a95e3SAndreas Gohr $response = (new Response())->withStatus($client->status ?: 500); 504c3a95e3SAndreas Gohr 514c3a95e3SAndreas Gohr foreach ((array)$client->resp_headers as $name => $value) { 524c3a95e3SAndreas Gohr if (is_array($value)) { 534c3a95e3SAndreas Gohr foreach ($value as $single) { 544c3a95e3SAndreas Gohr $response = $response->withHeader($name, $single); 554c3a95e3SAndreas Gohr } 564c3a95e3SAndreas Gohr } else { 574c3a95e3SAndreas Gohr $response = $response->withHeader($name, $value); 584c3a95e3SAndreas Gohr } 594c3a95e3SAndreas Gohr } 604c3a95e3SAndreas Gohr 614c3a95e3SAndreas Gohr return $response->withBody(Stream::create($client->resp_body)); 624c3a95e3SAndreas Gohr } 634c3a95e3SAndreas Gohr 644c3a95e3SAndreas Gohr /** 654c3a95e3SAndreas Gohr * Convert PSR-7 headers to the associative format expected by DokuHTTPClient. 664c3a95e3SAndreas Gohr * 674c3a95e3SAndreas Gohr * @param RequestInterface $request Original request from mPDF. 684c3a95e3SAndreas Gohr * @return array<string,string> 694c3a95e3SAndreas Gohr */ 704c3a95e3SAndreas Gohr private function buildHeaders(RequestInterface $request): array 714c3a95e3SAndreas Gohr { 724c3a95e3SAndreas Gohr $headers = []; 734c3a95e3SAndreas Gohr foreach ($request->getHeaders() as $name => $values) { 744c3a95e3SAndreas Gohr $headers[$name] = implode(', ', $values); 754c3a95e3SAndreas Gohr } 764c3a95e3SAndreas Gohr 774c3a95e3SAndreas Gohr return $headers; 784c3a95e3SAndreas Gohr } 794c3a95e3SAndreas Gohr} 80