Search in sources :

Example 91 with HttpFields

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

the class ThreadLimitHandler method getXForwardedFor.

private String getXForwardedFor(Request request) {
    // Get the right most XForwarded-For for value.
    // This is the value from the closest proxy and the only one that
    // can be trusted.
    String forwarded_for = null;
    HttpFields httpFields = request.getHttpFields();
    for (HttpField field : httpFields) if (_forwardedHeader.equalsIgnoreCase(field.getName()))
        forwarded_for = field.getValue();
    if (forwarded_for == null || forwarded_for.isEmpty())
        return null;
    int comma = forwarded_for.lastIndexOf(',');
    return (comma >= 0) ? forwarded_for.substring(comma + 1).trim() : forwarded_for;
}
Also used : HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields)

Example 92 with HttpFields

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

the class Request method getPushBuilder.

/* ------------------------------------------------------------ */
/** Get a PushBuilder associated with this request initialized as follows:<ul>
     * <li>The method is initialized to "GET"</li>
     * <li>The headers from this request are copied to the Builder, except for:<ul>
     *   <li>Conditional headers (eg. If-Modified-Since)
     *   <li>Range headers
     *   <li>Expect headers
     *   <li>Authorization headers
     *   <li>Referrer headers
     * </ul></li>
     * <li>If the request was Authenticated, an Authorization header will
     * be set with a container generated token that will result in equivalent
     * Authorization</li>
     * <li>The query string from {@link #getQueryString()}
     * <li>The {@link #getRequestedSessionId()} value, unless at the time
     * of the call {@link #getSession(boolean)}
     * has previously been called to create a new {@link HttpSession}, in
     * which case the new session ID will be used as the PushBuilders
     * requested session ID.</li>
     * <li>The source of the requested session id will be the same as for
     * this request</li>
     * <li>The builders Referer header will be set to {@link #getRequestURL()}
     * plus any {@link #getQueryString()} </li>
     * <li>If {@link HttpServletResponse#addCookie(Cookie)} has been called
     * on the associated response, then a corresponding Cookie header will be added
     * to the PushBuilder, unless the {@link Cookie#getMaxAge()} is &lt;=0, in which
     * case the Cookie will be removed from the builder.</li>
     * <li>If this request has has the conditional headers If-Modified-Since or
     * If-None-Match then the {@link PushBuilderImpl#isConditional()} header is set
     * to true.
     * </ul>
     *
     * <p>Each call to getPushBuilder() will return a new instance
     * of a PushBuilder based off this Request.  Any mutations to the
     * returned PushBuilder are not reflected on future returns.
     * @return A new PushBuilder or null if push is not supported
     */
public PushBuilder getPushBuilder() {
    if (!isPushSupported())
        throw new IllegalStateException(String.format("%s,push=%b,channel=%s", this, isPush(), getHttpChannel()));
    HttpFields fields = new HttpFields(getHttpFields().size() + 5);
    boolean conditional = false;
    for (HttpField field : getHttpFields()) {
        HttpHeader header = field.getHeader();
        if (header == null)
            fields.add(field);
        else {
            switch(header) {
                case IF_MATCH:
                case IF_RANGE:
                case IF_UNMODIFIED_SINCE:
                case RANGE:
                case EXPECT:
                case REFERER:
                case COOKIE:
                    continue;
                case AUTHORIZATION:
                    continue;
                case IF_NONE_MATCH:
                case IF_MODIFIED_SINCE:
                    conditional = true;
                    continue;
                default:
                    fields.add(field);
            }
        }
    }
    String id = null;
    try {
        HttpSession session = getSession();
        if (session != null) {
            // checks if session is valid
            session.getLastAccessedTime();
            id = session.getId();
        } else
            id = getRequestedSessionId();
    } catch (IllegalStateException e) {
        id = getRequestedSessionId();
    }
    PushBuilder builder = new PushBuilderImpl(this, fields, getMethod(), getQueryString(), id, conditional);
    builder.addHeader("referer", getRequestURL().toString());
    return builder;
}
Also used : HttpHeader(org.eclipse.jetty.http.HttpHeader) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) HttpField(org.eclipse.jetty.http.HttpField) HttpSession(javax.servlet.http.HttpSession) HttpFields(org.eclipse.jetty.http.HttpFields)

Example 93 with HttpFields

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

