Ejemplo n.º 1
0
  @Override
  public boolean parsedHeader(HttpField field) {
    HttpHeader header = field.getHeader();
    String value = field.getValue();
    if (value == null) value = "";
    if (header != null) {
      switch (header) {
        case EXPECT:
          if (_version.getVersion() >= HttpVersion.HTTP_1_1.getVersion()) {
            HttpHeaderValue expect = HttpHeaderValue.CACHE.get(value);
            switch (expect == null ? HttpHeaderValue.UNKNOWN : expect) {
              case CONTINUE:
                _expect100Continue = true;
                break;

              case PROCESSING:
                _expect102Processing = true;
                break;

              default:
                String[] values = value.split(",");
                for (int i = 0; values != null && i < values.length; i++) {
                  expect = HttpHeaderValue.CACHE.get(values[i].trim());
                  if (expect == null) _expect = true;
                  else {
                    switch (expect) {
                      case CONTINUE:
                        _expect100Continue = true;
                        break;
                      case PROCESSING:
                        _expect102Processing = true;
                        break;
                      default:
                        _expect = true;
                    }
                  }
                }
            }
          }
          break;

        case CONTENT_TYPE:
          MimeTypes.Type mime = MimeTypes.CACHE.get(value);
          String charset =
              (mime == null || mime.getCharset() == null)
                  ? MimeTypes.getCharsetFromContentType(value)
                  : mime.getCharset().toString();
          if (charset != null) _request.setCharacterEncodingUnchecked(charset);
          break;
        default:
      }
    }

    if (field.getName() != null) _request.getHttpFields().add(field);
    return false;
  }
Ejemplo n.º 2
0
  @Override
  public void setContentType(String contentType) {
    if (isCommitted() || isIncluding()) return;

    if (contentType == null) {
      if (isWriting() && _characterEncoding != null) throw new IllegalSelectorException();

      if (_locale == null) _characterEncoding = null;
      _mimeType = null;
      _contentType = null;
      _fields.remove(HttpHeader.CONTENT_TYPE);
    } else {
      _contentType = contentType;
      _mimeType = MimeTypes.CACHE.get(contentType);
      String charset;
      if (_mimeType != null && _mimeType.getCharset() != null)
        charset = _mimeType.getCharset().toString();
      else charset = MimeTypes.getCharsetFromContentType(contentType);

      if (charset == null) {
        if (_characterEncoding != null) {
          _contentType = contentType + ";charset=" + _characterEncoding;
          _mimeType = null;
        }
      } else if (isWriting() && !charset.equals(_characterEncoding)) {
        // too late to change the character encoding;
        _mimeType = null;
        _contentType = MimeTypes.getContentTypeWithoutCharset(_contentType);
        if (_characterEncoding != null)
          _contentType = _contentType + ";charset=" + _characterEncoding;
      } else {
        _characterEncoding = charset;
      }

      HttpField field = HttpField.CONTENT_TYPE.get(_contentType);
      if (field != null) _fields.put(field);
      else _fields.put(HttpHeader.CONTENT_TYPE, _contentType);
    }
  }
    /* ------------------------------------------------------------ */
    CachedHttpContent(String pathInContext, Resource resource, CachedHttpContent gzipped) {
      _key = pathInContext;
      _resource = resource;

      String contentType = _mimeTypes.getMimeByExtension(_resource.toString());
      _contentType =
          contentType == null
              ? null
              : new PreEncodedHttpField(HttpHeader.CONTENT_TYPE, contentType);
      _characterEncoding =
          _contentType == null ? null : MimeTypes.getCharsetFromContentType(contentType);
      _mimeType =
          _contentType == null
              ? null
              : MimeTypes.CACHE.get(MimeTypes.getContentTypeWithoutCharset(contentType));

      boolean exists = resource.exists();
      _lastModifiedValue = exists ? resource.lastModified() : -1L;
      _lastModified =
          _lastModifiedValue == -1
              ? null
              : new PreEncodedHttpField(
                  HttpHeader.LAST_MODIFIED, DateGenerator.formatDate(_lastModifiedValue));

      _contentLengthValue = exists ? (int) resource.length() : 0;
      _contentLength =
          new PreEncodedHttpField(HttpHeader.CONTENT_LENGTH, Long.toString(_contentLengthValue));

      if (_cachedFiles.incrementAndGet() > _maxCachedFiles) shrinkCache();

      _lastAccessed = System.currentTimeMillis();

      _etag =
          ResourceCache.this._etags
              ? new PreEncodedHttpField(HttpHeader.ETAG, resource.getWeakETag())
              : null;

      _gzipped = gzipped == null ? null : new CachedGzipHttpContent(this, gzipped);
    }
