public Object next() {
      _cb.clear();

      char ch;
      final int startDelims = _delims.length - 1;
      loop:
      for (; _i < _length; _i++) {
        ch = _value.charAt(_i);

        for (int j = startDelims; j >= 0; j--) {
          if (_delims[j] == ch) break loop;
        }

        _cb.append(ch);
      }

      for (_i++; _i < _length; _i++) {
        ch = _value.charAt(_i);

        boolean hasDelim = false;
        for (int j = startDelims; j >= 0; j--) {
          if (_delims[j] == ch) {
            hasDelim = true;
            break;
          }
        }

        if (!hasDelim) return _cb.toString();
      }

      return _cb.toString();
    }
Пример #2
0
  private int parseHeaders(HttpServletResponse res, InputStream is) throws IOException {
    CharBuffer key = new CharBuffer();
    CharBuffer value = new CharBuffer();

    int ch = is.read();

    if (ch < 0) {
      log.fine("Can't contact FastCGI");
      res.sendError(404);
      return -1;
    }

    while (ch >= 0) {
      key.clear();
      value.clear();

      for (; ch >= 0 && ch != ' ' && ch != '\r' && ch != '\n' && ch != ':'; ch = is.read()) {
        key.append((char) ch);
      }

      for (; ch >= 0 && ch == ' ' || ch == ':'; ch = is.read()) {}

      for (; ch >= 0 && ch != '\r' && ch != '\n'; ch = is.read()) {
        value.append((char) ch);
      }

      if (ch == '\r') {
        ch = is.read();
        if (ch == '\n') ch = is.read();
      }

      if (key.length() == 0) return ch;

      if (log.isLoggable(Level.FINE)) log.fine("fastcgi:" + key + ": " + value);

      if (key.equalsIgnoreCase("status")) {
        int status = 0;
        int len = value.length();

        for (int i = 0; i < len; i++) {
          char digit = value.charAt(i);

          if ('0' <= digit && digit <= '9') status = 10 * status + digit - '0';
          else break;
        }

        res.setStatus(status);
      } else if (key.startsWith("http") || key.startsWith("HTTP")) {
      } else if (key.equalsIgnoreCase("location")) {
        res.sendRedirect(value.toString());
      } else res.addHeader(key.toString(), value.toString());
    }

    return ch;
  }
  private void checkMetaEncoding(Element elt) {
    String http = elt.getAttribute("http-equiv");
    String content = elt.getAttribute("content");
    if (http.equals("") || content.equals("") || !http.equalsIgnoreCase("content-type")) return;

    CharCursor cursor = new StringCharCursor(content);
    charsetScanner.scan(cursor);
    charsetScanner.skip(cursor);
    CharBuffer buf = CharBuffer.allocate();
    while (cursor.current() != cursor.DONE) {
      buf.clear();
      charsetScanner.scan(cursor, buf);
      if (buf.toString().equalsIgnoreCase("charset")) {
        charsetScanner.skip(cursor);
        buf.clear();
        charsetScanner.scan(cursor, buf);
        if (buf.length() > 0) {
          try {
            is.setEncoding(buf.close());
          } catch (IOException e) {
          }
          return;
        }
      }
    }
  }
  private Path writeTempFile(Node node) throws IOException {
    Path workDir = CauchoSystem.getWorkPath().lookup("_xsl");
    workDir.mkdirs();

    // Path temp = workDir.createTempFile("tmp", "xsl");

    WriteStream os = Vfs.lookup("null:").openWrite();
    Crc64Stream crcStream = new Crc64Stream(os.getSource());
    os.init(crcStream);
    try {
      XmlPrinter printer = new XmlPrinter(os);

      printer.printNode(node);
    } finally {
      os.close();
    }

    long crc = crcStream.getCRC();
    CharBuffer cb = new CharBuffer();
    Base64.encode(cb, crc);

    String crcValue = cb.toString().replace('/', '-');

    Path xslPath = workDir.lookup(crcValue + ".xsl");

    // temp.renameTo(xslPath);

    return xslPath;
  }
  public void doFilter(
      ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
      throws ServletException, IOException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;

    String uri = request.getRequestURI();

    String servletPath = (String) request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH);
    String pathInfo;
    String queryString;

    if (servletPath == null) {
      servletPath = request.getServletPath();
      pathInfo = request.getPathInfo();

      if (isUseQuery()) queryString = request.getQueryString();
      else queryString = null;
    } else {
      pathInfo = (String) request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);

      if (isUseQuery())
        queryString = (String) request.getAttribute(RequestDispatcher.INCLUDE_QUERY_STRING);
      else queryString = null;
    }

    String name;

    if (pathInfo == null && queryString == null) name = servletPath;
    else {
      CharBuffer nameBuilder = new CharBuffer();

      nameBuilder.append(servletPath);

      if (pathInfo != null) nameBuilder.append(pathInfo);

      if (queryString != null) {
        nameBuilder.append('?');
        nameBuilder.append(queryString);
      }

      name = nameBuilder.toString();
    }

    ProfilerPoint profilerPoint = _profilerManager.getProfilerPoint(name);

    if (log.isLoggable(Level.FINEST)) log.finest(profilerPoint.toString());

    Profiler profiler = profilerPoint.start();

    try {
      chain.doFilter(request, response);
    } finally {
      profiler.finish();
    }
  }
  public String toSignatureString() {
    CharBuffer cb = new CharBuffer();

    cb.append(_methodName);
    cb.append("(");
    for (int i = 0; _paramTypes != null && i < _paramTypes.size(); i++) {
      if (i != 0) cb.append(", ");
      cb.append(_paramTypes.get(i));
    }
    cb.append(")");

    return cb.toString();
  }
