2 // HttpRequestChannel.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Generic;
32 using System.Net.Security;
33 using System.ServiceModel;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Security;
36 using System.Threading;
38 namespace System.ServiceModel.Channels
40 internal class HttpRequestChannel : RequestChannelBase
42 HttpChannelFactory<IRequestChannel> source;
43 EndpointAddress address;
46 WebRequest web_request;
48 // FIXME: supply maxSizeOfHeaders.
49 int max_headers = 0x10000;
53 public HttpRequestChannel (HttpChannelFactory<IRequestChannel> factory,
54 EndpointAddress address, Uri via)
57 this.source = factory;
58 this.address = address;
62 public int MaxSizeOfHeaders {
63 get { return max_headers; }
66 public MessageEncoder Encoder {
67 get { return source.MessageEncoder; }
70 public override EndpointAddress RemoteAddress {
71 get { return address; }
74 public override Uri Via {
80 public override Message Request (Message message, TimeSpan timeout)
82 return ProcessRequest (message, timeout);
85 Message ProcessRequest (Message message, TimeSpan timeout)
87 // FIXME: is distination really like this?
88 Uri destination = message.Headers.To ?? Via ?? RemoteAddress.Uri;
90 web_request = HttpWebRequest.Create (destination);
91 web_request.Method = "POST";
92 web_request.ContentType = Encoder.ContentType;
94 #if !NET_2_1 // FIXME: implement this to not depend on Timeout property
95 web_request.Timeout = (int) timeout.TotalMilliseconds;
98 // There is no SOAP Action/To header when AddressingVersion is None.
99 if (message.Version.Addressing == AddressingVersion.None) {
100 if (message.Headers.Action != null) {
101 web_request.Headers ["SOAPAction"] = message.Headers.Action;
102 message.Headers.RemoveAll ("Action", message.Version.Addressing.Namespace);
103 if (message.Headers.Action != null) throw new Exception (message.Headers.Action);
107 // apply HttpRequestMessageProperty if exists.
108 bool suppressEntityBody = false;
110 string pname = HttpRequestMessageProperty.Name;
111 if (message.Properties.ContainsKey (pname)) {
112 HttpRequestMessageProperty hp = (HttpRequestMessageProperty) message.Properties [pname];
113 web_request.Headers.Add (hp.Headers);
114 web_request.Method = hp.Method;
115 // FIXME: do we have to handle hp.QueryString ?
116 if (hp.SuppressEntityBody)
117 suppressEntityBody = true;
121 if (!suppressEntityBody && String.Compare (web_request.Method, "GET", StringComparison.OrdinalIgnoreCase) != 0) {
122 MemoryStream buffer = new MemoryStream ();
123 Encoder.WriteMessage (message, buffer);
125 if (buffer.Length > int.MaxValue)
126 throw new InvalidOperationException ("The argument message is too large.");
129 web_request.ContentLength = (int) buffer.Length;
131 Stream requestStream = web_request.EndGetRequestStream (web_request.BeginGetRequestStream (null, null));
132 requestStream.Write (buffer.GetBuffer (), 0, (int) buffer.Length);
133 requestStream.Close ();
138 res = web_request.EndGetResponse (web_request.BeginGetResponse (null, null));
140 catch (WebException we) {
144 using (Stream responseStream = res.GetResponseStream ()) {
145 MemoryStream ms = new MemoryStream ();
146 byte [] b = new byte [65536];
150 n = responseStream.Read (b, 0, 65536);
155 ms.Seek (0, SeekOrigin.Begin);
157 Message ret = Encoder.ReadMessage (
158 //responseStream, MaxSizeOfHeaders);
159 ms, MaxSizeOfHeaders, res.ContentType);
161 MessageBuffer buf = ret.CreateBufferedCopy (0x10000);
162 ret = buf.CreateMessage ();
163 System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter (Console.Out);
164 w.Formatting = System.Xml.Formatting.Indented;
165 buf.CreateMessage ().WriteMessage (w);
175 public override IAsyncResult BeginRequest (Message message, TimeSpan timeout, AsyncCallback callback, object state)
177 ThrowIfDisposedOrNotOpen ();
179 return new HttpChannelRequestAsyncResult (this, message, timeout, callback, state);
182 public override Message EndRequest (IAsyncResult result)
185 throw new ArgumentNullException ("result");
186 HttpChannelRequestAsyncResult r = result as HttpChannelRequestAsyncResult;
188 throw new InvalidOperationException ("Wrong IAsyncResult");
195 protected override void OnAbort ()
197 throw new NotImplementedException ();
202 protected override void OnClose (TimeSpan timeout)
204 if (web_request != null)
205 web_request.Abort ();
209 protected override IAsyncResult OnBeginClose (TimeSpan timeout, AsyncCallback callback, object state)
211 throw new NotImplementedException ();
214 protected override void OnEndClose (IAsyncResult result)
216 throw new NotImplementedException ();
221 protected override void OnOpen (TimeSpan timeout)
225 protected override IAsyncResult OnBeginOpen (TimeSpan timeout, AsyncCallback callback, object state)
227 throw new NotImplementedException ();
230 protected override void OnEndOpen (IAsyncResult result)
232 throw new NotImplementedException ();
235 class HttpChannelRequestAsyncResult : IAsyncResult
237 HttpRequestChannel channel;
240 AsyncCallback callback;
247 public HttpChannelRequestAsyncResult (HttpRequestChannel channel, Message message, TimeSpan timeout, AsyncCallback callback, object state)
249 this.channel = channel;
250 this.message = message;
251 this.timeout = timeout;
252 this.callback = callback;
255 wait = new AutoResetEvent (false);
256 Thread t = new Thread (delegate () {
258 response = channel.ProcessRequest (message, timeout);
259 if (callback != null)
261 } catch (Exception ex) {
271 public Message Response {
272 get { return response; }
275 public WaitHandle AsyncWaitHandle {
279 public object AsyncState {
280 get { return state; }
283 public bool CompletedSynchronously {
284 get { return done && !waiting; }
287 public bool IsCompleted {
291 public void WaitEnd ()
295 wait.WaitOne (timeout, true);