private void OnError(object sender, EventSource.ExceptionEventArgs e)
        {
            var ex = e.Exception;

            LogHelpers.LogException(_log, "Encountered EventSource error", ex);
            var recoverable = true;

            if (ex is EventSource.EventSourceServiceUnsuccessfulResponseException respEx)
            {
                int status = respEx.StatusCode;
                _log.Error(HttpErrors.ErrorMessage(status, "streaming connection", "will retry"));
                RecordStreamInit(true);
                if (!HttpErrors.IsRecoverable(status))
                {
                    recoverable = false;
                    _initTask.TrySetException(ex); // sends this exception to the client if we haven't already started up
                    ((IDisposable)this).Dispose();
                }
            }

            var errorInfo = ex is EventSource.EventSourceServiceUnsuccessfulResponseException re ?
                            DataSourceStatus.ErrorInfo.FromHttpError(re.StatusCode) :
                            DataSourceStatus.ErrorInfo.FromException(ex);

            _dataSourceUpdates.UpdateStatus(recoverable ? DataSourceState.Interrupted : DataSourceState.Off,
                                            errorInfo);
        }
Exemple #2
0
        public static HttpResponse WriteFile(HttpRequest request)
        {
            //Gets The Folder Of Executing Program
            if (programPath == "")
            {
                programPath = Environment.CurrentDirectory;
            }

            //Prepares Path For Usage By Removing First Symbol
            string trimmedPath = request.path.LocalPath;

            //Gets The Folder Of Executing Program
            string filePath = Path.Combine(programPath, trimmedPath);

            if (!File.Exists(filePath))
            {
                return(HttpErrors.GenerateHttpError(404));
            }

            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write))
            {
                StreamWriter writer = new StreamWriter(fs);

                writer.Write(request.data);

                fs.Close();
            }

            HttpResponse response = new HttpResponse("test", 200, "text/html");

            return(response);
        }
Exemple #3
0
        private async Task UpdateTaskAsync()
        {
            try
            {
                var response = await _featureFlagRequestor.FeatureFlagsAsync();

                if (response.statusCode == 200)
                {
                    var flagsAsJsonString = response.jsonResponse;
                    var allData           = DataModelSerialization.DeserializeV1Schema(flagsAsJsonString);
                    _updateSink.Init(_user, allData);

                    if (_initialized.GetAndSet(true) == false)
                    {
                        _startTask.SetResult(true);
                        _log.Info("Initialized LaunchDarkly Polling Processor.");
                    }
                }
            }
            catch (UnsuccessfulResponseException ex)
            {
                var errorInfo = DataSourceStatus.ErrorInfo.FromHttpError(ex.StatusCode);

                if (HttpErrors.IsRecoverable(ex.StatusCode))
                {
                    _log.Warn(HttpErrors.ErrorMessage(ex.StatusCode, "polling request", "will retry"));
                    _updateSink.UpdateStatus(DataSourceState.Interrupted, errorInfo);
                }
                else
                {
                    _log.Error(HttpErrors.ErrorMessage(ex.StatusCode, "polling request", ""));
                    _updateSink.UpdateStatus(DataSourceState.Shutdown, errorInfo);

                    // if client is initializing, make it stop waiting
                    _startTask.TrySetResult(false);

                    ((IDisposable)this).Dispose();
                }
            }
            catch (JsonReadException ex)
            {
                _log.Error("Polling request received malformed data: {0}", LogValues.ExceptionSummary(ex));
                _updateSink.UpdateStatus(DataSourceState.Interrupted,
                                         new DataSourceStatus.ErrorInfo
                {
                    Kind = DataSourceStatus.ErrorKind.InvalidData,
                    Time = DateTime.Now
                });
            }
            catch (Exception ex)
            {
                Exception realEx = (ex is AggregateException ae) ? ae.Flatten() : ex;
                _log.Warn("Polling for feature flag updates failed: {0}", LogValues.ExceptionSummary(realEx));
                _log.Debug(LogValues.ExceptionTrace(realEx));
                _updateSink.UpdateStatus(DataSourceState.Interrupted,
                                         DataSourceStatus.ErrorInfo.FromException(realEx));
            }
        }