Пример #7
0
  /**
   * Creates a pseudo-random session id. If there's an old id and the group matches, then use it
   * because different applications on the same matchine should use the same cookie.
   */
  public String createSessionId(Env env) {
    String id;

    do {
      CharBuffer sb = new CharBuffer();

      Base64.encode(sb, RandomUtil.getRandomLong());
      Base64.encode(sb, env.getCurrentTime());

      id = sb.toString();
    } while (getSession(env, id, 0) != null);

    if (id == null || id.equals("")) throw new RuntimeException();

    return id;
  }
Пример #8
0
  public void init() throws ConfigException {
    if (_id == null) throw new ConfigException(L.l("`{0}' is required", "id"));

    if (_location == null) throw new ConfigException(L.l("`{0}' is required", "location"));

    if (_name == null) _name = _location.toString();

    if (_indexString == null) _indexString = "index-all.html";

    _locationPath = Vfs.lookup(_location);

    int split = _indexString.indexOf('#');

    if (split > -1) {
      CharBuffer before = new CharBuffer(_indexString.substring(0, split));
      CharBuffer after = new CharBuffer(_indexString.substring(split + 1));
      CharBuffer index = CharBuffer.allocate();

      boolean isIndex = false;

      for (int i = 1; i <= 27; i++) {
        index.append(before);
        index.append(i);
        index.append(after);

        Path indexPath = _locationPath.lookup(index.toString());

        if (indexPath.exists()) {
          isIndex = true;
          _index.add(indexPath);
        }

        index.clear();
      }

      if (!isIndex) {
        throw new ConfigException(L.l("`{0}' not found", _locationPath.lookup(_indexString)));
      }
    } else _index.add(_locationPath.lookup(_indexString));

    if (_locationPath.getScheme().equals("file")) {
      _isLocal = true;
      Path pwd = Vfs.getPwd();

      if (!_locationPath.getPath().startsWith(pwd.getPath())) _isLocalAbsolute = true;
    }
  }
  private static String parseName(ReadStream is) throws IOException {
    int ch;

    for (ch = is.read(); ch > 0 && ch != '"'; ch = is.read()) {}

    if (ch < 0) return null;

    CharBuffer cb = new CharBuffer();

    for (ch = is.read(); ch > 0 && ch != '"'; ch = is.read()) {
      cb.append((char) ch);
    }

    if (ch < 0) return null;

    return cb.toString();
  }
  private void handleExternalBody(String url) throws JspException, ServletException, IOException {
    URL netURL = new URL(url);

    URLConnection conn = netURL.openConnection();

    if (conn instanceof HttpURLConnection) ((HttpURLConnection) conn).setFollowRedirects(true);

    InputStream is = conn.getInputStream();
    try {
      ReadStream in = Vfs.openRead(is);
      String encoding = conn.getContentEncoding();
      String contentType = conn.getContentType();

      if (_charEncoding != null) {
        encoding = _charEncoding;
        if (encoding != null && !encoding.equals("")) in.setEncoding(encoding);
      } else if (encoding != null) in.setEncoding(encoding);
      else if (contentType != null) {
        int p = contentType.indexOf("charset=");
        if (p > 0) {
          CharBuffer cb = new CharBuffer();
          for (int i = p + 8; i < contentType.length(); i++) {
            int ch = contentType.charAt(i);
            if (ch == '"' || ch == '\'') {
            } else if (ch >= 'a' && ch <= 'z') cb.append((char) ch);
            else if (ch >= 'A' && ch <= 'Z') cb.append((char) ch);
            else if (ch >= '0' && ch <= '9') cb.append((char) ch);
            else if (ch == '-' || ch == '_') cb.append((char) ch);
            else break;
          }
          encoding = cb.toString();

          in.setEncoding(encoding);
        }
      }

      JspWriter out = pageContext.getOut();

      int ch;
      while ((ch = in.readChar()) >= 0) out.print((char) ch);
    } finally {
      is.close();
    }
  }