the class ResourceService method passConditionalHeaders.

/* ------------------------------------------------------------ */
/* Check modification date headers.
     */
protected boolean passConditionalHeaders(HttpServletRequest request, HttpServletResponse response, HttpContent content) throws IOException {
    try {
        String ifm = null;
        String ifnm = null;
        String ifms = null;
        long ifums = -1;
        if (request instanceof Request) {
            // Find multiple fields by iteration as an optimization 
            HttpFields fields = ((Request) request).getHttpFields();
            for (int i = fields.size(); i-- > 0; ) {
                HttpField field = fields.getField(i);
                if (field.getHeader() != null) {
                    switch(field.getHeader()) {
                        case IF_MATCH:
                            ifm = field.getValue();
                            break;
                        case IF_NONE_MATCH:
                            ifnm = field.getValue();
                            break;
                        case IF_MODIFIED_SINCE:
                            ifms = field.getValue();
                            break;
                        case IF_UNMODIFIED_SINCE:
                            ifums = DateParser.parseDate(field.getValue());
                            break;
                        default:
                    }
                }
            }
        } else {
            ifm = request.getHeader(HttpHeader.IF_MATCH.asString());
            ifnm = request.getHeader(HttpHeader.IF_NONE_MATCH.asString());
            ifms = request.getHeader(HttpHeader.IF_MODIFIED_SINCE.asString());
            ifums = request.getDateHeader(HttpHeader.IF_UNMODIFIED_SINCE.asString());
        }
        if (!HttpMethod.HEAD.is(request.getMethod())) {
            if (_etags) {
                String etag = content.getETagValue();
                if (ifm != null) {
                    boolean match = false;
                    if (etag != null) {
                        QuotedCSV quoted = new QuotedCSV(true, ifm);
                        for (String tag : quoted) {
                            if (CompressedContentFormat.tagEquals(etag, tag)) {
                                match = true;
                                break;
                            }
                        }
                    }
                    if (!match) {
                        response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED);
                        return false;
                    }
                }
                if (ifnm != null && etag != null) {
                    // Handle special case of exact match OR gzip exact match
                    if (CompressedContentFormat.tagEquals(etag, ifnm) && ifnm.indexOf(',') < 0) {
                        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                        response.setHeader(HttpHeader.ETAG.asString(), ifnm);
                        return false;
                    }
                    // Handle list of tags
                    QuotedCSV quoted = new QuotedCSV(true, ifnm);
                    for (String tag : quoted) {
                        if (CompressedContentFormat.tagEquals(etag, tag)) {
                            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                            response.setHeader(HttpHeader.ETAG.asString(), tag);
                            return false;
                        }
                    }
                    // If etag requires content to be served, then do not check if-modified-since
                    return true;
                }
            }
            // Handle if modified since
            if (ifms != null) {
                //Get jetty's Response impl
                String mdlm = content.getLastModifiedValue();
                if (mdlm != null && ifms.equals(mdlm)) {
                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                    if (_etags)
                        response.setHeader(HttpHeader.ETAG.asString(), content.getETagValue());
                    response.flushBuffer();
                    return false;
                }
                long ifmsl = request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.asString());
                if (ifmsl != -1 && content.getResource().lastModified() / 1000 <= ifmsl / 1000) {
                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                    if (_etags)
                        response.setHeader(HttpHeader.ETAG.asString(), content.getETagValue());
                    response.flushBuffer();
                    return false;
                }
            }
            // Parse the if[un]modified dates and compare to resource
            if (ifums != -1 && content.getResource().lastModified() / 1000 > ifums / 1000) {
                response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
                return false;
            }
        }
    } catch (IllegalArgumentException iae) {
        if (!response.isCommitted())
            response.sendError(400, iae.getMessage());
        throw iae;
    }
    return true;
}
Also used : HttpField(org.eclipse.jetty.http.HttpField) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField) HttpFields(org.eclipse.jetty.http.HttpFields) HttpServletRequest(javax.servlet.http.HttpServletRequest) QuotedCSV(org.eclipse.jetty.http.QuotedCSV)

Example 94 with HttpFields

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

the class RFC2616BaseTest method test3_9.

/**
     * Test Quality Values
     * 
     * @see <a href="http://tools.ietf.org/html/rfc2616#section-3.9">RFC 2616 (section 3.9)</a>
     */
