Exemplo n.º 1
0
        private void DoWork()
        {
            try
            {
                SimplifiedClientRequest request = null;

                do
                {
                    string requestString = ReadRequest();

                    if (!String.IsNullOrEmpty(requestString))
                    {
                        request = SimplifiedClientRequest.ParseRequest(requestString);

                        SimplifiedServerResponse response = GenerateResponse(request);
                        WriteResponse(response);

                        Logger.WriteMessage(String.Format("{0} {1} - Status: {2}", request.RequestType, request.Url, response.ResponseStatus), LogType.SUCCESS);
                    }
                    else
                    {
                        request = null;
                    }
                }while (request != null && request.Connection == ConnectionType.KEEP_ALIVE);
            }
            catch (Exception ex)
            {
                Logger.WriteException(ex);
            }
            finally
            {
                client.Close();
            }
        }
Exemplo n.º 2
0
        private SimplifiedServerResponse GenerateResponse(SimplifiedClientRequest request)
        {
            switch (request.RequestType)
            {
            case ClientRequestType.GET:
                return(GenerateGetResponse(request));

            case ClientRequestType.POST:
                return(GeneratePostResponse(request));

            case ClientRequestType.PUT:
                return(GeneratePutResponse(request));

            case ClientRequestType.HEAD:
                return(GenerateHeadResponse(request));

            case ClientRequestType.DELETE:
                return(GenerateDeleteResponse(request));

            case ClientRequestType.CONNECT:
            case ClientRequestType.OPTIONS:
            case ClientRequestType.PATCH:
            case ClientRequestType.TRACE:
                return(GenerateNotImplementedResponse(request));

            default:
                throw new Exception("Unknown request type");
            }
        }
Exemplo n.º 3
0
        private SimplifiedServerResponse GenerateNotImplementedResponse(SimplifiedClientRequest request)
        {
            SimplifiedServerResponse response = GenerateHeadResponse(request);

            response.ResponseStatus = 501;

            return(response);
        }
Exemplo n.º 4
0
        private SimplifiedServerResponse GenerateGetResponse(SimplifiedClientRequest request)
        {
            SimplifiedServerResponse response = GenerateHeadResponse(request);

            if (ServerFileHelper.FileExists(request.Url))
            {
                response.Body = ServerFileHelper.GetFileContent(request.Url);
            }

            return(response);
        }
Exemplo n.º 5
0
        private SimplifiedServerResponse GenerateDeleteResponse(SimplifiedClientRequest request)
        {
            SimplifiedServerResponse response = GenerateHeadResponse(request);

            if (ServerFileHelper.FileExists(request.Url))
            {
                response.ResponseStatus = 202;
                ServerFileHelper.DeleteFile(request.Url);
            }

            return(response);
        }
Exemplo n.º 6
0
        private SimplifiedServerResponse GenerateHeadResponse(SimplifiedClientRequest request)
        {
            SimplifiedServerResponse response = new SimplifiedServerResponse()
            {
                HttpVersion = request.HttpVersion,
                Connection  = request.Connection,
                Cookies     = request.Cookies
            };

            if (ServerFileHelper.FileExists(request.Url))
            {
                response.ResponseStatus = 200;
                response.ContentType    = ServerFileHelper.GetContentType(request.Url);
                response.LastModified   = ServerFileHelper.GetModifyDate(request.Url);
            }
            else
            {
                response.ResponseStatus = 404;
            }

            return(response);
        }
Exemplo n.º 7
0
        private SimplifiedServerResponse GeneratePutResponse(SimplifiedClientRequest request)
        {
            SimplifiedServerResponse response = new SimplifiedServerResponse()
            {
                HttpVersion     = request.HttpVersion,
                ContentLocation = request.Url,
                Connection      = request.Connection,
                Cookies         = request.Cookies
            };

            if (ServerFileHelper.FileExists(request.Url))
            {
                response.ResponseStatus = 204;
            }
            else
            {
                response.ResponseStatus = 201;
                ServerFileHelper.WriteFileContent(request.Url, request.MessageBody);
            }

            return(response);
        }