예제 #1
0
 public string GetResponse(HttpRequestParser request)
 {
     _requestInfo = request;
     return(HandlerRequest());
 }
예제 #2
0
        private void Process(HttpListenerContext context)
        {
            string            resPath  = context.Request.Url.AbsolutePath;
            var               filename = LocalFilePath(resPath);
            HttpRequestParser request  = new HttpRequestParser(context.Request);

            if (File.Exists(filename))
            {
                try
                {
                    Stream input = new FileStream(filename, FileMode.Open);
                    //Adding permanent http response headers
                    string mime;
                    context.Response.ContentType = _mimeTypeMappings.TryGetValue(Path.GetExtension(filename), out mime)
                        ? mime
                        : "application/octet-stream";
                    context.Response.ContentLength64 = input.Length;
                    context.Response.AddHeader("Date", DateTime.Now.ToString("r"));
                    context.Response.AddHeader("Last-Modified", File.GetLastWriteTime(filename).ToString("r"));
                    byte[] buffer = new byte[1024 * 16];
                    int    nbytes;
                    while ((nbytes = input.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        context.Response.OutputStream.Write(buffer, 0, nbytes);
                    }
                    input.Close();
                    context.Response.StatusCode = (int)HttpStatusCode.OK;
                    context.Response.OutputStream.Flush();
                }
                catch (Exception ex)
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    _logger.ErrorFormat("error while servring page {0}", ex);
                }
            }
            else if (_handlers.ContainsKey(resPath))
            {
                try
                {
                    //get query
                    var    response = _handlers[resPath].GetResponse(request);
                    byte[] buffer   = Encoding.UTF8.GetBytes(response);
                    //Adding permanent http response headers
                    context.Response.ContentType     = "text/html; charset=utf-8";
                    context.Response.ContentLength64 = buffer.Length;
                    context.Response.AddHeader("Date", DateTime.Now.ToString("r"));
                    context.Response.StatusCode = (int)HttpStatusCode.OK;
                    context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                    context.Response.OutputStream.Flush();
                }
                catch (Exception ex)
                {
                    _logger.ErrorFormat("http error {0}", ex);
                }
            }
            else
            {
                context.Response.StatusCode = (int)HttpStatusCode.NotFound;
            }

            context.Response.OutputStream.Close();
        }