Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions SharpGs/Cors/Cors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using SharpGs.Cors.Internal;

namespace SharpGs.Cors
{
public class Cors : ICors
{
private IOrigin Origins = new Origin();
private IHttpMethods Methods = new HttpMethods();
private IResponseHeader ResponseHeaders = new ResponseHeader();
public int MaxAge { get; set; }
public Cors()
{
MaxAge = 1800;
}

public Cors(XDocument document)
{
foreach (var origin in document.Descendants("Origin"))
Origins.AddOrigin(origin.Value);
foreach (var method in document.Descendants("Method"))
Methods.AddMethod(method.Value.ToUpper());
foreach (var response in document.Descendants("ResponseHeader"))
ResponseHeaders.AddResponseHeader(response.Value);
var firstOrDefault = document.Descendants("MaxAgeSec").FirstOrDefault();
if (firstOrDefault != null)
MaxAge = Convert.ToInt16(firstOrDefault.Value);
}
public void AddOrigin(string origin)
{
Origins.AddOrigin(origin);
}

public void AddMethod(string method)
{
Methods.AddMethod(method);
}

public void AddHeader(string responseHeader)
{
ResponseHeaders.AddResponseHeader(responseHeader);
}

public string ToXmlString()
{
var sb = new StringBuilder("<?xml version='1.0' encoding='utf-8'?>").AppendLine();
sb.AppendLine("<CorsConfig>");
sb.AppendLine("\t<Cors>");
sb.Append(Origins.ToXmlString());
sb.Append(Methods.ToXmlString());
sb.Append(ResponseHeaders.ToXmlString());
sb.AppendLine("\t\t<MaxAgeSec>" + MaxAge + "</MaxAgeSec>");
sb.AppendLine("\t</Cors>");
sb.AppendLine("</CorsConfig>");
return sb.ToString();
}
}
}
45 changes: 45 additions & 0 deletions SharpGs/Cors/ICors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using SharpGs.RestApi;

namespace SharpGs.Cors
{
/// <summary>
/// Setup Cross Origin
/// </summary>
public interface ICors
{

/// <summary>
/// Adds an origin to the origin collection.
///
/// An Origin permitted for cross origin resource sharing with this Google Cloud Storage bucket.
/// For example, http://origin1.example.com. You can use wildcards ("*"). However, if the host part of the Origin begins with a *,
/// then any origin that ends with the same suffix will be considered a match. If you supply a value that consists of only
/// the wildcard (<Origin>*</Origin>), this gives access to ALL origins.
/// </summary>
/// <param name="origin">The origin to add</param>
void AddOrigin(string origin);

/// <summary>
/// Adds a request method to the collection of methods supported in this configuration. Valid values are GET, HEAD, PUT, POST, and DELETE.
///
/// </summary>
/// <param name="method"></param>
void AddMethod(string method);

/// <summary>
/// Adds a response header/s that the user agent is permitted to share across origins.
/// </summary>
void AddHeader(string responseHeader);

/// <summary>
/// This value is used to respond to preflight requests, indicating the number of seconds that the client (browser) is allowed to make
/// requests before the client must repeat the preflight request. (Indicates cache expiry time.) Preflight requests are required if
/// the request method contains non-simple headers or if the request method is not POST, GET, or HEAD. The value is returned in the
/// Access-Control-Max-Age header in responses to preflight requests.
/// </summary>
int MaxAge { get; set; }


string ToXmlString();
}
}
8 changes: 8 additions & 0 deletions SharpGs/Cors/IHttpMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SharpGs.Cors
{
public interface IHttpMethods
{
void AddMethod(string method);
string ToXmlString();
}
}
8 changes: 8 additions & 0 deletions SharpGs/Cors/IOrigin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SharpGs.Cors
{
public interface IOrigin
{
void AddOrigin(string origin);
string ToXmlString();
}
}
8 changes: 8 additions & 0 deletions SharpGs/Cors/IResponseHeader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SharpGs.Cors
{
public interface IResponseHeader
{
void AddResponseHeader(string responseHeader);
string ToXmlString();
}
}
31 changes: 31 additions & 0 deletions SharpGs/Cors/Internal/HttpMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Text;

namespace SharpGs.Cors.Internal
{
internal class HttpMethods : IHttpMethods
{
public List<string> RequestMethods;

public HttpMethods()
{
RequestMethods = new List<string>();
}

public void AddMethod(string method)
{
RequestMethods.Add(method);
}

public string ToXmlString()
{
var sb = new StringBuilder();
sb.AppendLine("\t\t<Methods>");
foreach (var requestMethod in RequestMethods)
{
sb.AppendLine(string.Format("\t\t\t<Method>{0}</Method>", requestMethod));
}
return sb.AppendLine("\t\t</Methods>").ToString();
}
}
}
32 changes: 32 additions & 0 deletions SharpGs/Cors/Internal/Origin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace SharpGs.Cors.Internal
{
internal class Origin : IOrigin
{
public List<string> Origins;
public Origin()
{
Origins = new List<string>();
}

public void AddOrigin(string origin)
{
Origins.Add(origin);
}

public string ToXmlString()
{
var sb = new StringBuilder();
sb.AppendLine("\t\t<Origins>");
foreach (var origin in Origins)
{
sb.AppendLine(String.Format("\t\t\t<Origin>{0}</Origin>", origin));
}
return sb.AppendLine("\t\t</Origins>").ToString();
}

}
}
27 changes: 27 additions & 0 deletions SharpGs/Cors/Internal/ResponseHeader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Text;

