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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
|
//
// System.Runtime.Remoting.Channels.Http.HttpClientChannel
//
// Summary: Implements a client channel that transmits method calls over HTTP.
//
// Classes: public HttpClientChannel
// internal HttpClientTransportSink
// Authors:
// Martin Willemoes Hansen (mwh@sysrq.dk)
// Ahmad Tantawy (popsito82@hotmail.com)
// Ahmad Kadry (kadrianoz@hotmail.com)
// Hussein Mehanna (hussein_mehanna@hotmail.com)
//
// (C) 2003 Martin Willemoes Hansen
//
//
// 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;
using System.IO;
using System.Net;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;
using System.Threading;
using System.Text;
namespace System.Runtime.Remoting.Channels.Http
{
public class HttpClientChannel : BaseChannelWithProperties, IChannelSender,IChannel
{
// Property Keys (purposely all lower-case)
private const String ProxyNameKey = "proxyname";
private const String ProxyPortKey = "proxyport";
// Settings
private int _channelPriority = 1; // channel priority
private String _channelName = "http"; // channel name
// Proxy settings (_proxyObject gets recreated when _proxyName and _proxyPort are updated)
//private IWebProxy _proxyObject = WebProxy.GetDefaultProxy(); // proxy object for request, can be overridden in transport sink
private IWebProxy _proxyObject = null;
private String _proxyName = null;
private int _proxyPort = -1;
private int _clientConnectionLimit = 0; // bump connection limit to at least this number (only meaningful if > 0)
private bool _bUseDefaultCredentials = false; // should default credentials be used?
private IClientChannelSinkProvider _sinkProvider = null; // sink chain provider
public HttpClientChannel()
{
SetupProvider (null,null);
}
public HttpClientChannel(String name, IClientChannelSinkProvider sinkProvider)
{
if(name != null)
_channelName = name;
SetupProvider (sinkProvider, null);
}
// constructor used by config file
public HttpClientChannel(IDictionary properties, IClientChannelSinkProvider sinkProvider)
{
if (properties != null)
{
foreach(DictionaryEntry Dict in properties)
{
switch(Dict.Key.ToString())
{
case "name":
_channelName = Dict.Value.ToString();
break;
case "priority":
_channelPriority = Convert.ToInt32(Dict.Value);
break;
case "clientConnectionLimit":
_clientConnectionLimit = Convert.ToInt32(Dict.Value);
break;
case "proxyName":
_proxyName = Dict.Value.ToString();
break;
case "proxyPort":
_proxyPort = Convert.ToInt32(Dict.Value);
break;
case "useDefaultCredentials":
_bUseDefaultCredentials = Convert.ToBoolean(Dict.Value);
break;
}
}
}
SetupProvider (sinkProvider, properties);
}
public int ChannelPriority
{
get { return _channelPriority; }
}
public String ChannelName
{
get { return _channelName; }
}
// returns channelURI and places object uri into out parameter
public String Parse(String url, out String objectURI)
{
return HttpHelper.Parse(url,out objectURI);
}
//
// end of IChannel implementation
//
//
// IChannelSender implementation
//
public virtual IMessageSink CreateMessageSink(String url, Object remoteChannelData, out String objectURI)
{
if ((url == null || !HttpHelper.StartsWithHttp (url)) && remoteChannelData != null && remoteChannelData as IChannelDataStore != null )
{
IChannelDataStore ds = (IChannelDataStore) remoteChannelData;
url = ds.ChannelUris[0];
}
if(url != null && HttpHelper.StartsWithHttp(url))
{
HttpHelper.Parse(url, out objectURI);
IMessageSink msgSink = (IMessageSink) _sinkProvider.CreateSink(this,url,remoteChannelData);
if(msgSink !=null )
SetServicePoint(url);
return msgSink;
}
else
{
objectURI = null;
return null;
}
}
private void UpdateProxy()
{
// If the user values for the proxy object are valid , then the proxy
// object will be created based on these values , if not it'll have the
// value given when declared , as a default proxy object
if(_proxyName!=null && _proxyPort !=-1)
_proxyObject = new WebProxy(_proxyName,_proxyPort);
// Either it's default or not it'll have this property
((WebProxy)_proxyObject).BypassProxyOnLocal = true;
}
private void SetServicePoint(string channelURI)
{
// Find a ServicePoint for the given url and assign the connection limit
// to the user given value only if it valid
ServicePoint sp = ServicePointManager.FindServicePoint(channelURI,ProxyObject);
if(_clientConnectionLimit> 0)
sp.ConnectionLimit = _clientConnectionLimit;
}
internal IWebProxy ProxyObject { get { return _proxyObject; } }
internal bool UseDefaultCredentials { get { return _bUseDefaultCredentials; } }
private void SetupProvider (IClientChannelSinkProvider sinkProvider, IDictionary properties)
{
if (properties == null) properties = new Hashtable ();
HttpClientTransportSinkProvider httpSink = new HttpClientTransportSinkProvider (properties);
SinksWithProperties = httpSink;
if(sinkProvider == null)
{
_sinkProvider = new SoapClientFormatterSinkProvider();
_sinkProvider.Next = httpSink;
}
else
{
IClientChannelSinkProvider dummySinkProvider;
dummySinkProvider = sinkProvider;
_sinkProvider = sinkProvider;
while(dummySinkProvider.Next != null)
{
dummySinkProvider = dummySinkProvider.Next;
}
dummySinkProvider.Next = httpSink;
}
}
public override object this [object key]
{
get { return Properties[key]; }
set { Properties[key] = value; }
}
public override ICollection Keys
{
get { return Properties.Keys; }
}
}
internal class HttpClientTransportSinkProvider : IClientChannelSinkProvider, IChannelSinkBase
{
IDictionary _properties;
internal HttpClientTransportSinkProvider (IDictionary properties)
{
_properties = properties;
}
public IClientChannelSink CreateSink(IChannelSender channel, String url,
Object remoteChannelData)
{
// url is set to the channel uri in CreateMessageSink
return new HttpClientTransportSink((HttpClientChannel)channel, url);
}
public IClientChannelSinkProvider Next
{
get { return null; }
set { throw new NotSupportedException(); }
}
public IDictionary Properties
{
get { return _properties; }
}
} // class HttpClientTransportSinkProvider
// transport sender sink used by HttpClientChannel
internal class HttpClientTransportSink : BaseChannelSinkWithProperties, IClientChannelSink
{
private const String s_defaultVerb = "POST";
private static String s_userAgent =
"Mono Remoting Client (Mono CLR " + System.Environment.Version.ToString() + ")";
// Property keys (purposely all lower-case)
private const String UserNameKey = "username";
private const String PasswordKey = "password";
private const String DomainKey = "domain";
private const String PreAuthenticateKey = "preauthenticate";
private const String CredentialsKey = "credentials";
private const String ClientCertificatesKey = "clientcertificates";
private const String ProxyNameKey = "proxyname";
private const String ProxyPortKey = "proxyport";
private const String TimeoutKey = "timeout";
private const String AllowAutoRedirectKey = "allowautoredirect";
// If above keys get modified be sure to modify, the KeySet property on this
// class.
private static ICollection s_keySet = null;
// Property values
private String _securityUserName = null;
private String _securityPassword = null;
private String _securityDomain = null;
private bool _bSecurityPreAuthenticate = false;
private ICredentials _credentials = null; // this overrides all of the other security settings
private int _timeout = System.Threading.Timeout.Infinite; // timeout value in milliseconds (only used if greater than 0)
private bool _bAllowAutoRedirect = false;
// Proxy settings (_proxyObject gets recreated when _proxyName and _proxyPort are updated)
private IWebProxy _proxyObject = null; // overrides channel proxy object if non-null
private String _proxyName = null;
private int _proxyPort = -1;
// Other members
private HttpClientChannel _channel; // channel that created this sink
private String _channelURI; // complete url to remote object
// settings
private bool _useChunked = false;
// private bool _useKeepAlive = true;
internal HttpClientTransportSink(HttpClientChannel channel, String channelURI) : base()
{
string dummy;
_channel = channel;
_channelURI = HttpHelper.Parse(channelURI,out dummy);
}
public void ProcessMessage(IMessage msg,
ITransportHeaders requestHeaders, Stream requestStream,
out ITransportHeaders responseHeaders, out Stream responseStream)
{
string url = null;
string uri = ((IMethodCallMessage)msg).Uri;
requestHeaders [CommonTransportKeys.RequestUri] = uri;
CreateUrl(uri,out url);
HttpWebRequest httpWebRequest = CreateWebRequest (url,requestHeaders,requestStream);
SendAndRecieve (httpWebRequest,out responseHeaders,out responseStream);
}
public void AsyncProcessRequest(IClientChannelSinkStack sinkStack, IMessage msg,
ITransportHeaders headers, Stream stream)
{
string url = null;
string uri = ((IMethodCallMessage)msg).Uri;
headers [CommonTransportKeys.RequestUri] = uri;
CreateUrl(uri,out url);
HttpWebRequest httpWebRequest = CreateWebRequest(url,headers,stream);
RequestState reqState = new RequestState(httpWebRequest,sinkStack);
httpWebRequest.BeginGetResponse(new AsyncCallback(AsyncRequestHandler),reqState);
}
private void AsyncRequestHandler(IAsyncResult ar)
{
HttpWebResponse httpWebResponse = null;
RequestState reqState = (RequestState) ar.AsyncState;
HttpWebRequest httpWebRequest = reqState.webRquest;
IClientChannelSinkStack sinkStack = reqState.sinkStack;
try
{
httpWebResponse = (HttpWebResponse) httpWebRequest.EndGetResponse(ar);
}
catch (WebException ex)
{
httpWebResponse = ex.Response as HttpWebResponse;
if (httpWebResponse == null) sinkStack.DispatchException (ex);
}
Stream responseStream;
ITransportHeaders responseHeaders;
try
{
ReceiveResponse (httpWebResponse, out responseHeaders, out responseStream);
sinkStack.AsyncProcessResponse(responseHeaders,responseStream);
}
catch (Exception ex)
{
sinkStack.DispatchException (ex);
}
}
public void AsyncProcessResponse(IClientResponseChannelSinkStack sinkStack, Object state,
ITransportHeaders headers, Stream stream)
{
// We don't have to implement this since we are always last in the chain.
} // AsyncProcessRequest
public Stream GetRequestStream(IMessage msg, ITransportHeaders headers)
{
return null;
} // GetRequestStream
public IClientChannelSink NextChannelSink
{
get { return null; }
}
public override Object this[Object key]
{
get
{
String keyStr = key as String;
if (keyStr == null)
return null;
switch (keyStr.ToLower())
{
case UserNameKey: return _securityUserName;
case PasswordKey: return null; // Intentionally refuse to return password.
case DomainKey: return _securityDomain;
case PreAuthenticateKey: return _bSecurityPreAuthenticate;
case CredentialsKey: return _credentials;
case ClientCertificatesKey: return null; // Intentionally refuse to return certificates
case ProxyNameKey: return _proxyName;
case ProxyPortKey: return _proxyPort;
case TimeoutKey: return _timeout;
case AllowAutoRedirectKey: return _bAllowAutoRedirect;
} // switch (keyStr.ToLower())
return null;
}
set
{
String keyStr = key as String;
if (keyStr == null)
return;
switch (keyStr.ToLower())
{
case UserNameKey: _securityUserName = (String)value; break;
case PasswordKey: _securityPassword = (String)value; break;
case DomainKey: _securityDomain = (String)value; break;
case PreAuthenticateKey: _bSecurityPreAuthenticate = Convert.ToBoolean(value); break;
case CredentialsKey: _credentials = (ICredentials)value; break;
case ProxyNameKey: _proxyName = (String)value; UpdateProxy(); break;
case ProxyPortKey: _proxyPort = Convert.ToInt32(value); UpdateProxy(); break;
case TimeoutKey:
{
if (value is TimeSpan)
_timeout = (int)((TimeSpan)value).TotalMilliseconds;
else
_timeout = Convert.ToInt32(value);
break;
} // case TimeoutKey
case AllowAutoRedirectKey: _bAllowAutoRedirect = Convert.ToBoolean(value); break;
} // switch (keyStr.ToLower())
}
} // this[]
public override ICollection Keys
{
get
{
if (s_keySet == null)
{
// No need for synchronization
ArrayList keys = new ArrayList(6);
keys.Add(UserNameKey);
keys.Add(PasswordKey);
keys.Add(DomainKey);
keys.Add(PreAuthenticateKey);
keys.Add(CredentialsKey);
keys.Add(ClientCertificatesKey);
keys.Add(ProxyNameKey);
keys.Add(ProxyPortKey);
keys.Add(TimeoutKey);
keys.Add(AllowAutoRedirectKey);
s_keySet = keys;
}
return s_keySet;
}
}
private void UpdateProxy()
{
// If the user values for the proxy object are valid , then the proxy
// object will be created based on these values , if not it'll have the
// value given when declared , as a default proxy object
if(_proxyName!=null && _proxyPort !=-1)
_proxyObject = new WebProxy(_proxyName,_proxyPort);
// Either it's default or not it'll have this property
((WebProxy)_proxyObject).BypassProxyOnLocal = true;
}
internal static String UserAgent
{
get { return s_userAgent; }
}
private void CreateUrl(string uri, out string fullURL)
{
if(HttpHelper.StartsWithHttp(uri)) //this is a full url
{
fullURL = uri;
return;
}
if(_channelURI.EndsWith("/") && uri.StartsWith("/"))
{
fullURL = _channelURI + uri.Substring(1);
return;
}
else
if(_channelURI.EndsWith("/") && !uri.StartsWith("/") ||
!_channelURI.EndsWith("/") && uri.StartsWith("/") )
{
fullURL = _channelURI +uri;
return;
}
else
{
fullURL = _channelURI +'/'+ uri;
return;
}
}
private HttpWebRequest CreateWebRequest(string url, ITransportHeaders requestHeaders, Stream requestStream)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);;
request.AllowAutoRedirect = _bAllowAutoRedirect;
request.ContentLength = requestStream.Length;
request.Credentials = GetCredenentials();
//request.Expect = "100-Continue";
//This caused us some troubles with the HttpWebResponse class
//maybe its fixed now. TODO
//request.KeepAlive = _useKeepAlive;
request.KeepAlive = false;;
request.Method = s_defaultVerb;
request.Pipelined = false;
request.SendChunked = _useChunked;
request.UserAgent = s_userAgent;
// write the remoting headers
IEnumerator headerenum = requestHeaders.GetEnumerator();
while (headerenum.MoveNext())
{
DictionaryEntry entry = (DictionaryEntry) headerenum.Current;
String key = entry.Key as String;
if(key == "Content-Type")
{
request.ContentType = entry.Value.ToString();
continue;
}
if (key == null || key.StartsWith("__"))
{
continue;
}
request.Headers.Add(entry.Key.ToString(),entry.Value.ToString());
}
Stream reqStream = request.GetRequestStream();
try {
HttpHelper.CopyStream(requestStream, reqStream);
} finally {
reqStream.Close();
}
return request;
}
private void SendAndRecieve(HttpWebRequest httpRequest,out ITransportHeaders responseHeaders,out Stream responseStream)
{
responseStream = null;
responseHeaders = null;
HttpWebResponse httpWebResponse = null;
try
{
httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
}
catch (WebException ex)
{
httpWebResponse = ex.Response as HttpWebResponse;
if (httpWebResponse == null || httpWebResponse.StatusCode < HttpStatusCode.InternalServerError)
throw ex;
}
ReceiveResponse (httpWebResponse, out responseHeaders, out responseStream);
}
private void ReceiveResponse (HttpWebResponse httpWebResponse, out ITransportHeaders responseHeaders, out Stream responseStream)
{
responseHeaders = new TransportHeaders();
Stream webStream = httpWebResponse.GetResponseStream();
try
{
if (httpWebResponse.ContentLength != -1)
{
byte[] buffer = new byte [httpWebResponse.ContentLength];
int nr = 0;
while (nr < buffer.Length) {
int pr = webStream.Read (buffer, nr, buffer.Length - nr);
if (pr == 0) throw new RemotingException ("Connection closed");
nr += pr;
}
responseStream = new MemoryStream (buffer);
}
else
{
responseStream = new MemoryStream();
HttpHelper.CopyStream(webStream, responseStream);
}
//Use the two commented lines below instead of the 3 below lines when HttpWebResponse
//class is fully implemented in order to support custom headers
//for(int i=0; i < httpWebResponse.Headers.Count; ++i)
// responseHeaders[httpWebResponse.Headers.Keys[i].ToString()] = httpWebResponse.Headers[i].ToString();
responseHeaders["Content-Type"] = httpWebResponse.ContentType;
responseHeaders["Server"] = httpWebResponse.Server;
responseHeaders["Content-Length"] = httpWebResponse.ContentLength;
}
finally
{
webStream.Close ();
httpWebResponse.Close();
}
}
private void ProcessErrorCode()
{
}
private ICredentials GetCredenentials()
{
if(_credentials!=null)
return _credentials;
//Now use the username , password and domain if provided
if(_securityUserName==null ||_securityUserName=="")
if(_channel.UseDefaultCredentials)
return CredentialCache.DefaultCredentials;
else
return null;
return new NetworkCredential(_securityUserName,_securityPassword,_securityDomain);
}
} // class HttpClientTransportSink
internal class RequestState
{
public HttpWebRequest webRquest;
public IClientChannelSinkStack sinkStack;
public RequestState(HttpWebRequest wr,IClientChannelSinkStack ss)
{
webRquest = wr;
sinkStack = ss;
}
}
} // namespace System.Runtime.Remoting.Channels.Http
|