4 import java.util.function.*;
7 import java.nio.channels.*;
9 public class FormData extends HashMap<String, String> {
10 public static final int MAX_LENGTH = 1 << 20;
12 private static int htoi(byte hex) {
13 if((hex >= '0') && (hex <= '9'))
15 if((hex >= 'A') && (hex <= 'F'))
16 return(hex - 'A' + 10);
17 if((hex >= 'a') && (hex <= 'f'))
18 return(hex - 'a' + 10);
22 private static ByteBuffer unquoteb(ByteBuffer part) {
23 ByteBuffer ret = ByteBuffer.allocate(part.remaining());
24 while(part.remaining() > 0) {
25 int b = part.get() & 0xff;
26 if((b == '%') && (part.remaining() >= 2)) {
27 int n1 = htoi(part.get()), n2 = htoi(part.get());
28 ret.put((byte)((n1 << 4) | n2));
37 private static String unquote(ByteBuffer part) {
38 ByteBuffer dec = unquoteb(part);
40 return(Http.UTF8.newDecoder().decode(dec.duplicate()).toString());
41 } catch(java.nio.charset.CharacterCodingException e) {
42 return(Http.LATIN1.decode(dec).toString());
46 private static String unquote(String part) {
47 if(part.indexOf('%') < 0)
49 return(unquote(Http.UTF8.encode(CharBuffer.wrap(part))));
52 public static void parse(Map<? super String, ? super String> buf, ByteBuffer data) {
53 int p = data.position(), p2, p3;
54 while(p < data.limit()) {
55 for(p2 = p; (p2 < data.limit()) && (data.get(p2) != '&'); p2++);
56 for(p3 = p; (p3 < p2) && (data.get(p3) != '='); p3++);
58 buf.put(unquote((ByteBuffer)data.duplicate().position(p).limit(p3)),
59 unquote((ByteBuffer)data.duplicate().position(p3 + 1).limit(p2)));
65 public static void parse(Map<? super String, ? super String> buf, String data) {
68 int p2 = data.indexOf('&', p);
69 String part = (p2 < 0) ? data.substring(p) : data.substring(p, p2);
70 int p3 = part.indexOf('=');
72 buf.put(unquote(part.substring(0, p3)), unquote(part.substring(p3 + 1)));
79 public static FormData read(Request req) {
80 FormData ret = new FormData();
81 String query = (String)req.env.get("QUERY_STRING");
84 if(req.ihead("Content-Type", "").equals("application/x-www-form-urlencoded")) {
86 String clen = req.ihead("Content-Length", null);
89 max = Math.min(max, Integer.parseInt(clen));
90 } catch(NumberFormatException e) {
93 ReadableByteChannel in = (ReadableByteChannel)req.env.get("jagi.input");
94 if(in instanceof SelectableChannel) {
96 ((SelectableChannel)in).configureBlocking(true);
97 } catch(IOException e) {
100 ByteBuffer buf = ByteBuffer.allocate(65536);
101 while(buf.position() < max) {
102 if(buf.remaining() == 0) {
103 ByteBuffer n = ByteBuffer.allocate(Math.min(buf.capacity() * 2, max));
109 int rv = in.read(buf);
112 } catch(IOException e) {
122 public static FormData get(Request req) {
123 FormData ret = (FormData)req.env.get(FormData.class);
125 req.env.put(FormData.class, ret = read(req));
129 static class Collector extends ByteArrayOutputStream {
130 final FormData form = new FormData();
133 Collector(Request req) {
135 req.env.put(FormData.class, this.form);
136 String query = (String)req.env.get("QUERY_STRING");
141 public void write(int b) {
142 if(count < MAX_LENGTH)
146 public void write(byte[] buf, int off, int len) {
147 len = Math.min(len, MAX_LENGTH - count);
149 super.write(buf, off, len);
152 public void close() {
153 parse(form, ByteBuffer.wrap(toByteArray()));
157 public static Map<Object, Object> feed(Request req, Handler next) {
158 Map<Object, Object> resp = new HashMap<>();
159 if(req.ihead("Content-Type", "").equals("application/x-www-form-urlencoded")) {
160 resp.put("jagi.status", "feed-input");
161 resp.put("jagi.next", (Function<Map<Object, Object>, Map<Object, Object>>)env -> Environment.dispatch(next, req));
162 resp.put("jagi.input-sink", new Collector(req));
165 resp.put("jagi.status", "chain");
166 resp.put("jagi.next", (Function<Map<Object, Object>, Map<Object, Object>>)env -> Environment.dispatch(next, req));