org.apache.tomcat.util.http.FastHttpDateFormat Java Examples

The following examples show how to use org.apache.tomcat.util.http.FastHttpDateFormat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebdavServlet.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Get a String representation of this lock token.
 */
@Override
public String toString() {

    StringBuilder result =  new StringBuilder("Type:");
    result.append(type);
    result.append("\nScope:");
    result.append(scope);
    result.append("\nDepth:");
    result.append(depth);
    result.append("\nOwner:");
    result.append(owner);
    result.append("\nExpiration:");
    result.append(FastHttpDateFormat.formatDate(expiresAt));
    Enumeration<String> tokensList = tokens.elements();
    while (tokensList.hasMoreElements()) {
        result.append("\nToken:");
        result.append(tokensList.nextElement());
    }
    result.append("\n");
    return result.toString();
}
 
Example #2
Source File: Response.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Add the specified date header to the specified value.
 *
 * @param name Name of the header to set
 * @param value Date value to be set
 */
@Override
public void addDateHeader(String name, long value) {

    if (name == null || name.length() == 0) {
        return;
    }

    if (isCommitted()) {
        return;
    }

    // Ignore any call from an included servlet
    if (included) {
        return;
    }

    addHeader(name, FastHttpDateFormat.formatDate(value));
}
 
Example #3
Source File: Response.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Set the specified date header to the specified value.
 *
 * @param name Name of the header to set
 * @param value Date value to be set
 */
@Override
public void setDateHeader(String name, long value) {

    if (name == null || name.length() == 0) {
        return;
    }

    if (isCommitted()) {
        return;
    }

    // Ignore any call from an included servlet
    if (included) {
        return;
    }

    setHeader(name, FastHttpDateFormat.formatDate(value));
}
 
Example #4
Source File: Request.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Return the value of the specified date header, if any; otherwise
 * return -1.
 *
 * @param name Name of the requested date header
 * @return the date as a long
 *
 * @exception IllegalArgumentException if the specified header value
 *  cannot be converted to a date
 */
@Override
public long getDateHeader(String name) {

    String value = getHeader(name);
    if (value == null) {
        return -1L;
    }

    // Attempt to convert the date header in a variety of formats
    long result = FastHttpDateFormat.parseDate(value);
    if (result != (-1L)) {
        return result;
    }
    throw new IllegalArgumentException(value);

}
 
Example #5
Source File: WebdavServlet.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Get a String representation of this lock token.
 */
@Override
public String toString() {

    StringBuilder result =  new StringBuilder("Type:");
    result.append(type);
    result.append("\nScope:");
    result.append(scope);
    result.append("\nDepth:");
    result.append(depth);
    result.append("\nOwner:");
    result.append(owner);
    result.append("\nExpiration:");
    result.append(FastHttpDateFormat.formatDate(expiresAt, null));
    Enumeration<String> tokensList = tokens.elements();
    while (tokensList.hasMoreElements()) {
        result.append("\nToken:");
        result.append(tokensList.nextElement());
    }
    result.append("\n");
    return result.toString();
}
 
Example #6
Source File: Request.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Return the value of the specified date header, if any; otherwise
 * return -1.
 *
 * @param name Name of the requested date header
 *
 * @exception IllegalArgumentException if the specified header value
 *  cannot be converted to a date
 */
@Override
public long getDateHeader(String name) {

    String value = getHeader(name);
    if (value == null) {
        return (-1L);
    }

    // Attempt to convert the date header in a variety of formats
    long result = FastHttpDateFormat.parseDate(value, formats);
    if (result != (-1L)) {
        return result;
    }
    throw new IllegalArgumentException(value);

}
 
Example #7
Source File: InMemoryRequest.java    From component-runtime with Apache License 2.0 6 votes vote down vote up
@Override
public long getDateHeader(final String name) {
    final String value = getHeader(name);
    if (value == null) {
        return -1L;
    }

    final SimpleDateFormat[] formats = new SimpleDateFormat[DATE_FORMATS.length];
    for (int i = 0; i < formats.length; i++) {
        formats[i] = SimpleDateFormat.class.cast(DATE_FORMATS[i].clone());
    }

    final long result = FastHttpDateFormat.parseDate(value, formats);
    if (result != -1L) {
        return result;
    }
    throw new IllegalArgumentException(value);
}
 
Example #8
Source File: WebdavServlet.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Get a String representation of this lock token.
 */
@Override
public String toString() {

    StringBuilder result =  new StringBuilder("Type:");
    result.append(type);
    result.append("\nScope:");
    result.append(scope);
    result.append("\nDepth:");
    result.append(depth);
    result.append("\nOwner:");
    result.append(owner);
    result.append("\nExpiration:");
    result.append(FastHttpDateFormat.formatDate(expiresAt, null));
    Enumeration<String> tokensList = tokens.elements();
    while (tokensList.hasMoreElements()) {
        result.append("\nToken:");
        result.append(tokensList.nextElement());
    }
    result.append("\n");
    return result.toString();
}
 
