1 29 30 package com.caucho.quercus.lib; 31 32 import com.caucho.quercus.QuercusModuleException; 33 import com.caucho.quercus.annotation.Optional; 34 import com.caucho.quercus.annotation.Reference; 35 import com.caucho.quercus.env.Env; 36 import com.caucho.quercus.env.NullValue; 37 import com.caucho.quercus.env.Value; 38 import com.caucho.quercus.module.AbstractQuercusModule; 39 import com.caucho.util.Alarm; 40 import com.caucho.util.L10N; 41 42 import javax.servlet.http.Cookie ; 43 import javax.servlet.http.HttpServletResponse ; 44 import java.io.IOException ; 45 46 49 public class HttpModule extends AbstractQuercusModule { 50 private static final L10N L = new L10N(HttpModule.class); 51 52 55 public static Value header(Env env, String header, 56 @Optional("true") boolean replace, 57 @Optional long httpResponseCode) 58 { 59 try { 60 HttpServletResponse res = env.getResponse(); 61 62 if (res == null) { 63 env.error(L.l("header requires a http context")); 64 return NullValue.NULL; 65 } 66 67 int len = header.length(); 68 69 if (header.startsWith("HTTP/")) { 70 int p = header.indexOf(' '); 71 int status = 0; 72 int ch; 73 74 for (; p < len && header.charAt(p) == ' '; p++) { 75 } 76 77 for (; p < len && '0' <= (ch = header.charAt(p)) && ch <= '9'; p++) { 78 status = 10 * status + ch - '0'; 79 } 80 81 for (; p < len && header.charAt(p) == ' '; p++) { 82 } 83 84 if (status > 0) { 85 res.setStatus(status, header.substring(p)); 86 87 return NullValue.NULL; 88 } 89 } 90 91 int p = header.indexOf(':'); 92 93 if (p > 0) { 94 String key = header.substring(0, p).trim(); 95 String value = header.substring(p + 1).trim(); 96 97 if (key.equalsIgnoreCase("Location")) 98 res.sendRedirect(value); 99 else if (replace) 100 res.setHeader(key, value); 101 else 102 res.addHeader(key, value); 103 104 if (key.equalsIgnoreCase("Content-Type")) { 105 if (value.indexOf("charset") < 0 && value.indexOf("text/") < 0) 106 res.setCharacterEncoding("iso-8859-1"); 107 108 env.getOut().setEncoding(res.getCharacterEncoding()); 109 } 110 } 111 112 return NullValue.NULL; 113 } catch (IOException e) { 114 throw new QuercusModuleException(e); 115 } 116 } 117 118 121 public static boolean headers_sent(Env env, 122 @Optional @Reference Value file, 123 @Optional @Reference Value line) 124 { 125 HttpServletResponse res = env.getResponse(); 126 127 return res.isCommitted(); 128 } 129 130 133 public static boolean setcookie(Env env, 134 String name, 135 @Optional String value, 136 @Optional long expire, 137 @Optional String path, 138 @Optional String domain, 139 @Optional boolean secure) 140 { 141 if (value == null || value.equals("")) 142 value = ""; 143 144 StringBuilder sb = new StringBuilder (); 145 int len = value.length(); 146 for (int i = 0; i < len; i++) { 147 char ch = value.charAt(i); 148 149 switch (ch) { 150 case '%': case ';': case ':': case '{': case '}': 151 case ' ': case '\t': case '\n': case '\r': 152 case '"': case '\'': 153 { 154 sb.append('%'); 155 156 int d = (ch / 16) & 0xf; 157 if (d < 10) 158 sb.append((char) ('0' + d)); 159 else 160 sb.append((char) ('A' + d - 10)); 161 162 d = ch & 0xf; 163 if (d < 10) 164 sb.append((char) ('0' + d)); 165 else 166 sb.append((char) ('A' + d - 10)); 167 168 break; 169 } 170 171 default: 172 sb.append(ch); 173 break; 174 } 175 } 176 177 Cookie cookie = new Cookie (name, sb.toString()); 178 179 if (expire > 0) 180 cookie.setMaxAge((int) (expire - Alarm.getCurrentTime() / 1000)); 181 182 if (path != null && ! path.equals("")) 183 cookie.setPath(path); 184 185 if (domain != null && ! domain.equals("")) 186 cookie.setDomain(domain); 187 188 if (secure) 189 cookie.setSecure(true); 190 191 env.getResponse().addCookie(cookie); 192 193 return true; 194 } 195 196 199 public static String urldecode(String s) 201 { 202 int len = s.length(); 203 StringBuilder sb = new StringBuilder (); 204 205 for (int i = 0; i < len; i++) { 206 char ch = s.charAt(i); 207 208 if (ch == '%' && i + 2 < len) { 209 int d1 = s.charAt(i + 1); 210 int d2 = s.charAt(i + 2); 211 212 int v = 0; 213 214 if ('0' <= d1 && d1 <= '9') 215 v = 16 * (d1 - '0'); 216 else if ('a' <= d1 && d1 <= 'f') 217 v = 16 * (d1 - 'a' + 10); 218 else if ('A' <= d1 && d1 <= 'F') 219 v = 16 * (d1 - 'A' + 10); 220 else { 221 sb.append('%'); 222 continue; 223 } 224 225 if ('0' <= d2 && d2 <= '9') 226 v += (d2 - '0'); 227 else if ('a' <= d2 && d2 <= 'f') 228 v += (d2 - 'a' + 10); 229 else if ('A' <= d2 && d2 <= 'F') 230 v += (d2 - 'A' + 10); 231 else { 232 sb.append('%'); 233 continue; 234 } 235 236 i += 2; 237 sb.append((char) v); 238 } 239 else 240 sb.append(ch); 241 } 242 243 return sb.toString(); 244 } 245 } 246 247 | Popular Tags |