internal WcfHttpServerService(WcfHttpServer server)
 {
     this._SERVER = server;
 }
Beispiel #2
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);
            }
        }