Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.75% covered (warning)
68.75%
55 / 80
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Rest
68.75% covered (warning)
68.75%
55 / 80
0.00% covered (danger)
0.00%
0 / 2
36.77
0.00% covered (danger)
0.00%
0 / 1
 request
80.88% covered (warning)
80.88%
55 / 68
0.00% covered (danger)
0.00%
0 / 1
21.52
 createMultipartData
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Message\Http
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Message\Http;
16
17use phpOMS\System\MimeType;
18
19/**
20 * Rest request class.
21 *
22 * @package phpOMS\Message\Http
23 * @license OMS License 2.0
24 * @link    https://jingga.app
25 * @since   1.0.0
26 */
27final class Rest
28{
29    /**
30     * Make request.
31     *
32     * @param HttpRequest $request Request
33     *
34     * @return HttpResponse Returns the request result
35     *
36     * @throws \Exception this exception is thrown if an internal curl_init error occurs
37     *
38     * @since 1.0.0
39     */
40    public static function request(HttpRequest $request) : HttpResponse
41    {
42        $curl = \curl_init();
43
44        if ($curl === false) {
45            throw new \Exception('Internal curl_init error.'); // @codeCoverageIgnore
46        }
47
48        \curl_setopt($curl, \CURLOPT_NOBODY, true);
49
50        // handle header
51        $requestHeaders = $request->header->get();
52        $headers        = [];
53
54        foreach ($requestHeaders as $key => $header) {
55            $headers[$key] = $key . ': ' . \implode('', $header);
56        }
57
58        \curl_setopt($curl, \CURLOPT_HTTPHEADER, $headers);
59        \curl_setopt($curl, \CURLOPT_HEADER, true);
60        \curl_setopt($curl, \CURLOPT_CONNECTTIMEOUT, 5);
61        \curl_setopt($curl, \CURLOPT_TIMEOUT, 30);
62
63        switch ($request->getMethod()) {
64            case RequestMethod::GET:
65                \curl_setopt($curl, \CURLOPT_HTTPGET, true);
66                break;
67            case RequestMethod::POST:
68                \curl_setopt($curl, \CURLOPT_CUSTOMREQUEST, 'POST');
69                break;
70            case RequestMethod::PUT:
71                \curl_setopt($curl, \CURLOPT_CUSTOMREQUEST, 'PUT');
72                break;
73            case RequestMethod::DELETE:
74                \curl_setopt($curl, \CURLOPT_CUSTOMREQUEST, 'DELETE');
75                break;
76        }
77
78        // handle none-get
79        if ($request->getMethod() !== RequestMethod::GET) {
80            \curl_setopt($curl, \CURLOPT_POST, 1);
81
82            // handle different content types
83            $contentType = $requestHeaders['content-type'] ?? [];
84            if (empty($contentType) || \in_array(MimeType::M_POST, $contentType)) {
85                /* @phpstan-ignore-next-line */
86                \curl_setopt($curl, \CURLOPT_POSTFIELDS, \http_build_query($request->data));
87            } elseif (\in_array(MimeType::M_JSON, $contentType)) {
88                \curl_setopt($curl, \CURLOPT_POSTFIELDS, \json_encode($request->data));
89            } elseif (\in_array(MimeType::M_MULT, $contentType)) {
90                $boundary = '----' . \uniqid();
91
92                /* @phpstan-ignore-next-line */
93                $data = self::createMultipartData($boundary, $request->data);
94
95                // @todo: Replace boundary/ with the correct boundary= in the future.
96                //        Currently this cannot be done due to a bug. If we do it now the server cannot correclty populate php://input
97                $headers['content-type']   = 'Content-Type: multipart/form-data; boundary/' . $boundary;
98                $headers['content-length'] = 'Content-Length: ' . \strlen($data);
99
100                \curl_setopt($curl, \CURLOPT_HTTPHEADER, $headers);
101                \curl_setopt($curl, \CURLOPT_POSTFIELDS, $data);
102            }
103        }
104
105        // handle user auth
106        if ($request->uri->user !== '') {
107            \curl_setopt($curl, \CURLOPT_HTTPAUTH, \CURLAUTH_BASIC);
108            \curl_setopt($curl, \CURLOPT_USERPWD, $request->uri->getUserInfo());
109        }
110
111        $cHeaderString = '';
112        $response      = new HttpResponse();
113
114        \curl_setopt($curl, \CURLOPT_HEADERFUNCTION,
115            function($curl, $header) use ($response, &$cHeaderString) {
116                $cHeaderString .= $header;
117
118                $length = \strlen($header);
119                $header = \explode(':', $header, 2);
120
121                if (\count($header) < 2) {
122                    $response->header->set('', $line = \trim($header[0]));
123
124                    if (\str_starts_with(\strtoupper($line), 'HTTP/')) {
125                        $statusCode               = \explode(' ', $line, 3);
126                        $response->header->status = (int) $statusCode[1];
127                    }
128
129                    return $length;
130                }
131
132                $name = \strtolower(\trim($header[0]));
133                $response->header->set($name, \trim($header[1]));
134
135                return $length;
136            }
137        );
138
139        \curl_setopt($curl, \CURLOPT_URL, $request->__toString());
140        \curl_setopt($curl, \CURLOPT_RETURNTRANSFER, 1);
141
142        $result = \curl_exec($curl);
143        $len    = \strlen($cHeaderString);
144
145        \curl_close($curl);
146
147        $raw = \substr(\is_bool($result) ? '' : $result, $len === false ? 0 : $len);
148        if (\stripos(\implode('', $response->header->get('content-type')), MimeType::M_JSON) !== false) {
149            $temp = \json_decode($raw, true);
150            if (!\is_array($temp)) {
151                $temp = [];
152            }
153
154            $response->setResponse($temp);
155        } else {
156            $response->set('', $raw);
157        }
158
159        return $response;
160    }
161
162    /**
163     * Create multipart data
164     *
165     * @param string $boundary Unique boundary id
166     * @param array  $fields   Data array (key value pair)
167     * @param array  $files    Files to upload
168     *
169     * @return string
170     *
171     * @since 1.0.0
172     */
173    private static function createMultipartData(string $boundary, array $fields = [], array $files = []) : string
174    {
175        $data  = '';
176        $delim = $boundary;
177
178        foreach ($fields as $name => $content) {
179            $data .= '--' . $delim . "\r\n"
180                . 'Content-Disposition: form-data; name="' . $name . "\"\r\n\r\n"
181                . $content . "\r\n";
182        }
183
184        foreach ($files as $name => $content) {
185            $data .= '--' . $delim . "\r\n"
186                . 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . "\"\r\n\r\n"
187                . 'Content-Transfer-Encoding: binary' . "\r\n"
188                . $content . "\r\n";
189        }
190
191        return $data . ('--' . $delim . "--\r\n");
192    }
193}