namespace SharpGs.Cors.Internal
{
internal class ResponseHeader: IResponseHeader
{
public List<string> ResponseHeaders = new List<string>();
public void AddResponseHeader(string responseHeader)
{
ResponseHeaders.Add(responseHeader);
}

public string ToXmlString()
{
if (ResponseHeaders.Count == 0)
return null;
var sb = new StringBuilder();
sb.AppendLine("\t\t<ResponseHeaders>");
foreach (var responseHeader in ResponseHeaders)
{
sb.AppendLine(string.Format("\t\t\t<ResponseHeader>{0}<ResponseHeader>", responseHeader));
}
return sb.AppendLine("\t\t</ResponseHeaders>").ToString();
}
}
}
13 changes: 13 additions & 0 deletions SharpGs/IBucket.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using SharpGs.Cors;

namespace SharpGs
{
Expand Down Expand Up @@ -51,5 +52,17 @@ public interface IBucket : IAclSetup
/// Delete current bucket
/// </summary>
void Delete();

/// <summary>
/// Retrieve Cross origin resource sharing object for the bucket
/// </summary>
ICors Cors { get; }

/// <summary>
/// Save a new resource sharing object for the bucket
/// </summary>
/// <param name="cors"></param>
void CorsSave(ICors cors);

}
}
104 changes: 52 additions & 52 deletions SharpGs/ISharpGs.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
using System;
using System.Collections.Generic;
using System.Net;
namespace SharpGs
{
/// <summary>
/// Google Storage service connector
/// </summary>
public interface ISharpGs : IDisposable
{
/// <summary>
/// Get the AuthKey of GoogleStorage account
/// </summary>
string AuthKey { get; }
/// <summary>
/// Get the AuthSecret of GoogleStorage account
/// </summary>
string AuthSecret { get; }
/// <summary>
/// If true, uses https secured protocol. True by default
/// </summary>
bool SecuredConnection { get; set; }
/// <summary>
/// If is not null, connection will be done through proxy
/// </summary>
IWebProxy WebProxy { get; set; }
/// <summary>
/// Request server for list of buckets
/// https://code.google.com/apis/storage/docs/reference-methods.html#getservice
/// </summary>
IEnumerable<IBucket> Buckets { get; }
/// <summary>
/// Create new bucket. If bucket with same name already exists in GS, exception will be thrown.
/// https://code.google.com/apis/storage/docs/reference-methods.html#putbucket
/// </summary>
/// <param name="name">unique name of the bucket</param>
void CreateBucket(string name);
/// <summary>
/// Get the bucket info
/// </summary>
/// <param name="name">name of the bucket</param>
/// <returns>bucket information</returns>
IBucket GetBucket(string name);
}
}
using System;
using System.Collections.Generic;
using System.Net;

namespace SharpGs
{
/// <summary>
/// Google Storage service connector
/// </summary>
public interface ISharpGs : IDisposable
{
/// <summary>
/// Get the AuthKey of GoogleStorage account
/// </summary>
string AuthKey { get; }

/// <summary>
/// Get the AuthSecret of GoogleStorage account
/// </summary>
string AuthSecret { get; }

/// <summary>
/// If true, uses https secured protocol. True by default
/// </summary>
bool SecuredConnection { get; set; }

/// <summary>
/// If is not null, connection will be done through proxy
/// </summary>
IWebProxy WebProxy { get; set; }

/// <summary>
/// Request server for list of buckets
/// https://code.google.com/apis/storage/docs/reference-methods.html#getservice
/// </summary>
IEnumerable<IBucket> Buckets { get; }

/// <summary>
/// Create new bucket. If bucket with same name already exists in GS, exception will be thrown.
/// https://code.google.com/apis/storage/docs/reference-methods.html#putbucket
/// </summary>
/// <param name="name">unique name of the bucket</param>
void CreateBucket(string name);

/// <summary>
/// Get the bucket info
/// </summary>
/// <param name="name">name of the bucket</param>
/// <returns>bucket information</returns>
IBucket GetBucket(string name);
}
}
22 changes: 22 additions & 0 deletions SharpGs/Internal/Bucket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Xml.Linq;
using SharpGs.Acl;
using SharpGs.Acl.Internal;
using SharpGs.Cors;
using SharpGs.RestApi;

namespace SharpGs.Internal
Expand Down Expand Up @@ -82,6 +83,27 @@ public void Delete()
_connector.Request(RequestMethod.DELETE, Name);
}

/// <summary>
/// Retrieve Cross origin resource sharing object for the bucket
/// </summary>
public ICors Cors
{
get
{
var result = _connector.Request(RequestMethod.CORS_GET, Name);
return new Cors.Cors(result);
}
}

/// <summary>
/// Save a new resource sharing object for the bucket
/// </summary>
/// <param name="cors"></param>
public void CorsSave(ICors cors)
{
_connector.Request(RequestMethod.CORS_SET, Name, null, Encoding.UTF8.GetBytes(cors.ToXmlString()), "application/xml");
}

public IAccessControlList Acl
{
get
Expand Down
Loading