コード例 #1
0
        private bool ProcessMultipartData(ref IDictionary <string, string> postVars, ref IDictionary <string, IFile> files)
        {
            bool?result = null;

            try
            {
                var match = _REGEX_MULTIPART_BOUNDARY.Match(this.ContentType ?? string.Empty);
                if (match.Success)
                {
                    postVars = new ConcurrentDictionary <string, string>(EqualityComparerFactory.CreateHttpKeyComparer());
                    files    = new ConcurrentDictionary <string, IFile>(EqualityComparerFactory.CreateHttpKeyComparer());

                    var parser = new HttpMultipartContentParser(this.GetBodyData(),
                                                                Encoding.ASCII.GetBytes("--" + match.Groups[9].Value));

                    foreach (var part in parser.PARTS
                             .Where(p => !string.IsNullOrWhiteSpace(p.NAME)))
                    {
                        try
                        {
                            var data = part.DATA ?? new byte[0];

                            if (string.IsNullOrWhiteSpace(part.FILE_NAME))
                            {
                                // POST data

                                postVars[part.NAME] = Encoding.ASCII.GetString(data);
                            }
                            else
                            {
                                // uploaded file
                                var newFile = new SimpleFile();
                                newFile.SetContentType(string.IsNullOrWhiteSpace(part.CONTENT_TYPE) ? "application/octet-stream" : part.CONTENT_TYPE.ToLower().Trim());
                                newFile.Name = part.FILE_NAME.Trim();
                                newFile.SetData(data);

                                files[part.NAME] = newFile;
                            }
                        }
                        catch
                        {
                            // ignore
                        }
                    }

                    parser = null;
                    result = true;
                }
            }
            catch
            {
                // ignore
            }

            return(result ?? false);
        }
コード例 #2
0
        // Private Methods (3) 

        private static IDictionary <string, string> ExtractVarsFromQueryString(IEnumerable <char> queryStr)
        {
            var result = new ConcurrentDictionary <string, string>(EqualityComparerFactory.CreateHttpKeyComparer());

            try
            {
                var coll = BclHttpUtility.ParseQueryString(queryStr.AsString() ?? string.Empty);
                foreach (var key in coll.AllKeys)
                {
                    result[key ?? string.Empty] = coll[key];
                }
            }
            catch
            {
                // ignore
            }

            return(result);
        }
コード例 #3
0
        internal HttpRequest(DateTimeOffset time,
                             HttpRequestMessageProperty property,
                             Message message,
                             WcfHttpServer server)
        {
            this._TIME     = time;
            this._PROPERTY = property;
            this._MESSAGE  = message;
            this._SERVER   = server;

            this._METHOD = (this._PROPERTY.Method ?? string.Empty).ToUpper().Trim();
            if (this._METHOD == string.Empty)
            {
                this._METHOD = null;
            }

            this._BODY = new Lazy <byte[]>(valueFactory: this.GetBodyDataInner,
                                           isThreadSafe: true);

            //  address of remote client
            try
            {
                var messageProperties = OperationContext.Current.IncomingMessageProperties;
                var endpointProperty  = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

                this._REMOTE_ADDRESS = new SimpleTcpAddress()
                {
                    Address = endpointProperty.Address,
                    Port    = endpointProperty.Port,
                };
            }
            catch
            {
                // ignore here
            }

            // requesting user
            try
            {
                ServiceSecurityContext context = ServiceSecurityContext.Current;
                if (context != null)
                {
                    var finder = this._SERVER.PrincipalFinder;
                    if (finder != null)
                    {
                        this._USER = finder(context.PrimaryIdentity);
                    }
                }
            }
            catch
            {
                // ignore here
            }

            // request headers
            {
                var headers = new ConcurrentDictionary <string, string>(EqualityComparerFactory.CreateHttpKeyComparer());
                foreach (var key in this._PROPERTY.Headers.AllKeys)
                {
                    headers[key] = this._PROPERTY.Headers[key];
                }

                this._HEADERS = new TMReadOnlyDictionary <string, string>(headers);
            }

            // content type
            this._HEADERS
            .TryGetValue("content-type",
                         out this._CONTENT_TYPE);
            if (string.IsNullOrWhiteSpace(this._CONTENT_TYPE))
            {
                this._CONTENT_TYPE = null;
            }
            else
            {
                this._CONTENT_TYPE = this._CONTENT_TYPE.ToLower().Trim();
            }

            // GET
            this._GET = new TMReadOnlyDictionary <string, string>(ExtractVarsFromQueryString(this._PROPERTY.QueryString));

            // POST
            {
                IDictionary <string, string> postVars = null;
                IDictionary <string, IFile>  files    = null;

                if (this.TryGetKnownMethod() == HttpMethod.POST)
                {
                    try
                    {
                        if (!this.ProcessMultipartData(ref postVars, ref files))
                        {
                            // no uploaded files, handle complete
                            // request body as variables

                            using (var reader = new StreamReader(this.GetBody(), Encoding.ASCII))
                            {
                                postVars = ExtractVarsFromQueryString(reader.ReadToEnd());
                            }
                        }
                    }
                    catch
                    {
                        // ignore
                    }
                }

                this._POST = new TMReadOnlyDictionary <string, string>(postVars ??
                                                                       new ConcurrentDictionary <string, string>(EqualityComparerFactory.CreateHttpKeyComparer()));

                this._FILES = new TMReadOnlyDictionary <string, IFile>(files ??
                                                                       new ConcurrentDictionary <string, IFile>(EqualityComparerFactory.CreateHttpKeyComparer()));
            }

            // REQUEST
            {
                var vars = new ConcurrentDictionary <string, string>(EqualityComparerFactory.CreateHttpKeyComparer());

                // first copy GET
                foreach (var g in this._GET)
                {
                    vars[g.Key] = g.Value;
                }

                // then set POST vars and overwrite existing ones
                foreach (var p in this._POST)
                {
                    vars[p.Key] = p.Value;
                }

                this._REQUEST = new TMReadOnlyDictionary <string, string>(vars);
            }
        }