Exemple #4
0
        public async Task noIndexHeader()
        {
            var listener  = Structure.Start(new SimpleProperties(), TestOptions(next => env => env.Response(200)));
            var exception = await HttpErrors.Dequeue();

            exception.ShouldBeOfType <InvalidOperationException>();

            await listener.Stop();
        }
Exemple #5
0
 public static void LoadAssemblies()
 {
     // Force loading of the XSP assembly
     new SecurityConfiguration();
     // FastCgi
     new NameValuePair();
     // and the WebServer one
     HttpErrors.BadRequest();
 }
Exemple #6
0
        void RunInternal(object state)
        {
            RequestData rdata = initial.RequestData;

            initial.FreeBuffer();
            string      vhost = null;        // TODO: read the headers in InitialWorkerRequest
            int         port  = localEP.Port;
            VPathToHost vapp;

            try {
                vapp = server.GetApplicationForPath(vhost, port, rdata.Path, true);
            } catch (Exception e) {
                //
                // This happens if the assembly is not in the GAC, so we report this
                // error here.
                //
                Logger.Write(e);
                return;
            }

            XSPApplicationHost host = null;

            if (vapp != null)
            {
                host = (XSPApplicationHost)vapp.AppHost;
            }

            if (host == null)
            {
                byte [] nf = HttpErrors.NotFound(rdata.Path);
                Write(nf, 0, nf.Length);
                Close();
                return;
            }

            broker    = (XSPRequestBroker)vapp.RequestBroker;
            requestId = broker.RegisterRequest(this);

            try {
                string redirect;
                vapp.Redirect(rdata.Path, out redirect);
                host.ProcessRequest(requestId, localEP,
                                    remoteEP, rdata.Verb,
                                    rdata.Path, rdata.QueryString,
                                    rdata.Protocol, rdata.InputBuffer, redirect, sock.Handle, ssl);
            } catch (FileNotFoundException fnf) {
                // We print this one, as it might be a sign of a bad deployment
                // once we require the .exe and Mono.WebServer in bin or the GAC.
                Logger.Write(fnf);
            } catch (IOException) {
                // This is ok (including EndOfStreamException)
            } catch (Exception e) {
                Logger.Write(e);
            }
        }
Exemple #7
0
        public async Task throws()
        {
            var listener = Structure.Start(
                new SimpleProperties(),
                TestOptions(next => env => { throw new OperationCanceledException(); }));

            var exception = await HttpErrors.Dequeue();

            exception.ShouldBeOfType <OperationCanceledException>();

            await listener.Stop();
        }
        public ErrorConfiguration(string virtualPath)
        {
            _configuration = OpenWebConfiguration("~");

            var customErrorsSection = GetCustomErrorsSection();
            CustomErrors = new CustomErrors(customErrorsSection);

            var httpRuntimeSection = GetHttpRuntimeSection();
            HttpRuntime = new HttpRuntimeSettings(httpRuntimeSection);

            var systemWebServerSection = GetSystemWebServerSection();
            HttpErrors = new HttpErrors(systemWebServerSection, virtualPath);

            var appSettings = GetAppSettingsSection();
            AppSettingsWriter = new AppSettingsWriter(appSettings);
        }
Exemple #9
0
        public async Task nanIndexHeader()
        {
            var listener = Structure.Start(
                new SimpleProperties(),
                TestOptions(
                    next => env =>
            {
                env.Response.Headers["X-Consul-Index"] = "nope";
                return(env.Response(200));
            }));

            ConsulKvSimulator.PutKey("kv", "nope");
            var exception = await HttpErrors.Dequeue();

            exception.ShouldBeOfType <InvalidOperationException>();

            await listener.Stop();
        }
        private ActionResult ProcessException(Exception ex)
        {
            HttpErrorMessage httpError;
            HttpErrors       httpErrors = new HttpErrors();

            if (ex is HttpException)
            {
                HttpException exCasted = (HttpException)ex;
                httpError = HttpErrorMessage.BuildFromDetail(exCasted.Message);
                httpErrors.Errors.Add(httpError);
                return(this.StatusCode(exCasted.ErrorCode, httpErrors));
            }

            httpError = HttpErrorMessage.BuildFromDetail("Internal error, contact your administrator");
            httpErrors.Errors.Add(httpError);
            Log.Error(string.Format("{0}: Internal error, contact your administrator, Unhandled exception {1}", httpError.Id, ex));
            return(this.StatusCode(StatusCodes.Status500InternalServerError, httpErrors));
        }
