| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605 |
- using System;
- using System.Collections;
- using System.Collections.Specialized;
- using System.Configuration;
- using System.Globalization;
- using System.IO;
- using System.Runtime;
- using System.Runtime.CompilerServices;
- using System.Runtime.Serialization;
- using System.Security;
- using System.Security.Cryptography.X509Certificates;
- using System.Security.Permissions;
- using System.Text;
- using System.Threading;
- using System.Net;
- namespace System.Net
- {
- [Serializable]
- public class HttpWebRequest : WebRequest
- {
- #region Fields
- private static readonly int DEFAULT_MAX_RESP_HEADER_LEN = 64;
- private static int _defaultMaxResponseHeadersLength = DEFAULT_MAX_RESP_HEADER_LEN;
- private HttpProvider _provider;
- #endregion
- #region Constructors
- internal HttpWebRequest(Uri uri)
- {
- _provider = HttpProvider.GetHttpProvider(uri);
- // Console.WriteLine("uri to string: " + uri.ToString());
- }
- #endregion
- #region Properties
- public string Accept
- {
- get{return Headers["Accept"];}
- set
- {
- if(_provider.IsRequestStarted ())
- throw new InvalidOperationException ("request started");
- _provider.Headers.RemoveAndAdd ("Accept", value);
- }
- }
- public Uri Address
- {
- get{return _provider.GetAddress();}
- }
- public bool AllowAutoRedirect
- {
- get{return _provider.AllowAutoRedirect;}
- set{_provider.AllowAutoRedirect = value;}
- }
- public bool AllowWriteStreamBuffering
- {
- get{return _provider.AllowWriteStreamBuffering;}
- set{_provider.AllowWriteStreamBuffering = value;}
- }
- [MonoTODO] //documentation related
- public X509CertificateCollection ClientCertificates
- {
- [MonoTODO]
- get{return _provider.GetX509Certificates();}
- [MonoNotSupported("")]
- set { throw new NotImplementedException (); }
- }
- public string Connection
- {
- get { return Headers["Connection"]; }
- set
- {
- if(_provider.IsRequestStarted())
- throw new InvalidOperationException ("request started");
- string val = value;
- if (val != null)
- val = val.Trim ().ToLower (CultureInfo.InvariantCulture);
- if (val == null || val.Length == 0)
- {
- Headers.RemoveInternal ("Connection");
- return;
- }
- if (val == "keep-alive" || val == "close")
- throw new ArgumentException ("Keep-Alive and Close may not be set with this property");
- // if (this.KeepAlive && val.IndexOf ("keep-alive") == -1)
- // value = value + ", Keep-Alive";
- Headers.RemoveAndAdd ("Connection", value);
- }
- }
- public override string ConnectionGroupName
- {
- get{return _provider.ConnectionGroupName;}
- set{_provider.ConnectionGroupName = value;}
- }
- public override long ContentLength
- {
- get{return _provider.ContentLength;}
- set
- {
- if(_provider.IsRequestStarted())
- throw new InvalidOperationException("Connection already opened");
- _provider.ContentLength = value;
- }
- }
- public override string ContentType
- {
- get { return Headers["Content-Type"]; }
- set
- {
- if (value == null || value.Trim().Length == 0)
- {
- Headers.RemoveInternal ("Content-Type");
- return;
- }
- Headers.RemoveAndAdd ("Content-Type", value);
- }
- }
- [MonoTODO] //needed for automatic documentation tools,
- //since currently we don't support this feature
- public HttpContinueDelegate ContinueDelegate
- {
- [MonoTODO]
- get{return _provider.ContinueDelegate;}
- [MonoTODO]
- set{_provider.ContinueDelegate = value;}
- }
- public CookieContainer CookieContainer
- {
- get{return _provider.CookieContainer;}
- set{_provider.CookieContainer = value;}
- }
- public override ICredentials Credentials
- {
- get{return _provider.Credentials;}
- set{_provider.Credentials = value;}
- }
- public static int DefaultMaximumResponseHeadersLength
- {
- get{return HttpProvider.DefaultMaxResponseHeadersLength;}
- set{HttpProvider.DefaultMaxResponseHeadersLength = value;}
- }
- public string Expect
- {
- get{return Headers["Expect"];}
- set
- {
- if(_provider.IsRequestStarted ())
- throw new InvalidOperationException("Connection already opened");
- string val = value;
- if (val != null)
- val = val.Trim ().ToLower (CultureInfo.InvariantCulture);
- if (val == null || val.Length == 0)
- {
- Headers.RemoveInternal ("Expect");
- return;
- }
- if (val == "100-continue")
- throw new ArgumentException ("100-Continue cannot be set with this property.",
- "value");
- Headers.RemoveAndAdd ("Expect", value);
- }
- }
- public bool HaveResponse
- {
- get{return _provider.IsHaveResponse();}
- }
- public override WebHeaderCollection Headers
- {
- get{return _provider.Headers;}
- set{_provider.Headers = value;}
- }
- public DateTime IfModifiedSince
- {
- get
- {
- string str = Headers["If-Modified-Since"];
- if (str == null)
- return DateTime.Now;
- try
- {
- return MonoHttpDate.Parse (str);
- }
- catch (Exception)
- {
- return DateTime.Now;
- }
- }
- set
- {
- if(_provider.IsRequestStarted ())
- throw new InvalidOperationException("Connection already started");
- // rfc-1123 pattern
- Headers.SetInternal ("If-Modified-Since",
- value.ToUniversalTime ().ToString ("r", null));
- // TODO: check last param when using different locale
- }
- }
- public bool KeepAlive
- {
- get{return _provider.KeepAlive;}
- set{_provider.KeepAlive = value;}
- }
- public int MaximumAutomaticRedirections
- {
- get{return _provider.MaxAutoRedirections;}
- set{_provider.MaxAutoRedirections = value;}
- }
- [MonoTODO] //documentation
- public int MaximumResponseHeadersLength
- {
- [MonoTODO]
- get{return _provider.MaximumResponseHeadersLength;}
- [MonoTODO]
- set{_provider.MaximumResponseHeadersLength = value;}
- }
- public string MediaType
- {
- get{return _provider.MediaType;}
- set{_provider.MediaType = value;}
- }
- public override string Method
- {
- get{return _provider.MethodName;}
- set{_provider.MethodName = value;}
- }
- [MonoTODO] //for documentation related - limited.
- public bool Pipelined
- {
- [MonoTODO]
- get{return _provider.Pipelined;}
- [MonoTODO]
- set{_provider.Pipelined = value;}
- }
- public override bool PreAuthenticate
- {
- get{return _provider.PreAuthenticate;}
- set{_provider.PreAuthenticate = value;}
- }
- public Version ProtocolVersion
- {
- get{return _provider.ProtocolVersion;}
- set{_provider.ProtocolVersion = value;}
- }
- public override IWebProxy Proxy
- {
- get{return _provider.Proxy;}
- set{_provider.Proxy = value;}
- }
- public int ReadWriteTimeout
- {
- get{return _provider.ReadWriteTimeout;}
- set{_provider.ReadWriteTimeout = value;}
- }
- public string Referer
- {
- get {return Headers["Referer"];}
- set
- {
- if(_provider.IsRequestStarted ())
- throw new InvalidOperationException("Connection already opened");
- if (value == null || value.Trim().Length == 0)
- {
- Headers.RemoveInternal ("Referer");
- return;
- }
- Headers.SetInternal ("Referer", value);
- }
- }
- internal Uri AuthUri
- {
- get { return RequestUri; }
- }
- public override Uri RequestUri
- {
- get{return _provider.GetOriginalAddress();}
- }
- public bool SendChunked
- {
- get{return _provider.SendChunked;}
- set{_provider.SendChunked = value;}
- }
- public ServicePoint ServicePoint
- {
- get{return _provider.ServicePoint;}
- }
- [MonoTODO] //once again - needed since our impl. still
- //doesn't support this feature we need document it..
- public override int Timeout
- {
- [MonoTODO]
- get{return _provider.Timeout;}
- [MonoTODO]
- set{_provider.Timeout = value;}
- }
- public string TransferEncoding
- {
- get { return Headers ["Transfer-Encoding"]; }
- set
- {
- if(_provider.IsRequestStarted ())
- {
- throw new InvalidOperationException("Connection has been already opened");
- }
- string val = value;
- if (val != null)
- val = val.Trim ().ToLower (CultureInfo.InvariantCulture);
- if (val == null || val.Length == 0)
- {
- Headers.RemoveInternal ("Transfer-Encoding");
- return;
- }
- if (val == "chunked")
- throw new ArgumentException ("Chunked encoding must be set with the SendChunked property");
- if (!this.SendChunked)
- throw new InvalidOperationException ("SendChunked must be True");
- Headers.RemoveAndAdd ("Transfer-Encoding", value);
- }
- }
- public bool UnsafeAuthenticatedConnectionSharing
- {
- get { throw new NotImplementedException (); }
- set { throw new NotImplementedException (); }
- }
- public string UserAgent
- {
- get { return Headers ["User-Agent"]; }
- set { Headers.SetInternal ("User-Agent", value); }
- }
- #endregion
- #region Methods
- //todo
- public override void Abort()
- {
- _provider.Abort();
- // _connection.disconnect();
- // _haveResponse = true;
- // //aborted = true;
- // if (_asyncWrite != null)
- // {
- // GHWebAsyncResult r = _asyncWrite;
- // WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
- // r.SetCompleted (false, wexc);
- // r.DoCallback ();
- // _asyncWrite = null;
- // }
- //
- // if (_asyncRead != null)
- // {
- // GHWebAsyncResult r = _asyncRead;
- // WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
- // r.SetCompleted (false, wexc);
- // r.DoCallback ();
- // _asyncRead = null;
- // }
- //
- //// if (abortHandler != null)
- //// {
- //// try
- //// {
- //// abortHandler (this, EventArgs.Empty);
- //// }
- //// catch {}
- //// abortHandler = null;
- //// }
- //
- // if (_writeStream != null)
- // {
- // try
- // {
- // _writeStream.Close ();
- // _writeStream = null;
- // }
- // catch {}
- // }
- //
- // if (_response != null)
- // {
- // try
- // {
- // _response.Close ();
- // _response = null;
- // }
- // catch {}
- // }
- }
- 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 = Headers ["Range"];
- if (value == null || value.Length == 0)
- value = rangeSpecifier + "=";
- else if (value.StartsWith (rangeSpecifier.ToLower () + "=", StringComparison.InvariantCultureIgnoreCase))
- value += ",";
- else
- throw new InvalidOperationException ("rangeSpecifier");
- Headers.RemoveAndAdd ("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 = Headers ["Range"];
- if (value == null || value.Length == 0)
- value = rangeSpecifier + "=";
- else if (value.StartsWith (rangeSpecifier.ToLower () + "=", StringComparison.InvariantCultureIgnoreCase))
- value += ",";
- else
- throw new InvalidOperationException ("rangeSpecifier");
- Headers.RemoveAndAdd ("Range", value + from + "-" + to);
- }
- public override Stream GetRequestStream()
- {
- return _provider.GetRequestStream();
- // lock(this)
- // {
- // Type t = Type.GetType("System.IO.ConsoleWriteStream", true);
- // _connection.setDoOutput(true);
- //
- //
- //// Console.WriteLine("Request is sent with following headers:");
- //// java.util.Map map = _connection.getRequestProperties();
- //// for(java.util.Iterator iter = map.keySet().iterator(); iter.hasNext();)
- //// {
- //// string key = (string) iter.next();
- //// Console.WriteLine(key + ": " + map.get(key));
- //// }
- //
- // foreach(string k in Headers)
- // {
- // string val = Headers[k];
- // val = (val == null) ? "" : val;
- // _connection.setRequestProperty(k, val);
- // }
- //
- // _writeStream = (Stream) Activator.CreateInstance(t, new object[]{_connection.getOutputStream()});
- // _haveRequest = true;
- // return _writeStream;
- // }
- }
- public override WebResponse GetResponse()
- {
- return _provider.GetResponse();
- }
- /*
- private void CommonChecks (bool putpost)
- {
- string method = _connection.getRequestMethod();
- if (method == null)
- throw new ProtocolViolationException ("Method is null.");
- bool keepAlive = _headers["Keep-Alive"] == null;
- bool allowBuffering = true;
- bool sendChunked = true;
- long contentLength = _connection.getContentLength();
- if (putpost && ((!keepAlive || (contentLength == -1 && !sendChunked)) && !allowBuffering))
- throw new ProtocolViolationException ("Content-Length not set");
- string transferEncoding = TransferEncoding;
- if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
- throw new ProtocolViolationException ("SendChunked should be true.");
- }
- */
- public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
- {
- return _provider.BeginGetRequestStream(callback, state);
- }
- public override Stream EndGetRequestStream(IAsyncResult asyncResult)
- {
- return _provider.EndGetRequestStream(asyncResult);
- }
- public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
- {
- //todo check input, http headers etc.
- return _provider.BeginGetResponse(callback, state);
- }
- public override WebResponse EndGetResponse(IAsyncResult asyncResult)
- {
- return _provider.EndGetResponse(asyncResult);
- }
- #endregion
- #region Inner Classes
- // #region JavaHeaders class
- // [Serializable]
- // internal sealed class JavaHeaders : WebHeaderCollection
- // {
- // private java.net.HttpURLConnection _connection;
- //
- // internal JavaHeaders(java.net.HttpURLConnection con)
- // {
- // _connection = con;
- // }
- //
- // public string this[string key]
- // {
- // get
- // {
- // return _connection.getHeaderField(key);
- // }
- // set
- // {
- // _connection.addRequestProperty(key, value);
- // }
- // }
- // }
- // #endregion
- #endregion
- }
- }
|