Example #1
0
 public Response(HttpResponse res)
     : base(res.Connection)
 {
     this.headers = res.Headers;
         this.status = res.StatusCode;
         this.buffer = res.Buffer;
 }
Example #2
0
        /// <summary>
        /// Writes the HTTP response to the stream.
        /// </summary>
        /// <param name="res">The resource.</param>
        public void WriteResponse(HttpResponse res)
        {
            // status line
            WriteStatusLine(res.StatusCode);

            // headers
            WriteHeaders(res.Headers);

            // end of headers
            writer.WriteLine();
            writer.Flush();

            // data
            if (res.Buffer.Length > 0) {
                byte[] data = res.Buffer.ToArray();
                writer.BaseStream.Write(data, 0, data.Length);
            }
        }
Example #3
0
        /// <summary>
        /// Reads the response.
        /// </summary>
        /// <returns></returns>
        public HttpResponse ReadResponse()
        {
            // create response
            HttpResponse response = new HttpResponse(transaction);
            response.StatusCode = ReadStatusLine();

            // headers
            response.Headers = ReadHeaders();

            return response;
        }
Example #4
0
        private void WriteResponse(HttpResponse res)
        {
            // post-process
            if (res.Headers.Exists("Location"))
                res.StatusCode = HttpStatus.Found;

            res.Headers["Content-Length"] = res.Buffer.Length.ToString();
            res.Headers["Connection"] = "Keep-Alive";

            if (res.Server.Name != null)
                res.Headers["Server"] = res.Server.Name;

            // write response
            //try {
            writer.WriteResponse(res);
            //}
            //catch (Exception) { }
        }
Example #5
0
        private HttpRequest ReadRequest(HttpResponse res)
        {
            HttpRequest req = null;

            try {
                req = reader.ReadRequest();
            }
            catch (HttpException ex) {
                // create event
                ErrorEventArgs e = new ErrorEventArgs(ex);

                // invoke
                server.OnError(e);

                if (!e.Handled) {
                    // send
                    res.StatusCode = ex.StatusCode;

                    // send error report
                    if (Application.CurrentDeployment == Deployment.Production)
                        res.Write("<b><p>Request Error</p></b><pre>" + ex.Message + "</pre>");
                }
            }
            catch (Exception ex) {
                // status code
                res.StatusCode = HttpStatus.InternalServerError;

                // send error report
                try {
                    if (Application.CurrentDeployment == Deployment.Production)
                        res.Write("<b><p>Internal Error</p></b><pre>" + ex.ToString() + "</pre>");
                }
                catch (Exception) { }

                // close and return
                Close();
                return req;
            }

            Console.WriteLine("FINISHED occuredreq");
            return req;
        }
Example #6
0
        /// <summary>
        /// Handles the request.
        /// </summary>
        private void Handle()
        {
            // create response
            HttpResponse res = new HttpResponse(this);
            HttpRequest req = null;

            // read request
            if (!Utilities.InvokeAsync(delegate() { req = ReadRequest(res);}, 1000)) {
                // close and return
                Close();

                Console.WriteLine("timeout occuredreq");
                return;
            }

            // handle request
            if (req != null) {
                if (Application.CurrentDeployment == Deployment.Production) {
                    server.OnRequest(new RequestEventArgs(req, res));
                } else {
                    try {
                        server.OnRequest(new RequestEventArgs(req, res));
                    }
                    catch (Exception ex) {
                        // create event
                        ErrorEventArgs e = new ErrorEventArgs(new HttpException("Route exception", HttpStatus.InternalServerError, ex) { Path = req.Path, Method = req.Method});

                        // invoke
                        res.Clear();
                        res.StatusCode = HttpStatus.InternalServerError;
                        server.OnError(e);

                        if (!e.Handled) {
                            // internal error with request handler
                            res.Write("<pre>An internal server error occured!</pre>");
                        }
                    }
                }
            }

            // write
            if (!Utilities.InvokeAsync(delegate() { WriteResponse(res); }, 1000)) {
                // close and return
                Close();

                Console.WriteLine("timeout occuredres");
                return;
            }
        }