|
| 1 | +package com.retailsvc.http.internal; |
| 2 | + |
| 3 | +import com.retailsvc.http.spec.schema.Schema; |
| 4 | +import java.net.URLDecoder; |
| 5 | +import java.nio.charset.Charset; |
| 6 | +import java.nio.charset.IllegalCharsetNameException; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.nio.charset.UnsupportedCharsetException; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.LinkedHashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +/** Parses an {@code application/x-www-form-urlencoded} request body. */ |
| 15 | +public final class FormUrlEncodedParser { |
| 16 | + |
| 17 | + /** Parses the body to a {@code Map<String, Object>} ({@code String} or {@code List<String>}). */ |
| 18 | + public Map<String, Object> parse(byte[] body, String contentTypeHeader) { |
| 19 | + Charset charset = resolveCharset(contentTypeHeader); |
| 20 | + if (body.length == 0) { |
| 21 | + return new LinkedHashMap<>(); |
| 22 | + } |
| 23 | + String text = new String(body, charset); |
| 24 | + Map<String, Object> out = new LinkedHashMap<>(); |
| 25 | + for (String pair : text.split("&")) { |
| 26 | + if (pair.isEmpty()) { |
| 27 | + continue; |
| 28 | + } |
| 29 | + int eq = pair.indexOf('='); |
| 30 | + String rawKey = eq < 0 ? pair : pair.substring(0, eq); |
| 31 | + String rawValue = eq < 0 ? "" : pair.substring(eq + 1); |
| 32 | + String key = URLDecoder.decode(rawKey, charset); |
| 33 | + String value = URLDecoder.decode(rawValue, charset); |
| 34 | + addEntry(out, key, value); |
| 35 | + } |
| 36 | + return out; |
| 37 | + } |
| 38 | + |
| 39 | + private static void addEntry(Map<String, Object> out, String key, String value) { |
| 40 | + Object existing = out.get(key); |
| 41 | + if (existing == null) { |
| 42 | + out.put(key, value); |
| 43 | + return; |
| 44 | + } |
| 45 | + if (existing instanceof List<?> list) { |
| 46 | + @SuppressWarnings("unchecked") |
| 47 | + List<String> typed = (List<String>) list; |
| 48 | + typed.add(value); |
| 49 | + return; |
| 50 | + } |
| 51 | + List<String> list = new ArrayList<>(); |
| 52 | + list.add((String) existing); |
| 53 | + list.add(value); |
| 54 | + out.put(key, list); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns the parsed map after coercing field values against the given body schema. Coercion is |
| 59 | + * added in a subsequent task; for now this delegates to {@link #parse}. |
| 60 | + */ |
| 61 | + public Map<String, Object> parseAndCoerce(byte[] body, String contentTypeHeader, Schema schema) { |
| 62 | + return parse(body, contentTypeHeader); |
| 63 | + } |
| 64 | + |
| 65 | + private static Charset resolveCharset(String header) { |
| 66 | + return ContentTypeHeader.parameter(header, "charset") |
| 67 | + .map(FormUrlEncodedParser::safeCharset) |
| 68 | + .orElse(StandardCharsets.UTF_8); |
| 69 | + } |
| 70 | + |
| 71 | + private static Charset safeCharset(String name) { |
| 72 | + try { |
| 73 | + return Charset.forName(name); |
| 74 | + } catch (IllegalCharsetNameException | UnsupportedCharsetException _) { |
| 75 | + return StandardCharsets.UTF_8; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments