43 import java.nio.charset.*;
51 class Reply
implements Sendable {
59 private String reason;
60 private Code(
int i, String r) { number = i; reason = r; }
61 public String toString() {
return number +
" " + reason; }
63 static Code OK =
new Code(200,
"OK");
64 static Code BAD_REQUEST =
new Code(400,
"Bad Request");
65 static Code NOT_FOUND =
new Code(404,
"Not Found");
66 static Code METHOD_NOT_ALLOWED =
new Code(405,
"Method Not Allowed");
71 private Content content;
72 private boolean headersOnly;
74 Reply(Code rc, Content c) {
78 Reply(Code rc, Content c, Request.Action head) {
81 headersOnly = (head == Request.Action.HEAD);
84 private static String CRLF =
"\r\n";
85 private static Charset ascii = Charset.forName(
"US-ASCII");
87 private ByteBuffer hbb =
null;
89 private ByteBuffer headers() {
90 CharBuffer cb = CharBuffer.allocate(1024);
93 cb.put(
"HTTP/1.0 ").put(code.toString()).put(CRLF);
94 cb.put(
"Server: niossl/0.1").put(CRLF);
95 cb.put(
"Content-type: ").put(content.type()).put(CRLF);
96 cb.put(
"Content-length: ")
97 .put(Long.toString(content.length())).put(CRLF);
100 }
catch (BufferOverflowException x) {
101 assert(cb.capacity() < (1 << 16));
102 cb = CharBuffer.allocate(cb.capacity() * 2);
107 return ascii.encode(cb);
110 public void prepare() throws IOException {
115 public boolean send(ChannelIO cio)
throws IOException {
118 throw new IllegalStateException();
120 if (hbb.hasRemaining()) {
121 if (cio.write(hbb) <= 0)
126 if (content.send(cio))
130 if (!cio.dataFlush())
136 public void release() throws IOException {