@Override public Response length(final long length) { len = length; rsp.header("Content-Length", Long.toString(length)); return this; }
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(); } }
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; }
@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 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(); }
@Override public Response length(final long length) { len = length; rsp.header("Content-Length", Long.toString(length)); return this; }
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(); } }
@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; }
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 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(); }