@Override public HttpEncoder body(SelectionKey key, Object...parts) { if (0 == (state & (REQ_LINE_SENT|RESP_LINE_SENT))) { throw new InvalidStateException("Cannot send body without initiation line."); } if (0 != (state & CONTENT_LENGTH_SENT) && 0 != (state & BODY_SENT)) { throw new InvalidStateException("Body with fixed content length already sent."); } if (method != null && !method.hasBody()) { throw new InvalidStateException("This request method does not have body."); } ByteBuffer body = (ByteBuffer)parts[0]; // TODO coerce to body or store refs somewhere if (0 == (state & CONTENT_LENGTH_SENT)) { // TODO if content length previously sent, check for proper file size header(key, H_CONTENT_LENGTH, "" + body.remaining()); } finishHeaders(key); state |= BODY_SENT; this.body = body; return this; }
@Override public HttpEncoder header(SelectionKey key, String name, String value) { if (0 == (state & (REQ_LINE_SENT|RESP_LINE_SENT))) { throw new InvalidStateException("Cannot send headers without initiation line."); } if (gsf(HEADERS_SENT)) { throw new InvalidStateException("Cannot send headers anymore."); } buf.append(TextUtil.camelCase(name)).append(": ").append(value).append(CRLF); chkh(H_CONTENT_LENGTH, name, CONTENT_LENGTH_SENT); chkh(H_DATE, name, DATE_SENT); chkh(H_SERVER, name, SERVER_NAME_SENT); chkh(H_USER_AGENT, name, USER_AGENT_SENT); chkh(H_CONNECTION, name, CONNECTION_SENT); chkh(H_HOST, name, HOST_SENT); if (H_CONNECTION.equalsIgnoreCase(name) && "close".equalsIgnoreCase(value)) { state |= CONNECTION_CLOSE; } return this; }