99use RoadRunner \HTTP \DTO \V1BETA1 \HeaderValue ;
1010use RoadRunner \HTTP \DTO \V1BETA1 \Request as RequestProto ;
1111use RoadRunner \HTTP \DTO \V1BETA1 \Response ;
12+ use Spiral \Goridge \Frame ;
1213use Spiral \RoadRunner \Http \Exception \StreamStoppedException ;
1314use Spiral \RoadRunner \Message \Command \StreamStop ;
1415use Spiral \RoadRunner \Payload ;
3839 */
3940class HttpWorker implements HttpWorkerInterface
4041{
41- private static ?bool $ isProto = null ;
42+ private static ?int $ codec = null ;
4243
4344 public function __construct (
4445 private readonly WorkerInterface $ worker ,
@@ -62,7 +63,11 @@ public function waitRequest(): ?Request
6263 return null ;
6364 }
6465
65- if ($ this ->isProtoPayload ($ payload )) {
66+ if (static ::$ codec === null ) {
67+ static ::$ codec = json_validate ($ payload ->header ) ? Frame::CODEC_JSON : Frame::CODEC_PROTO ;
68+ }
69+
70+ if (static ::$ codec === Frame::CODEC_PROTO ) {
6671 $ message = new RequestProto ();
6772 $ message ->mergeFromString ($ payload ->header );
6873
@@ -90,7 +95,8 @@ public function respond(int $status, string|Generator $body = '', array $headers
9095 return ;
9196 }
9297
93- $ this ->worker ->respond ($ this ->createRespondPayload ($ status , $ body , $ headers , $ endOfStream ));
98+ /** @psalm-suppress TooManyArguments */
99+ $ this ->worker ->respond ($ this ->createRespondPayload ($ status , $ body , $ headers , $ endOfStream ), static ::$ codec );
94100 }
95101
96102 /**
@@ -110,7 +116,11 @@ private function respondStream(int $status, Generator $body, array $headers = []
110116 // We don't need to send an empty frame if the stream is not ended
111117 return ;
112118 }
113- $ worker ->respond ($ this ->createRespondPayload ($ status , $ content , $ headers , $ endOfStream ));
119+ /** @psalm-suppress TooManyArguments */
120+ $ worker ->respond (
121+ $ this ->createRespondPayload ($ status , $ content , $ headers , $ endOfStream ),
122+ static ::$ codec
123+ );
114124 break ;
115125 }
116126
@@ -124,8 +134,11 @@ private function respondStream(int $status, Generator $body, array $headers = []
124134 return ;
125135 }
126136
127- // Send a chunk of data
128- $ worker ->respond ($ this ->createRespondPayload ($ status , $ content , $ headers , false ));
137+ /**
138+ * Send a chunk of data
139+ * @psalm-suppress TooManyArguments
140+ */
141+ $ worker ->respond ($ this ->createRespondPayload ($ status , $ content , $ headers , false ), static ::$ codec );
129142
130143 try {
131144 $ body ->next ();
@@ -260,20 +273,11 @@ private function arrayToHeaderValue(array $headers = []): array
260273 */
261274 private function createRespondPayload (int $ status , string $ body , array $ headers = [], bool $ eos = true ): Payload
262275 {
263- $ head = static ::$ isProto
276+ $ head = static ::$ codec === Frame:: CODEC_PROTO
264277 ? (new Response (['status ' => $ status , 'headers ' => $ this ->arrayToHeaderValue ($ headers )]))
265278 ->serializeToString ()
266279 : \json_encode (['status ' => $ status , 'headers ' => $ headers ?: (object )[]], \JSON_THROW_ON_ERROR );
267280
268281 return new Payload (body: $ body , header: $ head , eos: $ eos );
269282 }
270-
271- private function isProtoPayload (Payload $ payload ): bool
272- {
273- if (static ::$ isProto === null ) {
274- static ::$ isProto = !json_validate ($ payload ->header );
275- }
276-
277- return static ::$ isProto ;
278- }
279283}
0 commit comments