1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 19:22:22 -05:00

Caldav: Fix Connection: close handling and ensure full content read

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@351 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2009-02-12 01:33:49 +00:00
parent bb71ebf95a
commit 0ac3b1a938
2 changed files with 58 additions and 31 deletions

View File

@ -62,11 +62,20 @@ public class CaldavConnection extends AbstractConnection {
throw new IOException("Invalid content length: " + contentLength); throw new IOException("Invalid content length: " + contentLength);
} }
char[] buffer = new char[size]; char[] buffer = new char[size];
StringBuilder builder = new StringBuilder();
int actualSize = in.read(buffer); int actualSize = in.read(buffer);
builder.append(buffer, 0, actualSize);
if (actualSize < 0) { if (actualSize < 0) {
throw new IOException("End of stream reached reading content"); throw new IOException("End of stream reached reading content");
} }
return new String(buffer, 0, actualSize); // dirty hack to ensure full content read
// TODO : replace with a dedicated reader
while (builder.toString().getBytes("UTF-8").length < size) {
actualSize = in.read(buffer);
builder.append(buffer, 0, actualSize);
}
return builder.toString();
} }
} }
@ -105,6 +114,8 @@ public class CaldavConnection extends AbstractConnection {
String path = tokens.nextToken(); String path = tokens.nextToken();
String content = getContent(headers.get("content-length")); String content = getContent(headers.get("content-length"));
setSocketTimeout(headers.get("keep-alive")); setSocketTimeout(headers.get("keep-alive"));
// client requested connection close
closed = "close".equals(headers.get("connection"));
if ("OPTIONS".equals(command)) { if ("OPTIONS".equals(command)) {
sendOptions(); sendOptions();
} else if (!headers.containsKey("authorization")) { } else if (!headers.containsKey("authorization")) {
@ -227,8 +238,14 @@ public class CaldavConnection extends AbstractConnection {
&& session.getEmail().equalsIgnoreCase(paths[2])) { && session.getEmail().equalsIgnoreCase(paths[2])) {
String etag = headers.get("if-match"); String etag = headers.get("if-match");
String noneMatch = headers.get("if-none-match"); String noneMatch = headers.get("if-none-match");
int status = session.createOrUpdateEvent(paths[4], body, etag, noneMatch); ExchangeSession.EventResult eventResult = session.createOrUpdateEvent(paths[4], body, etag, noneMatch);
sendHttpResponse(status, true); if (eventResult.etag != null) {
HashMap<String, String> responseHeaders = new HashMap<String, String>();
responseHeaders.put("GetETag", eventResult.etag);
sendHttpResponse(eventResult.status, responseHeaders, "text/html", "", true);
} else {
sendHttpResponse(eventResult.status, true);
}
} else if ("DELETE".equals(command) && "users".equals(paths[1]) && paths.length == 5 && "calendar".equals(paths[3]) } else if ("DELETE".equals(command) && "users".equals(paths[1]) && paths.length == 5 && "calendar".equals(paths[3])
// only current user for now // only current user for now
@ -690,8 +707,8 @@ public class CaldavConnection extends AbstractConnection {
if (contentType != null) { if (contentType != null) {
sendClient("Content-Type: " + contentType); sendClient("Content-Type: " + contentType);
} }
sendClient("Connection: " + (keepAlive ? "keep-alive" : "close")); closed = closed || !keepAlive;
closed = !keepAlive; sendClient("Connection: " + (closed ? "close" : "keep-alive"));
if (content != null && content.length() > 0) { if (content != null && content.length() > 0) {
sendClient("Content-Length: " + content.getBytes("UTF-8").length); sendClient("Content-Length: " + content.getBytes("UTF-8").length);
} else { } else {

View File

@ -1329,9 +1329,14 @@ public class ExchangeSession {
} }
public class EventResult {
public int status;
public String etag;
}
public int sendEvent(String icsBody) throws IOException { public int sendEvent(String icsBody) throws IOException {
String messageUrl = URIUtil.encodePathQuery(draftsUrl + "/" + UUID.randomUUID().toString() + ".EML"); String messageUrl = URIUtil.encodePathQuery(draftsUrl + "/" + UUID.randomUUID().toString() + ".EML");
int status = internalCreateOrUpdateEvent(messageUrl, icsBody, null, null); int status = internalCreateOrUpdateEvent(messageUrl, icsBody, null, null).status;
if (status != HttpStatus.SC_CREATED) { if (status != HttpStatus.SC_CREATED) {
return status; return status;
} else { } else {
@ -1344,12 +1349,12 @@ public class ExchangeSession {
} }
} }
public int createOrUpdateEvent(String path, String icsBody, String etag, String noneMatch) throws IOException { public EventResult createOrUpdateEvent(String path, String icsBody, String etag, String noneMatch) throws IOException {
String messageUrl = URIUtil.encodePathQuery(calendarUrl + "/" + URIUtil.decode(path)); String messageUrl = URIUtil.encodePathQuery(calendarUrl + "/" + URIUtil.decode(path));
return internalCreateOrUpdateEvent(messageUrl, icsBody, etag, noneMatch); return internalCreateOrUpdateEvent(messageUrl, icsBody, etag, noneMatch);
} }
protected int internalCreateOrUpdateEvent(String messageUrl, String icsBody, String etag, String noneMatch) throws IOException { protected EventResult internalCreateOrUpdateEvent(String messageUrl, String icsBody, String etag, String noneMatch) throws IOException {
String uid = UUID.randomUUID().toString(); String uid = UUID.randomUUID().toString();
PutMethod putmethod = new PutMethod(messageUrl); PutMethod putmethod = new PutMethod(messageUrl);
putmethod.setRequestHeader("Translate", "f"); putmethod.setRequestHeader("Translate", "f");
@ -1391,7 +1396,12 @@ public class ExchangeSession {
} finally { } finally {
putmethod.releaseConnection(); putmethod.releaseConnection();
} }
return status; EventResult eventResult = new EventResult();
eventResult.status = status;
if (putmethod.getResponseHeader("GetETag") != null) {
eventResult.etag = putmethod.getResponseHeader("GetETag").getValue();
}
return eventResult;
} }