Example #9
Source File: Request.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Return the value of the specified date header, if any; otherwise
 * return -1.
 *
 * @param name Name of the requested date header
 *
 * @exception IllegalArgumentException if the specified header value
 *  cannot be converted to a date
 */
@Override
public long getDateHeader(String name) {

    String value = getHeader(name);
    if (value == null) {
        return (-1L);
    }

    // Attempt to convert the date header in a variety of formats
    long result = FastHttpDateFormat.parseDate(value, formats);
    if (result != (-1L)) {
        return result;
    }
    throw new IllegalArgumentException(value);

}
 
Example #10
Source File: RemoteIpFilter.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public long getDateHeader(String name) {
    String value = getHeader(name);
    if (value == null) {
        return -1;
    }
    long date = FastHttpDateFormat.parseDate(value);
    if (date == -1) {
        throw new IllegalArgumentException(value);
    }
    return date;
}
 
Example #11
Source File: DirContextURLConnection.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
protected String getHeaderValueAsString(Object headerValue) {
    if (headerValue == null) return null;
    if (headerValue instanceof Date) {
        // return date strings (ie Last-Modified) in HTTP format, rather
        // than Java format
        return FastHttpDateFormat.formatDate(
                ((Date)headerValue).getTime(), null);
    }
    return headerValue.toString();
}
 
Example #12
Source File: DirContextURLConnection.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
protected String getHeaderValueAsString(Object headerValue) {
    if (headerValue == null) return null;
    if (headerValue instanceof Date) {
        // return date strings (ie Last-Modified) in HTTP format, rather
        // than Java format
        return FastHttpDateFormat.formatDate(
                ((Date)headerValue).getTime(), null);
    }
    return headerValue.toString();
}
 
Example #13
Source File: ResolverImpl.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * The following are not implemented:
 * - SERVER_ADMIN
 * - API_VERSION
 * - IS_SUBREQ
 */
@Override
public String resolve(String key) {
    if (key.equals("HTTP_USER_AGENT")) {
        return request.getHeader("user-agent");
    } else if (key.equals("HTTP_REFERER")) {
        return request.getHeader("referer");
    } else if (key.equals("HTTP_COOKIE")) {
        return request.getHeader("cookie");
    } else if (key.equals("HTTP_FORWARDED")) {
        return request.getHeader("forwarded");
    } else if (key.equals("HTTP_HOST")) {
        String host = request.getHeader("host");
        if (host != null) {
            int index = host.indexOf(':');
            if (index != -1) {
                host = host.substring(0, index);
            }
        }
        return host;
    } else if (key.equals("HTTP_PROXY_CONNECTION")) {
        return request.getHeader("proxy-connection");
    } else if (key.equals("HTTP_ACCEPT")) {
        return request.getHeader("accept");
    } else if (key.equals("REMOTE_ADDR")) {
        return request.getRemoteAddr();
    } else if (key.equals("REMOTE_HOST")) {
        return request.getRemoteHost();
    } else if (key.equals("REMOTE_PORT")) {
        return String.valueOf(request.getRemotePort());
    } else if (key.equals("REMOTE_USER")) {
        return request.getRemoteUser();
    } else if (key.equals("REMOTE_IDENT")) {
        return request.getRemoteUser();
    } else if (key.equals("REQUEST_METHOD")) {
        return request.getMethod();
    } else if (key.equals("SCRIPT_FILENAME")) {
        return request.getServletContext().getRealPath(request.getServletPath());
    } else if (key.equals("REQUEST_PATH")) {
        return request.getRequestPathMB().toString();
    } else if (key.equals("CONTEXT_PATH")) {
        return request.getContextPath();
    } else if (key.equals("SERVLET_PATH")) {
        return emptyStringIfNull(request.getServletPath());
    } else if (key.equals("PATH_INFO")) {
        return emptyStringIfNull(request.getPathInfo());
    } else if (key.equals("QUERY_STRING")) {
        return emptyStringIfNull(request.getQueryString());
    } else if (key.equals("AUTH_TYPE")) {
        return request.getAuthType();
    } else if (key.equals("DOCUMENT_ROOT")) {
        return request.getServletContext().getRealPath("/");
    } else if (key.equals("SERVER_NAME")) {
        return request.getLocalName();
    } else if (key.equals("SERVER_ADDR")) {
        return request.getLocalAddr();
    } else if (key.equals("SERVER_PORT")) {
        return String.valueOf(request.getLocalPort());
    } else if (key.equals("SERVER_PROTOCOL")) {
        return request.getProtocol();
    } else if (key.equals("SERVER_SOFTWARE")) {
        return "tomcat";
    } else if (key.equals("THE_REQUEST")) {
        return request.getMethod() + " " + request.getRequestURI()
        + " " + request.getProtocol();
    } else if (key.equals("REQUEST_URI")) {
        return request.getRequestURI();
    } else if (key.equals("REQUEST_FILENAME")) {
        return request.getPathTranslated();
    } else if (key.equals("HTTPS")) {
        return request.isSecure() ? "on" : "off";
    } else if (key.equals("TIME_YEAR")) {
        return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
    } else if (key.equals("TIME_MON")) {
        return String.valueOf(Calendar.getInstance().get(Calendar.MONTH));
    } else if (key.equals("TIME_DAY")) {
        return String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
    } else if (key.equals("TIME_HOUR")) {
        return String.valueOf(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
    } else if (key.equals("TIME_MIN")) {
        return String.valueOf(Calendar.getInstance().get(Calendar.MINUTE));
    } else if (key.equals("TIME_SEC")) {
        return String.valueOf(Calendar.getInstance().get(Calendar.SECOND));
    } else if (key.equals("TIME_WDAY")) {
        return String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_WEEK));
    } else if (key.equals("TIME")) {
        return FastHttpDateFormat.getCurrentDate();
    }
    return null;
}
 
