5 import java.util.function.*;
8 import java.nio.channels.*;
10 public class FormData extends HashMap<String, String> {
11 public static final int MAX_LENGTH = 1 << 20;
13 private static int htoi(byte hex) {
14 if((hex >= '0') && (hex <= '9'))
16 if((hex >= 'A') && (hex <= 'F'))
17 return(hex - 'A' + 10);
18 if((hex >= 'a') && (hex <= 'f'))
19 return(hex - 'a' + 10);
23 private static ByteBuffer unquoteb(ByteBuffer part) {
24 ByteBuffer ret = ByteBuffer.allocate(part.remaining());
25 while(part.remaining() > 0) {
26 int b = part.get() & 0xff;
27 if((b == '%') && (part.remaining() >= 2)) {
28 int n1 = htoi(part.get()), n2 = htoi(part.get());
29 ret.put((byte)((n1 << 4) | n2));
38 private static String unquote(ByteBuffer part) {
39 ByteBuffer dec = unquoteb(part);
41 return(Http.UTF8.newDecoder().decode(dec.duplicate()).toString());
42 } catch(java.nio.charset.CharacterCodingException e) {
43 return(Http.LATIN1.decode(dec).toString());
47 private static String unquote(String part) {
48 if(part.indexOf('%') < 0)
50 return(unquote(Http.UTF8.encode(CharBuffer.wrap(part))));
53 public static void parse(Map<? super String, ? super String> buf, ByteBuffer data) {
54 int p = data.position(), p2, p3;
55 while(p < data.limit()) {
56 for(p2 = p; (p2 < data.limit()) && (data.get(p2) != '&'); p2++);
57 for(p3 = p; (p3 < p2) && (data.get(p3) != '='); p3++);
59 buf.put(unquote((ByteBuffer)data.duplicate().position(p).limit(p3)),
60 unquote((ByteBuffer)data.duplicate().position(p3 + 1).limit(p2)));
66 public static void parse(Map<? super String, ? super String> buf, String data) {
69 int p2 = data.indexOf('&', p);
70 String part = (p2 < 0) ? data.substring(p) : data.substring(p, p2);
71 int p3 = part.indexOf('=');
73 buf.put(unquote(part.substring(0, p3)), unquote(part.substring(p3 + 1)));
80 public static FormData read(Request req) {
81 FormData ret = new FormData();
82 String query = (String)req.env.get("QUERY_STRING");
85 if(req.ihead("Content-Type", "").equals("application/x-www-form-urlencoded")) {
87 String clen = req.ihead("Content-Length", null);
90 max = Math.min(max, Integer.parseInt(clen));
91 } catch(NumberFormatException e) {
94 ReadableByteChannel in = (ReadableByteChannel)req.env.get("jagi.input");
95 if(in instanceof SelectableChannel) {
97 ((SelectableChannel)in).configureBlocking(true);
98 } catch(IOException e) {
101 ByteBuffer buf = ByteBuffer.allocate(65536);
102 while(buf.position() < max) {
103 if(buf.remaining() == 0) {
104 ByteBuffer n = ByteBuffer.allocate(Math.min(buf.capacity() * 2, max));
110 int rv = in.read(buf);
113 } catch(IOException e) {
123 public static FormData get(Request req) {
124 FormData ret = (FormData)req.env.get(FormData.class);
126 req.env.put(FormData.class, ret = read(req));
130 static class Collector extends ByteArrayOutputStream {
131 final FormData form = new FormData();
134 Collector(Request req) {
136 req.env.put(FormData.class, this.form);
137 String query = (String)req.env.get("QUERY_STRING");
142 public void write(int b) {
143 if(count < MAX_LENGTH)
147 public void write(byte[] buf, int off, int len) {
148 len = Math.min(len, MAX_LENGTH - count);
150 super.write(buf, off, len);
153 public void close() {
154 parse(form, ByteBuffer.wrap(toByteArray()));
158 public static Map<Object, Object> feed(Request req, Handler next) {
159 Map<Object, Object> resp = new HashMap<>();
160 if(req.ihead("Content-Type", "").equals("application/x-www-form-urlencoded")) {
161 resp.put("jagi.status", "feed-input");
162 resp.put("jagi.next", (Function<Map<Object, Object>, Map<Object, Object>>)env -> Dispatch.handle(next, req));
163 resp.put("jagi.input-sink", new Collector(req));
166 resp.put("jagi.status", "chain");
167 resp.put("jagi.next", (Function<Map<Object, Object>, Map<Object, Object>>)env -> Dispatch.handle(next, req));