Example #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;
  }
Example #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);
    }