public override void Process(Bamboo.WebServer.HttpRequest request, Bamboo.WebServer.HttpResponse response) { try { this._processor.Process(request, response); } catch (System.Exception exception) { response.Reset(); Error(exception); response.Status = 500; response.Headers.Add("Content-Type", "text/plain"); response.WriteLine(exception.Message); response.WriteLine(""); response.WriteLine(exception.StackTrace); while (exception.InnerException != null) { response.WriteLine(""); response.WriteLine(""); response.WriteLine(""); exception = exception.InnerException; response.WriteLine(exception.Message); response.WriteLine(""); response.WriteLine(exception.StackTrace); } } }
public override void Process(Bamboo.WebServer.HttpRequest request, Bamboo.WebServer.HttpResponse response) { response.Headers.Add("Content-Type", this._contentType); System.IO.FileStream fileStream = System.IO.File.OpenRead(this._path); Copy(fileStream, response.Output); fileStream.Close(); }
public override void Process(Bamboo.WebServer.HttpRequest request, Bamboo.WebServer.HttpResponse response) { if (request.Method.ToUpper().Equals("GET")) { response.Headers.Add("Content-Type", this._contentType); response.Headers.Add("ETag", this._etag); response.Output.Write(this._content, 0, this._content.Length); } else { throw new System.Exception("Invalid method: " + request.Method); } }
public virtual void PreProcess(System.Net.Sockets.Socket socket, System.IO.Stream stream) { #region Process //socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.SendTimeout, 15000); //socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReceiveTimeout, 15000); Bamboo.WebServer.HttpRequest request = new Bamboo.WebServer.HttpRequest(); Bamboo.WebServer.HttpResponse response = new Bamboo.WebServer.HttpResponse(); byte[] buffer = new byte[8192]; try { ReadRequest(socket, stream, request, buffer); this._processor.Process(request, response); KeepCookies(request, response); WriteResponse(response, stream); stream.Flush(); } catch (System.Exception exception) { throw new System.Exception("HTTP ERROR", exception); } finally { if (buffer != null) { buffer = null; } if (request != null) { request.Dispose(); request = null; } if (response != null) { response.Dispose(); response = null; } } //stream.Flush(); //TODO DELETE stream.Close(); //TODO set everyting = null #endregion }
public override void Process(Bamboo.WebServer.HttpRequest request, Bamboo.WebServer.HttpResponse response) { //TODO check containskey instead. HttpProcessor processor = (HttpProcessor)this._processors[request.Path]; if (processor != null) { processor.Process(request, response); return; } else if (this._defaultProcessor != null) { this._defaultProcessor.Process(request, response); return; } else { response.Reset(); response.Status = 404; response.Headers.Add("Content-Type", "text/plain"); response.WriteLine("Page not found."); } }
public abstract void Process(Bamboo.WebServer.HttpRequest request, Bamboo.WebServer.HttpResponse response);
public static void ReadRequest(System.Net.Sockets.Socket socket, System.IO.Stream stream, Bamboo.WebServer.HttpRequest request, byte[] buffer) { #region ReadRequest Bamboo.WebServer.HttpReader httpReader = new Bamboo.WebServer.HttpReader(stream); // Address:Port info. string localEndPoint = socket.LocalEndPoint.ToString(); int index = localEndPoint.LastIndexOf(":"); request.LocalAddress = localEndPoint.Substring(0, index); request.LocalPort = Int32.Parse(localEndPoint.Substring(index + 1)); string remoteEndPoint = socket.RemoteEndPoint.ToString(); index = remoteEndPoint.LastIndexOf(":"); request.RemoteAddress = remoteEndPoint.Substring(0, index); request.RemotePort = Int32.Parse(remoteEndPoint.Substring(index + 1)); // Status line string line = httpReader.ReadStartLine(); if (line == null) { throw new System.Exception("Could not read request line."); } string[] sublines = line.Split(" ".ToCharArray()); request.Method = sublines[0].ToUpper(); request.PathAndQuery = sublines[1]; index = request.PathAndQuery.IndexOf("?"); if (index == -1) { request.Path = request.PathAndQuery; request.Query = ""; } else { request.Path = request.PathAndQuery.Substring(0, index); request.Query = request.PathAndQuery.Substring(index + 1); } string[] header; while ((header = httpReader.ReadHeader()) != null) { if (header.Length == 1) { request.Headers.Add(header[0], ""); } else if (header.Length == 2) { request.Headers.Add(header[0], header[1]); } } // Load cookies request.Cookies = new System.Collections.Hashtable(new System.Collections.CaseInsensitiveHashCodeProvider(), new System.Collections.CaseInsensitiveComparer()); string cookies = (string)request.Headers["Cookie"]; if (cookies != null) { foreach (string cookie in cookies.Split(";".ToCharArray())) { index = cookie.IndexOf("="); if (index != -1) { string name = cookie.Substring(0, index).Trim(); string value = cookie.Substring(index + 1).Trim(); request.Cookies[name] = value; } else { //throw new System.Exception("ERROR: " + headers["Cookie"].ToString()); } } } request.QueryString = new System.Collections.Hashtable(0, new System.Collections.CaseInsensitiveHashCodeProvider(), new System.Collections.CaseInsensitiveComparer()); if (request.Query.Length > 0) { ParseGetData(request.Query, request.QueryString); } httpReader.ReadBody(request.Input, buffer); request.Input.Position = 0; string contentType = (string)request.Headers["Content-Type"]; if (contentType != null && request.Input.Length > 0) { if (contentType == "application/x-www-form-urlencoded") { ParsePostData(request.Input, request.Form); } else if (contentType.StartsWith("multipart/form-data")) { string boundary = contentType.Substring(contentType.IndexOf(";") + 1).Trim(); boundary = "--" + boundary.Substring(boundary.IndexOf("=") + 1); ParseMultipartFormData(boundary, request.Input, request.Form, request.Files); } } #endregion }
public override void PreProcess(System.Net.Sockets.Socket socket, System.IO.Stream stream) { // Address:Port info. string localAddress = ""; int localPort = 0; string remoteAddress = ""; int remotePort = 0; string start = System.DateTime.UtcNow.ToString(); byte[] request_bytes = null; byte[] response_bytes = null; System.Exception error = null; System.IO.MemoryStream requestStream = new System.IO.MemoryStream(); System.IO.MemoryStream responseStream = new System.IO.MemoryStream(); Bamboo.WebServer.TappedStream tappedStream = new Bamboo.WebServer.TappedStream(stream, requestStream, responseStream); Bamboo.WebServer.HttpRequest request = new Bamboo.WebServer.HttpRequest(); Bamboo.WebServer.HttpResponse response = new Bamboo.WebServer.HttpResponse(); byte[] buffer = new byte[8192]; try { // Address:Port info. string localEndPoint = socket.LocalEndPoint.ToString(); int index = localEndPoint.LastIndexOf(":"); localAddress = localEndPoint.Substring(0, index); localPort = Int32.Parse(localEndPoint.Substring(index + 1)); string remoteEndPoint = socket.RemoteEndPoint.ToString(); index = remoteEndPoint.LastIndexOf(":"); remoteAddress = remoteEndPoint.Substring(0, index); remotePort = Int32.Parse(remoteEndPoint.Substring(index + 1)); socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.SendTimeout, 15000); socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.ReceiveTimeout, 15000); ReadRequest(socket, tappedStream, request, buffer); this._processor.Process(request, response); KeepCookies(request, response); WriteResponse(response, tappedStream); tappedStream.Flush(); //TODO DELETE tappedStream.Close(); } catch (System.Exception exception) { error = exception; } finally { if (buffer != null) { buffer = null; } if (request != null) { request = null; } if (response != null) { response = null; } } requestStream.Flush(); responseStream.Flush(); request_bytes = requestStream.ToArray(); response_bytes = responseStream.ToArray(); requestStream.Close(); requestStream = null; responseStream.Close(); responseStream = null; string end = System.DateTime.UtcNow.ToString(); this.Log(localAddress, localPort, remoteAddress, remotePort, start, end, request_bytes, response_bytes, request, response, error); if (request != null) { request = null; } if (response != null) { response = null; } //TODO set everyting = null }