2222import java .nio .charset .Charset ;
2323import java .nio .charset .StandardCharsets ;
2424import java .util .ArrayList ;
25- import java .util .Collection ;
2625import java .util .Collections ;
2726import java .util .HashMap ;
2827import java .util .List ;
2928import java .util .Map ;
29+ import java .util .Objects ;
3030import java .util .Optional ;
3131import java .util .TreeMap ;
3232import java .util .concurrent .ExecutionException ;
33- import java .util .regex .Matcher ;
34- import java .util .regex .Pattern ;
3533import org .eclipse .jetty .http .HttpField ;
3634import org .eclipse .jetty .http .HttpFields ;
3735import org .eclipse .jetty .http .HttpHeader ;
36+ import org .eclipse .jetty .http .MimeTypes ;
3837import org .eclipse .jetty .http .MultiPart ;
3938import org .eclipse .jetty .http .MultiPart .Part ;
4039import org .eclipse .jetty .http .MultiPartFormData ;
@@ -149,7 +148,7 @@ public BufferedReader getReader() throws IOException {
149148 }
150149 inputStream = Content .Source .asInputStream (request );
151150 reader = new BufferedReader (new InputStreamReader (getInputStream (),
152- getCharacterEncoding (). orElse ( StandardCharsets .UTF_8 . name () )));
151+ Objects . requireNonNullElse ( Request . getCharset ( request ), StandardCharsets .UTF_8 )));
153152 }
154153 return reader ;
155154 }
@@ -169,9 +168,11 @@ static Map<String, List<String>> toStringListMap(HttpFields headers) {
169168
170169 private static class HttpPartImpl implements HttpPart {
171170 private final Part part ;
171+ private final String contentType ;
172172
173173 private HttpPartImpl (Part part ) {
174174 this .part = part ;
175+ contentType = part .getHeaders ().get (HttpHeader .CONTENT_TYPE );
175176 }
176177
177178 public String getName () {
@@ -185,7 +186,7 @@ public Optional<String> getFileName() {
185186
186187 @ Override
187188 public Optional <String > getContentType () {
188- return Optional .ofNullable (part . getHeaders (). get ( HttpHeader . CONTENT_TYPE ) );
189+ return Optional .ofNullable (contentType );
189190 }
190191
191192 @ Override
@@ -195,13 +196,7 @@ public long getContentLength() {
195196
196197 @ Override
197198 public Optional <String > getCharacterEncoding () {
198- String contentType = getContentType ().orElse (null );
199- if (contentType == null ) {
200- return Optional .empty ();
201- }
202- Pattern charsetPattern = Pattern .compile ("(?i).*;\\ s*charset\\ s*=([^;\\ s]*)\\ s*(;|$)" );
203- Matcher matcher = charsetPattern .matcher (contentType );
204- return matcher .matches () ? Optional .of (matcher .group (1 )) : Optional .empty ();
199+ return Optional .ofNullable (MimeTypes .getCharsetFromContentType (contentType ));
205200 }
206201
207202 @ Override
@@ -211,19 +206,17 @@ public InputStream getInputStream() throws IOException {
211206
212207 @ Override
213208 public BufferedReader getReader () throws IOException {
214- String encoding = getCharacterEncoding ().orElse ("utf-8" );
215- return new BufferedReader (new InputStreamReader (getInputStream (), encoding ));
209+ return new BufferedReader (
210+ new InputStreamReader (getInputStream (),
211+ Objects .requireNonNullElse (MimeTypes .DEFAULTS .getCharset (contentType ),
212+ StandardCharsets .UTF_8 )));
216213 }
217214
218215 @ Override
219216 public Map <String , List <String >> getHeaders () {
220217 return HttpRequestImpl .toStringListMap (part .getHeaders ());
221218 }
222219
223- private static <T > List <T > list (Collection <T > collection ) {
224- return (collection instanceof List <?>) ? (List <T >) collection : new ArrayList <>(collection );
225- }
226-
227220 @ Override
228221 public String toString () {
229222 return "%s{%s}" .formatted (super .toString (), part );
0 commit comments