From 05201c4e5a77fd2f32c83afe7d16238881ea57f7 Mon Sep 17 00:00:00 2001 From: Chris Lloyd Date: Fri, 12 Apr 2024 12:39:17 -0400 Subject: [PATCH] WIP --- src/Helper/Constants.php | 1 + src/PathSegment/Batch/Multipart.php | 24 +++++++++++++++++-- src/Transaction/MultipartDocument.php | 9 +++++++ ...est__test_bad_document_content_type__1.txt | 1 + .../BatchMultipartTest__test_batch__1.txt | 2 ++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Helper/Constants.php b/src/Helper/Constants.php index 8357dcd8d..4579640e6 100644 --- a/src/Helper/Constants.php +++ b/src/Helper/Constants.php @@ -13,6 +13,7 @@ class Constants public const cacheControl = 'cache-control'; public const charset = 'charset'; public const contentEncoding = 'content-encoding'; + public const contentId = 'content-id'; public const contentLanguage = 'content-language'; public const contentType = 'content-type'; public const date = 'date'; diff --git a/src/PathSegment/Batch/Multipart.php b/src/PathSegment/Batch/Multipart.php index 3211610b5..f4218f822 100644 --- a/src/PathSegment/Batch/Multipart.php +++ b/src/PathSegment/Batch/Multipart.php @@ -9,6 +9,7 @@ use Flat3\Lodata\Controller\Transaction; use Flat3\Lodata\Exception\Protocol\BadRequestException; use Flat3\Lodata\Exception\Protocol\ProtocolException; +use Flat3\Lodata\Helper\Constants; use Flat3\Lodata\Interfaces\ContextInterface; use Flat3\Lodata\Interfaces\ResourceInterface; use Flat3\Lodata\Interfaces\StreamInterface; @@ -79,7 +80,6 @@ public function emitStream(Transaction $transaction): void array_shift($this->boundaries); } else { - $transaction->sendOutput("content-type: application/http\r\n\r\n"); $requestTransaction = new Transaction(); $requestTransaction->initialize(new Request($document->toRequest())); @@ -92,6 +92,26 @@ public function emitStream(Transaction $transaction): void $response = $e->toResponse(); } + $responseHeaders = [ + Constants::contentType => ['application/http'], + ]; + + $requestHeaders = $document->getHeaders(); + + foreach ([Constants::contentId] as $key) { + if (array_key_exists($key, $requestHeaders)) { + $responseHeaders[$key] = $requestHeaders[$key]; + } + } + + foreach ($responseHeaders as $key => $values) { + foreach ($values as $value) { + $transaction->sendOutput(strtolower((string) $key).': '.$value."\r\n"); + } + } + + $transaction->sendOutput("\r\n"); + $transaction->sendOutput(sprintf( "HTTP/%s %s %s\r\n", $response->getProtocolVersion(), @@ -101,7 +121,7 @@ public function emitStream(Transaction $transaction): void foreach ($this->getResponseHeaders($response) as $key => $values) { foreach ($values as $value) { - $transaction->sendOutput($key.': '.$value."\r\n"); + $transaction->sendOutput(strtolower($key).': '.$value."\r\n"); } } diff --git a/src/Transaction/MultipartDocument.php b/src/Transaction/MultipartDocument.php index 0088b1f88..1728bcbb8 100644 --- a/src/Transaction/MultipartDocument.php +++ b/src/Transaction/MultipartDocument.php @@ -63,6 +63,15 @@ public function setHeaders(array $headers): self return $this; } + /** + * Get the headers of this document + * @return array + */ + public function getHeaders(): array + { + return $this->headers; + } + /** * Set the body of this document * @param string $body diff --git a/tests/__snapshots__/Protocol/BatchMultipartTest__test_bad_document_content_type__1.txt b/tests/__snapshots__/Protocol/BatchMultipartTest__test_bad_document_content_type__1.txt index af806aa63..db0e9bb67 100644 --- a/tests/__snapshots__/Protocol/BatchMultipartTest__test_bad_document_content_type__1.txt +++ b/tests/__snapshots__/Protocol/BatchMultipartTest__test_bad_document_content_type__1.txt @@ -1,6 +1,7 @@ --00000000-0000-0000-0000-000000000002 content-type: application/http +content-id: 1 HTTP/1.1 406 Not Acceptable content-type: application/json diff --git a/tests/__snapshots__/Protocol/BatchMultipartTest__test_batch__1.txt b/tests/__snapshots__/Protocol/BatchMultipartTest__test_batch__1.txt index 044edc221..9bcf4cad7 100644 --- a/tests/__snapshots__/Protocol/BatchMultipartTest__test_batch__1.txt +++ b/tests/__snapshots__/Protocol/BatchMultipartTest__test_batch__1.txt @@ -13,6 +13,7 @@ content-type: multipart/mixed;boundary=00000000-0000-0000-0000-000000000004 --00000000-0000-0000-0000-000000000004 content-type: application/http +content-id: 1 HTTP/1.0 201 Created content-type: application/json;metadata=minimal @@ -22,6 +23,7 @@ etag: W/"9dcfebb4cce3bc22f0333343727cdc6ef96cc0d0026b89ca181a1c40cb250907" {"@context":"http://localhost/odata/$metadata#airports/$entity","id":5,"name":"One","code":"one","construction_date":null,"open_time":null,"sam_datetime":null,"review_score":null,"is_big":null,"country_id":null} --00000000-0000-0000-0000-000000000004 content-type: application/http +content-id: 2 HTTP/1.1 412 Precondition Failed content-type: application/json