Пример #1
0
        private async Task HandleRequestAsync(HttpListenerRequest request, HttpListenerResponse response)
        {
            Logger.Debug($"New HttpListenerRequest: {request.Url}");

            if (!_handlers.ContainsKey(request.RawUrl))
            {
                response.StatusCode = (int)HttpStatusCode.NotFound;
            }
            else
            {
                HttpServerResult result = await _handlers[request.RawUrl]().ConfigureAwait(false);

                response.StatusCode      = (int)HttpStatusCode.OK;
                response.ContentEncoding = result.ContentEncoding;
                response.ContentType     = result.ContentType;
                response.ContentLength64 = result.ContentLength;

                if (result.AllowCrossOrigin)
                {
                    response.AddHeader("Access-Control-Allow-Origin", "*");
                }

                if (null != result.Content)
                {
                    await response.OutputStream.WriteAsync(result.Content, 0, result.ContentLength).ConfigureAwait(false);
                }
            }

            response.OutputStream.Close();
        }
Пример #2
0
        /// <summary>
        /// Returns a view result.
        /// </summary>
        /// <param name="path">The view path.</param>
        /// <returns>The result.</returns>
        protected async Task <HttpServerResult> ViewResultAsync(string path)
        {
            HttpServerResult result = new HttpServerResult()
            {
                ContentType = "text/html",
            };

            // TODO: read the view from disk
            await Task.Delay(0).ConfigureAwait(false);

            return(result);
        }
Пример #3
0
        /// <summary>
        /// Returns a JSON result.
        /// </summary>
        /// <param name="obj">The object to serialize.</param>
        /// <returns>The result.</returns>
        protected HttpServerResult JsonResult(object obj)
        {
            HttpServerResult result = new HttpServerResult()
            {
                ContentType = "application/json",
            };

            if (null == obj)
            {
                return(result);
            }

            using (MemoryStream stream = new MemoryStream()) {
                DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());
                json.WriteObject(stream, obj);

                result.Content = stream.ToArray();
            }

            return(result);
        }