3434import java .util .Map ;
3535
3636import io .undertow .UndertowMessages ;
37+ import io .undertow .util .FileUtils ;
3738import io .undertow .util .HeaderMap ;
3839
3940/**
@@ -102,11 +103,15 @@ public void add(String name, String value, String charset, final HeaderMap heade
102103 }
103104
104105 public void add (String name , Path value , String fileName , final HeaderMap headers ) {
106+ add (name , value , fileName , headers , false , null );
107+ }
108+
109+ public void add (String name , Path value , String fileName , final HeaderMap headers , boolean bigField , String charset ) {
105110 Deque <FormValue > values = this .values .get (name );
106111 if (values == null ) {
107112 this .values .put (name , values = new ArrayDeque <>(1 ));
108113 }
109- values .add (new FormValueImpl (value , fileName , headers ));
114+ values .add (new FormValueImpl (value , fileName , headers , bigField , charset ));
110115 if (values .size () > maxValues ) {
111116 throw new RuntimeException (UndertowMessages .MESSAGES .tooManyParameters (maxValues ));
112117 }
@@ -211,6 +216,16 @@ public interface FormValue {
211216 * @return The headers that were present in the multipart request, or null if this was not a multipart request
212217 */
213218 HeaderMap getHeaders ();
219+
220+ /**
221+ * @return true if size of the FormValue comes from a multipart request exceeds the fieldSizeThreshold of
222+ * {@link MultiPartParserDefinition} without filename specified.
223+ *
224+ * A big field is stored in disk, it is a file based FileItem.
225+ *
226+ * getValue() returns getCharset() decoded string from the file if it is true.
227+ */
228+ boolean isBigField ();
214229 }
215230
216231 public static class FileItem {
@@ -284,13 +299,15 @@ static class FormValueImpl implements FormValue {
284299 private final HeaderMap headers ;
285300 private final FileItem fileItem ;
286301 private final String charset ;
302+ private final boolean bigField ;
287303
288304 FormValueImpl (String value , HeaderMap headers ) {
289305 this .value = value ;
290306 this .headers = headers ;
291307 this .fileName = null ;
292308 this .fileItem = null ;
293309 this .charset = null ;
310+ this .bigField = false ;
294311 }
295312
296313 FormValueImpl (String value , String charset , HeaderMap headers ) {
@@ -299,14 +316,16 @@ static class FormValueImpl implements FormValue {
299316 this .headers = headers ;
300317 this .fileName = null ;
301318 this .fileItem = null ;
319+ this .bigField = false ;
302320 }
303321
304- FormValueImpl (Path file , final String fileName , HeaderMap headers ) {
322+ FormValueImpl (Path file , final String fileName , HeaderMap headers , boolean bigField , String charset ) {
305323 this .fileItem = new FileItem (file );
306324 this .headers = headers ;
307325 this .fileName = fileName ;
308326 this .value = null ;
309- this .charset = null ;
327+ this .charset = charset ;
328+ this .bigField = bigField ;
310329 }
311330
312331 FormValueImpl (byte [] data , String fileName , HeaderMap headers ) {
@@ -315,12 +334,20 @@ static class FormValueImpl implements FormValue {
315334 this .headers = headers ;
316335 this .value = null ;
317336 this .charset = null ;
337+ this .bigField = false ;
318338 }
319339
320340
321341 @ Override
322342 public String getValue () {
323343 if (value == null ) {
344+ if (bigField ) {
345+ try (InputStream inputStream = getFileItem ().getInputStream ()) {
346+ return FileUtils .readFile (inputStream , this .charset );
347+ } catch (IOException e ) {
348+ // ignore
349+ }
350+ }
324351 throw UndertowMessages .MESSAGES .formValueIsAFile ();
325352 }
326353 return value ;
@@ -360,6 +387,10 @@ public FileItem getFileItem() {
360387 return fileItem ;
361388 }
362389
390+ public boolean isBigField () {
391+ return bigField ;
392+ }
393+
363394 @ Override
364395 public boolean isFileItem () {
365396 return fileItem != null ;
0 commit comments