Ejemplo n.º 1
0
 @Override
 public void log(Request request, Response response) {
   if (_ignorePathMap != null && _ignorePathMap.getMatch(request.getRequestURI()) != null) return;
   int status = response.getStatus();
   long written = response.getContentLength();
   transmitter.queue(
       new AccessLogJettyAdapter(request, status, written, _preferProxiedForAddress));
 }
Ejemplo n.º 2
0
  /* ------------------------------------------------------------ */
  public void commitResponse(boolean last) throws IOException {
    if (!_generator.isCommitted()) {
      _generator.setResponse(_response.getStatus(), _response.getReason());
      try {
        // If the client was expecting 100 continues, but we sent something
        // else, then we need to close the connection
        if (_expect100Continue && _response.getStatus() != 100) _generator.setPersistent(false);
        _generator.completeHeader(_responseFields, last);
      } catch (IOException io) {
        throw io;
      } catch (RuntimeException e) {
        LOG.warn("header full: " + e);

        _response.reset();
        _generator.reset(true);
        _generator.setResponse(HttpStatus.INTERNAL_SERVER_ERROR_500, null);
        _generator.completeHeader(_responseFields, Generator.LAST);
        _generator.complete();
        throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR_500);
      }
    }
    if (last) _generator.complete();
  }
Ejemplo n.º 3
0
  /* ------------------------------------------------------------ */
  public void completeResponse() throws IOException {
    if (!_generator.isCommitted()) {
      _generator.setResponse(_response.getStatus(), _response.getReason());
      try {
        _generator.completeHeader(_responseFields, Generator.LAST);
      } catch (IOException io) {
        throw io;
      } catch (RuntimeException e) {
        LOG.warn("header full: " + e);
        LOG.debug(e);

        _response.reset();
        _generator.reset(true);
        _generator.setResponse(HttpStatus.INTERNAL_SERVER_ERROR_500, null);
        _generator.completeHeader(_responseFields, Generator.LAST);
        _generator.complete();
        throw new HttpException(HttpStatus.INTERNAL_SERVER_ERROR_500);
      }
    }

    _generator.complete();
  }
  protected void commit(ByteBuffer content, boolean complete, Callback callback) {
    // Are we excluding because of status?
    Response response = _channel.getResponse();
    int sc = response.getStatus();
    if (sc > 0 && (sc < 200 || sc == 204 || sc == 205 || sc >= 300)) {
      LOG.debug("{} exclude by status {}", this, sc);
      noCompression();

      if (sc == 304) {
        String request_etags =
            (String) _channel.getRequest().getAttribute("o.e.j.s.h.gzip.GzipHandler.etag");
        String response_etag = response.getHttpFields().get(HttpHeader.ETAG);
        if (request_etags != null && response_etag != null) {
          String response_etag_gzip = etagGzip(response_etag);
          if (request_etags.contains(response_etag_gzip))
            response.getHttpFields().put(HttpHeader.ETAG, response_etag_gzip);
        }
      }

      _interceptor.write(content, complete, callback);
      return;
    }

    // Are we excluding because of mime-type?
    String ct = response.getContentType();
    if (ct != null) {
      ct = MimeTypes.getContentTypeWithoutCharset(ct);
      if (!_factory.isMimeTypeGzipable(StringUtil.asciiToLowerCase(ct))) {
        LOG.debug("{} exclude by mimeType {}", this, ct);
        noCompression();
        _interceptor.write(content, complete, callback);
        return;
      }
    }

    // Has the Content-Encoding header already been set?
    HttpFields fields = response.getHttpFields();
    String ce = fields.get(HttpHeader.CONTENT_ENCODING);
    if (ce != null) {
      LOG.debug("{} exclude by content-encoding {}", this, ce);
      noCompression();
      _interceptor.write(content, complete, callback);
      return;
    }

    // Are we the thread that commits?
    if (_state.compareAndSet(GZState.MIGHT_COMPRESS, GZState.COMMITTING)) {
      // We are varying the response due to accept encoding header.
      if (_vary != null) {
        if (fields.contains(HttpHeader.VARY)) fields.addCSV(HttpHeader.VARY, _vary.getValues());
        else fields.add(_vary);
      }

      long content_length = response.getContentLength();
      if (content_length < 0 && complete) content_length = content.remaining();

      _deflater = _factory.getDeflater(_channel.getRequest(), content_length);

      if (_deflater == null) {
        LOG.debug("{} exclude no deflater", this);
        _state.set(GZState.NOT_COMPRESSING);
        _interceptor.write(content, complete, callback);
        return;
      }

      fields.put(GZIP._contentEncoding);
      _crc.reset();
      _buffer = _channel.getByteBufferPool().acquire(_bufferSize, false);
      BufferUtil.fill(_buffer, GZIP_HEADER, 0, GZIP_HEADER.length);

      // Adjust headers
      response.setContentLength(-1);
      String etag = fields.get(HttpHeader.ETAG);
      if (etag != null) fields.put(HttpHeader.ETAG, etagGzip(etag));

      LOG.debug("{} compressing {}", this, _deflater);
      _state.set(GZState.COMPRESSING);

      gzip(content, complete, callback);
    } else callback.failed(new WritePendingException());
  }