Exemple #11
0
        private void OnError(object sender, EventSource.ExceptionEventArgs e)
        {
            var ex          = e.Exception;
            var recoverable = true;

            DataSourceStatus.ErrorInfo errorInfo;

            RecordStreamInit(true);

            if (ex is EventSourceServiceUnsuccessfulResponseException respEx)
            {
                int status = respEx.StatusCode;
                errorInfo = DataSourceStatus.ErrorInfo.FromHttpError(status);
                if (!HttpErrors.IsRecoverable(status))
                {
                    recoverable = false;
                    _log.Error(HttpErrors.ErrorMessage(status, "streaming connection", ""));
                }
                else
                {
                    _log.Warn(HttpErrors.ErrorMessage(status, "streaming connection", "will retry"));
                }
            }
            else
            {
                errorInfo = DataSourceStatus.ErrorInfo.FromException(ex);
                _log.Warn("Encountered EventSource error: {0}", LogValues.ExceptionSummary(ex));
                _log.Debug(LogValues.ExceptionTrace(ex));
            }

            _updateSink.UpdateStatus(recoverable ? DataSourceState.Interrupted : DataSourceState.Shutdown,
                                     errorInfo);

            if (!recoverable)
            {
                // Make _initTask complete to tell the client to stop waiting for initialization. We use
                // TrySetResult rather than SetResult here because it might have already been completed
                // (if for instance the stream started successfully, then restarted and got a 401).
                _initTask.TrySetResult(false);
                ((IDisposable)this).Dispose();
            }
        }
Exemple #12
0
        public async Task tooManyIndexHeader()
        {
            var listener = Structure.Start(
                new SimpleProperties(),
                TestOptions(
                    next => env =>
            {
                env.Response.Headers.Add("X-Consul-Index", new[] { "1", "2" });

                return(env.Response(200));
            }));

            ConsulKvSimulator.PutKey("kv", "nope");
            var exception = await HttpErrors.Dequeue();

            Trace.WriteLine(exception.Message);
            exception.ShouldBeOfType <InvalidOperationException>();

            await listener.Stop();
        }
Exemple #13
0
        public static HttpResponse ReadFile(HttpRequest request)
        {
            if (programPath == "")
            {
                programPath = Environment.CurrentDirectory;
                programPath = Path.Combine(programPath + "/data/Web");
            }

            //Gets Local Request File
            string path = request.path.LocalPath;

            //Removes The First Symbol for Better Path Combining
            string trimmedPath = path.Remove(0, 1);

            //Combines Program And Request Paths Into One
            string filePath = Path.Combine(programPath, trimmedPath);

            //If File Is Not Found Returns 404
            if (!File.Exists(filePath))
            {
                return(HttpErrors.GenerateHttpError(404));
            }

            //If File Access Is Restricted And Not Authorized Returns 401
            if (AccessManager.IsFileRestricted(filePath, out JSON.FileAccessess authorizationFile))
            {
                if (!IsAuthorized(request, filePath, authorizationFile))
                {
                    return(HttpErrors.GenerateHttpError(401));
                }
            }

            //Creates New Http Response
            HttpResponse response = new HttpResponse("test", 200, "text/html");

            //Adds File As Http Response Body
            response.AddFile(filePath);

            return(response);
        }
Exemple #14
0
        void HandleInitialException(Exception e)
        {
            //bool ignore = ((e is RequestLineException) || (e is IOException));
            //if (!ignore)
            //	Console.WriteLine (e);

            try {
                if (initial != null && initial.GotSomeInput && sock.Connected)
                {
                    byte [] error = HttpErrors.ServerError();
                    Write(error, 0, error.Length);
                }
            } catch {}

            try {
                Close();
            } catch {}

            if (broker != null && requestId != -1)
            {
                broker.UnregisterRequest(requestId);
            }
        }
