Beispiel #1
0
        // Constructors

        internal HttpWebResponse(Uri uri, string method, WebConnectionData data, CookieContainer container)
        {
            this.uri          = uri;
            this.method       = method;
            webHeaders        = data.Headers;
            version           = data.Version;
            statusCode        = (HttpStatusCode)data.StatusCode;
            statusDescription = data.StatusDescription;
            stream            = data.stream;
            contentLength     = -1;

            try
            {
                string cl = webHeaders["Content-Length"];
#if SSHARP
                if (String.IsNullOrEmpty(cl) || !TryParsers.Int64TryParse(cl, out contentLength))
#else
                if (String.IsNullOrEmpty(cl) || !Int64.TryParse(cl, out contentLength))
#endif
                { contentLength = -1; }
            }
            catch (Exception)
            {
                contentLength = -1;
            }

            if (container != null)
            {
                this.cookie_container = container;
                FillCookies();
            }

            string content_encoding = webHeaders["Content-Encoding"];
            if (content_encoding == "gzip" && (data.request.AutomaticDecompression & DecompressionMethods.GZip) != 0)
            {
                stream = new GZipStream(stream, CompressionMode.Decompress);
            }
            else if (content_encoding == "deflate" && (data.request.AutomaticDecompression & DecompressionMethods.Deflate) != 0)
            {
                stream = new DeflateStream(stream, CompressionMode.Decompress);
            }
        }
Beispiel #2
0
		/// <summary>
		/// Creates a new instance of the <see cref="IP"/> class from a specific <see cref="String"/>.
		/// </summary>
		/// <param name="ip">IP string</param>
		public IP (string ip)
			{
			// IMPORTANT: copied from Mono's IPAddress.cs
			int pos = ip.IndexOf (' ');
			if (pos != -1)
				{
				string[] nets = ip.Substring (pos + 1).Split (new char[] {'.'});
				if (nets.Length > 0)
					{
					string lastNet = nets[nets.Length - 1];
					if (lastNet.Length == 0)
						throw new FormatException ("An invalid IP address was specified.");
#if NET_2_1 //workaround for smcs, as it generate code that can't access string.GetEnumerator ()
					foreach (char c in lastNet.ToCharArray ())
#else
					foreach (char c in lastNet)
						{
#endif
						if (!IsHexDigit (c))
							throw new FormatException ("An invalid IP address was specified.");
						}
					}
				ip = ip.Substring (0, pos);
				}

			if (ip.Length == 0 || ip[ip.Length - 1] == '.')
				throw new FormatException ("An invalid IP address was specified.");

			string[] ips = ip.Split (new char[] {'.'});
			if (ips.Length > IPv4Length)
				throw new FormatException ("An invalid IP address was specified.");

			// Make the number in network order
			try
				{
				_ip = new byte[IPv4Length];
				long val = 0;
				for (int i = 0; i < ips.Length; i++)
					{
					string subnet = ips[i];
					if ((3 <= subnet.Length && subnet.Length <= 4) && (subnet[0] == '0') && (subnet[1] == 'x' || subnet[1] == 'X'))
						{
						if (subnet.Length == 3)
							val = (byte)FromHex (subnet[2]);
						else
							val = (byte)((FromHex (subnet[2]) << 4) | FromHex (subnet[3]));
						}
					else if (subnet.Length == 0)
						throw new FormatException ("An invalid IP address was specified.");
					else if (subnet[0] == '0')
						{
						// octal
						val = 0;
						for (int j = 1; j < subnet.Length; j++)
							{
							if ('0' <= subnet[j] && subnet[j] <= '7')
								val = (val << 3) + subnet[j] - '0';
							else
								throw new FormatException ("An invalid IP address was specified.");
							}
						}
					else
						{
#if NETCF
						if (!TryParsers.Int64TryParse (subnet, NumberStyles.None, null, out val))
#else
						if (!long.TryParse (subnet, NumberStyles.None, null, out val))
#endif
							throw new FormatException ("An invalid IP address was specified.");
						}

					if (i == (ips.Length - 1))
						{
						if (i != 0 && val >= (256 << ((3 - i) * 8)))
							throw new FormatException ("An invalid IP address was specified.");
						else if (val > 0x3fffffffe) // this is the last number that parses correctly with MS
							throw new FormatException ("An invalid IP address was specified.");
						i = 3;
						}
					else if (val >= 0x100)
						throw new FormatException ("An invalid IP address was specified.");
					_ip[i] = (byte)val;
					}
				}
			catch (Exception)
				{
				throw new FormatException ("An invalid IP address was specified.");
				}
			}
Beispiel #3
0
        private void ProcessSimpleMethod()
        {
            State = RequestState.TransferInProgress;

            FtpStatus status;

            if (method == WebRequestMethods.Ftp.PrintWorkingDirectory)
            {
                method = "PWD";
            }

            if (method == WebRequestMethods.Ftp.Rename)
            {
                method = RenameFromCommand;
            }

            status = SendCommand(method, file_name);

            ftpResponse.Stream = Stream.Null;

            string desc = status.StatusDescription;

            switch (method)
            {
            case WebRequestMethods.Ftp.GetFileSize:
            {
                if (status.StatusCode != FtpStatusCode.FileStatus)
                {
                    throw CreateExceptionFromResponse(status);
                }

                int  i, len;
                long size;
                for (i = 4, len = 0; i < desc.Length && Char.IsDigit(desc[i]); i++, len++)
                {
                    ;
                }

                if (len == 0)
                {
                    throw new WebException("Bad format for server response in " + method);
                }

#if SSHARP
                if (!TryParsers.Int64TryParse(desc.Substring(4, len), out size))
#else
                if (!Int64.TryParse(desc.Substring(4, len), out size))
#endif
                { throw new WebException("Bad format for server response in " + method); }

                ftpResponse.contentLength = size;
            }
            break;

            case WebRequestMethods.Ftp.GetDateTimestamp:
                if (status.StatusCode != FtpStatusCode.FileStatus)
                {
                    throw CreateExceptionFromResponse(status);
                }
                ftpResponse.LastModified = DateTime.ParseExact(desc.Substring(4), "yyyyMMddHHmmss", null);
                break;

            case WebRequestMethods.Ftp.MakeDirectory:
                if (status.StatusCode != FtpStatusCode.PathnameCreated)
                {
                    throw CreateExceptionFromResponse(status);
                }
                break;

            case ChangeDir:
                method = WebRequestMethods.Ftp.PrintWorkingDirectory;

                if (status.StatusCode != FtpStatusCode.FileActionOK)
                {
                    throw CreateExceptionFromResponse(status);
                }

                status = SendCommand(method);

                if (status.StatusCode != FtpStatusCode.PathnameCreated)
                {
                    throw CreateExceptionFromResponse(status);
                }
                break;

            case RenameFromCommand:
                method = WebRequestMethods.Ftp.Rename;
                if (status.StatusCode != FtpStatusCode.FileCommandPending)
                {
                    throw CreateExceptionFromResponse(status);
                }
                // Pass an empty string if RenameTo wasn't specified
                status = SendCommand(RenameToCommand, renameTo != null ? renameTo : String.Empty);
                if (status.StatusCode != FtpStatusCode.FileActionOK)
                {
                    throw CreateExceptionFromResponse(status);
                }
                break;

            case WebRequestMethods.Ftp.DeleteFile:
                if (status.StatusCode != FtpStatusCode.FileActionOK)
                {
                    throw CreateExceptionFromResponse(status);
                }
                break;
            }

            State = RequestState.Finished;
        }