Passed
Pull Request — master (#255)
by Christopher
03:17
created

ChangeSetParser::parse_headers()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 5
nop 3
dl 0
loc 13
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace POData\BatchProcessor;
6
7
use Exception;
8
use POData\BaseService;
9
use POData\Common\ODataException;
10
use POData\Common\UrlFormatException;
11
use POData\OperationContext\HTTPRequestMethod;
12
use POData\OperationContext\ServiceHost;
13
use POData\OperationContext\Web\IncomingRequest;
14
use POData\OperationContext\Web\WebOperationContext;
15
use POData\StringUtility;
16
17
/**
18
 * Class ChangeSetParser.
19
 * @package POData\BatchProcessor
20
 */
21
class ChangeSetParser implements IBatchParser
22
{
23
    protected $data;
24
    protected $changeSetBoundary;
25
    /**
26
     * @var WebOperationContext[]
27
     */
28
    protected $rawRequests = [];
29
    protected $service;
30
    protected $contentIDToLocationLookup = [];
31
32
    /**
33
     * ChangeSetParser constructor.
34
     * @param BaseService $service
35
     * @param $body
36
     */
37
    public function __construct(BaseService $service, $body)
38
    {
39
        $this->service            = $service;
40
        $this->data               = trim(str_replace("\r", '', $body)); // removes windows specific character
41
    }
42
43
    /**
44
     * @return mixed
45
     */
46
    public function getBoundary()
47
    {
48
        return $this->changeSetBoundary;
49
    }
50
51
    /**
52
     * @throws ODataException
53
     * @throws UrlFormatException
54
     * @throws Exception
55
     */
56
    public function process()
57
    {
58
        $raw = $this->getRawRequests();
59
        foreach ($raw as $contentID => &$workingObject) {
60
            foreach ($this->contentIDToLocationLookup as $lookupID => $location) {
61
                if (0 > $lookupID) {
62
                    continue;
63
                }
64
                $workingObject->incomingRequest()->applyContentID($lookupID, $location);
65
            }
66
67
            $this->processSubRequest($workingObject);
68
            if (HTTPRequestMethod::GET() != $workingObject->incomingRequest()->getMethod() &&
69
                strpos($workingObject->incomingRequest()->getRawUrl(), '/$links/') === false) {
70
                if (null === $workingObject->outgoingResponse()->getHeaders()['Location']) {
71
                    $msg = 'Location header not set in subrequest response for ' . $workingObject->incomingRequest()->getMethod()
72
                        . ' request url ' . $workingObject->incomingRequest()->getRawUrl();
73
                    throw new Exception($msg);
74
                }
75
                $this->contentIDToLocationLookup[$contentID] = $workingObject->outgoingResponse()->getHeaders()['Location'];
76
            }
77
        }
78
    }
79
80
    /**
81
     * @return WebOperationContext[]
82
     */
83
    public function getRawRequests()
84
    {
85
        return $this->rawRequests;
86
    }
87
88
    /**
89
     * @param $newContext
90
     * @throws ODataException
91
     * @throws UrlFormatException
92
     */
93
    protected function processSubRequest(&$newContext)
94
    {
95
        $newHost    = new ServiceHost($newContext);
96
        $oldHost    = $this->getService()->getHost();
97
        $this->getService()->setHost($newHost);
98
        $this->getService()->handleRequest();
99
        $this->getService()->setHost($oldHost);
100
    }
101
102
    /**
103
     * @return BaseService
104
     */
105
    public function getService()
106
    {
107
        return $this->service;
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function getResponse()
114
    {
115
        $ODataEOL = $this->getService()->getConfiguration()->getLineEndings();
116
117
        $response = '';
118
        $splitter = false === $this->changeSetBoundary ?
119
            '' :
120
            '--' . $this->changeSetBoundary . $ODataEOL;
121
        $raw = $this->getRawRequests();
122
        foreach ($raw as $contentID => &$workingObject) {
123
            $headers = $workingObject->outgoingResponse()->getHeaders();
124
            $response .= $splitter;
125
126
            $response .= 'Content-Type: application/http' . $ODataEOL;
127
            $response .= 'Content-Transfer-Encoding: binary' . $ODataEOL;
128
            $response .= $ODataEOL;
129
            $response .= 'HTTP/1.1 ' . $headers['Status'] . $ODataEOL;
130
            $response .= 'Content-ID: ' . $contentID . $ODataEOL;
131
132
            foreach ($headers as $headerName => $headerValue) {
133
                if (null !== $headerValue) {
134
                    $response .= $headerName . ': ' . $headerValue . $ODataEOL;
135
                }
136
            }
137
            $response .= $ODataEOL;
138
            $response .= $workingObject->outgoingResponse()->getStream();
139
        }
140
        $response .= trim($splitter);
141
        $response .= false === $this->changeSetBoundary ?
142
            $ODataEOL :
143
            '--' . $ODataEOL;
144
        $response = 'Content-Length: ' .
145
            strlen($response) .
146
            $ODataEOL .
147
            $ODataEOL .
148
            $response;
149
        $response = false === $this->changeSetBoundary ?
150
            $response :
151
            'Content-Type: multipart/mixed; boundary=' .
152
            $this->changeSetBoundary .
153
            $ODataEOL .
154
            $response;
155
        return $response;
156
    }
157
158
    public function handleData()
159
    {
160
        list($headerBlock, $contentBlock) = explode("\n\n", $this->getData(), 2);
161
        $headers                          = self::parse_headers($headerBlock);
162
        $this->changeSetBoundary          = $headers['Content-Type']['boundary'];
163
        $matches                          = array_filter(explode('--' . $this->changeSetBoundary, $contentBlock));
164
        $contentIDinit                    = -1;
165
        foreach ($matches as $match) {
166
            if ('--' === trim($match)) {
167
                continue;
168
            }
169
            $request   = new IncomingChangeSetRequest($match);
170
            $contentID = $request->getContentId() ?? $contentIDinit;
171
            $this->rawRequests[$contentID] = new WebOperationContext($request);
172
            if ($contentIDinit == $contentID) {
173
                $contentIDinit--;
174
            }
175
            continue;
176
        }
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    public function getData()
183
    {
184
        return $this->data;
185
    }
186
    private static function parse_headers($headers, $splitter = "\n", $assignmentChar = ':')
187
    {
188
        $results = [];
189
        foreach (array_filter(explode($splitter, trim($headers))) as $line) {
190
            list($key, $value) = strpos($line, $assignmentChar) !== false ? explode($assignmentChar, $line, 2) : ['default', $line];
191
            $key               = trim($key);
192
            $value             = trim($value);
193
            if (strpos($value, ';') !== false) {
194
                $value = self::parse_headers($value, ';', '=');
195
            }
196
            $results[$key] = $value;
197
        }
198
        return $results;
199
    }
200
}
201