Exemple #15
0
        private async Task UpdateTaskAsync()
        {
            _log.Info("Polling LaunchDarkly for feature flag updates");
            try
            {
                var allData = await _featureRequestor.GetAllDataAsync();

                if (allData is null)
                {
                    // This means it was cached, and alreadyInited was true
                    _dataSourceUpdates.UpdateStatus(DataSourceState.Valid, null);
                }
                else
                {
                    if (_dataSourceUpdates.Init(allData.Value))
                    {
                        _dataSourceUpdates.UpdateStatus(DataSourceState.Valid, null);

                        if (!_initialized.GetAndSet(true))
                        {
                            _initTask.SetResult(true);
                            _log.Info("First polling request successful");
                        }
                    }
                }
            }
            catch (UnsuccessfulResponseException ex)
            {
                _log.Error(HttpErrors.ErrorMessage(ex.StatusCode, "polling request", "will retry"));
                var errorInfo = DataSourceStatus.ErrorInfo.FromHttpError(ex.StatusCode);
                if (HttpErrors.IsRecoverable(ex.StatusCode))
                {
                    _dataSourceUpdates.UpdateStatus(DataSourceState.Interrupted, errorInfo);
                }
                else
                {
                    _dataSourceUpdates.UpdateStatus(DataSourceState.Off, errorInfo);
                    try
                    {
                        // if client is initializing, make it stop waiting
                        _initTask.SetResult(true);
                    }
                    catch (InvalidOperationException)
                    {
                        // the task was already set - nothing more to do
                    }
                    ((IDisposable)this).Dispose();
                }
            }
            catch (JsonReadException ex)
            {
                _log.Error("Polling request received malformed data: {0}", LogValues.ExceptionSummary(ex));
                _dataSourceUpdates.UpdateStatus(DataSourceState.Interrupted,
                                                new DataSourceStatus.ErrorInfo
                {
                    Kind = DataSourceStatus.ErrorKind.InvalidData,
                    Time = DateTime.Now
                });
            }
            catch (Exception ex)
            {
                Exception realEx = (ex is AggregateException ae) ? ae.Flatten() : ex;
                LogHelpers.LogException(_log, "Polling for feature flag updates failed", realEx);

                _dataSourceUpdates.UpdateStatus(DataSourceState.Interrupted,
                                                DataSourceStatus.ErrorInfo.FromException(realEx));
            }
        }
        /// <summary>
        /// Parses a UrlReservation element.
        /// </summary>
        /// <param name="node">The element to parse.</param>
        /// <param name="componentId">Identifier of the component that owns this URL reservation.</param>
        /// <param name="securityPrincipal">The security principal of the parent element (null if nested under Component).</param>
        private void ParseUrlReservationElement(XElement node, string componentId, string securityPrincipal)
        {
            SourceLineNumber sourceLineNumbers = Preprocessor.GetSourceLineNumbers(node);
            Identifier       id        = null;
            int    handleExisting      = HttpConstants.heReplace;
            string handleExistingValue = null;
            string sddl     = null;
            string url      = null;
            bool   foundACE = false;

            foreach (XAttribute attrib in node.Attributes())
            {
                if (String.IsNullOrEmpty(attrib.Name.NamespaceName) || this.Namespace == attrib.Name.Namespace)
                {
                    switch (attrib.Name.LocalName)
                    {
                    case "Id":
                        id = this.Core.GetAttributeIdentifier(sourceLineNumbers, attrib);
                        break;

                    case "HandleExisting":
                        handleExistingValue = this.Core.GetAttributeValue(sourceLineNumbers, attrib);
                        switch (handleExistingValue)
                        {
                        case "replace":
                            handleExisting = HttpConstants.heReplace;
                            break;

                        case "ignore":
                            handleExisting = HttpConstants.heIgnore;
                            break;

                        case "fail":
                            handleExisting = HttpConstants.heFail;
                            break;

                        default:
                            this.Core.OnMessage(WixErrors.IllegalAttributeValue(sourceLineNumbers, node.Name.LocalName, "HandleExisting", handleExistingValue, "replace", "ignore", "fail"));
                            break;
                        }
                        break;

                    case "Sddl":
                        sddl = this.Core.GetAttributeValue(sourceLineNumbers, attrib);
                        break;

                    case "Url":
                        url = this.Core.GetAttributeValue(sourceLineNumbers, attrib);
                        break;

                    default:
                        this.Core.UnexpectedAttribute(node, attrib);
                        break;
                    }
                }
                else
                {
                    this.Core.ParseExtensionAttribute(node, attrib);
                }
            }

            // Need the element ID for child element processing, so generate now if not authored.
            if (null == id)
            {
                id = this.Core.CreateIdentifier("url", componentId, securityPrincipal, url);
            }

            // Parse UrlAce children.
            foreach (XElement child in node.Elements())
            {
                if (this.Namespace == child.Name.Namespace)
                {
                    switch (child.Name.LocalName)
                    {
                    case "UrlAce":
                        if (null != sddl)
                        {
                            this.Core.OnMessage(WixErrors.IllegalParentAttributeWhenNested(sourceLineNumbers, "UrlReservation", "Sddl", "UrlAce"));
                        }
                        else
                        {
                            foundACE = true;
                            this.ParseUrlAceElement(child, id.Id, securityPrincipal);
                        }
                        break;

                    default:
                        this.Core.UnexpectedElement(node, child);
                        break;
                    }
                }
                else
                {
                    this.Core.ParseExtensionElement(node, child);
                }
            }

            // Url is required.
            if (null == url)
            {
                this.Core.OnMessage(WixErrors.ExpectedAttribute(sourceLineNumbers, node.Name.LocalName, "Url"));
            }

            // Security is required.
            if (null == sddl && !foundACE)
            {
                this.Core.OnMessage(HttpErrors.NoSecuritySpecified(sourceLineNumbers));
            }

            if (!this.Core.EncounteredError)
            {
                Row row = this.Core.CreateRow(sourceLineNumbers, "WixHttpUrlReservation");
                row[0] = id.Id;
                row[1] = handleExisting;
                row[2] = sddl;
                row[3] = url;
                row[4] = componentId;

                if (this.Core.CurrentPlatform == Platform.ARM)
                {
                    // Ensure ARM version of the CA is referenced.
                    this.Core.CreateSimpleReference(sourceLineNumbers, "CustomAction", "WixSchedHttpUrlReservationsInstall_ARM");
                    this.Core.CreateSimpleReference(sourceLineNumbers, "CustomAction", "WixSchedHttpUrlReservationsUninstall_ARM");
                }
                else
                {
                    // All other supported platforms use x86.
                    this.Core.CreateSimpleReference(sourceLineNumbers, "CustomAction", "WixSchedHttpUrlReservationsInstall");
                    this.Core.CreateSimpleReference(sourceLineNumbers, "CustomAction", "WixSchedHttpUrlReservationsUninstall");
                }
            }
        }
        public async Task <EventSenderResult> SendEventDataAsync(EventDataKind kind, string data, int eventCount)
        {
            Uri    uri;
            string description;
            string payloadId;

            if (kind == EventDataKind.DiagnosticEvent)
            {
                uri         = _diagnosticUri;
                description = "diagnostic event";
                payloadId   = null;
            }
            else
            {
                uri         = _eventsUri;
                description = string.Format("{0} event(s)", eventCount);
                payloadId   = Guid.NewGuid().ToString();
            }

            _logger.Debug("Submitting {0} to {1} with json: {2}", description, uri.AbsoluteUri, data);

            for (var attempt = 0; attempt < MaxAttempts; attempt++)
            {
                if (attempt > 0)
                {
                    await Task.Delay(_retryInterval);
                }

                using (var cts = new CancellationTokenSource(_timeout))
                {
                    string errorMessage = null;
                    bool   canRetry     = false;
                    bool   mustShutDown = false;

                    try
                    {
                        using (var request = PrepareRequest(uri, payloadId))
                            using (var stringContent = new StringContent(data, Encoding.UTF8, "application/json"))
                            {
                                request.Content = stringContent;
                                Stopwatch timer = new Stopwatch();
                                using (var response = await _httpClient.SendAsync(request, cts.Token))
                                {
                                    timer.Stop();
                                    _logger.Debug("Event delivery took {0} ms, response status {1}",
                                                  timer.ElapsedMilliseconds, (int)response.StatusCode);
                                    if (response.IsSuccessStatusCode)
                                    {
                                        DateTimeOffset?respDate = response.Headers.Date;
                                        return(new EventSenderResult(DeliveryStatus.Succeeded,
                                                                     respDate.HasValue ? (DateTime?)respDate.Value.DateTime : null));
                                    }
                                    else
                                    {
                                        errorMessage = HttpErrors.ErrorMessageBase((int)response.StatusCode);
                                        canRetry     = HttpErrors.IsRecoverable((int)response.StatusCode);
                                        mustShutDown = !canRetry;
                                    }
                                }
                            }
                    }
                    catch (TaskCanceledException e)
                    {
                        if (e.CancellationToken == cts.Token)
                        {
                            // Indicates the task was cancelled deliberately somehow; in this case don't retry
                            _logger.Warn("Event sending task was cancelled");
                            return(new EventSenderResult(DeliveryStatus.Failed, null));
                        }
                        else
                        {
                            // Otherwise this was a request timeout.
                            errorMessage = "Timed out";
                            canRetry     = true;
                        }
                    }
                    catch (Exception e)
                    {
                        errorMessage = string.Format("Error ({0})", LogValues.ExceptionSummary(e));
                        canRetry     = true;
                    }
                    string nextStepDesc = canRetry ?
                                          (attempt == MaxAttempts - 1 ? "will not retry" : "will retry after one second") :
                                          "giving up permanently";
                    _logger.Warn(errorMessage + " sending {0}; {1}", description, nextStepDesc);
                    if (mustShutDown)
                    {
                        return(new EventSenderResult(DeliveryStatus.FailedAndMustShutDown, null));
                    }
                    if (!canRetry)
                    {
                        break;
                    }
                }
            }
            return(new EventSenderResult(DeliveryStatus.Failed, null));
        }
        private bool AddHeader(string name, string value, bool isHeader, object userData)
        {
            TransportRequest req = userData as TransportRequest;

            //if we did not find a route yet
            if (!IsHostFound(req))
            {
                req.tempKeys.Add(new KeyValuePair()
                {
                    Key      = name,
                    Value    = value,
                    IsHeader = isHeader
                });

                if (req.VHost == null && name == "SERVER_NAME")
                {
                    req.VHost = value;
                }
                if (req.VPort == -1 && name == "SERVER_PORT")
                {
                    int.TryParse(value, out req.VPort);
                }
                if (req.VPath == null && name == "SCRIPT_NAME")
                {
                    req.VPath = value;
                }

                if (req.VHost != null && req.VPort != -1 && req.VPath != null)
                {
                    GetRoute(req, req.VHost, req.VPort, req.VPath);

                    if (IsHostFound(req))
                    {
                        CreateRequest(req);

                        foreach (KeyValuePair pair in req.tempKeys)
                        {
                            if (pair.IsHeader)
                            {
                                AddHeader(req, pair.Key, pair.Value);
                            }
                            else
                            {
                                AddServerVariable(req, pair.Key, pair.Value);
                            }
                        }
                    }
                    else
                    {
                        Logger.Write(LogLevel.Error, "Can't find app {0}:{1} {2}", req.VHost, req.VPort, req.VPath);
                        //TODO: Send EndRequest with error message
                        //SendError (request.Hash, req.RequestNumber, Strings.Connection_AbortRecordReceived);
                        byte[] notFound = HttpErrors.NotFound(req.VPath);
                        SendOutput(req.Hash, req.RequestNumber, notFound, notFound.Length);
                        EndRequest(req.Hash, req.RequestNumber, 0);
                        return(false);
                    }
                }
            }
            else
            {
                if (isHeader)
                {
                    AddHeader(req, name, value);
                }
                else
                {
                    AddServerVariable(req, name, value);
                }
            }

            return(true);
        }