コード例 #1
0
        public async Task Request_MultiplePrefixes(string requestUri, string expectedPathBase, string expectedPath)
        {
            // TODO: We're just doing this to get a dynamic port. This can be removed later when we add support for hot-adding prefixes.
            string root;
            var    server = Utilities.CreateHttpServerReturnRoot("/", out root);

            server.Dispose();
            server = new WebListener();
            using (server)
            {
                var uriBuilder = new UriBuilder(root);
                foreach (string path in new[] { "/", "/11", "/2/3", "/2", "/11/2" })
                {
                    server.UrlPrefixes.Add(UrlPrefix.Create(uriBuilder.Scheme, uriBuilder.Host, uriBuilder.Port, path));
                }
                server.Start();

                Task <string> responseTask = SendRequestAsync(root + requestUri);

                var context = await server.GetContextAsync();

                var request = context.Request;

                Assert.Equal(expectedPath, request.Path);
                Assert.Equal(expectedPathBase, request.PathBase);

                context.Dispose();

                string response = await responseTask;
                Assert.Equal(string.Empty, response);
            }
        }
コード例 #2
0
ファイル: Utilities.cs プロジェクト: mah1212/WebListener
 internal static WebListener CreateDynamicHttpServer(string basePath, out string root, out string baseAddress)
 {
     lock (PortLock)
     {
         while (NextPort < MaxPort)
         {
             var port   = NextPort++;
             var prefix = UrlPrefix.Create("http", "localhost", port, basePath);
             root        = prefix.Scheme + "://" + prefix.Host + ":" + prefix.Port;
             baseAddress = prefix.ToString();
             var listener = new WebListener();
             listener.UrlPrefixes.Add(prefix);
             try
             {
                 listener.Start();
                 return(listener);
             }
             catch (WebListenerException)
             {
                 listener.Dispose();
             }
         }
         NextPort = BasePort;
     }
     throw new Exception("Failed to locate a free port.");
 }
コード例 #3
0
 private void ParseAddresses(ICollection <string> addresses, Microsoft.Net.Http.Server.WebListener listener)
 {
     foreach (var value in addresses)
     {
         listener.UrlPrefixes.Add(UrlPrefix.Create(value));
     }
 }
コード例 #4
0
ファイル: Utilities.cs プロジェクト: mah1212/WebListener
        internal static WebListener CreateServer(string scheme, string host, int port, string path)
        {
            WebListener listener = new WebListener();

            listener.UrlPrefixes.Add(UrlPrefix.Create(scheme, host, port, path));
            listener.Start();
            return(listener);
        }
コード例 #5
0
 internal RequestContext(WebListener server, NativeRequestContext memoryBlob)
 {
     // TODO: Verbose log
     _server     = server;
     _memoryBlob = memoryBlob;
     _request    = new Request(this, _memoryBlob);
     _response   = new Response(this);
     _request.ReleasePins();
     AuthenticationChallenges = server.AuthenticationManager.AuthenticationSchemes & ~AuthenticationSchemes.AllowAnonymous;
 }
コード例 #6
0
        internal TimeoutManager(WebListener listener)
        {
            _server = listener;

            // We have to maintain local state since we allow applications to set individual timeouts. Native Http
            // API for setting timeouts expects all timeout values in every call so we have remember timeout values
            // to fill in the blanks. Except MinSendBytesPerSecond, local state for remaining five timeouts is
            // maintained in timeouts array.
            //
            // No initialization is required because a value of zero indicates that system defaults should be used.
            _timeouts = new int[5];

            LoadConfigurationSettings();
        }
コード例 #7
0
        public MessagePump(IOptions <WebListenerOptions> options, ILoggerFactory loggerFactory)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            _listener        = options.Value?.Listener ?? new Microsoft.Net.Http.Server.WebListener(loggerFactory);
            _logger          = LogHelper.CreateLogger(loggerFactory, typeof(MessagePump));
            Features         = new FeatureCollection();
            _serverAddresses = new ServerAddressesFeature();
            Features.Set <IServerAddressesFeature>(_serverAddresses);

            _processRequest = new Action <object>(ProcessRequestAsync);
            _maxAccepts     = DefaultMaxAccepts;
            _shutdownSignal = new ManualResetEvent(false);
        }
コード例 #8
0
 public ConnectionCancellation(WebListener parent)
 {
     _parent = parent;
 }
コード例 #9
0
 internal UrlPrefixCollection(WebListener webListener)
 {
     _webListener = webListener;
 }
コード例 #10
0
 internal AuthenticationManager(WebListener listener)
 {
     _server      = listener;
     _authSchemes = AuthenticationSchemes.AllowAnonymous;
 }
コード例 #11
0
        private static void IOCompleted(AsyncAcceptContext asyncResult, uint errorCode, uint numBytes)
        {
            bool complete = false;

            try
            {
                if (errorCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS &&
                    errorCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_MORE_DATA)
                {
                    asyncResult.Tcs.TrySetException(new WebListenerException((int)errorCode));
                    complete = true;
                }
                else
                {
                    WebListener server = asyncResult.Server;
                    if (errorCode == UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS)
                    {
                        // at this point we have received an unmanaged HTTP_REQUEST and memoryBlob
                        // points to it we need to hook up our authentication handling code here.
                        bool stoleBlob = false;
                        try
                        {
                            if (server.ValidateRequest(asyncResult._nativeRequestContext) && server.ValidateAuth(asyncResult._nativeRequestContext))
                            {
                                stoleBlob = true;
                                RequestContext requestContext = new RequestContext(server, asyncResult._nativeRequestContext);
                                asyncResult.Tcs.TrySetResult(requestContext);
                                complete = true;
                            }
                        }
                        finally
                        {
                            if (stoleBlob)
                            {
                                // The request has been handed to the user, which means this code can't reuse the blob.  Reset it here.
                                asyncResult._nativeRequestContext = complete ? null : new NativeRequestContext(asyncResult);
                            }
                            else
                            {
                                asyncResult._nativeRequestContext.Reset(0, 0);
                            }
                        }
                    }
                    else
                    {
                        asyncResult._nativeRequestContext.Reset(asyncResult._nativeRequestContext.RequestBlob->RequestId, numBytes);
                    }

                    // We need to issue a new request, either because auth failed, or because our buffer was too small the first time.
                    if (!complete)
                    {
                        uint statusCode = asyncResult.QueueBeginGetContext();
                        if (statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_SUCCESS &&
                            statusCode != UnsafeNclNativeMethods.ErrorCodes.ERROR_IO_PENDING)
                        {
                            // someother bad error, possible(?) return values are:
                            // ERROR_INVALID_HANDLE, ERROR_INSUFFICIENT_BUFFER, ERROR_OPERATION_ABORTED
                            asyncResult.Tcs.TrySetException(new WebListenerException((int)statusCode));
                            complete = true;
                        }
                    }
                    if (!complete)
                    {
                        return;
                    }
                }

                if (complete)
                {
                    asyncResult.Dispose();
                }
            }
            catch (Exception exception)
            {
                // Logged by caller
                asyncResult.Tcs.TrySetException(exception);
                asyncResult.Dispose();
            }
        }
コード例 #12
0
 internal AsyncAcceptContext(WebListener server)
 {
     _server = server;
     _tcs    = new TaskCompletionSource <RequestContext>();
     _nativeRequestContext = new NativeRequestContext(this);
 }