From d55cd9fe42b7492306f70c0e1b21f68adfec0323 Mon Sep 17 00:00:00 2001 From: shijiayun Date: Fri, 6 Jul 2012 10:34:02 +0800 Subject: [PATCH 1/3] add UpToken --- Example/RSDemo.cs | 68 ++++++++++++++++++----------- Json/JsonWriter.cs | 1 - QBox/AuthPolicy.cs | 74 ++++++++++++++++++++++++++++++++ QBox/Base64UrlSafe.cs | 2 - QBox/CallRet.cs | 3 -- QBox/Client.cs | 55 ++++++------------------ QBox/Config.cs | 5 +-- QBox/DeleteRet.cs | 3 -- QBox/DigestAuthClient.cs | 2 - QBox/DropRet.cs | 3 -- QBox/FileParameter.cs | 4 -- QBox/GetRet.cs | 3 -- QBox/MultiPartFormDataPost.cs | 81 ++++++++++++++--------------------- QBox/PublishRet.cs | 3 -- QBox/PutAuthRet.cs | 3 -- QBox/PutFileRet.cs | 3 -- QBox/RSClient.cs | 3 -- QBox/RSService.cs | 6 +-- QBox/StatRet.cs | 4 -- 19 files changed, 165 insertions(+), 161 deletions(-) create mode 100644 QBox/AuthPolicy.cs diff --git a/Example/RSDemo.cs b/Example/RSDemo.cs index e6be9b61..afdc4f92 100755 --- a/Example/RSDemo.cs +++ b/Example/RSDemo.cs @@ -15,6 +15,40 @@ public class RSDemo public static Client conn; public static RSService rs; + public static void Main() + { + Config.ACCESS_KEY = ""; + Config.SECRET_KEY = ""; + + conn = new DigestAuthClient(); + rs = new RSService(conn, tableName); + localFile = Process.GetCurrentProcess().MainModule.FileName; + key = System.IO.Path.GetFileName(localFile); + + try + { + PutFile(); + Get(); + Stat(); + Delete(); + CliPutFile(); + Get(); + Stat(); + Publish(); + UnPublish(); + Drop(); + + UpToken(); + } + catch (Exception e) + { + Console.WriteLine("RS Exception"); + Console.WriteLine(e.ToString()); + } + + Console.ReadLine(); + } + public static void PutFile() { Console.WriteLine("\n--- PutFile ---"); @@ -125,7 +159,7 @@ public static void UnPublish() { Console.WriteLine("\n--- UnPublish ---"); PrintInput(tableName, null, null, DEMO_DOMAIN); - PublishRet publishRet = rs.UnPublish(DEMO_DOMAIN); + PublishRet publishRet = rs.Unpublish(DEMO_DOMAIN); PrintRet(publishRet); if (!publishRet.OK) { @@ -170,6 +204,14 @@ public static void CliPutFile() } + public static void UpToken() + { + Console.WriteLine("\n--- Gen UpToken ---"); + var authPolicy = new AuthPolicy(tableName, 3600); + Console.WriteLine("Json: " + authPolicy.Marshal()); + Console.WriteLine("Token: " + authPolicy.MakeAuthTokenString()); + } + public static void PrintInput( string tblName, string key, string localFile, string domain) { @@ -196,29 +238,5 @@ public static void PrintRet(CallRet callRet) Console.WriteLine("Response:\n" + callRet.Response); Console.WriteLine(); } - - public static void Main() - { - Config.ACCESS_KEY = "RLT1NBD08g3kih5-0v8Yi6nX6cBhesa2Dju4P7mT"; - Config.SECRET_KEY = "k6uZoSDAdKBXQcNYG3UOm4bP3spDVkTg-9hWHIKm"; - - conn = new DigestAuthClient(); - rs = new RSService(conn, tableName); - localFile = Process.GetCurrentProcess().MainModule.FileName; - key = System.IO.Path.GetFileName(localFile); - - PutFile(); - Get(); - Stat(); - Delete(); - CliPutFile(); - Get(); - Stat(); - Publish(); - UnPublish(); - Drop(); - - Console.ReadLine(); - } } } diff --git a/Json/JsonWriter.cs b/Json/JsonWriter.cs index 1b266d83..bdcf9c44 100755 --- a/Json/JsonWriter.cs +++ b/Json/JsonWriter.cs @@ -365,7 +365,6 @@ public void Write (string str) context.ExpectingValue = false; } - [CLSCompliant(false)] public void Write (ulong number) { DoValidation (Condition.Value); diff --git a/QBox/AuthPolicy.cs b/QBox/AuthPolicy.cs new file mode 100644 index 00000000..23099506 --- /dev/null +++ b/QBox/AuthPolicy.cs @@ -0,0 +1,74 @@ +using System; +using System.Text; +using System.IO; +using LitJson; +using System.Security.Cryptography; + +namespace QBox +{ + public class AuthPolicy + { + public string Scope { get; set; } + public long Deadline { get; set; } + public string CallbackUrl { get; set; } + public string ReturnUrl { get; set; } + + public AuthPolicy(string scope, long expires) + { + Scope = scope; + DateTime begin = new DateTime(1970, 1, 1); + DateTime now = DateTime.Now; + TimeSpan interval = new TimeSpan(now.Ticks - begin.Ticks); + Deadline = (long)interval.TotalSeconds + expires; + } + + public string Marshal() + { + JsonData data = new JsonData(); + data["scope"] = Scope; + data["deadline"] = Deadline; + if (CallbackUrl != null) + data["callbackUrl"] = CallbackUrl; + if (ReturnUrl != null) + data["returnUrl"] = ReturnUrl; + return data.ToJson(); + } + + public byte[] MakeAuthToken() + { + Encoding encoding = Encoding.ASCII; + byte[] accessKey = encoding.GetBytes(Config.ACCESS_KEY); + byte[] secretKey = encoding.GetBytes(Config.SECRET_KEY); + byte[] upToken = null; + try + { + byte[] policyBase64 = encoding.GetBytes(Base64UrlSafe.Encode(Marshal())); + byte[] digestBase64 = null; + using (HMACSHA1 hmac = new HMACSHA1(secretKey)) + { + byte[] digest = hmac.ComputeHash(policyBase64); + digestBase64 = encoding.GetBytes(Base64UrlSafe.Encode(digest)); + } + using (MemoryStream buffer = new MemoryStream()) + { + buffer.Write(accessKey, 0, accessKey.Length); + buffer.WriteByte((byte)':'); + buffer.Write(digestBase64, 0, digestBase64.Length); + buffer.WriteByte((byte)':'); + buffer.Write(policyBase64, 0, policyBase64.Length); + upToken = buffer.ToArray(); + } + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + } + return upToken; + } + + public string MakeAuthTokenString() + { + return Encoding.ASCII.GetString(MakeAuthToken()); + } + } +} diff --git a/QBox/Base64UrlSafe.cs b/QBox/Base64UrlSafe.cs index df761a85..d36195f2 100755 --- a/QBox/Base64UrlSafe.cs +++ b/QBox/Base64UrlSafe.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Text; namespace QBox diff --git a/QBox/CallRet.cs b/QBox/CallRet.cs index 77921303..94d1632f 100755 --- a/QBox/CallRet.cs +++ b/QBox/CallRet.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Net; namespace QBox diff --git a/QBox/Client.cs b/QBox/Client.cs index c3ff81a0..d9d39177 100755 --- a/QBox/Client.cs +++ b/QBox/Client.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Net; using System.IO; @@ -14,18 +11,15 @@ public abstract class Client public CallRet Call(string url) { Console.WriteLine("URL: " + url); - HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; - if (request == null) - throw new NullReferenceException("request is not a http request"); - try { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; SetAuth(request); - HttpWebResponse response = request.GetResponse() as HttpWebResponse; - CallRet callRet = HandleResult(response); - response.Close(); - return callRet; + using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) + { + return HandleResult(response); + } } catch (Exception e) { @@ -37,12 +31,9 @@ public CallRet Call(string url) public CallRet CallWithBinary(string url, string contentType, byte[] body) { Console.WriteLine("URL: " + url); - HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; - if (request == null) - throw new NullReferenceException("request is not a http request"); - try { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = contentType; request.ContentLength = body.Length; @@ -51,19 +42,10 @@ public CallRet CallWithBinary(string url, string contentType, byte[] body) { requestStream.Write(body, 0, body.Length); } - } - catch (Exception e) - { - Console.WriteLine(e.ToString()); - return new CallRet(HttpStatusCode.BadRequest, e); - } - - try - { - HttpWebResponse response = request.GetResponse() as HttpWebResponse; - CallRet callRet = HandleResult(response); - response.Close(); - return callRet; + using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) + { + return HandleResult(response); + } } catch (Exception e) { @@ -74,23 +56,12 @@ public CallRet CallWithBinary(string url, string contentType, byte[] body) public static CallRet HandleResult(HttpWebResponse response) { - if (response == null) - return new CallRet(HttpStatusCode.BadRequest, "No response"); - HttpStatusCode statusCode = response.StatusCode; - string responseStr; - try + using (StreamReader reader = new StreamReader(response.GetResponseStream())) { - StreamReader reader = new StreamReader(response.GetResponseStream()); - responseStr = reader.ReadToEnd(); + string responseStr = reader.ReadToEnd(); + return new CallRet(statusCode, responseStr); } - catch (Exception e) - { - Console.WriteLine(e.ToString()); - return new CallRet(HttpStatusCode.BadRequest, e); - } - - return new CallRet(statusCode, responseStr); } } } diff --git a/QBox/Config.cs b/QBox/Config.cs index 5b65ed98..84583a04 100755 --- a/QBox/Config.cs +++ b/QBox/Config.cs @@ -1,14 +1,11 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace QBox { public class Config { public static string ACCESS_KEY = ""; - public static string SECRET_KEY = ""; + public static string SECRET_KEY = ""; public static string REDIRECT_URI = ""; public static string AUTHORIZATION_ENDPOINT = ""; diff --git a/QBox/DeleteRet.cs b/QBox/DeleteRet.cs index f28d681e..b9f6575f 100755 --- a/QBox/DeleteRet.cs +++ b/QBox/DeleteRet.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace QBox { diff --git a/QBox/DigestAuthClient.cs b/QBox/DigestAuthClient.cs index d27dd502..518f51ed 100755 --- a/QBox/DigestAuthClient.cs +++ b/QBox/DigestAuthClient.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Text; using System.Net; using System.IO; diff --git a/QBox/DropRet.cs b/QBox/DropRet.cs index 36392961..3a25194f 100755 --- a/QBox/DropRet.cs +++ b/QBox/DropRet.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace QBox { diff --git a/QBox/FileParameter.cs b/QBox/FileParameter.cs index c144fe6a..7c3b7583 100755 --- a/QBox/FileParameter.cs +++ b/QBox/FileParameter.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.IO; namespace QBox { diff --git a/QBox/GetRet.cs b/QBox/GetRet.cs index cc40ed06..ade6c865 100755 --- a/QBox/GetRet.cs +++ b/QBox/GetRet.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Net; using LitJson; diff --git a/QBox/MultiPartFormDataPost.cs b/QBox/MultiPartFormDataPost.cs index 49a8c480..7d8de541 100755 --- a/QBox/MultiPartFormDataPost.cs +++ b/QBox/MultiPartFormDataPost.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Text; using System.IO; using System.Net; @@ -13,57 +12,52 @@ public static class MultiPartFormDataPost private static byte[] GetBody(Dictionary postParams, string boundary) { - var buffer = new MemoryStream(); - - bool needsCLRF = false; - foreach (var param in postParams) + using (var buffer = new MemoryStream()) { - if (needsCLRF) - buffer.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n")); + bool needsCLRF = false; + foreach (var param in postParams) + { + if (needsCLRF) + buffer.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n")); - needsCLRF = true; + needsCLRF = true; - if (param.Value is FileParameter) - { - FileParameter fileToUpload = (FileParameter)param.Value; + if (param.Value is FileParameter) + { + FileParameter fileToUpload = (FileParameter)param.Value; - string headfmt = "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n"; - string header = string.Format(headfmt, boundary, param.Key, - fileToUpload.FileName ?? param.Key, - fileToUpload.MimeType ?? "application/octet-stream"); + string headfmt = "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n"; + string header = string.Format(headfmt, boundary, param.Key, + fileToUpload.FileName ?? param.Key, + fileToUpload.MimeType ?? "application/octet-stream"); - buffer.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header)); - using (FileStream fs = File.OpenRead(fileToUpload.FileName)) + buffer.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header)); + using (FileStream fs = File.OpenRead(fileToUpload.FileName)) + { + fs.CopyTo(buffer); + } + } + else { - fs.CopyTo(buffer); + string headfmt = "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}"; + string partData = string.Format(headfmt, boundary, param.Key, param.Value); + buffer.Write(encoding.GetBytes(partData), 0, encoding.GetByteCount(partData)); } } - else - { - string headfmt = "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}"; - string partData = string.Format(headfmt, boundary, param.Key, param.Value); - buffer.Write(encoding.GetBytes(partData), 0, encoding.GetByteCount(partData)); - } - } - - string footer = "\r\n--" + boundary + "--\r\n"; - buffer.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer)); - byte[] body = buffer.ToArray(); - buffer.Close(); + string footer = "\r\n--" + boundary + "--\r\n"; + buffer.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer)); - return body; + return buffer.ToArray(); + } } private static CallRet CallWithBinary(string url, string contentType, byte[] body) { Console.WriteLine("URL: " + url); - HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; - if (request == null) - throw new NullReferenceException("request is not a http request"); - try { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = contentType; request.ContentLength = body.Length; @@ -71,19 +65,10 @@ private static CallRet CallWithBinary(string url, string contentType, byte[] bod { requestStream.Write(body, 0, body.Length); } - } - catch (Exception e) - { - Console.WriteLine(e.ToString()); - return new CallRet(HttpStatusCode.BadRequest, e); - } - - try - { - HttpWebResponse response = request.GetResponse() as HttpWebResponse; - CallRet callRet = Client.HandleResult(response); - response.Close(); - return callRet; + using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) + { + return Client.HandleResult(response); + } } catch (Exception e) { diff --git a/QBox/PublishRet.cs b/QBox/PublishRet.cs index dd12f88a..22374a00 100755 --- a/QBox/PublishRet.cs +++ b/QBox/PublishRet.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace QBox { diff --git a/QBox/PutAuthRet.cs b/QBox/PutAuthRet.cs index cf9d7cb3..ec4e1e1e 100755 --- a/QBox/PutAuthRet.cs +++ b/QBox/PutAuthRet.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using LitJson; namespace QBox diff --git a/QBox/PutFileRet.cs b/QBox/PutFileRet.cs index c426e1df..dd46ba3a 100755 --- a/QBox/PutFileRet.cs +++ b/QBox/PutFileRet.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using LitJson; namespace QBox diff --git a/QBox/RSClient.cs b/QBox/RSClient.cs index 56dd1bb5..b2c87912 100755 --- a/QBox/RSClient.cs +++ b/QBox/RSClient.cs @@ -1,8 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.IO; using System.Net; namespace QBox diff --git a/QBox/RSService.cs b/QBox/RSService.cs index f6f02591..f797a084 100755 --- a/QBox/RSService.cs +++ b/QBox/RSService.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.IO; using System.Net; @@ -52,7 +49,6 @@ public PutFileRet PutFile(string key, string mimeType, string localFile, string bs = new byte[fs.Length]; fs.Read(bs, 0, bs.Length); } - } catch (Exception e) { @@ -97,7 +93,7 @@ public PublishRet Publish(string domain) return new PublishRet(callRet); } - public PublishRet UnPublish(string domain) + public PublishRet Unpublish(string domain) { string url = Config.RS_HOST + "/unpublish/" + Base64UrlSafe.Encode(domain); CallRet callRet = Conn.Call(url); diff --git a/QBox/StatRet.cs b/QBox/StatRet.cs index ca41b576..99c0365d 100755 --- a/QBox/StatRet.cs +++ b/QBox/StatRet.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Net; using LitJson; namespace QBox From 61396d701001a8d9bbdd4e7d8d51b5f795ab6d5d Mon Sep 17 00:00:00 2001 From: shijiayun Date: Fri, 6 Jul 2012 10:41:48 +0800 Subject: [PATCH 2/3] add readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 00000000..67d672c1 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +Qiniu Resource (Cloud) Storage SDK for C# +http://docs.qiniutek.com/ From 9e73f1d055b45383ffab25cdc702aa501ef28f87 Mon Sep 17 00:00:00 2001 From: shijiayun Date: Fri, 6 Jul 2012 10:45:35 +0800 Subject: [PATCH 3/3] modify readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 67d672c1..b525369b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -Qiniu Resource (Cloud) Storage SDK for C# +Qiniu Resource (Cloud) Storage SDK for C# http://docs.qiniutek.com/