KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > HttpModule


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
43 import javax.servlet.http.HttpServletResponse JavaDoc;
44 import java.io.IOException JavaDoc;
45
46 /**
47  * PHP HTTP functions
48  */

49 public class HttpModule extends AbstractQuercusModule {
50   private static final L10N L = new L10N(HttpModule.class);
51
52   /**
53    * Adds a header.
54    */

55   public static Value header(Env env, String JavaDoc header,
56                              @Optional("true") boolean replace,
57                              @Optional long httpResponseCode)
58   {
59     try {
60       HttpServletResponse JavaDoc 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 JavaDoc key = header.substring(0, p).trim();
95     String JavaDoc 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 JavaDoc e) {
114       throw new QuercusModuleException(e);
115     }
116   }
117
118   /**
119    * Return true if the headers have been sent.
120    */

121   public static boolean headers_sent(Env env,
122                                      @Optional @Reference Value file,
123                                      @Optional @Reference Value line)
124   {
125     HttpServletResponse JavaDoc res = env.getResponse();
126
127     return res.isCommitted();
128   }
129
130   /**
131    * Sets a cookie
132    */

133   public static boolean setcookie(Env env,
134                                   String JavaDoc name,
135                                   @Optional String JavaDoc value,
136                                   @Optional long expire,
137                                   @Optional String JavaDoc path,
138                                   @Optional String JavaDoc domain,
139                                   @Optional boolean secure)
140   {
141     if (value == null || value.equals(""))
142       value = "";
143
144     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
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 JavaDoc cookie = new Cookie JavaDoc(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   /**
197    * Returns the decoded string.
198    */

199   // XXX: see duplicate in env
200
public static String JavaDoc urldecode(String JavaDoc s)
201   {
202     int len = s.length();
203     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
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