1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
//------------------------------------------------------------------------------
// <copyright file="HttpServerProtocol.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Services.Protocols {
using System;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;
using System.Web.Services.Description;
using System.Web.Services.Configuration;
using System.Net;
using System.Globalization;
internal class HttpServerType : ServerType {
Hashtable methods = new Hashtable();
internal HttpServerType(Type type) : base(type) {
WebServicesSection config = WebServicesSection.Current;
Type[] returnWriterTypes = config.ReturnWriterTypes;
Type[] parameterReaderTypes = config.ParameterReaderTypes;
LogicalMethodInfo[] methodInfos = WebMethodReflector.GetMethods(type);
HttpServerMethod[] methods = new HttpServerMethod[methodInfos.Length];
object[] initializersByType = new object[returnWriterTypes.Length];
for (int i = 0; i < initializersByType.Length; i++) {
initializersByType[i] = MimeFormatter.GetInitializers(returnWriterTypes[i], methodInfos);
}
for (int i = 0; i < methodInfos.Length; i++) {
LogicalMethodInfo methodInfo = methodInfos[i];
HttpServerMethod method = null;
if (methodInfo.ReturnType == typeof(void)) {
method = new HttpServerMethod();
}
else {
for (int j = 0; j < returnWriterTypes.Length; j++) {
object[] initializers = (object[])initializersByType[j];
if (initializers[i] != null) {
method = new HttpServerMethod();
method.writerInitializer = initializers[i];
method.writerType = returnWriterTypes[j];
break;
}
}
}
if (method != null) {
method.methodInfo = methodInfo;
methods[i] = method;
}
}
initializersByType = new object[parameterReaderTypes.Length];
for (int i = 0; i < initializersByType.Length; i++) {
initializersByType[i] = MimeFormatter.GetInitializers(parameterReaderTypes[i], methodInfos);
}
for (int i = 0; i < methodInfos.Length; i++) {
HttpServerMethod method = methods[i];
if (method == null) continue;
LogicalMethodInfo methodInfo = methodInfos[i];
if (methodInfo.InParameters.Length > 0) {
int count = 0;
for (int j = 0; j < parameterReaderTypes.Length; j++) {
object[] initializers = (object[])initializersByType[j];
if (initializers[i] != null) {
count++;
}
}
if (count == 0) {
methods[i] = null;
}
else {
method.readerTypes = new Type[count];
method.readerInitializers = new object[count];
count = 0;
for (int j = 0; j < parameterReaderTypes.Length; j++) {
object[] initializers = (object[])initializersByType[j];
if (initializers[i] != null) {
method.readerTypes[count] = parameterReaderTypes[j];
method.readerInitializers[count] = initializers[i];
count++;
}
}
}
}
}
for (int i = 0; i < methods.Length; i++) {
HttpServerMethod method = methods[i];
if (method != null) {
WebMethodAttribute methodAttribute = method.methodInfo.MethodAttribute;
method.name = methodAttribute.MessageName;
if (method.name.Length == 0) method.name = method.methodInfo.Name;
this.methods.Add(method.name, method);
}
}
}
internal HttpServerMethod GetMethod(string name) {
return (HttpServerMethod)methods[name];
}
internal HttpServerMethod GetMethodIgnoreCase(string name) {
foreach (DictionaryEntry entry in methods) {
HttpServerMethod method = (HttpServerMethod)entry.Value;
if (String.Compare(method.name, name, StringComparison.OrdinalIgnoreCase) == 0)
return method;
}
return null;
}
}
internal class HttpServerMethod {
internal string name;
internal LogicalMethodInfo methodInfo;
internal Type[] readerTypes;
internal object[] readerInitializers;
internal Type writerType;
internal object writerInitializer;
}
internal abstract class HttpServerProtocol : ServerProtocol {
HttpServerMethod serverMethod;
HttpServerType serverType;
bool hasInputPayload;
protected HttpServerProtocol(bool hasInputPayload) {
this.hasInputPayload = hasInputPayload;
}
internal override bool Initialize() {
// The derived class better check the verb!
string methodName = Request.PathInfo.Substring(1); // Skip leading '/'
if (null == (serverType = (HttpServerType)GetFromCache(typeof(HttpServerProtocol), Type))
&& null == (serverType = (HttpServerType)GetFromCache(typeof(HttpServerProtocol), Type, true)))
{
lock (InternalSyncObject)
{
if (null == (serverType = (HttpServerType)GetFromCache(typeof(HttpServerProtocol), Type))
&& null == (serverType = (HttpServerType)GetFromCache(typeof(HttpServerProtocol), Type, true)))
{
bool excludeSchemeHostPortFromCachingKey = this.IsCacheUnderPressure(typeof(HttpServerProtocol), Type);
serverType = new HttpServerType(Type);
AddToCache(typeof(HttpServerProtocol), Type, serverType, excludeSchemeHostPortFromCachingKey);
}
}
}
serverMethod = serverType.GetMethod(methodName);
if (serverMethod == null) {
serverMethod = serverType.GetMethodIgnoreCase(methodName);
if (serverMethod != null)
throw new ArgumentException(Res.GetString(Res.WebInvalidMethodNameCase, methodName, serverMethod.name), "methodName");
else {
// it's possible that the method name came in as UTF-8 but was mangled by IIS so we try it
// again as UTF8...
string utf8MethodName = Encoding.UTF8.GetString(Encoding.Default.GetBytes(methodName));
serverMethod = serverType.GetMethod(utf8MethodName);
if (serverMethod == null)
throw new InvalidOperationException(Res.GetString(Res.WebInvalidMethodName, methodName));
}
}
return true;
}
internal override bool IsOneWay {
get { return false; }
}
internal override LogicalMethodInfo MethodInfo {
get { return serverMethod.methodInfo; }
}
internal override ServerType ServerType {
get { return serverType; }
}
internal override object[] ReadParameters() {
if (serverMethod.readerTypes == null) return new object[0];
for (int i = 0; i < serverMethod.readerTypes.Length; i++) {
if (!hasInputPayload) {
// only allow URL parameters if doesn't have payload
if (serverMethod.readerTypes[i] != typeof(UrlParameterReader)) continue;
}
else {
// don't allow URL params if has payload
if (serverMethod.readerTypes[i] == typeof(UrlParameterReader)) continue;
}
MimeParameterReader reader = (MimeParameterReader)MimeFormatter.CreateInstance(serverMethod.readerTypes[i],
serverMethod.readerInitializers[i]);
object[] parameters = reader.Read(Request);
if (parameters != null) return parameters;
}
if (!hasInputPayload)
throw new InvalidOperationException(Res.GetString(Res.WebInvalidRequestFormat));
else
throw new InvalidOperationException(Res.GetString(Res.WebInvalidRequestFormatDetails, Request.ContentType));
}
internal override void WriteReturns(object[] returnValues, Stream outputStream) {
if (serverMethod.writerType == null) return;
MimeReturnWriter writer = (MimeReturnWriter)MimeFormatter.CreateInstance(serverMethod.writerType,
serverMethod.writerInitializer);
writer.Write(Response, outputStream, returnValues[0]);
}
internal override bool WriteException(Exception e, Stream outputStream) {
Response.Clear();
Response.ClearHeaders();
Response.ContentType = ContentType.Compose("text/plain", Encoding.UTF8);
SetHttpResponseStatusCode(Response, (int)HttpStatusCode.InternalServerError);
Response.StatusDescription = HttpWorkerRequest.GetStatusDescription(Response.StatusCode);
StreamWriter writer = new StreamWriter(outputStream, new UTF8Encoding(false));
if (System.Web.Services.Configuration.WebServicesSection.Current.Diagnostics.SuppressReturningExceptions) {
writer.WriteLine(Res.GetString(Res.WebSuppressedExceptionMessage));
}
else {
writer.WriteLine(GenerateFaultString(e, true));
}
writer.Flush();
return true;
}
internal static bool AreUrlParametersSupported(LogicalMethodInfo methodInfo) {
if (methodInfo.OutParameters.Length > 0) return false;
ParameterInfo[] parameters = methodInfo.InParameters;
for (int i = 0; i < parameters.Length; i++) {
ParameterInfo parameter = parameters[i];
Type parameterType = parameter.ParameterType;
if (parameterType.IsArray) {
if (!ScalarFormatter.IsTypeSupported(parameterType.GetElementType()))
return false;
}
else {
if (!ScalarFormatter.IsTypeSupported(parameterType))
return false;
}
}
return true;
}
}
}
|