Skip to content

Commit

Permalink
Bugfix #20: Throw an exception if the response is null
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Murphy committed Dec 13, 2011
1 parent 7bf7c17 commit 258a50c
Showing 1 changed file with 33 additions and 64 deletions.
97 changes: 33 additions & 64 deletions LoveSeat/Support/CouchRequest.cs
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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;
}
Expand Down

0 comments on commit 258a50c

Please sign in to comment.