@Test
public void test3_9() {
    HttpFields fields = new HttpFields();
    fields.put("Q", "bbb;q=0.5,aaa,ccc;q=0.002,d;q=0,e;q=0.0001,ddd;q=0.001,aa2,abb;q=0.7");
    Enumeration<String> qualities = fields.getValues("Q", ", \t");
    List<?> list = HttpFields.qualityList(qualities);
    Assert.assertEquals("Quality parameters", "aaa", HttpFields.valueParameters(list.get(0).toString(), null));
    Assert.assertEquals("Quality parameters", "aa2", HttpFields.valueParameters(list.get(1).toString(), null));
    Assert.assertEquals("Quality parameters", "abb", HttpFields.valueParameters(list.get(2).toString(), null));
    Assert.assertEquals("Quality parameters", "bbb", HttpFields.valueParameters(list.get(3).toString(), null));
    Assert.assertEquals("Quality parameters", "ccc", HttpFields.valueParameters(list.get(4).toString(), null));
    Assert.assertEquals("Quality parameters", "ddd", HttpFields.valueParameters(list.get(5).toString(), null));
}
Also used : HttpFields(org.eclipse.jetty.http.HttpFields) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 95 with HttpFields

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

the class RFC2616BaseTest method test3_3.

/**
     * Test Date/Time format Specs.
     * 
     * @see <a href="http://tools.ietf.org/html/rfc2616#section-3.3">RFC 2616 (section 3.3)</a>
     */
@Test
public void test3_3() {
    Calendar expected = Calendar.getInstance();
    expected.set(Calendar.YEAR, 1994);
    expected.set(Calendar.MONTH, Calendar.NOVEMBER);
    expected.set(Calendar.DAY_OF_MONTH, 6);
    expected.set(Calendar.HOUR_OF_DAY, 8);
    expected.set(Calendar.MINUTE, 49);
    expected.set(Calendar.SECOND, 37);
    // Milliseconds is not compared
    expected.set(Calendar.MILLISECOND, 0);
    // Use GMT+0:00
    expected.set(Calendar.ZONE_OFFSET, 0);
    // No Daylight Savings Offset
    expected.set(Calendar.DST_OFFSET, 0);
    HttpFields fields = new HttpFields();
    // RFC 822 Preferred Format
    fields.put("D1", "Sun, 6 Nov 1994 08:49:37 GMT");
    // RFC 822 / RFC 850 Format
    fields.put("D2", "Sunday, 6-Nov-94 08:49:37 GMT");
    // RFC 850 / ANSIC C Format
    fields.put("D3", "Sun Nov  6 08:49:37 1994");
    // Test parsing
    assertDate("3.3.1 RFC 822 Preferred", expected, fields.getDateField("D1"));
    assertDate("3.3.1 RFC 822 / RFC 850", expected, fields.getDateField("D2"));
    assertDate("3.3.1 RFC 850 / ANSI C", expected, fields.getDateField("D3"));
    // Test formatting
    fields.putDateField("Date", expected.getTime().getTime());
    Assert.assertEquals("3.3.1 RFC 822 preferred", "Sun, 06 Nov 1994 08:49:37 GMT", fields.get("Date"));
}
Also used : Calendar(java.util.Calendar) HttpFields(org.eclipse.jetty.http.HttpFields) Test(org.junit.Test)

Aggregations

HttpFields (org.eclipse.jetty.http.HttpFields)172 Test (org.junit.Test)142 MetaData (org.eclipse.jetty.http.MetaData)117 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)105 CountDownLatch (java.util.concurrent.CountDownLatch)96 Stream (org.eclipse.jetty.http2.api.Stream)93 Session (org.eclipse.jetty.http2.api.Session)89 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)79 FuturePromise (org.eclipse.jetty.util.FuturePromise)69 HttpServletResponse (javax.servlet.http.HttpServletResponse)59 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)55 Callback (org.eclipse.jetty.util.Callback)53 ByteBuffer (java.nio.ByteBuffer)52 Promise (org.eclipse.jetty.util.Promise)49 HttpServletRequest (javax.servlet.http.HttpServletRequest)46 IOException (java.io.IOException)42 ServletException (javax.servlet.ServletException)39 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)37 HttpServlet (javax.servlet.http.HttpServlet)33 HashMap (java.util.HashMap)32