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:
parent
64e0688f0d
commit
9aca6b5ae7
|
@ -25,7 +25,7 @@ public class PopConnection extends AbstractConnection {
|
||||||
|
|
||||||
// Initialize the streams and start the thread
|
// Initialize the streams and start the thread
|
||||||
public PopConnection(Socket clientSocket) {
|
public PopConnection(Socket clientSocket) {
|
||||||
super("PopConnection", clientSocket);
|
super("PopConnection", clientSocket, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getTotalMessagesLength() {
|
public long getTotalMessagesLength() {
|
||||||
|
@ -138,7 +138,7 @@ public class PopConnection extends AbstractConnection {
|
||||||
sendOK(""+messageNumber+" "+message.size);
|
sendOK(""+messageNumber+" "+message.size);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
sendERR("Invalid message index: "+token);
|
sendERR("Invalid message index: "+token);
|
||||||
} catch (ArrayIndexOutOfBoundsException e) {
|
} catch (IndexOutOfBoundsException e) {
|
||||||
sendERR("Invalid message index: "+token);
|
sendERR("Invalid message index: "+token);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -189,10 +189,11 @@ public class PopConnection extends AbstractConnection {
|
||||||
sendERR("invalid message number");
|
sendERR("invalid message number");
|
||||||
}
|
}
|
||||||
} else if ("TOP".equalsIgnoreCase(command)) {
|
} else if ("TOP".equalsIgnoreCase(command)) {
|
||||||
|
int message = 0;
|
||||||
try {
|
try {
|
||||||
int message = Integer.valueOf(tokens.nextToken()) - 1;
|
message = Integer.valueOf(tokens.nextToken());
|
||||||
int lines = Integer.valueOf(tokens.nextToken());
|
int lines = Integer.valueOf(tokens.nextToken());
|
||||||
ExchangeSession.Message m = messages.get(message);
|
ExchangeSession.Message m = messages.get(message-1);
|
||||||
sendOK("");
|
sendOK("");
|
||||||
m.write(new TopOutputStream(os, lines));
|
m.write(new TopOutputStream(os, lines));
|
||||||
sendClient("");
|
sendClient("");
|
||||||
|
@ -200,6 +201,8 @@ public class PopConnection extends AbstractConnection {
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
// can not send error to client after a socket exception
|
// can not send error to client after a socket exception
|
||||||
DavGatewayTray.warn("Client closed connection ", e);
|
DavGatewayTray.warn("Client closed connection ", e);
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
sendERR("Invalid message index: "+message);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
sendERR("error retreiving top of messages");
|
sendERR("error retreiving top of messages");
|
||||||
DavGatewayTray.error(e.getMessage(), e);
|
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 {
|
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) {
|
public TopOutputStream(OutputStream os, int maxLines) {
|
||||||
super(os);
|
super(os);
|
||||||
|
@ -260,12 +269,35 @@ public class PopConnection extends AbstractConnection {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(int b) throws IOException {
|
public void write(int b) throws IOException {
|
||||||
if (maxLines > 0) {
|
if (STATE != BODY || maxLines > 0) {
|
||||||
super.write(b);
|
super.write(b);
|
||||||
}
|
}
|
||||||
|
if (STATE == BODY) {
|
||||||
if (b == '\n') {
|
if (b == '\n') {
|
||||||
maxLines--;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue