Skip to content

Commit

Permalink
authentication support
Browse files Browse the repository at this point in the history
  • Loading branch information
kolosy committed Nov 26, 2009
1 parent 27ec922 commit 0ad1376
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
5 changes: 1 addition & 4 deletions Tests/Divan.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{09FB3192-8F25-4112-A8BB-93C83F6C3DDD}</ProjectGuid>
<OutputType>Library</OutputType>
Expand Down Expand Up @@ -76,9 +76,6 @@
<Name>Divan</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
2 changes: 1 addition & 1 deletion src/CouchDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public ICouchDocument SaveDocument(ICouchDocument document)

savedDoc = WriteDocument(document);
}
catch (CouchConflictException ex)
catch (CouchConflictException)
{
if (reconcilingDoc == null)
throw;
Expand Down
5 changes: 4 additions & 1 deletion src/CouchRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public CouchRequest Check(string message)

private HttpWebRequest GetRequest()
{
Uri requestUri = new UriBuilder("http", server.Host, server.Port, ((db != null) ? db.Name + "/" : "") + path, query).Uri;
var requestUri = new UriBuilder("http", server.Host, server.Port, ((db != null) ? db.Name + "/" : "") + path, query).Uri;
var request = WebRequest.Create(requestUri) as HttpWebRequest;
if (request == null)
{
Expand All @@ -210,6 +210,9 @@ private HttpWebRequest GetRequest()
request.Headers.Add(header.Key, header.Value);
}

if (!string.IsNullOrEmpty(server.EncodedCredentials))
request.Headers.Add("Authorization", server.EncodedCredentials);

if (postData != null)
{
//byte[] bytes = Encoding.UTF8.GetBytes(postData);
Expand Down
17 changes: 16 additions & 1 deletion src/CouchServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

Expand All @@ -25,16 +26,30 @@ public class CouchServer
public readonly string Host;
public readonly int Port;

public readonly string UserName;
public readonly string Password;
public readonly string EncodedCredentials;

public string DatabasePrefix = ""; // Used by databases to prefix their names

public CouchServer(string host, int port)
public CouchServer(string host, int port, string user, string pass)
{
Host = host;
Port = port;
UserName = user;
Password = pass;

if (!String.IsNullOrEmpty(UserName))
EncodedCredentials = "Basic " +
Convert.ToBase64String(Encoding.ASCII.GetBytes(UserName + ":" + Password));

Debug(string.Format("CouchServer({0}:{1})", host, port));
}

public CouchServer(string host, int port): this(host, port, null, null)
{
}

public CouchServer(string host)
: this(host, DefaultPort)
{
Expand Down

0 comments on commit 0ad1376

Please sign in to comment.