| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- //
- // System.Net.HttpWebRequest
- //
- // Author:
- // Lawrence Pit ([email protected])
- //
- using System;
- using System.Collections;
- using System.IO;
- using System.Net.Sockets;
- using System.Runtime.Remoting.Messaging;
- using System.Runtime.Serialization;
- using System.Security.Cryptography.X509Certificates;
- using System.Threading;
- namespace System.Net
- {
- [Serializable]
- public class HttpWebRequest : WebRequest, ISerializable
- {
- private Uri requestUri;
- private Uri actualUri = null;
- private bool allowAutoRedirect = true;
- private bool allowBuffering = true;
- private X509CertificateCollection certificate = null;
- private string connectionGroup = null;
- private long contentLength = -1;
- private HttpContinueDelegate continueDelegate = null;
- private CookieContainer cookieContainer = null;
- private ICredentials credentials = null;
- private bool haveResponse = false;
- private WebHeaderCollection webHeaders;
- private bool keepAlive = true;
- private int maxAutoRedirect = 50;
- private string mediaType = String.Empty;
- private string method;
- private bool pipelined = true;
- private bool preAuthenticate = false;
- private Version version;
- private IWebProxy proxy;
- private bool sendChunked = false;
- private ServicePoint servicePoint = null;
- private int timeout = System.Threading.Timeout.Infinite;
-
- private Stream requestStream = null;
- private HttpWebResponse webResponse = null;
- private AutoResetEvent requestEndEvent = null;
- private bool requesting = false;
- private bool asyncResponding = false;
-
- // Constructors
-
- internal HttpWebRequest (Uri uri)
- {
- this.requestUri = uri;
- this.actualUri = uri;
- this.webHeaders = new WebHeaderCollection (true);
- this.webHeaders.SetInternal ("Host", uri.Authority);
- this.webHeaders.SetInternal ("Date", DateTime.Now.ToUniversalTime ().ToString ("r", null));
- this.webHeaders.SetInternal ("Expect", "100-continue");
- this.method = "GET";
- this.version = HttpVersion.Version11;
- this.proxy = GlobalProxySelection.Select;
- }
-
- [MonoTODO]
- protected HttpWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
- {
- throw new NotImplementedException ();
- }
-
- // Properties
-
- public string Accept {
- get { return webHeaders ["Accept"]; }
- set {
- CheckRequestStarted ();
- webHeaders.SetInternal ("Accept", value);
- }
- }
-
- public Uri Address {
- get { return actualUri; }
- }
-
- public bool AllowAutoRedirect {
- get { return allowAutoRedirect; }
- set { this.allowAutoRedirect = value; }
- }
-
- public bool AllowWriteStreamBuffering {
- get { return allowBuffering; }
- set { this.allowBuffering = value; }
- }
-
- public X509CertificateCollection ClientCertificates {
- get { return certificate; }
- }
-
- public string Connection {
- get { return webHeaders ["Connection"]; }
- set {
- CheckRequestStarted ();
- string val = value;
- if (val != null)
- val = val.Trim ().ToLower ();
- if (val == null || val.Length == 0) {
- webHeaders.RemoveInternal ("Connection");
- return;
- }
- if (val == "keep-alive" || val == "close")
- throw new ArgumentException ("value");
- if (KeepAlive && val.IndexOf ("keep-alive") == -1)
- value = value + ", Keep-Alive";
-
- webHeaders.SetInternal ("Connection", value);
- }
- }
-
- public override string ConnectionGroupName {
- get { return connectionGroup; }
- set { connectionGroup = value; }
- }
-
- public override long ContentLength {
- get { return contentLength; }
- set {
- CheckRequestStarted ();
- if (value < 0)
- throw new ArgumentException ("value");
- contentLength = value;
- webHeaders.SetInternal ("Content-Length", Convert.ToString (value));
- }
- }
-
- public override string ContentType {
- get { return webHeaders ["Content-Type"]; }
- set {
- CheckRequestStarted ();
- if (value == null || value.Trim().Length == 0) {
- webHeaders.RemoveInternal ("Content-Type");
- return;
- }
- webHeaders.SetInternal ("Content-Type", value);
- }
- }
-
- public HttpContinueDelegate ContinueDelegate {
- get { return continueDelegate; }
- set { continueDelegate = value; }
- }
-
- public CookieContainer CookieContainer {
- get { return cookieContainer; }
- set { cookieContainer = value; }
- }
-
- public override ICredentials Credentials {
- get { return credentials; }
- set { credentials = value; }
- }
-
- public string Expect {
- get { return webHeaders ["Expect"]; }
- set {
- CheckRequestStarted ();
- string val = value;
- if (val != null)
- val = val.Trim ().ToLower ();
- if (val == null || val.Length == 0) {
- webHeaders.RemoveInternal ("Expect");
- return;
- }
- if (val == "100-continue")
- throw new ArgumentException ("value");
- webHeaders.SetInternal ("Expect", value);
- }
- }
-
- public bool HaveResponse {
- get { return haveResponse; }
- }
-
- public override WebHeaderCollection Headers {
- get { return webHeaders; }
- set {
- CheckRequestStarted ();
- WebHeaderCollection newHeaders = new WebHeaderCollection (true);
- int count = value.Count;
- for (int i = 0; i < count; i++)
- newHeaders.Add (value.GetKey (i), value.Get (i));
- newHeaders.SetInternal ("Host", this.webHeaders["Host"]);
- newHeaders.SetInternal ("Date", this.webHeaders["Date"]);
- newHeaders.SetInternal ("Expect", this.webHeaders["Expect"]);
- newHeaders.SetInternal ("Connection", this.webHeaders["Connection"]);
- webHeaders = newHeaders;
- }
- }
-
- public DateTime IfModifiedSince {
- get {
- string str = webHeaders ["If-Modified-Since"];
- if (str == null)
- return DateTime.Now;
- try {
- return MonoHttpDate.Parse (str);
- } catch (Exception) {
- return DateTime.Now;
- }
- }
- set {
- CheckRequestStarted ();
- // rfc-1123 pattern
- webHeaders.SetInternal ("If-Modified-Since",
- value.ToUniversalTime ().ToString ("r", null));
- // TODO: check last param when using different locale
- }
- }
- public bool KeepAlive {
- get {
- CheckRequestStarted ();
- return keepAlive;
- }
- set {
- CheckRequestStarted ();
- keepAlive = value;
- if (Connection == null)
- webHeaders.SetInternal ("Connection", value ? "Keep-Alive" : "Close");
- }
- }
-
- public int MaximumAutomaticRedirections {
- get { return maxAutoRedirect; }
- set {
- if (value < 0)
- throw new ArgumentException ("value");
- maxAutoRedirect = value;
- }
- }
-
- public string MediaType {
- get { return mediaType; }
- set {
- CheckRequestStarted ();
- mediaType = value;
- }
- }
-
- public override string Method {
- get { return this.method; }
- set {
- CheckRequestStarted ();
-
- if (value == null ||
- (value != "GET" &&
- value != "HEAD" &&
- value != "POST" &&
- value != "PUT" &&
- value != "DELETE" &&
- value != "TRACE" &&
- value != "OPTIONS"))
- throw new ArgumentException ("not a valid method");
- if (contentLength != -1 &&
- value != "POST" &&
- value != "PUT")
- throw new ArgumentException ("method must be PUT or POST");
-
- method = value;
- }
- }
-
- public bool Pipelined {
- get { return pipelined; }
- set { this.pipelined = value; }
- }
-
- public override bool PreAuthenticate {
- get { return preAuthenticate; }
- set { preAuthenticate = value; }
- }
-
- public Version ProtocolVersion {
- get { return version; }
- set {
- if (value != HttpVersion.Version10 && value != HttpVersion.Version11)
- throw new ArgumentException ("value");
- version = (Version) value;
- }
- }
-
- public override IWebProxy Proxy {
- get { return proxy; }
- set {
- if (value == null)
- throw new ArgumentNullException ("value");
- proxy = value;
- }
- }
-
- public string Referer {
- get { return webHeaders ["Referer" ]; }
- set {
- CheckRequestStarted ();
- if (value == null || value.Trim().Length == 0) {
- webHeaders.RemoveInternal ("Referer");
- return;
- }
- webHeaders.SetInternal ("Referer", value);
- }
- }
- public override Uri RequestUri {
- get { return requestUri; }
- }
-
- public bool SendChunked {
- get { return sendChunked; }
- set {
- CheckRequestStarted ();
- sendChunked = value;
- }
- }
-
- public ServicePoint ServicePoint {
- get { return servicePoint; }
- }
-
- public override int Timeout {
- get { return timeout; }
- set { timeout = value; }
- }
-
- public string TransferEncoding {
- get { return webHeaders ["Transfer-Encoding"]; }
- set {
- CheckRequestStarted ();
- if (!sendChunked)
- throw new InvalidOperationException ("SendChunked must be True");
- string val = value;
- if (val != null)
- val = val.Trim ().ToLower ();
- if (val == null || val.Length == 0) {
- webHeaders.RemoveInternal ("Transfer-Encoding");
- return;
- }
- if (val == "chunked")
- throw new ArgumentException ("Cannot set value to Chunked");
- webHeaders.SetInternal ("Transfer-Encoding", value);
- }
- }
-
- public string UserAgent {
- get { return webHeaders ["User-Agent"]; }
- set { webHeaders.SetInternal ("User-Agent", value); }
- }
-
- // Methods
-
- public void AddRange (int range)
- {
- AddRange ("bytes", range);
- }
-
- public void AddRange (int from, int to)
- {
- AddRange ("bytes", from, to);
- }
-
- public void AddRange (string rangeSpecifier, int range)
- {
- if (rangeSpecifier == null)
- throw new ArgumentNullException ("rangeSpecifier");
- string value = webHeaders ["Range"];
- if (value == null || value.Length == 0)
- value = rangeSpecifier + "=";
- else if (value.ToLower ().StartsWith (rangeSpecifier.ToLower () + "="))
- value += ",";
- else
- throw new InvalidOperationException ("rangeSpecifier");
- webHeaders.SetInternal ("Range", value + range + "-");
- }
-
- public void AddRange (string rangeSpecifier, int from, int to)
- {
- if (rangeSpecifier == null)
- throw new ArgumentNullException ("rangeSpecifier");
- if (from < 0 || to < 0 || from > to)
- throw new ArgumentOutOfRangeException ();
- string value = webHeaders ["Range"];
- if (value == null || value.Length == 0)
- value = rangeSpecifier + "=";
- else if (value.ToLower ().StartsWith (rangeSpecifier.ToLower () + "="))
- value += ",";
- else
- throw new InvalidOperationException ("rangeSpecifier");
- webHeaders.SetInternal ("Range", value + from + "-" + to);
- }
-
- public override int GetHashCode ()
- {
- return base.GetHashCode ();
- }
-
- private delegate Stream GetRequestStreamCallback ();
- private delegate WebResponse GetResponseCallback ();
-
- public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
- {
- if (method == null || (!method.Equals ("PUT") && !method.Equals ("POST")))
- throw new ProtocolViolationException ("Cannot send file when method is: " + this.method + ". Method must be PUT.");
- // workaround for bug 24943
- Exception e = null;
- lock (this) {
- if (asyncResponding || webResponse != null)
- e = new InvalidOperationException ("This operation cannot be performed after the request has been submitted.");
- else if (requesting)
- e = new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress.");
- else
- requesting = true;
- }
- if (e != null)
- throw e;
- /*
- lock (this) {
- if (asyncResponding || webResponse != null)
- throw new InvalidOperationException ("This operation cannot be performed after the request has been submitted.");
- if (requesting)
- throw new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress.");
- requesting = true;
- }
- */
- GetRequestStreamCallback c = new GetRequestStreamCallback (this.GetRequestStreamInternal);
- return c.BeginInvoke (callback, state);
- }
- public override Stream EndGetRequestStream (IAsyncResult asyncResult)
- {
- if (asyncResult == null)
- throw new ArgumentNullException ("asyncResult");
- if (!asyncResult.IsCompleted)
- asyncResult.AsyncWaitHandle.WaitOne ();
- AsyncResult async = (AsyncResult) asyncResult;
- GetRequestStreamCallback cb = (GetRequestStreamCallback) async.AsyncDelegate;
- return cb.EndInvoke (asyncResult);
- }
-
- public override Stream GetRequestStream()
- {
- IAsyncResult asyncResult = BeginGetRequestStream (null, null);
- if (!(asyncResult.AsyncWaitHandle.WaitOne (timeout, false))) {
- throw new WebException("The request timed out", WebExceptionStatus.Timeout);
- }
- return EndGetRequestStream (asyncResult);
- }
-
- internal Stream GetRequestStreamInternal ()
- {
- if (this.requestStream == null)
- this.requestStream = new HttpWebStream (this);
- return this.requestStream;
- }
-
- public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
- {
- // workaround for bug 24943
- Exception e = null;
- lock (this) {
- if (asyncResponding)
- e = new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress.");
- else
- asyncResponding = true;
- }
- if (e != null)
- throw e;
- /*
- lock (this) {
- if (asyncResponding)
- throw new InvalidOperationException ("Cannot re-call start of asynchronous method while a previous call is still in progress.");
- asyncResponding = true;
- }
- */
- GetResponseCallback c = new GetResponseCallback (this.GetResponseInternal);
- return c.BeginInvoke (callback, state);
- }
-
- public override WebResponse EndGetResponse (IAsyncResult asyncResult)
- {
- if (asyncResult == null)
- throw new ArgumentNullException ("asyncResult");
- if (!asyncResult.IsCompleted)
- asyncResult.AsyncWaitHandle.WaitOne ();
- AsyncResult async = (AsyncResult) asyncResult;
- GetResponseCallback cb = (GetResponseCallback) async.AsyncDelegate;
- WebResponse webResponse = cb.EndInvoke(asyncResult);
- asyncResponding = false;
- return webResponse;
- }
-
- public override WebResponse GetResponse()
- {
- IAsyncResult asyncResult = BeginGetResponse (null, null);
- if (!(asyncResult.AsyncWaitHandle.WaitOne (timeout, false))) {
- throw new WebException("The request timed out", WebExceptionStatus.Timeout);
- }
- return EndGetResponse (asyncResult);
- }
-
- public WebResponse GetResponseInternal ()
- {
- if (webResponse != null)
- return webResponse;
- Stream responseStream = this.requestStream == null ?
- new HttpWebStream (this) : this.requestStream;
- do {
- this.webResponse = new HttpWebResponse (this.actualUri, method, responseStream);
- } while (this.webResponse.StatusCode == HttpStatusCode.Continue);
- return (WebResponse) this.webResponse;
- }
- [MonoTODO]
- public override void Abort()
- {
- this.haveResponse = true;
- throw new NotImplementedException ();
- }
-
- [MonoTODO]
- void ISerializable.GetObjectData (SerializationInfo serializationInfo,
- StreamingContext streamingContext)
- {
- throw new NotImplementedException ();
- }
-
- // Private Methods
-
- private void CheckRequestStarted ()
- {
- if (requesting)
- throw new InvalidOperationException ("request started");
- }
-
- internal void Close ()
- {
- // already done in class below
- // if (requestStream != null) {
- // requestStream.Close ();
- // }
- lock (this) {
- requesting = false;
- if (requestEndEvent != null)
- requestEndEvent.Set ();
- // requestEndEvent = null;
- }
- }
-
- // Private Classes
-
- // to catch the Close called on the NetworkStream
- internal class HttpWebStream : NetworkStream
- {
- HttpWebRequest webRequest;
-
- internal HttpWebStream (HttpWebRequest webRequest)
- : base (HttpWebStream.CreateSocket (webRequest), true)
- {
- StreamWriter webWriter = null;
- webWriter = new StreamWriter (this);
-
- webWriter.Write (webRequest.Method + " " +
- webRequest.actualUri.PathAndQuery + " HTTP/" + webRequest.version.ToString(2) + "\r\n");
- foreach (string header in webRequest.webHeaders)
- webWriter.Write (header + ": " + webRequest.webHeaders[header] + "\r\n");
- // FIXME: write cookie headers (CookieContainer not yet implemented)
- webWriter.Write ("\r\n");
- webWriter.Flush();
- this.webRequest = webRequest;
- }
-
- private static Socket CreateSocket (HttpWebRequest webRequest)
- {
- IPAddress hostAddr = Dns.Resolve (webRequest.actualUri.Host).AddressList[0];
- IPEndPoint endPoint = new IPEndPoint (hostAddr, webRequest.actualUri.Port);
- Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
- ProtocolType.Tcp);
- socket.Connect (endPoint);
- return socket;
- }
-
- public override void Close()
- {
- base.Close ();
- webRequest.Close ();
- }
- }
- }
- }
|