Пример #11
0
  public ESString toSource(IntMap map, boolean isLoopPass) throws Throwable {
    CharBuffer cb = new CharBuffer();
    Global resin = Global.getGlobalProto();

    int mark = map.get(this);

    if (mark > 0 && isLoopPass) return null;
    else if (mark > 0) {
      cb.append("#" + mark + "=");
      map.put(this, -mark);
    } else if (mark == 0 && isLoopPass) {
      map.put(this, resin.addMark());
      return null;
    } else if (mark < 0 && !isLoopPass) {
      return ESString.create("#" + -mark + "#");
    }

    cb.append("{");

    if (isLoopPass) map.put(this, 0);

    Iterator e = keys();

    boolean isFirst = true;
    while (e.hasNext()) {
      if (!isFirst) cb.append(", ");
      isFirst = false;

      ESString key = (ESString) e.next();

      cb.append(key);
      cb.append(":");
      ESBase value = getProperty(key);
      if (isLoopPass) value.toSource(map, isLoopPass);
      else cb.append(value.toSource(map, isLoopPass));
    }

    cb.append("}");

    return new ESString(cb.toString());
  }
  public String toString() {
    if (_components.size() == 1) {
      return _components.get(0).toString();
    } else {
      CharBuffer cb = new CharBuffer();

      cb.append('(');

      for (int i = 0; i < _components.size(); i++) {
        if (i != 0) cb.append(" OR ");

        AmberExpr expr = _components.get(i);

        cb.append(expr);
      }

      cb.append(')');

      return cb.toString();
    }
  }
Пример #13
0
  public RequestDispatcher getRequestDispatcher(String path) {
    if (path == null || path.length() == 0) return null;
    else if (path.charAt(0) == '/') return getWebApp().getRequestDispatcher(path);
    else {
      CharBuffer cb = new CharBuffer();

      WebApp webApp = getWebApp();

      String servletPath = getPageServletPath();
      if (servletPath != null) cb.append(servletPath);
      String pathInfo = getPagePathInfo();
      if (pathInfo != null) cb.append(pathInfo);

      int p = cb.lastIndexOf('/');
      if (p >= 0) cb.setLength(p);
      cb.append('/');
      cb.append(path);

      if (webApp != null) return webApp.getRequestDispatcher(cb.toString());

      return null;
    }
  }
