Search in sources :

Example 1 with PreEncodedHttpField

use of org.eclipse.jetty.http.PreEncodedHttpField in project jetty.project by eclipse.

the class DefaultServlet method init.

/* ------------------------------------------------------------ */
@Override
public void init() throws UnavailableException {
    _servletContext = getServletContext();
    _contextHandler = initContextHandler(_servletContext);
    _mimeTypes = _contextHandler.getMimeTypes();
    _welcomes = _contextHandler.getWelcomeFiles();
    if (_welcomes == null)
        _welcomes = new String[] { "index.html", "index.jsp" };
    _resourceService.setAcceptRanges(getInitBoolean("acceptRanges", _resourceService.isAcceptRanges()));
    _resourceService.setDirAllowed(getInitBoolean("dirAllowed", _resourceService.isDirAllowed()));
    _resourceService.setRedirectWelcome(getInitBoolean("redirectWelcome", _resourceService.isRedirectWelcome()));
    _resourceService.setPrecompressedFormats(parsePrecompressedFormats(getInitParameter("precompressed"), getInitBoolean("gzip", false)));
    _resourceService.setPathInfoOnly(getInitBoolean("pathInfoOnly", _resourceService.isPathInfoOnly()));
    _resourceService.setEtags(getInitBoolean("etags", _resourceService.isEtags()));
    if ("exact".equals(getInitParameter("welcomeServlets"))) {
        _welcomeExactServlets = true;
        _welcomeServlets = false;
    } else
        _welcomeServlets = getInitBoolean("welcomeServlets", _welcomeServlets);
    _useFileMappedBuffer = getInitBoolean("useFileMappedBuffer", _useFileMappedBuffer);
    _relativeResourceBase = getInitParameter("relativeResourceBase");
    String rb = getInitParameter("resourceBase");
    if (rb != null) {
        if (_relativeResourceBase != null)
            throw new UnavailableException("resourceBase & relativeResourceBase");
        try {
            _resourceBase = _contextHandler.newResource(rb);
        } catch (Exception e) {
            LOG.warn(Log.EXCEPTION, e);
            throw new UnavailableException(e.toString());
        }
    }
    String css = getInitParameter("stylesheet");
    try {
        if (css != null) {
            _stylesheet = Resource.newResource(css);
            if (!_stylesheet.exists()) {
                LOG.warn("!" + css);
                _stylesheet = null;
            }
        }
        if (_stylesheet == null) {
            _stylesheet = Resource.newResource(this.getClass().getResource("/jetty-dir.css"));
        }
    } catch (Exception e) {
        LOG.warn(e.toString());
        LOG.debug(e);
    }
    int encodingHeaderCacheSize = getInitInt("encodingHeaderCacheSize", -1);
    if (encodingHeaderCacheSize >= 0)
        _resourceService.setEncodingCacheSize(encodingHeaderCacheSize);
    String cc = getInitParameter("cacheControl");
    if (cc != null)
        _resourceService.setCacheControl(new PreEncodedHttpField(HttpHeader.CACHE_CONTROL, cc));
    String resourceCache = getInitParameter("resourceCache");
    int max_cache_size = getInitInt("maxCacheSize", -2);
    int max_cached_file_size = getInitInt("maxCachedFileSize", -2);
    int max_cached_files = getInitInt("maxCachedFiles", -2);
    if (resourceCache != null) {
        if (max_cache_size != -1 || max_cached_file_size != -2 || max_cached_files != -2)
            LOG.debug("ignoring resource cache configuration, using resourceCache attribute");
        if (_relativeResourceBase != null || _resourceBase != null)
            throw new UnavailableException("resourceCache specified with resource bases");
        _cache = (CachedContentFactory) _servletContext.getAttribute(resourceCache);
    }
    try {
        if (_cache == null && (max_cached_files != -2 || max_cache_size != -2 || max_cached_file_size != -2)) {
            _cache = new CachedContentFactory(null, this, _mimeTypes, _useFileMappedBuffer, _resourceService.isEtags(), _resourceService.getPrecompressedFormats());
            if (max_cache_size >= 0)
                _cache.setMaxCacheSize(max_cache_size);
            if (max_cached_file_size >= -1)
                _cache.setMaxCachedFileSize(max_cached_file_size);
            if (max_cached_files >= -1)
                _cache.setMaxCachedFiles(max_cached_files);
            _servletContext.setAttribute(resourceCache == null ? "resourceCache" : resourceCache, _cache);
        }
    } catch (Exception e) {
        LOG.warn(Log.EXCEPTION, e);
        throw new UnavailableException(e.toString());
    }
    HttpContent.ContentFactory contentFactory = _cache;
    if (contentFactory == null) {
        contentFactory = new ResourceContentFactory(this, _mimeTypes, _resourceService.getPrecompressedFormats());
        if (resourceCache != null)
            _servletContext.setAttribute(resourceCache, contentFactory);
    }
    _resourceService.setContentFactory(contentFactory);
    _resourceService.setWelcomeFactory(this);
    List<String> gzip_equivalent_file_extensions = new ArrayList<String>();
    String otherGzipExtensions = getInitParameter("otherGzipFileExtensions");
    if (otherGzipExtensions != null) {
        //comma separated list
        StringTokenizer tok = new StringTokenizer(otherGzipExtensions, ",", false);
        while (tok.hasMoreTokens()) {
            String s = tok.nextToken().trim();
            gzip_equivalent_file_extensions.add((s.charAt(0) == '.' ? s : "." + s));
        }
    } else {
        //.svgz files are gzipped svg files and must be served with Content-Encoding:gzip
        gzip_equivalent_file_extensions.add(".svgz");
    }
    _resourceService.setGzipEquivalentFileExtensions(gzip_equivalent_file_extensions);
    _servletHandler = _contextHandler.getChildHandlerByClass(ServletHandler.class);
    for (ServletHolder h : _servletHandler.getServlets()) if (h.getServletInstance() == this)
        _defaultHolder = h;
    if (LOG.isDebugEnabled())
        LOG.debug("resource base = " + _resourceBase);
}
Also used : CachedContentFactory(org.eclipse.jetty.server.CachedContentFactory) UnavailableException(javax.servlet.UnavailableException) ArrayList(java.util.ArrayList) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) UnavailableException(javax.servlet.UnavailableException) StringTokenizer(java.util.StringTokenizer) ResourceContentFactory(org.eclipse.jetty.server.ResourceContentFactory) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField) HttpContent(org.eclipse.jetty.http.HttpContent)

