| 1 | package dolda.jsvc.util; |
| 2 | |
| 3 | import dolda.jsvc.*; |
| 4 | import java.util.*; |
| 5 | import java.io.*; |
| 6 | |
| 7 | public class Misc { |
| 8 | public static final java.nio.charset.Charset utf8 = java.nio.charset.Charset.forName("UTF-8"); |
| 9 | private static Map<Integer, String> stext = new HashMap<Integer, String>(); |
| 10 | |
| 11 | static { |
| 12 | stext.put(200, "OK"); |
| 13 | stext.put(300, "Multiple Choices"); |
| 14 | stext.put(301, "Permanently Moved"); |
| 15 | stext.put(302, "Temporarily Moved"); |
| 16 | stext.put(303, "See Other"); |
| 17 | stext.put(304, "Not Modified"); |
| 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"); |
| 23 | } |
| 24 | |
| 25 | public static String statustext(int status) { |
| 26 | String text; |
| 27 | if((text = stext.get(status)) != null) |
| 28 | return(text); |
| 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); |
| 38 | } |
| 39 | |
| 40 | static byte[] readall(InputStream in) throws IOException { |
| 41 | byte[] buf = new byte[4096]; |
| 42 | int off = 0; |
| 43 | while(true) { |
| 44 | if(off == buf.length) { |
| 45 | byte[] n = new byte[buf.length * 2]; |
| 46 | System.arraycopy(buf, 0, n, 0, buf.length); |
| 47 | buf = n; |
| 48 | } |
| 49 | int ret = in.read(buf, off, buf.length - off); |
| 50 | if(ret < 0) { |
| 51 | byte[] n = new byte[off]; |
| 52 | System.arraycopy(buf, 0, n, 0, off); |
| 53 | return(n); |
| 54 | } |
| 55 | off += ret; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public static void cpstream(InputStream in, OutputStream out) throws IOException { |
| 60 | byte[] buf = new byte[4096]; |
| 61 | while(true) { |
| 62 | int ret = in.read(buf, 0, buf.length); |
| 63 | if(ret < 0) |
| 64 | return; |
| 65 | out.write(buf, 0, ret); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public static Responder stdroot(Responder root) { |
| 70 | Responder ret = root; |
| 71 | ret = new Rehandler(ret); |
| 72 | ret = new ErrorHandler(ret); |
| 73 | return(ret); |
| 74 | } |
| 75 | |
| 76 | public static int hex2int(char digit) { |
| 77 | if((digit >= '0') && (digit <= '9')) |
| 78 | return(digit - '0'); |
| 79 | if((digit >= 'a') && (digit <= 'f')) |
| 80 | return(digit - 'a' + 10); |
| 81 | if((digit >= 'A') && (digit <= 'F')) |
| 82 | return(digit - 'A' + 10); |
| 83 | throw(new NumberFormatException("Invalid hex digit " + digit)); |
| 84 | } |
| 85 | |
| 86 | public static char int2hex(int nibble, boolean upper) { |
| 87 | if((nibble >= 0) && (nibble <= 9)) |
| 88 | return((char)('0' + nibble)); |
| 89 | if((nibble >= 10) && (nibble <= 15)) |
| 90 | return((char)((upper?'A':'a') + nibble - 10)); |
| 91 | throw(new NumberFormatException("Invalid hex nibble " + nibble)); |
| 92 | } |
| 93 | |
| 94 | public static String htmlq(String in) { |
| 95 | StringBuilder buf = new StringBuilder(); |
| 96 | for(int i = 0; i < in.length(); i++) { |
| 97 | char c = in.charAt(i); |
| 98 | if(c == '&') |
| 99 | buf.append("&"); |
| 100 | else if(c == '<') |
| 101 | buf.append("<"); |
| 102 | else if(c == '>') |
| 103 | buf.append(">"); |
| 104 | else |
| 105 | buf.append(c); |
| 106 | } |
| 107 | return(buf.toString()); |
| 108 | } |
| 109 | |
| 110 | public static String urlq(String in) { |
| 111 | byte[] bytes = in.getBytes(utf8); |
| 112 | StringBuilder buf = new StringBuilder(); |
| 113 | for(int i = 0; i < bytes.length; i++) { |
| 114 | byte b = bytes[i]; |
| 115 | if((b < 32) || (b == ' ') || (b == '&') || (b == '?') || (b == '/') || (b == '=') || (b == '#') || (b == '%') || (b == '+') || (b >= 128)) { |
| 116 | buf.append('%'); |
| 117 | buf.append(int2hex((b & 0xf0) >> 4, true)); |
| 118 | buf.append(int2hex(b & 0x0f, true)); |
| 119 | } else { |
| 120 | buf.append((char)b); |
| 121 | } |
| 122 | } |
| 123 | return(buf.toString()); |
| 124 | } |
| 125 | } |