Woopsa.HTTPResponse.Respond C# (CSharp) Method

Respond() public method

Writes the response to the output stream.

This method should only be called in certain special cases where the route solving system is being short-circuited somehow, because the route solving system is usually in charge of calling this method.

public Respond ( Stream outputStream ) : void
outputStream Stream The stream in which to copy the output. Does not close the stream
return void
        public void Respond(Stream outputStream)
        {
            try
            {
                MemoryStream responseStream = new MemoryStream();
                _bufferWriter.Flush();
                SetHeaderIfNotExists(HTTPHeader.ContentType, MIMETypes.Text.Plain);
                ResponseLength = _bufferStream.Length;
                SetHeader(HTTPHeader.ContentLength, ResponseLength.ToString());
                SetHeader(HTTPHeader.Date, DateTime.Now.ToHTTPDate());
                using (StreamWriter writer = new StreamWriter(responseStream, new UTF8Encoding(false), 4096))
                {
                    writer.Write("HTTP/1.1 " + ResponseCode + " " + ResponseMessage + "\r\n");
                    foreach (string header in Headers)
                        writer.Write(header + "\r\n");
                    writer.Write("\r\n");
                    writer.Flush();

                    _bufferStream.Position = 0;
                    _bufferStream.CopyTo(responseStream);
                    responseStream.Position = 0;
                    responseStream.CopyTo(outputStream);
                }
            }
            catch (IOException e)
            {
                // This error will be "caught" by the Web Server and evented up so the
                // user can do whatever he wants (or not) with it
                DoError(this, e);
            }
        }

Usage Example

Beispiel #1
0
        internal void HandleRequest(HTTPRequest request, HTTPResponse response, Stream stream)
        {
            try
            {
                bool        matchFound = false;
                RouteMapper mapper     = null;
                int         i          = 0;

                for (;;)
                {
                    lock (_routes)
                    {
                        if (i < _routes.Count)
                        {
                            mapper = _routes[i++];
                        }
                        else
                        {
                            break;
                        }
                    }
                    string regex = "^" + mapper.Route;
                    if (!mapper.AcceptSubroutes)
                    {
                        regex += "$";
                    }
                    if (Regex.IsMatch(request.BaseURL, regex))
                    {
                        if ((mapper.Methods & request.Method) != 0)
                        {
                            if (mapper.AcceptSubroutes)
                            {
                                int pos = request.BaseURL.IndexOf(mapper.Route);
                                request.Subroute = request.BaseURL.Substring(0, pos) + request.BaseURL.Substring(pos + mapper.Route.Length);
                            }
                            matchFound = true;
                            break;
                        }
                    }
                }

                if (!matchFound)
                {
                    response.WriteError(HTTPStatusCode.NotFound, "Not Found");
                    response.Respond(stream);
                    OnError(RoutingErrorType.NO_MATCHES, "No route found for request", request);
                }
                else
                {
                    mapper.HandleRequest(request, response);
                    response.Respond(stream);
                }
            }
            catch (Exception e)
            {
                response.WriteError(HTTPStatusCode.InternalServerError, String.Format("Internal Server Error {0}", e.Message));
                response.Respond(stream);
                OnError(RoutingErrorType.INTERNAL, "A RouteHandler threw an exception.", request);
            }
        }
All Usage Examples Of Woopsa.HTTPResponse::Respond