Пример #1
0
    /// <summary>
    /// Adds a file to the collection of posted files
    /// </summary>
    /// <param name="file">The file to add</param>
    /// <exception cref="ArgumentException">If the file is already added</exception>
    /// <exception cref="ArgumentNullException">If file is null</exception>
    /// <exception cref="InvalidOperationException">If the instance is HttpForm.EmptyForm which cannot be modified</exception>
    public void AddFile(HttpFile file)
    {
      if (_ignoreChanges)
        throw new InvalidOperationException("Cannot add files to instance HttpForm.EmptyForm.");
      if (file == null)
        throw new ArgumentNullException("file");

      if (_files.ContainsKey(file.Name))
        throw new ArgumentException("File named '" + file.Name + "' already exists!");

      _files.Add(file.Name, file);
    }
Пример #2
0
 /// <summary>
 /// Add a new file.
 /// </summary>
 /// <param name="file">File to add.</param>
 public void Add(HttpFile file)
 {
     _files.Add(file.Name, file);
 }
Пример #3
0
        public OSHttpRequest(IHttpClientContext context, IHttpRequest req)
        {
            _request = req;
            _context = context;

            if (null != req.Headers["content-encoding"])
                _contentEncoding = Encoding.GetEncoding(_request.Headers["content-encoding"]);
            if (null != req.Headers["content-type"])
                _contentType = _request.Headers["content-type"];
            if (null != req.Headers["user-agent"])
                _userAgent = req.Headers["user-agent"];
            if (null != req.Headers["remote_addr"])
            {
                try
                {
                    IPAddress addr = IPAddress.Parse(req.Headers["remote_addr"]);
                    // sometimes req.Headers["remote_port"] returns a comma separated list, so use
                    // the first one in the list and log it 
                    string[] strPorts = req.Headers["remote_port"].Split(new char[] { ',' });
                    if (strPorts.Length > 1)
                    {
                        MainConsole.Instance.ErrorFormat("[OSHttpRequest]: format exception on addr/port {0}:{1}, ignoring",
                                     req.Headers["remote_addr"], req.Headers["remote_port"]);
                    }
                    int port = Int32.Parse(strPorts[0]);
                    _remoteIPEndPoint = new IPEndPoint(addr, port);
                }
                catch (FormatException)
                {
                    MainConsole.Instance.ErrorFormat("[OSHttpRequest]: format exception on addr/port {0}:{1}, ignoring",
                                     req.Headers["remote_addr"], req.Headers["remote_port"]);
                }
            }

            _queryString = new NameValueCollection();
            _query = new Hashtable();
            try
            {
                foreach (HttpInputItem item in req.QueryString)
                {
                    try
                    {
                        _queryString.Add(item.Name, item.Value);
                        _query[item.Name] = item.Value;
                    }
                    catch (InvalidCastException)
                    {
                        MainConsole.Instance.DebugFormat("[OSHttpRequest]: error parsing {0} query item, skipping it", item.Name);
                        continue;
                    }
                }
            }
            catch (Exception)
            {
                MainConsole.Instance.ErrorFormat("[OSHttpRequest]: Error parsing querystring");
            }

            if (ContentType != null && ContentType.StartsWith("multipart/form-data"))
            {
                HttpMultipart.Element element;
                var boundry = "";
                var multipart = new HttpMultipart(InputStream, boundry, ContentEncoding ?? Encoding.UTF8);

                while ((element = multipart.ReadNextElement()) != null)
                {
                    if (string.IsNullOrEmpty(element.Name))
                        throw new FormatException("Error parsing request. Missing value name.\nElement: " + element);

                    if (!string.IsNullOrEmpty(element.Filename))
                    {
                        if (string.IsNullOrEmpty(element.ContentType))
                            throw new FormatException("Error parsing request. Value '" + element.Name +
                                                      "' lacks a content type.");

                        // Read the file data
                        var buffer = new byte[element.Length];
                        InputStream.Seek(element.Start, SeekOrigin.Begin);
                        InputStream.Read(buffer, 0, (int)element.Length);

                        // Generate a filename
                        var originalFileName = element.Filename;
                        var internetCache = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);

                        // if the internet path doesn't exist, assume mono and /var/tmp
                        var path = string.IsNullOrEmpty(internetCache)
                                       ? Path.Combine("var", "tmp")
                                       : Path.Combine(internetCache.Replace("\\\\", "\\"), "tmp");

                        element.Filename = Path.Combine(path, Math.Abs(element.Filename.GetHashCode()) + ".tmp");

                        // If the file exists generate a new filename
                        while (File.Exists(element.Filename))
                            element.Filename = Path.Combine(path, Math.Abs(element.Filename.GetHashCode() + 1) + ".tmp");

                        if (!Directory.Exists(path))
                            Directory.CreateDirectory(path);

                        File.WriteAllBytes(element.Filename, buffer);

                        var file = new HttpFile
                        {
                            Name = element.Name,
                            OriginalFileName = originalFileName,
                            ContentType = element.ContentType,
                            TempFileName = element.Filename
                        };
                        Files.Add(element.Name, file);
                    }
                    /*else
                    {
                        var buffer = new byte[element.Length];
                        message.Body.Seek(element.Start, SeekOrigin.Begin);
                        message.Body.Read(buffer, 0, (int)element.Length);

                        form.Add(Uri.UnescapeDataString(element.Name), message.ContentEncoding.GetString(buffer));
                    }*/
                }
            }
        }