Example 2 with PreEncodedHttpField

use of org.eclipse.jetty.http.PreEncodedHttpField in project jetty.project by eclipse.

the class Server method getDateField.

/* ------------------------------------------------------------ */
public HttpField getDateField() {
    long now = System.currentTimeMillis();
    long seconds = now / 1000;
    DateField df = _dateField;
    if (df == null || df._seconds != seconds) {
        try (Locker.Lock lock = _dateLocker.lock()) {
            df = _dateField;
            if (df == null || df._seconds != seconds) {
                HttpField field = new PreEncodedHttpField(HttpHeader.DATE, DateGenerator.formatDate(now));
                _dateField = new DateField(seconds, field);
                return field;
            }
        }
    }
    return df._dateField;
}
Also used : Locker(org.eclipse.jetty.util.thread.Locker) HttpField(org.eclipse.jetty.http.HttpField) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField)

Example 3 with PreEncodedHttpField

use of org.eclipse.jetty.http.PreEncodedHttpField in project jetty.project by eclipse.

the class HpackEncoder method encode.

public void encode(ByteBuffer buffer, HttpField field) {
    if (field.getValue() == null)
        field = new HttpField(field.getHeader(), field.getName(), "");
    int field_size = field.getName().length() + field.getValue().length();
    _headerListSize += field_size + 32;
    final int p = _debug ? buffer.position() : -1;
    String encoding = null;
    // Is there an entry for the field?
    Entry entry = _context.get(field);
    if (entry != null) {
        // Known field entry, so encode it as indexed
        if (entry.isStatic()) {
            buffer.put(((StaticEntry) entry).getEncodedField());
            if (_debug)
                encoding = "IdxFieldS1";
        } else {
            int index = _context.index(entry);
            buffer.put((byte) 0x80);
            NBitInteger.encode(buffer, 7, index);
            if (_debug)
                encoding = "IdxField" + (entry.isStatic() ? "S" : "") + (1 + NBitInteger.octectsNeeded(7, index));
        }
    } else {
        // Unknown field entry, so we will have to send literally.
        final boolean indexed;
        // But do we know it's name?
        HttpHeader header = field.getHeader();
        // Select encoding strategy
        if (header == null) {
            // Select encoding strategy for unknown header names
            Entry name = _context.get(field.getName());
            if (field instanceof PreEncodedHttpField) {
                int i = buffer.position();
                ((PreEncodedHttpField) field).putTo(buffer, HttpVersion.HTTP_2);
                byte b = buffer.get(i);
                indexed = b < 0 || b >= 0x40;
                if (_debug)
                    encoding = indexed ? "PreEncodedIdx" : "PreEncoded";
            } else // has the custom header name been seen before?
            if (name == null) {
                // unknown name and value, so let's index this just in case it is
                // the first time we have seen a custom name or a custom field.
                // unless the name is changing, this is worthwhile
                indexed = true;
                encodeName(buffer, (byte) 0x40, 6, field.getName(), null);
                encodeValue(buffer, true, field.getValue());
                if (_debug)
                    encoding = "LitHuffNHuffVIdx";
            } else {
                // known custom name, but unknown value.
                // This is probably a custom field with changing value, so don't index.
                indexed = false;
                encodeName(buffer, (byte) 0x00, 4, field.getName(), null);
                encodeValue(buffer, true, field.getValue());
                if (_debug)
                    encoding = "LitHuffNHuffV!Idx";
            }
        } else {
            // Select encoding strategy for known header names
            Entry name = _context.get(header);
            if (field instanceof PreEncodedHttpField) {
                // Preencoded field
                int i = buffer.position();
                ((PreEncodedHttpField) field).putTo(buffer, HttpVersion.HTTP_2);
                byte b = buffer.get(i);
                indexed = b < 0 || b >= 0x40;
                if (_debug)
                    encoding = indexed ? "PreEncodedIdx" : "PreEncoded";
            } else if (__DO_NOT_INDEX.contains(header)) {
                // Non indexed field
                indexed = false;
                boolean never_index = __NEVER_INDEX.contains(header);
                boolean huffman = !__DO_NOT_HUFFMAN.contains(header);
                encodeName(buffer, never_index ? (byte) 0x10 : (byte) 0x00, 4, header.asString(), name);
                encodeValue(buffer, huffman, field.getValue());
                if (_debug)
                    encoding = "Lit" + ((name == null) ? "HuffN" : ("IdxN" + (name.isStatic() ? "S" : "") + (1 + NBitInteger.octectsNeeded(4, _context.index(name))))) + (huffman ? "HuffV" : "LitV") + (indexed ? "Idx" : (never_index ? "!!Idx" : "!Idx"));
            } else if (field_size >= _context.getMaxDynamicTableSize() || header == HttpHeader.CONTENT_LENGTH && field.getValue().length() > 2) {
                // Non indexed if field too large or a content length for 3 digits or more
                indexed = false;
                encodeName(buffer, (byte) 0x00, 4, header.asString(), name);
                encodeValue(buffer, true, field.getValue());
                if (_debug)
                    encoding = "LitIdxNS" + (1 + NBitInteger.octectsNeeded(4, _context.index(name))) + "HuffV!Idx";
            } else {
                // indexed
                indexed = true;
                boolean huffman = !__DO_NOT_HUFFMAN.contains(header);
                encodeName(buffer, (byte) 0x40, 6, header.asString(), name);
                encodeValue(buffer, huffman, field.getValue());
                if (_debug)
                    encoding = ((name == null) ? "LitHuffN" : ("LitIdxN" + (name.isStatic() ? "S" : "") + (1 + NBitInteger.octectsNeeded(6, _context.index(name))))) + (huffman ? "HuffVIdx" : "LitVIdx");
            }
        }
        // table and reference set.
        if (indexed)
            if (_context.add(field) == null)
                throw new IllegalStateException();
    }
    if (_debug) {
        int e = buffer.position();
        if (LOG.isDebugEnabled())
            LOG.debug("encode {}:'{}' to '{}'", encoding, field, TypeUtil.toHexString(buffer.array(), buffer.arrayOffset() + p, e - p));
    }
}
Also used : StaticEntry(org.eclipse.jetty.http2.hpack.HpackContext.StaticEntry) Entry(org.eclipse.jetty.http2.hpack.HpackContext.Entry) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpField(org.eclipse.jetty.http.HttpField) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField)

Aggregations

PreEncodedHttpField (org.eclipse.jetty.http.PreEncodedHttpField)3 HttpField (org.eclipse.jetty.http.HttpField)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 ServletException (javax.servlet.ServletException)1 UnavailableException (javax.servlet.UnavailableException)1 HttpContent (org.eclipse.jetty.http.HttpContent)1 HttpHeader (org.eclipse.jetty.http.HttpHeader)1 Entry (org.eclipse.jetty.http2.hpack.HpackContext.Entry)1 StaticEntry (org.eclipse.jetty.http2.hpack.HpackContext.StaticEntry)1 CachedContentFactory (org.eclipse.jetty.server.CachedContentFactory)1 ResourceContentFactory (org.eclipse.jetty.server.ResourceContentFactory)1 Locker (org.eclipse.jetty.util.thread.Locker)1