@@ -93,36 +93,49 @@ public static function convertResponse(ResponseInterface $response): HttpRespons
93
93
return new HttpResponse ($ body , $ response ->getHeaders (), $ response ->getStatusCode ());
94
94
}
95
95
96
+ /**
97
+ * @return array{0: array<string, UploadedFile>, 1: array<string, mixed>|null}
98
+ */
96
99
private static function parseBodyAndUploadedFiles (HttpRequestEvent $ event ): array
97
100
{
98
- $ bodyString = $ event ->getBody ();
99
- $ files = [];
100
- $ parsedBody = null ;
101
101
$ contentType = $ event ->getContentType ();
102
- if ($ contentType !== null && $ event ->getMethod () === 'POST ' ) {
103
- if (str_starts_with ($ contentType , 'application/x-www-form-urlencoded ' )) {
104
- parse_str ($ bodyString , $ parsedBody );
105
- } else {
106
- $ document = new Part ("Content-type: $ contentType \r\n\r\n" . $ bodyString );
107
- if ($ document ->isMultiPart ()) {
108
- $ parsedBody = [];
109
- foreach ($ document ->getParts () as $ part ) {
110
- if ($ part ->isFile ()) {
102
+ if ($ contentType === null || $ event ->getMethod () !== 'POST ' ) {
103
+ return [[], null ];
104
+ }
105
+
106
+ if (str_starts_with ($ contentType , 'application/x-www-form-urlencoded ' )) {
107
+ $ parsedBody = [];
108
+ parse_str ($ event ->getBody (), $ parsedBody );
109
+ return [[], $ parsedBody ];
110
+ }
111
+
112
+ // Parse the body as multipart/form-data
113
+ $ document = new Part ("Content-type: $ contentType \r\n\r\n" . $ event ->getBody ());
114
+ if (!$ document ->isMultiPart ()) {
115
+ return [[], null ];
116
+ }
117
+ $ files = [];
118
+ $ queryString = '' ;
119
+ foreach ($ document ->getParts () as $ part ) {
120
+ if ($ part ->isFile ()) {
111
121
$ tmpPath = tempnam (sys_get_temp_dir (), self ::UPLOADED_FILES_PREFIX );
112
- if ($ tmpPath === false ) {
113
- throw new RuntimeException ('Unable to create a temporary directory ' );
114
- }
115
- file_put_contents ($ tmpPath , $ part ->getBody ());
116
- $ file = new UploadedFile ($ tmpPath , filesize ($ tmpPath ), UPLOAD_ERR_OK , $ part ->getFileName (), $ part ->getMimeType ());
117
-
118
- self ::parseKeyAndInsertValueInArray ($ files , $ part ->getName (), $ file );
119
- } else {
120
- self ::parseKeyAndInsertValueInArray ($ parsedBody , $ part ->getName (), $ part ->getBody ());
121
- }
122
- }
122
+ if ($ tmpPath === false ) {
123
+ throw new RuntimeException ('Unable to create a temporary directory ' );
123
124
}
125
+ file_put_contents ($ tmpPath , $ part ->getBody ());
126
+ $ file = new UploadedFile ($ tmpPath , filesize ($ tmpPath ), UPLOAD_ERR_OK , $ part ->getFileName (), $ part ->getMimeType ());
127
+ self ::parseKeyAndInsertValueInArray ($ files , $ part ->getName (), $ file );
128
+ } else {
129
+ // Temporarily store as a query string so that we can use PHP's native parse_str function to parse keys
130
+ $ queryString .= urlencode ($ part ->getName ()) . '= ' . urlencode ($ part ->getBody ()) . '& ' ;
124
131
}
125
132
}
133
+ if ($ queryString !== '' ) {
134
+ $ parsedBody = [];
135
+ parse_str ($ queryString , $ parsedBody );
136
+ } else {
137
+ $ parsedBody = null ;
138
+ }
126
139
return [$ files , $ parsedBody ];
127
140
}
128
141
0 commit comments