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"); |
78f5d120 FT |
9 | private static Map<Integer, String> stext = new HashMap<Integer, String>(); |
10 | ||
11 | static { | |
12 | stext.put(200, "OK"); | |
c25c3aad FT |
13 | stext.put(300, "Multiple Choices"); |
14 | stext.put(301, "Permanently Moved"); | |
15 | stext.put(302, "Temporarily Moved"); | |
16 | stext.put(303, "See Other"); | |
7779099a | 17 | stext.put(304, "Not Modified"); |
c25c3aad FT |
18 | stext.put(400, "Bad Request"); |
19 | stext.put(401, "Authentication Required"); | |
20 | stext.put(403, "Access Forbidden"); | |
21 | stext.put(404, "Resource Not Found"); | |
22 | stext.put(500, "Server Error"); | |
78f5d120 FT |
23 | } |
24 | ||
25 | public static String statustext(int status) { | |
26 | String text; | |
27 | if((text = stext.get(status)) != null) | |
28 | return(text); | |
c25c3aad FT |
29 | return("Server Flimsiness"); |
30 | } | |
31 | ||
32 | public static String stripslashes(String p, boolean beg, boolean end) { | |
33 | while(end && (p.length() > 0) && (p.charAt(p.length() - 1) == '/')) | |
34 | p = p.substring(0, p.length() - 1); | |
35 | while(beg && (p.length() > 0) && (p.charAt(0) == '/')) | |
36 | p = p.substring(1); | |
37 | return(p); | |
78f5d120 | 38 | } |
7779099a FT |
39 | |
40 | public static void cpstream(InputStream in, OutputStream out) throws IOException { | |
41 | byte[] buf = new byte[4096]; | |
42 | while(true) { | |
43 | int ret = in.read(buf, 0, buf.length); | |
44 | if(ret < 0) | |
45 | return; | |
46 | out.write(buf, 0, ret); | |
47 | } | |
48 | } | |
49 | ||
50 | public static Responder stdroot(Responder root) { | |
51 | Responder ret = root; | |
52 | ret = new Rehandler(ret); | |
53 | ret = new ErrorHandler(ret); | |
54 | return(ret); | |
55 | } | |
efa9722b FT |
56 | |
57 | public static int hex2int(char digit) { | |
58 | if((digit >= '0') && (digit <= '9')) | |
59 | return(digit - '0'); | |
60 | if((digit >= 'a') && (digit <= 'f')) | |
61 | return(digit - 'a' + 10); | |
62 | if((digit >= 'A') && (digit <= 'F')) | |
63 | return(digit - 'A' + 10); | |
64 | throw(new NumberFormatException("Invalid hex digit " + digit)); | |
65 | } | |
66 | ||
67 | public static char int2hex(int nibble, boolean upper) { | |
68 | if((nibble >= 0) && (nibble <= 9)) | |
69 | return((char)('0' + nibble)); | |
70 | if((nibble >= 10) && (nibble <= 15)) | |
71 | return((char)((upper?'A':'a') + nibble - 10)); | |
72 | throw(new NumberFormatException("Invalid hex nibble " + nibble)); | |
73 | } | |
74 | ||
75 | public static String htmlq(String in) { | |
76 | StringBuilder buf = new StringBuilder(); | |
77 | for(int i = 0; i < in.length(); i++) { | |
78 | char c = in.charAt(i); | |
79 | if(c == '&') | |
80 | buf.append("&"); | |
81 | else if(c == '<') | |
82 | buf.append("<"); | |
83 | else if(c == '>') | |
84 | buf.append(">"); | |
85 | else | |
86 | buf.append(c); | |
87 | } | |
88 | return(buf.toString()); | |
89 | } | |
90 | ||
91 | public static String urlq(String in) { | |
92 | byte[] bytes = in.getBytes(utf8); | |
93 | StringBuilder buf = new StringBuilder(); | |
94 | for(int i = 0; i < bytes.length; i++) { | |
95 | byte b = bytes[i]; | |
96 | if((b < 32) || (b == ' ') || (b == '&') || (b == '?') || (b == '/') || (b == '=') || (b == '#') || (b == '%') || (b == '+') || (b >= 128)) { | |
97 | buf.append('%'); | |
98 | buf.append(int2hex((b & 0xf0) >> 4, true)); | |
99 | buf.append(int2hex(b & 0x0f, true)); | |
100 | } else { | |
101 | buf.append((char)b); | |
102 | } | |
103 | } | |
104 | return(buf.toString()); | |
105 | } | |
78f5d120 | 106 | } |