Example #1
0
 private HttpServiceEndpoint CreateHttpServiceEndpoint(HttpServiceRequestHandler handler, string rootUri)
 {
     return(new HttpServiceEndpoint
     {
         Name = handler.Name,
         Type = handler.Type,
         Path = handler.Path,
         Url = rootUri + Path,
     });
 }
Example #2
0
        private bool GetRequestHandler(string absolutePath, out HttpServiceRequestHandler handler, out string matchedPath, out List <string> unmatchedSegments)
        {
            handler           = null;
            matchedPath       = null;
            unmatchedSegments = null;
            string path = absolutePath;

            // first: find handler that matches the whole path
            if (HttpServiceRequestHandler.Handlers.TryGetValue(absolutePath, out handler))
            {
                matchedPath = absolutePath;
            }
            else
            {
                // then remove the last segment of the path and find a match until reaching "/"
                while (true)
                {
                    int index = path.LastIndexOf('/');
                    if (index == 0)
                    {
                        if (HttpServiceRequestHandler.Handlers.TryGetValue("/", out handler))
                        {
                            matchedPath = "/";
                        }
                        break;
                    }
                    else
                    {
                        string unmatched = path.Substring(index + 1);
                        if (!string.IsNullOrEmpty(unmatched))
                        {
                            if (unmatchedSegments == null)
                            {
                                unmatchedSegments = new List <string>();
                            }
                            unmatchedSegments.Insert(0, unmatched);
                        }
                        path = path.Substring(0, index);
                        if (HttpServiceRequestHandler.Handlers.TryGetValue(path, out handler))
                        {
                            matchedPath = path;
                            break;
                        }
                    }
                }
            }
            return(handler != null && !string.IsNullOrEmpty(matchedPath));
        }
Example #3
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);
        }