annotate src/goodjava/webserver/Util.java @ 1797:ba43135bb98d

backticks docs
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 11 Feb 2024 18:52:53 -0700
parents 27efb1fcbcb5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
1 package goodjava.webserver;
1146
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.UnsupportedEncodingException;
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.net.URLEncoder;
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.util.Map;
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.util.List;
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.util.ArrayList;
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 final class Util {
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 static String urlEncode(String s) {
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 try {
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 return URLEncoder.encode(s,"UTF-8");
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 } catch(UnsupportedEncodingException e) {
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 throw new RuntimeException(e);
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 }
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 }
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
1147
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
20 static void add(Map<String,Object> map,String name,Object value) {
1146
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 Object current = map.get(name);
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 if( current == null ) {
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 map.put(name,value);
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 } else if( current instanceof List ) {
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 List list = (List)current;
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 list.add(value);
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 } else {
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 List list = new ArrayList();
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 list.add(current);
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 list.add(value);
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 map.put(name,list);
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 }
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 }
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
1266
05934fbf635a content-type "application/x-www-form-urlencoded; charset=utf-8"
Franklin Schmidt <fschmidt@gmail.com>
parents: 1212
diff changeset
35 static String toString(byte[] a,String charset) throws UnsupportedEncodingException {
05934fbf635a content-type "application/x-www-form-urlencoded; charset=utf-8"
Franklin Schmidt <fschmidt@gmail.com>
parents: 1212
diff changeset
36 if( charset != null )
05934fbf635a content-type "application/x-www-form-urlencoded; charset=utf-8"
Franklin Schmidt <fschmidt@gmail.com>
parents: 1212
diff changeset
37 return new String(a,charset);
1147
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
38 char[] ac = new char[a.length];
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
39 for( int i=0; i<a.length; i++ ) {
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
40 ac[i] = (char)a[i];
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
41 }
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
42 return new String(ac);
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
43 }
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
44
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
45 static byte[] toBytes(String s) {
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
46 char[] ac = s.toCharArray();
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
47 byte[] a = new byte[ac.length];
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
48 for( int i=0; i<ac.length; i++ ) {
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
49 a[i] = (byte)ac[i];
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
50 }
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
51 return a;
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
52 }
30d87b7d1d62 webserver - support multipart/form-data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1146
diff changeset
53
1146
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 private Util() {} // never
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 }