Ejemplo n.º 5
0
  @Override
  public void log(Request request, Response response) {
    // copied almost entirely from NCSARequestLog
    final StringBuilder buf = new StringBuilder(256);
    String address = request.getHeader(HttpHeaders.X_FORWARDED_FOR);
    if (address == null) {
      address = request.getRemoteAddr();
    }

    buf.append(address);
    buf.append(" - ");
    final Authentication authentication = request.getAuthentication();
    if (authentication instanceof Authentication.User) {
      buf.append(
          ((Authentication.User) authentication).getUserIdentity().getUserPrincipal().getName());
    } else {
      buf.append('-');
    }

    buf.append(" [");
    buf.append(dateCache.format(request.getTimeStamp()));

    buf.append("] \"");
    buf.append(request.getMethod());
    buf.append(' ');
    buf.append(request.getUri().toString());
    buf.append(' ');
    buf.append(request.getProtocol());
    buf.append("\" ");

    // TODO: Handle async requests?
    // e.g. if (request.getAsyncContinuation().isInitial())
    assert !request.isAsyncStarted();

    int status = response.getStatus();
    if (status <= 0) {
      if (request.isHandled()) {
        status = 200;
      } else {
        status = 404;
      }
    }
    buf.append((char) ('0' + ((status / 100) % 10)));
    buf.append((char) ('0' + ((status / 10) % 10)));
    buf.append((char) ('0' + (status % 10)));

    final long responseLength = response.getContentCount();
    if (responseLength >= 0) {
      buf.append(' ');
      if (responseLength > 99999) {
        buf.append(responseLength);
      } else {
        if (responseLength > 9999) {
          buf.append((char) ('0' + ((responseLength / 10000) % 10)));
        }
        if (responseLength > 999) {
          buf.append((char) ('0' + ((responseLength / 1000) % 10)));
        }
        if (responseLength > 99) {
          buf.append((char) ('0' + ((responseLength / 100) % 10)));
        }
        if (responseLength > 9) {
          buf.append((char) ('0' + ((responseLength / 10) % 10)));
        }
        buf.append((char) ('0' + (responseLength % 10)));
      }
    } else {
      buf.append(" -");
    }

    final long now = System.currentTimeMillis();
    final long dispatchTime = request.getDispatchTime();

    buf.append(' ');
    buf.append(now - ((dispatchTime == 0) ? request.getTimeStamp() : dispatchTime));

    buf.append(' ');
    buf.append(now - request.getTimeStamp());

    System.out.println(buf.toString());
  }