@@ -38,18 +38,45 @@ public static function fromSwoole(Request $swooleRequest): ServerRequestInterfac
3838 $ body = $ httpFactory ->createStream ((string ) $ swooleRequest ->rawContent ());
3939 $ protocol = isset ($ server ['server_protocol ' ]) ? str_replace ('HTTP/ ' , '' , $ server ['server_protocol ' ]) : '1.1 ' ;
4040 $ request = new ServerRequest ($ method , $ uri , $ headers , $ body , $ protocol , $ server );
41+
4142 return $ request ->withCookieParams ($ swooleRequest ->cookie ?? [])
4243 ->withQueryParams ($ swooleRequest ->get ?? [])
4344 ->withParsedBody (self ::normalizeParsedBody ($ swooleRequest ->post ?? [], $ request ))
4445 ->withUploadedFiles (self ::normalizeFiles ($ swooleRequest ->files ?? []));
4546 }
4647
48+ protected static function normalizeParsedBody (array $ data = [], RequestInterface $ request = null ): array
49+ {
50+ if (! $ request ) {
51+ return $ data ;
52+ }
53+
54+ $ rawContentType = $ request ->getHeaderLine ('content-type ' );
55+ if (($ pos = strpos ($ rawContentType , '; ' )) !== false ) {
56+ $ contentType = strtolower (substr ($ rawContentType , 0 , $ pos ));
57+ } else {
58+ $ contentType = strtolower ($ rawContentType );
59+ }
60+ switch ($ contentType ) {
61+ case 'application/json ' :
62+ case 'text/json ' :
63+ $ data = json_decode ((string ) $ request ->getBody (), true );
64+ break ;
65+ case 'application/xml ' :
66+ case 'text/xml ' :
67+ $ data = (array ) simplexml_load_string ((string ) $ request ->getBody ());
68+ break ;
69+ }
70+
71+ return $ data ;
72+ }
73+
4774 private static function getUriFromSwooleRequest (Request $ swooleRequest ): UriInterface
4875 {
4976 $ server = $ swooleRequest ->server ;
5077 $ header = $ swooleRequest ->header ;
5178 $ uri = new Uri ();
52- $ uri = $ uri ->withScheme (! empty ($ server ['https ' ]) && $ server [ ' https ' ] !== ' off ' ? 'https ' : 'http ' );
79+ $ uri = $ uri ->withScheme (! empty ($ server ['https ' ]) && ' off ' !== $ server [ ' https ' ] ? 'https ' : 'http ' );
5380
5481 $ hasPort = false ;
5582 if (isset ($ server ['http_host ' ])) {
@@ -109,32 +136,6 @@ private static function parseHost(string $httpHost): array
109136
110137 private static function getUriDefaultPort (UriInterface $ uri ): ?int
111138 {
112- return $ uri ->getScheme () === 'https ' ? 443 : ($ uri ->getScheme () === 'http ' ? 80 : null );
113- }
114-
115- protected static function normalizeParsedBody (array $ data = [], ?RequestInterface $ request = null ): array
116- {
117- if (! $ request ) {
118- return $ data ;
119- }
120-
121- $ rawContentType = $ request ->getHeaderLine ('content-type ' );
122- if (($ pos = strpos ($ rawContentType , '; ' )) !== false ) {
123- $ contentType = strtolower (substr ($ rawContentType , 0 , $ pos ));
124- } else {
125- $ contentType = strtolower ($ rawContentType );
126- }
127- switch ($ contentType ) {
128- case 'application/json ' :
129- case 'text/json ' :
130- $ data = json_decode ((string ) $ request ->getBody (), true );
131- break ;
132- case 'application/xml ' :
133- case 'text/xml ' :
134- $ data = (array ) simplexml_load_string ((string ) $ request ->getBody ());
135- break ;
136- }
137-
138- return $ data ;
139+ return 'https ' === $ uri ->getScheme () ? 443 : ('http ' === $ uri ->getScheme () ? 80 : null );
139140 }
140141}
0 commit comments