@Override public void end() { if (!rsp.committed()) { if (status == null) { status(rsp.statusCode()); } writeCookies(); /** * Do we need to figure it out Content-Length? */ boolean lenSet = rsp.header("Content-Length").isPresent() || rsp.header("Transfer-Encoding").isPresent(); if (!lenSet) { int statusCode = status.value(); boolean hasBody = true; if (statusCode >= 100 && statusCode < 200) { hasBody = false; } else if (statusCode == 204 || statusCode == 304) { hasBody = false; } if (hasBody) { rsp.header("Content-Length", "0"); } } } rsp.end(); }
private void contentDisposition(final String filename) throws IOException { List<String> headers = rsp.headers("Content-Disposition"); if (headers.isEmpty()) { String basename = filename; int last = filename.lastIndexOf('/'); if (last >= 0) { basename = basename.substring(last + 1); } String cs = charset.name(); String ebasename = URLEncoder.encode(basename, cs).replaceAll("\\+", "%20"); header("Content-Disposition", String.format(CONTENT_DISPOSITION, basename, cs, ebasename)); } }
public void reset() { status = null; this.cookies.clear(); rsp.reset(); }
private long[] byteRange() { long len = rsp.header("Content-Length").map(Long::parseLong).orElse(-1L); if (len > 0) { if (byteRange.isPresent()) { String raw = byteRange.get(); long[] range = ByteRange.parse(raw); long start = range[0]; long end = range[1]; if (start == -1) { start = len - end; end = len - 1; } if (end == -1 || end > len - 1) { end = len - 1; } if (start > end) { throw new Err(Status.REQUESTED_RANGE_NOT_SATISFIABLE, raw); } // offset long limit = (end - start + 1); rsp.header("Accept-Ranges", "bytes"); rsp.header("Content-Range", "bytes " + start + "-" + end + "/" + len); rsp.header("Content-Length", Long.toString(limit)); rsp.statusCode(Status.PARTIAL_CONTENT.value()); return new long[]{start, limit}; } } return null; }
@Override protected void _send(final byte[] bytes) throws Exception { rsp.send(bytes); }
@Override public Response length(final long length) { len = length; rsp.header("Content-Length", Long.toString(length)); return this; }
@Override public Response status(final Status status) { this.status = requireNonNull(status, "Status required."); rsp.statusCode(status.value()); failure = status.isError(); return this; }
@Override public boolean committed() { return rsp.committed(); }
@Override protected void _send(final ByteBuffer buffer) throws Exception { rsp.send(buffer); }
private long[] byteRange() { long len = rsp.header("Content-Length").map(Long::parseLong).orElse(-1L); if (len > 0) { if (byteRange.isPresent()) { String raw = byteRange.get(); long[] range = ByteRange.parse(raw); long start = range[0]; long end = range[1]; if (start == -1) { start = len - end; end = len - 1; } if (end == -1 || end > len - 1) { end = len - 1; } if (start > end) { throw new Err(Status.REQUESTED_RANGE_NOT_SATISFIABLE, raw); } // offset long limit = (end - start + 1); rsp.header("Accept-Ranges", "bytes"); rsp.header("Content-Range", "bytes " + start + "-" + end + "/" + len); rsp.header("Content-Length", Long.toString(limit)); rsp.statusCode(Status.PARTIAL_CONTENT.value()); return new long[]{start, limit}; } } return null; }
private void writeCookies() { if (cookies.size() > 0) { List<String> setCookie = cookies.values().stream() .map(Cookie::encode) .collect(Collectors.toList()); rsp.header("Set-Cookie", setCookie); cookies.clear(); } }
@Override public Response status(final Status status) { this.status = requireNonNull(status, "Status required."); rsp.statusCode(status.value()); failure = status.isError(); return this; }
@Override public boolean committed() { return rsp.committed(); }
@Override public void end() { if (!rsp.committed()) { if (status == null) { status(rsp.statusCode()); } writeCookies(); /** * Do we need to figure it out Content-Length? */ boolean lenSet = rsp.header("Content-Length").isPresent() || rsp.header("Transfer-Encoding").isPresent(); if (!lenSet) { int statusCode = status.value(); boolean hasBody = true; if (statusCode >= 100 && statusCode < 200) { hasBody = false; } else if (statusCode == 204 || statusCode == 304) { hasBody = false; } if (hasBody) { rsp.header("Content-Length", "0"); } } } rsp.end(); }
@SuppressWarnings("unchecked") private Response setHeader(final String name, final Object value) { if (!committed()) { if (value instanceof Iterable) { List<String> values = StreamSupport.stream(((Iterable<Object>) value).spliterator(), false) .map(Headers::encode) .collect(Collectors.toList()); rsp.header(name, values); } else { if (LOCATION.equalsIgnoreCase(name)) { String location = value.toString(); String cpath = req.contextPath(); if ("back".equalsIgnoreCase(location)) { location = referer.orElse(cpath + "/"); } else if (location.startsWith("/") && !location.startsWith(cpath)) { location = cpath + location; } rsp.header(LOCATION, location); } else { if ("Content-Type".equalsIgnoreCase(name)) { // keep type reference this.type = MediaType.valueOf(value.toString()); } rsp.header(name, Headers.encode(value)); } } } return this; }
@Override public Mutant header(final String name) { requireNonNull(name, "A header's name is required."); return new MutantImpl(parserExecutor, new StrParamReferenceImpl("header", name, rsp.headers(name))); }
public void reset() { status = null; this.cookies.clear(); rsp.reset(); }
@Override public Response length(final long length) { len = length; rsp.header("Content-Length", Long.toString(length)); return this; }