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 256
|
//
// HttpReplyChannel.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2010 Novell, Inc. http://www.novell.com
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.IO;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Security;
using System.Text;
using System.Threading;
namespace System.ServiceModel.Channels.Http
{
internal class HttpReplyChannel : InternalReplyChannelBase
{
HttpChannelListener<IReplyChannel> source;
RequestContext reqctx;
SecurityTokenAuthenticator security_token_authenticator;
SecurityTokenResolver security_token_resolver;
public HttpReplyChannel (HttpChannelListener<IReplyChannel> listener)
: base (listener)
{
this.source = listener;
if (listener.SecurityTokenManager != null) {
var str = new SecurityTokenRequirement () { TokenType = SecurityTokenTypes.UserName };
security_token_authenticator = listener.SecurityTokenManager.CreateSecurityTokenAuthenticator (str, out security_token_resolver);
}
}
internal HttpChannelListener<IReplyChannel> Source {
get { return source; }
}
public MessageEncoder Encoder {
get { return source.MessageEncoder; }
}
internal MessageVersion MessageVersion {
get { return source.MessageEncoder.MessageVersion; }
}
public override RequestContext ReceiveRequest (TimeSpan timeout)
{
RequestContext ctx;
if (!TryReceiveRequest (timeout, out ctx))
throw new TimeoutException ();
return ctx;
}
protected override void OnOpen (TimeSpan timeout)
{
}
protected override void OnAbort ()
{
AbortConnections (TimeSpan.Zero);
base.OnAbort (); // FIXME: remove it. The base is wrong. But it is somehow required to not block some tests.
}
public override bool CancelAsync (TimeSpan timeout)
{
AbortConnections (timeout);
// FIXME: this wait is sort of hack (because it should not be required), but without it some tests are blocked.
// This hack even had better be moved to base.CancelAsync().
// if (CurrentAsyncResult != null)
// CurrentAsyncResult.AsyncWaitHandle.WaitOne (TimeSpan.FromMilliseconds (300));
return base.CancelAsync (timeout);
}
void AbortConnections (TimeSpan timeout)
{
if (reqctx != null)
reqctx.Close (timeout);
}
bool close_started;
object close_lock = new object ();
protected override void OnClose (TimeSpan timeout)
{
lock (close_lock) {
if (close_started)
return;
close_started = true;
}
DateTime start = DateTime.UtcNow;
// FIXME: consider timeout
AbortConnections (timeout - (DateTime.UtcNow - start));
base.OnClose (timeout - (DateTime.UtcNow - start));
}
protected string GetHeaderItem (string raw)
{
if (raw == null || raw.Length == 0)
return raw;
switch (raw [0]) {
case '\'':
case '"':
if (raw [raw.Length - 1] == raw [0])
return raw.Substring (1, raw.Length - 2);
// FIXME: is it simply an error?
break;
}
return raw;
}
protected HttpRequestMessageProperty CreateRequestProperty (HttpContextInfo ctxi)
{
var query = ctxi.Request.Url.Query;
var prop = new HttpRequestMessageProperty ();
prop.Method = ctxi.Request.HttpMethod;
prop.QueryString = query.StartsWith ("?") ? query.Substring (1) : query;
// FIXME: prop.SuppressEntityBody
prop.Headers.Add (ctxi.Request.Headers);
return prop;
}
public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
{
context = null;
HttpContextInfo ctxi;
if (!source.ListenerManager.TryDequeueRequest (source.ChannelDispatcher, timeout, out ctxi))
return false;
if (ctxi == null)
return true; // returning true, yet context is null. This happens at closing phase.
if (source.Source.AuthenticationScheme != AuthenticationSchemes.Anonymous) {
if (security_token_authenticator != null)
// FIXME: use return value?
try {
security_token_authenticator.ValidateToken (new UserNameSecurityToken (ctxi.User, ctxi.Password));
} catch (Exception) {
ctxi.ReturnUnauthorized ();
}
else {
ctxi.ReturnUnauthorized ();
}
}
Message msg = null;
if (ctxi.Request.HttpMethod == "POST")
msg = CreatePostMessage (ctxi);
else if (ctxi.Request.HttpMethod == "GET")
msg = Message.CreateMessage (MessageVersion.None, null); // HTTP GET-based request
if (msg == null)
return false;
if (msg.Headers.To == null)
msg.Headers.To = ctxi.Request.Url;
msg.Properties.Add ("Via", LocalAddress.Uri);
msg.Properties.Add (HttpRequestMessageProperty.Name, CreateRequestProperty (ctxi));
Logger.LogMessage (MessageLogSourceKind.TransportReceive, ref msg, source.Source.MaxReceivedMessageSize);
context = new HttpRequestContext (this, ctxi, msg);
reqctx = context;
return true;
}
protected Message CreatePostMessage (HttpContextInfo ctxi)
{
if (ctxi.Response.StatusCode != 200) { // it's already invalid.
ctxi.Close ();
return null;
}
if (!Encoder.IsContentTypeSupported (ctxi.Request.ContentType)) {
ctxi.Response.StatusCode = (int) HttpStatusCode.UnsupportedMediaType;
ctxi.Response.StatusDescription = String.Format (
"Expected content-type '{0}' but got '{1}'", Encoder.ContentType, ctxi.Request.ContentType);
ctxi.Close ();
return null;
}
// FIXME: supply maxSizeOfHeaders.
int maxSizeOfHeaders = 0x10000;
#if false // FIXME: enable it, once duplex callback test gets passed.
Stream stream = ctxi.Request.InputStream;
if (source.Source.TransferMode == TransferMode.Buffered) {
if (ctxi.Request.ContentLength64 <= 0)
throw new ArgumentException ("This HTTP channel is configured to use buffered mode, and thus expects Content-Length sent to the listener");
long size = 0;
var ms = new MemoryStream ();
var buf = new byte [0x1000];
while (size < ctxi.Request.ContentLength64) {
if ((size += stream.Read (buf, 0, 0x1000)) > source.Source.MaxBufferSize)
throw new QuotaExceededException ("Message quota exceeded");
ms.Write (buf, 0, (int) (size - ms.Length));
}
ms.Position = 0;
stream = ms;
}
var msg = Encoder.ReadMessage (
stream, maxSizeOfHeaders, ctxi.Request.ContentType);
#else
var msg = Encoder.ReadMessage (
ctxi.Request.InputStream, maxSizeOfHeaders, ctxi.Request.ContentType);
#endif
if (MessageVersion.Envelope.Equals (EnvelopeVersion.Soap11) ||
MessageVersion.Addressing.Equals (AddressingVersion.None)) {
string action = GetHeaderItem (ctxi.Request.Headers ["SOAPAction"]);
if (action != null) {
if (action.Length > 2 && action [0] == '"' && action [action.Length] == '"')
action = action.Substring (1, action.Length - 2);
msg.Headers.Action = action;
}
}
msg.Properties.Add (RemoteEndpointMessageProperty.Name, new RemoteEndpointMessageProperty (ctxi.Request.ClientIPAddress, ctxi.Request.ClientPort));
return msg;
}
public override bool WaitForRequest (TimeSpan timeout)
{
throw new NotImplementedException ();
}
}
}
|