Ejemplo n.º 1
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;
        }
      }
    }
  }
  /** Adds a parameter. */
  public void addParam(String name, String value) {
    if (name == null) return;

    if (value == null) value = "";

    if (_query.length() != 0) _query.append('&');

    _query.append(name);
    _query.append('=');
    int len = value.length();
    for (int i = 0; i < len; i++) {
      char ch = value.charAt(i);

      switch (ch) {
        case '&':
          _query.append("%26");
          break;

        case '%':
          _query.append("%25");
          break;

        case '+':
          _query.append("%2b");
          break;

        case '=':
          _query.append("%3d");
          break;

        default:
          _query.append(ch);
          break;
      }
    }
  }
Ejemplo n.º 4
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;
  }
Ejemplo n.º 5
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);
    }
  }