예제 #1
0
        /// <summary>
        /// derived class must implement to handle GET request
        /// </summary>
        public HttpServiceResponse ProcessRequest(HttpServiceContext hostContext)
        {
            HttpListenerRequest request      = hostContext.Context.Request;
            HttpServiceResponse hostResponse = null;

            if (!IsAuthorized(request))
            {
                return(CreateErrorResponse(hostContext, Name, 401, "NotAuthorized"));
            }

            if (string.Equals(request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
            {
                hostResponse = ProcessGetRequest(hostContext);
            }
            else if (string.Equals(request.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase))
            {
                hostResponse = ProcessPostRequest(hostContext);
            }
            else if (string.Equals(request.HttpMethod, "PUT", StringComparison.OrdinalIgnoreCase))
            {
                hostResponse = ProcessPutRequest(hostContext);
            }
            else if (string.Equals(request.HttpMethod, "DELETE", StringComparison.OrdinalIgnoreCase))
            {
                hostResponse = ProcessDeleteRequest(hostContext);
            }
            else
            {
                hostResponse = CreateResponseForBadRequest(hostContext, Name, "MethodNotSupported: " + request.HttpMethod);
            }
            return(hostResponse);
        }
예제 #2
0
        /// <summary>
        /// get context
        /// </summary>
        /// <returns></returns>
        public async Task <HttpServiceResponse> GetContextAsync()
        {
            HttpListenerContext context = await _listener.GetContextAsync();

            HttpListenerRequest  request  = context.Request;
            HttpListenerResponse response = context.Response;

            LogUtil.WriteAction($"{request.HttpMethod} {request.Url.AbsoluteUri}");

            HttpServiceRequestHandler handler;
            string        matchedPath;
            List <string> unmatchedSegments;

            if (!GetRequestHandler(request.Url.AbsolutePath, out handler, out matchedPath, out unmatchedSegments))
            {
                return(HttpServiceRequestHandler.CreateResponseForBadRequest(new HttpServiceContext {
                    Context = context, MatchedPath = matchedPath
                }, $"NotSupported: {request.Url.AbsolutePath}"));
            }

            HttpServiceContext hostContext = new HttpServiceContext {
                Context = context, MatchedPath = matchedPath, UnmatchedSegments = unmatchedSegments
            };
            HttpServiceResponse hostResponse = null;

            try
            {
                hostResponse = handler.ProcessRequest(hostContext);
            }
            catch (Exception err)
            {
                hostResponse = HttpServiceRequestHandler.CreateResponseForInternalError(hostContext, handler.Name, err);
            }

            try
            {
                if (hostResponse != null)
                {
                    if (hostResponse.Content == null)
                    {
                        hostResponse.Content = string.Empty;
                    }
                    // send content to response
                    byte[] buffer = Encoding.UTF8.GetBytes(hostResponse.Content);
                    // Get a response stream and write the response
                    response.ContentEncoding = Encoding.UTF8;
                    response.ContentLength64 = buffer.Length;
                    response.KeepAlive       = KeepAlive;
                    Stream output = response.OutputStream;
                    output.Write(buffer, 0, buffer.Length);
                    // must close the output stream.
                    output.Close();

                    if (string.IsNullOrEmpty(response.ContentType))
                    {
                        response.ContentType = "application/json";
                    }
                }
                else
                {
                    hostResponse = HttpServiceRequestHandler.CreateResponseForInternalError(hostContext, handler.Name);
                }
            }
            catch (Exception err)
            {
                hostResponse = HttpServiceRequestHandler.CreateResponseForInternalError(hostContext, handler.Name, err);
            }
            LogUtil.WriteAction($"{request.HttpMethod} {request.Url.AbsoluteUri} Status: {response.StatusCode} Result: {hostResponse.Content} Error: {hostResponse.ErrorMessage}");
            return(hostResponse);
        }