1 //Copyright 2010 Microsoft Corporation 2 // 3 //Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 4 //You may obtain a copy of the License at 5 // 6 //http://www.apache.org/licenses/LICENSE-2.0 7 // 8 //Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an 9 //"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 //See the License for the specific language governing permissions and limitations under the License. 11 12 13 namespace System.Data.Services.Client 14 { 15 using System.Collections.Generic; 16 using System.Diagnostics; 17 using System.IO; 18 using System.Reflection; 19 using System.Runtime.CompilerServices; 20 #if !ASTORIA_LIGHT 21 using System.Net; 22 #else 23 using System.Data.Services.Http; 24 #endif 25 26 internal static partial class WebUtil 27 { 28 private static bool? dataServiceCollectionAvailable = null; 29 30 private static bool DataServiceCollectionAvailable 31 { 32 get 33 { 34 if (dataServiceCollectionAvailable == null) 35 { 36 try 37 { 38 dataServiceCollectionAvailable = GetDataServiceCollectionOfTType() != null; 39 } 40 catch (FileNotFoundException) 41 { 42 dataServiceCollectionAvailable = false; 43 } 44 } 45 46 Debug.Assert(dataServiceCollectionAvailable != null, "observableCollectionOfTAvailable must not be null here."); 47 48 return (bool)dataServiceCollectionAvailable; 49 } 50 } 51 CopyStream(Stream input, Stream output, ref byte[] refBuffer)52 internal static long CopyStream(Stream input, Stream output, ref byte[] refBuffer) 53 { 54 Debug.Assert(null != input, "null input stream"); 55 Debug.Assert(null != output, "null output stream"); 56 57 long total = 0; 58 byte[] buffer = refBuffer; 59 if (null == buffer) 60 { 61 refBuffer = buffer = new byte[1000]; 62 } 63 64 int count = 0; 65 while (input.CanRead && (0 < (count = input.Read(buffer, 0, buffer.Length)))) 66 { 67 output.Write(buffer, 0, count); 68 total += count; 69 } 70 71 return total; 72 } 73 GetHttpWebResponse(InvalidOperationException exception, ref HttpWebResponse response)74 internal static void GetHttpWebResponse(InvalidOperationException exception, ref HttpWebResponse response) 75 { 76 if (null == response) 77 { 78 WebException webexception = (exception as WebException); 79 if (null != webexception) 80 { 81 response = (HttpWebResponse)webexception.Response; 82 } 83 } 84 } 85 SuccessStatusCode(HttpStatusCode status)86 internal static bool SuccessStatusCode(HttpStatusCode status) 87 { 88 return (200 <= (int)status && (int)status < 300); 89 } 90 WrapResponseHeaders(HttpWebResponse response)91 internal static Dictionary<string, string> WrapResponseHeaders(HttpWebResponse response) 92 { 93 Dictionary<string, string> headers = new Dictionary<string, string>(EqualityComparer<string>.Default); 94 if (null != response) 95 { 96 foreach (string name in response.Headers.AllKeys) 97 { 98 headers.Add(name, response.Headers[name]); 99 } 100 } 101 102 return headers; 103 } 104 ApplyHeadersToRequest(Dictionary<string, string> headers, HttpWebRequest request, bool ignoreAcceptHeader)105 internal static void ApplyHeadersToRequest(Dictionary<string, string> headers, HttpWebRequest request, bool ignoreAcceptHeader) 106 { 107 foreach (KeyValuePair<string, string> header in headers) 108 { 109 if (string.Equals(header.Key, XmlConstants.HttpRequestAccept, StringComparison.Ordinal)) 110 { 111 if (!ignoreAcceptHeader) 112 { 113 request.Accept = header.Value; 114 } 115 } 116 else if (string.Equals(header.Key, XmlConstants.HttpContentType, StringComparison.Ordinal)) 117 { 118 request.ContentType = header.Value; 119 } 120 else 121 { 122 request.Headers[header.Key] = header.Value; 123 } 124 } 125 } 126 IsDataServiceCollectionType(Type t)127 internal static bool IsDataServiceCollectionType(Type t) 128 { 129 if (DataServiceCollectionAvailable) 130 { 131 return t == GetDataServiceCollectionOfTType(); 132 } 133 134 return false; 135 } 136 GetDataServiceCollectionOfT(params Type[] typeArguments)137 internal static Type GetDataServiceCollectionOfT(params Type[] typeArguments) 138 { 139 if (DataServiceCollectionAvailable) 140 { 141 Debug.Assert( 142 GetDataServiceCollectionOfTType() != null, 143 "DataServiceCollection is available so GetDataServiceCollectionOfTType() must not return null."); 144 145 return GetDataServiceCollectionOfTType().MakeGenericType(typeArguments); 146 } 147 148 return null; 149 } 150 151 [MethodImpl(MethodImplOptions.NoInlining)] GetDataServiceCollectionOfTType()152 private static Type GetDataServiceCollectionOfTType() 153 { 154 return typeof(DataServiceCollection<>); 155 } 156 } 157 } 158