1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-24 08:38:51 -05:00

Added support for non-Exchange 2003 standard login paths (web scraping for login URL).

Added support for default mailbox path setting if we can't automatically determine what it is.
Fixed a bug exposed by changes relating to a fix for non-relative email paths being returned.
This commit is contained in:
Matthew Brace 2009-01-08 05:47:10 +00:00
parent 7954b02da6
commit 7d5d29e078

View File

@ -181,7 +181,6 @@ public class WebDavStore extends Store {
messageBody = getFolderListXml();
headers.put("Brief", "t");
dataset = processRequest(this.mUrl, "SEARCH", messageBody, headers);
folderUrls = dataset.getHrefs();
@ -439,6 +438,7 @@ public class WebDavStore extends Store {
CookieStore cookies = null;
String[] urlParts = url.split("/");
String finalUrl = "";
String loginUrl = new String();
for (int i = 0; i <= 2; i++) {
if (i != 0) {
@ -448,11 +448,54 @@ public class WebDavStore extends Store {
}
}
if (finalUrl.equals("")) {
throw new MessagingException("doAuthentication failed, unable to construct URL to post login credentials to.");
}
loginUrl = finalUrl + authPath;
try {
/* Browser Client */
DefaultHttpClient httpclient = getTrustedHttpClient();
/* Verb Fix issue */
/**
* This is in a separate block because I really don't like how it's done.
* This basically scrapes the OWA login page for the form submission URL.
* UGLY!
*/
{
HttpGet httpget = new HttpGet(finalUrl);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode > 300 ||
statusCode < 200) {
throw new MessagingException("Error during authentication: "+
response.getStatusLine().toString()+"\n\n");
}
if (entity != null) {
InputStream istream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(istream), 4096);
String tempText = new String();
boolean matched = false;
while ((tempText = reader.readLine()) != null &&
!matched) {
if (tempText.indexOf(" action") >= 0) {
String[] tagParts = tempText.split("\"");
loginUrl = finalUrl + tagParts[1];
matched = true;
}
}
istream.close();
}
}
/* Post Method */
HttpPost httppost = new HttpPost(finalUrl + authPath);
HttpPost httppost = new HttpPost(loginUrl);
/** Build the POST data to use */
ArrayList<BasicNameValuePair> pairs = new ArrayList();
@ -466,6 +509,7 @@ public class WebDavStore extends Store {
try {
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs);
String tempUrl = "";
httppost.setEntity(formEntity);
@ -497,11 +541,17 @@ public class WebDavStore extends Store {
while ((tempText = reader.readLine()) != null) {
if (tempText.indexOf("BASE href") >= 0) {
String[] tagParts = tempText.split("\"");
this.mUrl = tagParts[1];
tempUrl = tagParts[1];
}
}
}
if (tempUrl.equals("")) {
this.mUrl = finalUrl + "/Exchange/" + this.alias + "/";
} else {
this.mUrl = tempUrl;
}
} catch (UnsupportedEncodingException uee) {
Log.e(Email.LOG_TAG, "Error encoding POST data for authencation");
}
@ -1355,18 +1405,15 @@ public class WebDavStore extends Store {
}
public void setUrl(String url) {
//TODO: bleh. This is an ugly hack, but does prevent a crash if the url is relative
//FIXME: so fix, or better yet:
//XXX: prevent URLs from getting to us that are broken.
if (! (url.toLowerCase().contains(WebDavStore.this.mUrl.toLowerCase()+this.mFolder.toString().toLowerCase()))) {
//TODO: This is a not as ugly hack (ie, it will actually work)
//XXX: prevent URLs from getting to us that are broken
if (!(url.toLowerCase().contains("http"))) {
if (!(url.startsWith("/"))){
url = "/" + url;
}
url = WebDavStore.this.mUrl + this.mFolder + url;
}
if (!(url.toLowerCase().contains(WebDavStore.this.mUrl.toLowerCase()))) {
url = WebDavStore.this.mUrl + url;
}
String[] urlParts = url.split("/");
int length = urlParts.length;
String end = urlParts[length - 1];