Example #1
0
        protected override void HandleRequest(HttpListenerContext context)
        {
            // NOTE: This method is run under different threads for different http requests.
            Platform.Log(LogLevel.Debug, "Received image streaming request from {0}:{1}", context.Request.RemoteEndPoint.Address, context.Request.RemoteEndPoint.Port);

            AddContext(context);

            try
            {
                Validate(context);

                WADORequestProcessor processor = new WADORequestProcessor();
                processor.Process(context);
            }
            catch (WADOException e)
            {
                SetResponseError(context, e.GetHttpCode(), e.Message);
            }
            catch (HttpException e)
            {
                SetResponseError(context, e.GetHttpCode(), e.Message);
            }
            catch (ServerTransientError e)
            {
                SetResponseError(context, (int)HttpStatusCode.NoContent, e.Message);
            }
            catch (StudyNotFoundException e)
            {
                SetResponseError(context, (int)HttpStatusCode.BadRequest, e.Message);
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    SetResponseError(context, (int)HttpStatusCode.InternalServerError, e.InnerException.Message);
                }
                else
                {
                    SetResponseError(context, (int)HttpStatusCode.InternalServerError, e.Message);
                }
            }
            finally
            {
                // note: the connection might have been aborted or lost too
                try
                {
                    context.Response.OutputStream.Flush();
                    context.Response.OutputStream.Close();
                }
                catch (Exception ex)
                {
                    Platform.Log(LogLevel.Warn, "Unexpected exception occurred: {0}", ex.Message);
                }
                finally
                {
                    RemoveContext(context);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Event handler for <see cref="HttpServer.HttpRequestReceived"/> events.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        protected void OnHttpRequestReceived(object sender, HttpRequestReceivedEventArg args)
        {
            // NOTE: This method is run under different threads for different http requests.
            HttpListenerContext context = args.Context;

            if (string.IsNullOrEmpty(Thread.CurrentThread.Name))
            {
                Thread.CurrentThread.Name = String.Format("Streaming (client: {0}:{1})", context.Request.RemoteEndPoint.Address, context.Request.RemoteEndPoint.Port);
            }

            AddContext(context);

            try
            {
                Validate(context);

                WADORequestProcessor processor = new WADORequestProcessor();
                processor.Process(context);
            }
            catch (WADOException e)
            {
                SetResponseError(context, e.GetHttpCode(), e.Message);
            }
            catch (HttpException e)
            {
                SetResponseError(context, e.GetHttpCode(), e.Message);
            }
            catch (ServerTransientError e)
            {
                SetResponseError(context, (int)HttpStatusCode.NoContent, e.Message);
            }
            catch (StudyNotFoundException e)
            {
                SetResponseError(context, (int)HttpStatusCode.BadRequest, e.Message);
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    SetResponseError(context, (int)HttpStatusCode.InternalServerError, e.InnerException.Message);
                }
                else
                {
                    SetResponseError(context, (int)HttpStatusCode.InternalServerError, e.Message);
                }
            }
            finally
            {
                // note: the connection might have been aborted or lost too
                try
                {
                    context.Response.OutputStream.Flush();
                    context.Response.OutputStream.Close();
                }
                catch (Exception ex)
                {
                    Platform.Log(LogLevel.Warn, "Unexpected exception occurred: {0}", ex.Message);
                }
                finally
                {
                    RemoveContext(context);
                }
            }
        }