Fix POP3 TOP command : line count is *after* headers

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@180 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2008-11-29 14:22:00 +00:00
parent 64e0688f0d
commit 9aca6b5ae7
1 changed files with 42 additions and 10 deletions

View File

@ -25,7 +25,7 @@ public class PopConnection extends AbstractConnection {
// Initialize the streams and start the thread
public PopConnection(Socket clientSocket) {
super("PopConnection", clientSocket);
super("PopConnection", clientSocket, null);
}
public long getTotalMessagesLength() {
@ -138,7 +138,7 @@ public class PopConnection extends AbstractConnection {
sendOK(""+messageNumber+" "+message.size);
} catch (NumberFormatException e) {
sendERR("Invalid message index: "+token);
} catch (ArrayIndexOutOfBoundsException e) {
} catch (IndexOutOfBoundsException e) {
sendERR("Invalid message index: "+token);
}
} else {
@ -189,10 +189,11 @@ public class PopConnection extends AbstractConnection {
sendERR("invalid message number");
}
} else if ("TOP".equalsIgnoreCase(command)) {
int message = 0;
try {
int message = Integer.valueOf(tokens.nextToken()) - 1;
message = Integer.valueOf(tokens.nextToken());
int lines = Integer.valueOf(tokens.nextToken());
ExchangeSession.Message m = messages.get(message);
ExchangeSession.Message m = messages.get(message-1);
sendOK("");
m.write(new TopOutputStream(os, lines));
sendClient("");
@ -200,6 +201,8 @@ public class PopConnection extends AbstractConnection {
} catch (SocketException e) {
// can not send error to client after a socket exception
DavGatewayTray.warn("Client closed connection ", e);
} catch (IndexOutOfBoundsException e) {
sendERR("Invalid message index: "+message);
} catch (Exception e) {
sendERR("error retreiving top of messages");
DavGatewayTray.error(e.getMessage(), e);
@ -247,11 +250,17 @@ public class PopConnection extends AbstractConnection {
}
/**
* Filter to limit output lines to maxLines
* Filter to limit output lines to max body lines after header
*/
private class TopOutputStream extends FilterOutputStream {
protected static final int START = 0;
protected static final int CR = 1;
protected static final int CRLF = 2;
protected static final int CRLFCR = 3;
protected static final int BODY = 4;
private int maxLines;
protected int maxLines;
protected int STATE = START;
public TopOutputStream(OutputStream os, int maxLines) {
super(os);
@ -260,12 +269,35 @@ public class PopConnection extends AbstractConnection {
@Override
public void write(int b) throws IOException {
if (maxLines > 0) {
if (STATE != BODY || maxLines > 0) {
super.write(b);
}
if (b == '\n') {
maxLines--;
if (STATE == BODY) {
if (b == '\n') {
maxLines--;
}
} else if (STATE == START) {
if (b == '\r') {
STATE = CR;
}
} else if (STATE == CR) {
if (b == '\n') {
STATE = CRLF;
} else {
STATE = START;
}
} else if (STATE == CRLF) {
if (b == '\r') {
STATE = CRLFCR;
} else {
STATE = START;
}
} else if (STATE == CRLFCR) {
if (b == '\n') {
STATE = BODY;
} else {
STATE = START;
}
}
}
}