Iterator<HttpString> nameIterator = this.nameIterator;
Iterator<String> valueIterator = this.valueIterator;
int charIndex = this.charIndex;
int length;
String string = this.string;
HttpString headerName = this.headerName;
int res;
// BUFFER IS FLIPPED COMING IN
if (state != STATE_START && buffer.hasRemaining()) {
log.trace("Flushing remaining buffer");
do {
res = next.write(buffer);
if (res == 0) {
return state;
}
} while (buffer.hasRemaining());
}
buffer.clear();
// BUFFER IS NOW EMPTY FOR FILLING
for (;;) {
switch (state) {
case STATE_BODY: {
// shouldn't be possible, but might as well do the right thing anyway
return state;
}
case STATE_START: {
log.trace("Starting request");
// we assume that our buffer has enough space for the initial request line plus one more CR+LF
assert buffer.remaining() >= 0x100;
string = request.getMethod();
length = string.length();
for (charIndex = 0; charIndex < length; charIndex ++) {
buffer.put((byte) string.charAt(charIndex));
}
buffer.put((byte) ' ');
string = request.getURIString();
length = string.length();
for (charIndex = 0; charIndex < length; charIndex ++) {
buffer.put((byte) string.charAt(charIndex));
}
buffer.put((byte) ' ');
string = request.getProtocol();
length = string.length();
for (charIndex = 0; charIndex < length; charIndex ++) {
buffer.put((byte) string.charAt(charIndex));
}
buffer.put((byte) '\r').put((byte) '\n');
HeaderMap headers = request.getRequestHeaders();
nameIterator = headers.getHeaderNames().iterator();
if (! nameIterator.hasNext()) {
log.trace("No request headers");
buffer.put((byte) '\r').put((byte) '\n');
buffer.flip();
while (buffer.hasRemaining()) {
res = next.write(buffer);
if (res == 0) {
log.trace("Continuation");
return STATE_BUF_FLUSH;
}
}
pooledBuffer.free();
pooledBuffer = null;
log.trace("Body");
return STATE_BODY;
}
headerName = nameIterator.next();
charIndex = 0;
// fall thru
}
case STATE_HDR_NAME: {
log.tracef("Processing header '%s'", headerName);
length = headerName.length();
while (charIndex < length) {
if (buffer.hasRemaining()) {
buffer.put(headerName.byteAt(charIndex++));
} else {
log.trace("Buffer flush");
buffer.flip();
do {
res = next.write(buffer);