use of clojure.lang.Seqable in project http-kit by http-kit.
the class HeaderMap method encodeHeaders.
public void encodeHeaders(DynamicBytes bytes) {
final int total = size * 2;
for (int i = 0; i < total; i += 2) {
String k = (String) arrays[i];
Object v = arrays[i + 1];
// omit invalid headers and prevent possible exceptions (e.g., NullPointerException)
if (k == null || v == null) {
continue;
}
// ring spec says it could be a seq
if (v instanceof Seqable) {
ISeq seq = ((Seqable) v).seq();
while (seq != null) {
bytes.append(k);
bytes.append(COLON, SP);
bytes.append(seq.first().toString(), HttpUtils.UTF_8);
bytes.append(CR, LF);
seq = seq.next();
}
} else {
bytes.append(k);
bytes.append(COLON, SP);
// supposed to be ISO-8859-1, but utf-8 is compatible.
// filename in Content-Disposition can be utf8
bytes.append(v.toString(), HttpUtils.UTF_8);
bytes.append(CR, LF);
}
}
bytes.append(CR, LF);
}
use of clojure.lang.Seqable in project http-kit by http-kit.
the class HttpUtils method bodyBuffer.
public static ByteBuffer bodyBuffer(Object body) throws IOException {
if (body == null) {
return null;
} else if (body instanceof String) {
byte[] b = ((String) body).getBytes(UTF_8);
return ByteBuffer.wrap(b);
} else if (body instanceof InputStream) {
DynamicBytes b = readAll((InputStream) body);
return ByteBuffer.wrap(b.get(), 0, b.length());
} else if (body instanceof File) {
// serving file is better be done by Nginx
return readAll((File) body);
} else if (body instanceof Seqable) {
ISeq seq = ((Seqable) body).seq();
if (seq == null) {
return null;
} else {
DynamicBytes b = new DynamicBytes(seq.count() * 512);
while (seq != null) {
b.append(seq.first().toString(), UTF_8);
seq = seq.next();
}
return ByteBuffer.wrap(b.get(), 0, b.length());
}
// makes ultimate optimization possible: no copy
} else if (body instanceof ByteBuffer) {
return (ByteBuffer) body;
} else {
throw new RuntimeException(body.getClass() + " is not understandable");
}
}
Aggregations