Permalink
Browse files

Bugfix #20: Throw an exception if the response is null

  • Loading branch information...
1 parent 7bf7c17 commit 258a50c7e6665841d780ca0411edcd5de0aab859 @soitgoes committed Dec 13, 2011
Showing with 33 additions and 64 deletions.
  1. +33 −64 LoveSeat/Support/CouchRequest.cs
@@ -1,18 +1,16 @@
using System.Net;
using System.Text;
+using System.Web;
using Newtonsoft.Json.Linq;
-namespace LoveSeat.Support
-{
- public class CouchRequest
- {
+namespace LoveSeat.Support {
+ public class CouchRequest {
private const string INVALID_USERNAME_OR_PASSWORD = "reason=Name or password is incorrect";
private const string NOT_AUTHORIZED = "reason=You are not authorized to access this db.";
private readonly HttpWebRequest request;
public CouchRequest(string uri)
- : this(uri, new Cookie(), null)
- {
+ : this(uri, new Cookie(), null) {
}
/// <summary>
@@ -21,8 +19,7 @@ public CouchRequest(string uri)
/// <param name="uri"></param>
/// <param name="authCookie"></param>
/// <param name="eTag"></param>
- public CouchRequest(string uri, Cookie authCookie, string eTag)
- {
+ public CouchRequest(string uri, Cookie authCookie, string eTag) {
request = (HttpWebRequest)WebRequest.Create(uri);
request.Headers.Clear(); //important
if (!string.IsNullOrEmpty(eTag))
@@ -43,8 +40,7 @@ public CouchRequest(string uri, Cookie authCookie, string eTag)
/// <param name="uri"></param>
/// <param name="username"></param>
/// <param name="password"></param>
- public CouchRequest(string uri, string username, string password)
- {
+ public CouchRequest(string uri, string username, string password) {
request = (HttpWebRequest)WebRequest.Create(uri);
request.Headers.Clear(); //important
@@ -68,127 +64,100 @@ public CouchRequest(string uri, string username, string password)
public int? Timeout { get; set; }
- public CouchRequest Put()
- {
+ public CouchRequest Put() {
request.Method = "PUT";
return this;
}
- public CouchRequest Get()
- {
+ public CouchRequest Get() {
request.Method = "GET";
return this;
}
- public CouchRequest Post()
- {
+ public CouchRequest Post() {
request.Method = "POST";
return this;
}
- public CouchRequest Delete()
- {
+ public CouchRequest Delete() {
request.Method = "DELETE";
return this;
}
- public CouchRequest Data(string data)
- {
- using (var body = request.GetRequestStream())
- {
+ public CouchRequest Data(string data) {
+ using (var body = request.GetRequestStream()) {
var encodedData = Encoding.UTF8.GetBytes(data);
body.Write(encodedData, 0, encodedData.Length);
}
return this;
}
- public CouchRequest Data(byte[] attachment)
- {
- using (var body = request.GetRequestStream())
- {
+ public CouchRequest Data(byte[] attachment) {
+ using (var body = request.GetRequestStream()) {
body.Write(attachment, 0, attachment.Length);
}
return this;
}
- public CouchRequest Data(JObject obj)
- {
+ public CouchRequest Data(JObject obj) {
return Data(obj.ToString());
}
- public CouchRequest ContentType(string contentType)
- {
+ public CouchRequest ContentType(string contentType) {
request.ContentType = contentType;
return this;
}
- public CouchRequest Form()
- {
+ public CouchRequest Form() {
request.ContentType = "application/x-www-form-urlencoded";
return this;
}
- public CouchRequest Json()
- {
+ public CouchRequest Json() {
request.ContentType = "application/json";
return this;
}
- public HttpWebRequest GetRequest()
- {
+ public HttpWebRequest GetRequest() {
return request;
}
- public HttpWebResponse GetResponse()
- {
+ public HttpWebResponse GetResponse() {
bool failedAuth = false;
- try
- {
+ try {
var response = (HttpWebResponse)request.GetResponse();
-
string msg = "";
- if (isAuthenticateOrAuthorized(response, ref msg) == false)
- {
+ if (isAuthenticateOrAuthorized(response, ref msg) == false) {
failedAuth = true;
throw new WebException(msg, new System.Exception(msg));
}
return response;
- }
- catch (WebException webEx)
- {
- if (failedAuth == true)
- {
+ } catch (WebException webEx) {
+ if (failedAuth == true) {
throw webEx;
}
var response = (HttpWebResponse)webEx.Response;
- if (response != null)
- {
- return response;
- }
+ if (response == null)
+ throw new HttpException("Request failed to receive a response");
+ return response;
}
- return null;
+ throw new HttpException("Request failed to receive a response");
}
/// <summary>
/// Checks response if username and password was valid
/// </summary>
/// <param name="response"></param>
- private bool isAuthenticateOrAuthorized(HttpWebResponse response, ref string message)
- {
+ private bool isAuthenticateOrAuthorized(HttpWebResponse response, ref string message) {
//"reason=Name or password is incorrect"
// Check if query string is okay
string[] split = response.ResponseUri.Query.Split('&');
- if (split.Length > 0)
- {
- for (int i = 0; i < split.Length; i++)
- {
+ if (split.Length > 0) {
+ for (int i = 0; i < split.Length; i++) {
string temp = System.Web.HttpUtility.UrlDecode(split[i]);
- if (temp.Contains(INVALID_USERNAME_OR_PASSWORD) == true)
- {
+ if (temp.Contains(INVALID_USERNAME_OR_PASSWORD) == true) {
message = "Invalid username or password";
return false;
- }
- else if (temp.Contains(NOT_AUTHORIZED) == true)
- {
+ } else if (temp.Contains(NOT_AUTHORIZED) == true) {
message = "Not Authorized to access database";
return false;
}

0 comments on commit 258a50c

Please sign in to comment.