import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
-import java.util.ArrayList;
import java.util.Map;
import java.util.TimeZone;
-import javax.servlet.http.Cookie;
-
/**
* General purpose request parsing and encoding utility methods.
/**
- * Encode a cookie as per RFC 2109. The resulting string can be used
- * as the value for a <code>Set-Cookie</code> header.
- *
- * @param cookie The cookie to encode.
- * @return A string following RFC 2109.
- */
- public static String encodeCookie(Cookie cookie) {
-
- StringBuffer buf = new StringBuffer( cookie.getName() );
- buf.append("=");
- buf.append(cookie.getValue());
-
- if (cookie.getComment() != null) {
- buf.append("; Comment=\"");
- buf.append(cookie.getComment());
- buf.append("\"");
- }
-
- if (cookie.getDomain() != null) {
- buf.append("; Domain=\"");
- buf.append(cookie.getDomain());
- buf.append("\"");
- }
-
- long age = cookie.getMaxAge();
- if (cookie.getMaxAge() >= 0) {
- buf.append("; Max-Age=\"");
- buf.append(cookie.getMaxAge());
- buf.append("\"");
- }
-
- if (cookie.getPath() != null) {
- buf.append("; Path=\"");
- buf.append(cookie.getPath());
- buf.append("\"");
- }
-
- if (cookie.getSecure()) {
- buf.append("; Secure");
- }
-
- if (cookie.getVersion() > 0) {
- buf.append("; Version=\"");
- buf.append(cookie.getVersion());
- buf.append("\"");
- }
-
- return (buf.toString());
- }
-
-
- /**
* Filter the specified message string for characters that are sensitive
* in HTML. This avoids potential attacks caused by including JavaScript
* codes in the request URL that is often reported in error messages.
/**
- * Parse the character encoding from the specified content type header.
- * If the content type is null, or there is no explicit character encoding,
- * <code>null</code> is returned.
- *
- * @param contentType a content type header
- */
- public static String parseCharacterEncoding(String contentType) {
-
- if (contentType == null)
- return (null);
- int start = contentType.indexOf("charset=");
- if (start < 0)
- return (null);
- String encoding = contentType.substring(start + 8);
- int end = encoding.indexOf(';');
- if (end >= 0)
- encoding = encoding.substring(0, end);
- encoding = encoding.trim();
- if ((encoding.length() > 2) && (encoding.startsWith("\""))
- && (encoding.endsWith("\"")))
- encoding = encoding.substring(1, encoding.length() - 1);
- return (encoding.trim());
-
- }
-
-
- /**
- * Parse a cookie header into an array of cookies according to RFC 2109.
- *
- * @param header Value of an HTTP "Cookie" header
- */
- public static Cookie[] parseCookieHeader(String header) {
-
- if ((header == null) || (header.length() < 1))
- return (new Cookie[0]);
-
- ArrayList cookies = new ArrayList();
- while (header.length() > 0) {
- int semicolon = header.indexOf(';');
- if (semicolon < 0)
- semicolon = header.length();
- if (semicolon == 0)
- break;
- String token = header.substring(0, semicolon);
- if (semicolon < header.length())
- header = header.substring(semicolon + 1);
- else
- header = "";
- try {
- int equals = token.indexOf('=');
- if (equals > 0) {
- String name = token.substring(0, equals).trim();
- String value = token.substring(equals+1).trim();
- cookies.add(new Cookie(name, value));
- }
- } catch (Throwable e) {
- ;
- }
- }
-
- return ((Cookie[]) cookies.toArray(new Cookie[cookies.size()]));
-
- }
-
-
- /**
* Append request parameters from the specified String to the specified
* Map. It is presumed that the specified Map is not accessed from any
* other thread, so no synchronization is performed.
throws UnsupportedEncodingException {
if (data != null && data.length > 0) {
- int pos = 0;
int ix = 0;
int ox = 0;
String key = null;