Commit | Line | Data |
---|---|---|
78f5d120 FT |
1 | package dolda.jsvc.util; |
2 | ||
7779099a | 3 | import dolda.jsvc.*; |
78f5d120 | 4 | import java.util.*; |
7779099a | 5 | import java.io.*; |
78f5d120 FT |
6 | |
7 | public class Misc { | |
efa9722b | 8 | public static final java.nio.charset.Charset utf8 = java.nio.charset.Charset.forName("UTF-8"); |
13e578b1 | 9 | public static final java.nio.charset.Charset ascii = java.nio.charset.Charset.forName("US-ASCII"); |
78f5d120 FT |
10 | private static Map<Integer, String> stext = new HashMap<Integer, String>(); |
11 | ||
12 | static { | |
13 | stext.put(200, "OK"); | |
c25c3aad FT |
14 | stext.put(300, "Multiple Choices"); |
15 | stext.put(301, "Permanently Moved"); | |
16 | stext.put(302, "Temporarily Moved"); | |
17 | stext.put(303, "See Other"); | |
7779099a | 18 | stext.put(304, "Not Modified"); |
c25c3aad FT |
19 | stext.put(400, "Bad Request"); |
20 | stext.put(401, "Authentication Required"); | |
21 | stext.put(403, "Access Forbidden"); | |
22 | stext.put(404, "Resource Not Found"); | |
23 | stext.put(500, "Server Error"); | |
78f5d120 FT |
24 | } |
25 | ||
26 | public static String statustext(int status) { | |
27 | String text; | |
28 | if((text = stext.get(status)) != null) | |
29 | return(text); | |
c25c3aad FT |
30 | return("Server Flimsiness"); |
31 | } | |
32 | ||
33 | public static String stripslashes(String p, boolean beg, boolean end) { | |
34 | while(end && (p.length() > 0) && (p.charAt(p.length() - 1) == '/')) | |
35 | p = p.substring(0, p.length() - 1); | |
36 | while(beg && (p.length() > 0) && (p.charAt(0) == '/')) | |
37 | p = p.substring(1); | |
38 | return(p); | |
78f5d120 | 39 | } |
7779099a | 40 | |
ca045757 FT |
41 | static byte[] readall(InputStream in) throws IOException { |
42 | byte[] buf = new byte[4096]; | |
43 | int off = 0; | |
44 | while(true) { | |
45 | if(off == buf.length) { | |
46 | byte[] n = new byte[buf.length * 2]; | |
47 | System.arraycopy(buf, 0, n, 0, buf.length); | |
48 | buf = n; | |
49 | } | |
50 | int ret = in.read(buf, off, buf.length - off); | |
51 | if(ret < 0) { | |
52 | byte[] n = new byte[off]; | |
53 | System.arraycopy(buf, 0, n, 0, off); | |
54 | return(n); | |
55 | } | |
56 | off += ret; | |
57 | } | |
58 | } | |
59 | ||
7779099a FT |
60 | public static void cpstream(InputStream in, OutputStream out) throws IOException { |
61 | byte[] buf = new byte[4096]; | |
62 | while(true) { | |
63 | int ret = in.read(buf, 0, buf.length); | |
64 | if(ret < 0) | |
65 | return; | |
66 | out.write(buf, 0, ret); | |
67 | } | |
68 | } | |
69 | ||
70 | public static Responder stdroot(Responder root) { | |
71 | Responder ret = root; | |
72 | ret = new Rehandler(ret); | |
73 | ret = new ErrorHandler(ret); | |
74 | return(ret); | |
75 | } | |
efa9722b FT |
76 | |
77 | public static int hex2int(char digit) { | |
78 | if((digit >= '0') && (digit <= '9')) | |
79 | return(digit - '0'); | |
80 | if((digit >= 'a') && (digit <= 'f')) | |
81 | return(digit - 'a' + 10); | |
82 | if((digit >= 'A') && (digit <= 'F')) | |
83 | return(digit - 'A' + 10); | |
84 | throw(new NumberFormatException("Invalid hex digit " + digit)); | |
85 | } | |
86 | ||
87 | public static char int2hex(int nibble, boolean upper) { | |
88 | if((nibble >= 0) && (nibble <= 9)) | |
89 | return((char)('0' + nibble)); | |
90 | if((nibble >= 10) && (nibble <= 15)) | |
91 | return((char)((upper?'A':'a') + nibble - 10)); | |
92 | throw(new NumberFormatException("Invalid hex nibble " + nibble)); | |
93 | } | |
94 | ||
95 | public static String htmlq(String in) { | |
96 | StringBuilder buf = new StringBuilder(); | |
97 | for(int i = 0; i < in.length(); i++) { | |
98 | char c = in.charAt(i); | |
99 | if(c == '&') | |
100 | buf.append("&"); | |
101 | else if(c == '<') | |
102 | buf.append("<"); | |
103 | else if(c == '>') | |
104 | buf.append(">"); | |
105 | else | |
106 | buf.append(c); | |
107 | } | |
108 | return(buf.toString()); | |
109 | } | |
110 | ||
111 | public static String urlq(String in) { | |
112 | byte[] bytes = in.getBytes(utf8); | |
113 | StringBuilder buf = new StringBuilder(); | |
114 | for(int i = 0; i < bytes.length; i++) { | |
115 | byte b = bytes[i]; | |
116 | if((b < 32) || (b == ' ') || (b == '&') || (b == '?') || (b == '/') || (b == '=') || (b == '#') || (b == '%') || (b == '+') || (b >= 128)) { | |
117 | buf.append('%'); | |
118 | buf.append(int2hex((b & 0xf0) >> 4, true)); | |
119 | buf.append(int2hex(b & 0x0f, true)); | |
120 | } else { | |
121 | buf.append((char)b); | |
122 | } | |
123 | } | |
124 | return(buf.toString()); | |
125 | } | |
5cdd61df FT |
126 | |
127 | public static boolean boolval(String val) { | |
128 | val = val.trim().toLowerCase(); | |
129 | if(val.equals("1") || val.equals("on") || val.equals("true") || val.equals("yes") || val.equals("\u22a4")) | |
130 | return(true); | |
131 | if(val.equals("0") || val.equals("off") || val.equals("false") || val.equals("no") || val.equals("\u22a5")) | |
132 | return(false); | |
133 | throw(new IllegalArgumentException("value not recognized as boolean: " + val)); | |
134 | } | |
5e8bab52 FT |
135 | |
136 | public static void eatws(PushbackReader in) throws IOException { | |
137 | int c; | |
138 | do { | |
139 | c = in.read(); | |
140 | if(c < 0) | |
141 | return; | |
142 | } while(Character.isWhitespace(c)); | |
143 | in.unread(c); | |
144 | } | |
78f5d120 | 145 | } |