Пример #14
0
  /** Parses the access log string. */
  private ArrayList<Segment> parseFormat(String format) {
    ArrayList<Segment> segments = new ArrayList<Segment>();
    CharBuffer cb = new CharBuffer();

    int i = 0;
    while (i < _format.length()) {
      char ch = _format.charAt(i++);

      if (ch != '%' || i >= _format.length()) {
        cb.append((char) ch);
        continue;
      }

      String arg = null;
      ch = _format.charAt(i++);
      if (ch == '>') ch = _format.charAt(i++);
      else if (ch == '{') {
        if (cb.length() > 0) segments.add(new Segment(this, Segment.TEXT, cb.toString()));
        cb.clear();
        while (i < _format.length() && _format.charAt(i++) != '}') cb.append(_format.charAt(i - 1));
        arg = cb.toString();
        cb.clear();

        ch = _format.charAt(i++);
      }

      switch (ch) {
        case 'b':
        case 'c':
        case 'h':
        case 'i':
        case 'l':
        case 'n':
        case 'r':
        case 's':
        case 'T':
        case 'D':
        case 'o':
        case 'u':
        case 'U':
        case 'v':
          if (cb.length() > 0) segments.add(new Segment(this, Segment.TEXT, cb.toString()));
          cb.clear();
          segments.add(new Segment(this, ch, arg));
          break;

        case 't':
          if (cb.length() > 0) segments.add(new Segment(this, Segment.TEXT, cb.toString()));
          cb.clear();
          if (arg != null) _timeFormat = arg;
          segments.add(new Segment(this, ch, arg));
          break;

        default:
          cb.append('%');
          i--;
          break;
      }
    }

    cb.append(CauchoSystem.getNewlineString());
    segments.add(new Segment(this, Segment.TEXT, cb.toString()));

    return segments;
  }
Пример #15
0
  /** Parse the headers returned from the server. */
  private void parseHeaders() throws IOException {
    CharBuffer line = new CharBuffer();

    // Skip blank lines
    int count = 0;
    do {
      line.clear();
      if (!_rs.readln(line)) {
        _isKeepalive = false;
        return;
      }
    } while (line.length() == 0 && ++count < 10);

    if (line.length() == 0) {
      _isKeepalive = false;
      return;
    }

    if (line.startsWith("HTTP/1.1 100")) {
      count = 100;
      do {
        line.clear();
        if (!_rs.readln(line)) {
          _isKeepalive = false;
          return;
        }
      } while (line.length() != 0 && count-- > 0);

      count = 100;
      do {
        line.clear();
        if (!_rs.readln(line)) {
          _isKeepalive = false;
          return;
        }
      } while (line.length() == 0 && count-- > 0);
    }

    if (line.length() == 0) {
      _isKeepalive = false;
      return;
    }

    int i = 0;
    for (i = 0; i < line.length() && line.charAt(i) != ' '; i++) {}

    for (; i < line.length() && line.charAt(i) == ' '; i++) {}

    int status = 0;
    for (; i < line.length(); i++) {
      char ch = line.charAt(i);
      if (ch >= '0' && ch <= '9') status = 10 * status + ch - '0';
      else break;
    }

    if (status != 200) _isKeepalive = false;
    else if (!line.startsWith("HTTP/1.1 ")) _isKeepalive = false;

    _attributes.put("status", String.valueOf(status));
    _attributes.put("status-message", line.toString());

    CharBuffer key = new CharBuffer();
    while (true) {
      line.clear();
      if (!_rs.readln(line) || line.length() == 0) break;

      int lineLength = line.length();

      for (i = 0; i < lineLength && Character.isWhitespace(line.charAt(i)); i++) {}

      key.clear();
      for (;
          i < lineLength && !Character.isWhitespace(line.charAt(i)) && line.charAt(i) != ':';
          i++) {
        key.append((char) line.charAt(i));
      }

      for (; i < lineLength && Character.isWhitespace(line.charAt(i)); i++) {}

      if (key.length() == 0 || lineLength <= i || line.charAt(i) != ':') continue;

      for (i++; i < lineLength && Character.isWhitespace(line.charAt(i)); i++) {}

      key.toLowerCase();
      String value = line.substring(i);

      if (log.isLoggable(Level.FINE)) log.fine(key + ": " + value);

      if (key.matchesIgnoreCase("content-length")) {
        _contentLength = Integer.parseInt(value.trim());
      } else if (key.matchesIgnoreCase("connection") && value.equalsIgnoreCase("close")) {
        _isKeepalive = false;
      } else if (key.matchesIgnoreCase("transfer-encoding") && value.equalsIgnoreCase("chunked")) {

        _isChunked = true;
        _chunkLength = 0;
      }

      String keyString = key.toLowerCase().toString();

      String oldValue = (String) _attributes.put(keyString, value);

      if (oldValue != null) {
        value = oldValue + '\n' + value;
      }

      _attributes.put(keyString, value);
    }
  }