Ejemplo n.º 4
0
    /*
     * @see org.eclipse.jetty.server.server.HttpParser.EventHandler#parsedHeaderValue(org.eclipse.io.Buffer)
     */
    @Override
    public void parsedHeader(Buffer name, Buffer value) {
      int ho = HttpHeaders.CACHE.getOrdinal(name);
      switch (ho) {
        case HttpHeaders.HOST_ORDINAL:
          // TODO check if host matched a host in the URI.
          _host = true;
          break;

        case HttpHeaders.EXPECT_ORDINAL:
          value = HttpHeaderValues.CACHE.lookup(value);
          switch (HttpHeaderValues.CACHE.getOrdinal(value)) {
            case HttpHeaderValues.CONTINUE_ORDINAL:
              _expect100Continue = _generator instanceof HttpGenerator;
              break;

            case HttpHeaderValues.PROCESSING_ORDINAL:
              _expect102Processing = _generator instanceof HttpGenerator;
              break;

            default:
              String[] values = value.toString().split(",");
              for (int i = 0; values != null && i < values.length; i++) {
                CachedBuffer cb = HttpHeaderValues.CACHE.get(values[i].trim());
                if (cb == null) _expect = true;
                else {
                  switch (cb.getOrdinal()) {
                    case HttpHeaderValues.CONTINUE_ORDINAL:
                      _expect100Continue = _generator instanceof HttpGenerator;
                      break;
                    case HttpHeaderValues.PROCESSING_ORDINAL:
                      _expect102Processing = _generator instanceof HttpGenerator;
                      break;
                    default:
                      _expect = true;
                  }
                }
              }
          }
          break;

        case HttpHeaders.ACCEPT_ENCODING_ORDINAL:
        case HttpHeaders.USER_AGENT_ORDINAL:
          value = HttpHeaderValues.CACHE.lookup(value);
          break;

        case HttpHeaders.CONTENT_TYPE_ORDINAL:
          value = MimeTypes.CACHE.lookup(value);
          _charset = MimeTypes.getCharsetFromContentType(value);
          break;

        case HttpHeaders.CONNECTION_ORDINAL:
          // looks rather clumsy, but the idea is to optimize for a single valued header
          switch (HttpHeaderValues.CACHE.getOrdinal(value)) {
            case -1:
              {
                String[] values = value.toString().split(",");
                for (int i = 0; values != null && i < values.length; i++) {
                  CachedBuffer cb = HttpHeaderValues.CACHE.get(values[i].trim());

                  if (cb != null) {
                    switch (cb.getOrdinal()) {
                      case HttpHeaderValues.CLOSE_ORDINAL:
                        _responseFields.add(
                            HttpHeaders.CONNECTION_BUFFER, HttpHeaderValues.CLOSE_BUFFER);
                        _generator.setPersistent(false);
                        break;

                      case HttpHeaderValues.KEEP_ALIVE_ORDINAL:
                        if (_version == HttpVersions.HTTP_1_0_ORDINAL)
                          _responseFields.add(
                              HttpHeaders.CONNECTION_BUFFER, HttpHeaderValues.KEEP_ALIVE_BUFFER);
                        break;
                    }
                  }
                }
                break;
              }
            case HttpHeaderValues.CLOSE_ORDINAL:
              _responseFields.put(HttpHeaders.CONNECTION_BUFFER, HttpHeaderValues.CLOSE_BUFFER);
              _generator.setPersistent(false);
              break;

            case HttpHeaderValues.KEEP_ALIVE_ORDINAL:
              if (_version == HttpVersions.HTTP_1_0_ORDINAL)
                _responseFields.put(
                    HttpHeaders.CONNECTION_BUFFER, HttpHeaderValues.KEEP_ALIVE_BUFFER);
              break;
          }
      }

      _requestFields.add(name, value);
    }