Exemple #1
0
            public Request(BasicServer.Request Raw, StandardRequestHeaders Headers)
            {
                this.RawRequest = Raw;
                this.RawHeaders = Raw.Metadata.Headers;
                this.Method     = Raw.Method;

                Query        = new Query(Raw.Metadata.Query);
                this.Headers = Headers;
            }
Exemple #2
0
 protected static Request Refine(BasicServer.Request Raw)
 {
     if (Raw is BasicServer.Request_With_Body)
     {
         return(new Request_With_Body(Raw as BasicServer.Request_With_Body, ParseRequestHeaders(Raw.Metadata.Headers)));
     }
     else
     {
         return(new Request(Raw, ParseRequestHeaders(Raw.Metadata.Headers)));
     }
 }
Exemple #3
0
        protected override void WriteResponse(Socket ClientSocket, BasicServer.Request Request, Response Response)
        {
            if (Response.Headers == null)
            {
                throw new ArgumentException("headers missing");
            }

            if (Response.Body != null)
            {
                if (Request == null || Response_Has_Body(Request.Method))
                {
                    if (!Response.Headers.ContainsKey("Content-Length"))
                    {
                        Response.Headers["Content-Length"] = Response.Body.Length.ToString();
                    }
                }
            }

            base.WriteResponse(ClientSocket, Request, Response);
        }
Exemple #4
0
        public sealed override Response Handle(BasicServer.Request Request)
        {
            Request request;

            try
            {
                request = Refine(Request);
            }
            catch (Exception e)
            {
                ErrorLog(e);
                return(RenderServerError(StatusCode.BAD_REQUEST));
            }

            Response response;

            try
            {
                switch (request.Method)
                {
                case Method.GET:
                    response = Handle_GET(request);
                    break;

                case Method.HEAD:
                    response = Handle_HEAD(request);
                    break;

                case Method.POST:
                    response = Handle_POST(request as Request_With_Body);
                    break;

                case Method.PUT:
                    response = Handle_PUT(request as Request_With_Body);
                    break;

                case Method.DELETE:
                    response = Handle_DELETE(request);
                    break;

                case Method.TRACE:
                    response = Handle_TRACE(request);
                    break;

                case Method.OPTIONS:
                    response = Handle_OPTIONS(request);
                    break;

                case Method.CONNECT:
                    response = Handle_CONNECT(request);
                    break;

                case Method.PATCH:
                    response = Handle_PATCH(request as Request_With_Body);
                    break;

                default:
                    response = RenderServerError(StatusCode.BAD_REQUEST);
                    break;
                }
            } catch (Exception e)
            {
                ErrorLog(e);
                response = RenderServerError(StatusCode.INTERNAL_SERVER_ERROR);
            }

            if (response.Body != null && !response.Headers.ContainsKey("Content-Encoding"))
            {
                if (request.Headers.Accept_Encoding.HasValue)
                {
                    foreach (var encoding in request.Headers.Accept_Encoding.Value.OrderBy(encoding => - encoding.Quality).Select(qval => qval.Value))
                    {
                        switch (encoding)
                        {
                        case "gzip":
                            using (var compressed = new MemoryStream())
                            {
                                using (var gzip = new GZipStream(compressed, CompressionMode.Compress))
                                {
                                    gzip.Write(response.Body, 0, response.Body.Length);
                                }

                                response.Body = compressed.ToArray();
                                response.Headers["Content-Length"]   = response.Body.Length.ToString();
                                response.Headers["Content-Encoding"] = "gzip";
                            }
                            break;

                        case "deflate":
                            using (var compressed = new MemoryStream())
                            {
                                using (var deflate = new DeflateStream(compressed, CompressionMode.Compress))
                                {
                                    deflate.Write(response.Body, 0, response.Body.Length);
                                }

                                response.Body = compressed.ToArray();
                                response.Headers["Content-Length"]   = response.Body.Length.ToString();
                                response.Headers["Content-Encoding"] = "deflate";
                            }
                            break;

                        case "identity":
                            response.Headers["Content-Length"]   = response.Body.Length.ToString();
                            response.Headers["Content-Encoding"] = "identity";
                            break;
                        }

                        if (response.Headers.ContainsKey("Content-Encoding"))
                        {
                            break;
                        }
                    }
                }
            }

            return(response);
        }