Example #14
Source File: AbstractResource.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public final String getLastModifiedHttp() {
    return FastHttpDateFormat.formatDate(getLastModified());
}
 
Example #15
Source File: StreamProcessor.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
static void prepareHeaders(Request coyoteRequest, Response coyoteResponse,
        Http2Protocol protocol, Stream stream) {
    MimeHeaders headers = coyoteResponse.getMimeHeaders();
    int statusCode = coyoteResponse.getStatus();

    // Add the pseudo header for status
    headers.addValue(":status").setString(Integer.toString(statusCode));

    // Check to see if a response body is present
    if (!(statusCode < 200 || statusCode == 204 || statusCode == 205 || statusCode == 304)) {
        String contentType = coyoteResponse.getContentType();
        if (contentType != null) {
            headers.setValue("content-type").setString(contentType);
        }
        String contentLanguage = coyoteResponse.getContentLanguage();
        if (contentLanguage != null) {
            headers.setValue("content-language").setString(contentLanguage);
        }
        // Add a content-length header if a content length has been set unless
        // the application has already added one
        long contentLength = coyoteResponse.getContentLengthLong();
        if (contentLength != -1 && headers.getValue("content-length") == null) {
            headers.addValue("content-length").setLong(contentLength);
        }
    } else {
        if (statusCode == 205) {
            // RFC 7231 requires the server to explicitly signal an empty
            // response in this case
            coyoteResponse.setContentLength(0);
        } else {
            coyoteResponse.setContentLength(-1);
        }
    }

    // Add date header unless it is an informational response or the
    // application has already set one
    if (statusCode >= 200 && headers.getValue("date") == null) {
        headers.addValue("date").setString(FastHttpDateFormat.getCurrentDate());
    }

    if (protocol != null && protocol.useCompression(coyoteRequest, coyoteResponse)) {
        // Enable compression. Headers will have been set. Need to configure
        // output filter at this point.
        stream.addOutputFilter(new GzipOutputFilter());
    }
}
 
Example #16
Source File: TestStreamProcessor.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testPrepareHeaders() throws Exception {
    enableHttp2();

    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp");
    Context ctxt = tomcat.addWebapp(null, "", appDir.getAbsolutePath());

    Tomcat.addServlet(ctxt, "simple", new SimpleServlet());
    ctxt.addServletMappingDecoded("/simple", "simple");

    tomcat.start();

    openClientConnection();
    doHttpUpgrade();
    sendClientPreface();
    validateHttp2InitialResponse();

    byte[] frameHeader = new byte[9];
    ByteBuffer headersPayload = ByteBuffer.allocate(128);

    List<Header> headers = new ArrayList<>(3);
    headers.add(new Header(":method", "GET"));
    headers.add(new Header(":scheme", "http"));
    headers.add(new Header(":path", "/index.html"));
    headers.add(new Header(":authority", "localhost:" + getPort()));
    headers.add(new Header("if-modified-since", FastHttpDateFormat.getCurrentDate()));

    buildGetRequest(frameHeader, headersPayload, null, headers, 3);

    writeFrame(frameHeader, headersPayload);

    parser.readFrame(true);

    StringBuilder expected = new StringBuilder();
    expected.append("3-HeadersStart\n");
    expected.append("3-Header-[:status]-[304]\n");
    // Different line-endings -> different files size -> different weak eTag
    if (JrePlatform.IS_WINDOWS) {
        expected.append("3-Header-[etag]-[W/\"957-1447269522000\"]\n");
    } else {
        expected.append("3-Header-[etag]-[W/\"934-1447269522000\"]\n");
    }
    expected.append("3-Header-[date]-[Wed, 11 Nov 2015 19:18:42 GMT]\n");
    expected.append("3-HeadersEnd\n");

    Assert.assertEquals(expected.toString(), output.getTrace());
}