Пример #1
0
        public HttpResponse Process(HttpRequest request)
        {
            var code = request.ErrorCode;
            var extracode = request.ExtraErrorCode;
            byte[] result;
            var file = $"{ErrorPagePath}{code}.{extracode}.htm";
            if (file.IsExistsFile())
            {
                result = File.ReadAllBytes(file);
            }
            else
            {
                file = $"{ErrorPagePath}{code}.htm";
                if (file.IsExistsFile())
                {
                    result = File.ReadAllBytes(file);
                }
                else
                {
                    result = BuildPage(code, extracode).ConvertToBytes(Encoding.ASCII);
                }
            }
            var response = new HttpResponse(request);
            response.WriteError(result, code);

            return response;
        }
Пример #2
0
        public static HttpResponse StaticProcess(HttpRequest request)
        {
            var code = request.ErrorCode;
            byte[] result= BuildPage(code).ConvertToBytes(Encoding.ASCII);

            var response = new HttpResponse(request);
            response.WriteError(result, code);

            return response;
        }
Пример #3
0
 public override void Process()
 {
     response = new HttpResponse(request);
     response.Write("Activepagetest");
 }
Пример #4
0
        public byte[] GetResponseBytes(Uri RequestUri, eHttpMethod method, byte[] postbytes)
        {
            if (uri == null || !uri.Equals(RequestUri))
            {
                this.uri = RequestUri;
            }
            if (socket == null)
            {
                socket = new TcpSocket();
            }
            if (method != eHttpMethod.GET && method != eHttpMethod.POST)
            {
                throw new Exception("Unsupport Method");
            }
            if (!uri.IsAbsoluteUri)
            {
                throw new Exception("Absolute Uri is required");
            }
            if (uri.Scheme != "http")
            {
                throw new Exception("Unsupport protocol");
            }
            var request = new HttpRequest();
            request.Method = method;
            request.uri = this.uri.PathAndQuery;
            request.headers[eHttpRequestHeader.Host] = this.uri.Host;
            request.headers[eHttpRequestHeader.Connection] = "keep-alive";
            request.Cookies = this.Cookies;
            //request.headers[eHttpRequestHeader.AcceptEncoding] = "gzip;q=1.0";
            if (request.Method == eHttpMethod.POST)
            {
                if (postbytes == null)
                {
                    throw new ArgumentNullException("postbytes cannot be null");
                }
                request.headers[eHttpRequestHeader.ContentLength] = postbytes.Length.ToString();
                request.headers[eHttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=UTF-8";
                request.Write(postbytes);
            }
            var data = request.GetAll();
            try
            {
                if(!connected)
                {
                    socket.Connect(Dns.GetHostAddresses(uri.DnsSafeHost)[0], this.uri.Port);
                    connected = true;
                }
                socket.Send(data);
                var response = new HttpResponse(Cookies);
                while (!response.IsComplete)
                {
                    while (socket.IsDataAvailable())
                    {
                        byte[] buffer = new byte[8 * 1024];
                        socket.Receive(buffer);
                        response.Read(buffer);
                    }
                }
                this.Cookies = response.Cookies;
                if (response.headers.Connection == eConnectionType.Close)
                {
                    socket = null;
                }
                return response.GetContent();

            }
            catch(Exception e)
            {
                Log.Log.Default.Error(e);
            }
            return new byte[] { };
        }