public override bool Handle(Request request, Response response)
		{
			using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(_strValue)))
			{
				response.SendData(_contentType, ms);
			}
			return true;
		}
		public override bool Handle(Request request, Response response)
		{
			if (pattern.IsMatch(request.Path))
			{
				response.SendErrorCode(errorCode, errorMessage);
				return true;
			}
			else
				return false;
		}
		public override bool Handle(Request request, Response response)
		{
			if (request.HttpMethod != "GET")
			{
				response.SendErrorCode(501, "Method not implemented");
				return true;
			}

			string result = _template.Execute(new object[] {_blogConfig});
			response.SendHtml(result);
			return true;
		}
		public override bool Handle(Request request, Response response)
		{
			XmlDocument requestDocument = new XmlDocument();
			requestDocument.Load(request.RequestBody);
			XmlDocument responseDocument;
			if (Handle(request.HttpMethod, request.Path, request.Querystring, request.Headers, requestDocument, out responseDocument))
			{
				Stream docStream = new MemoryStream();
				responseDocument.Save(docStream);
				docStream.Seek(0, SeekOrigin.Begin);
				response.SendData(ContentType, docStream);
				return true;
			}
			return false;
		}
Esempio n. 5
0
		protected internal override bool Filter(Request request, Response response)
		{
			Console.WriteLine("REQUEST: {0} {1}{2}", request.HttpMethod, request.Path, request.Querystring != null ? "?" + request.Querystring : "");
			PrintStream(request.RequestBody);
			request.RequestBody.Seek(0, SeekOrigin.Begin);
			bool result = base.Filter (request, response);
			if (result)
			{
				response.Stream.Seek(0, SeekOrigin.Begin);
				Console.WriteLine();
				Console.WriteLine("RESPONSE:");
				PrintStream(response.Stream);
				Console.WriteLine();
			}
			return result;
		}
		private bool Handler(string method, string uri, HttpHeaders headers, SocketReader reader, Socket socket)
		{
			if (_handlers.Count == 0)
				return false;
			
			string path, querystring;
			int qindex = uri.IndexOf('?');
			if (qindex < 0)
			{
				path = uri;
				querystring = null;
			}
			else
			{
				path = uri.Substring(0, qindex);
				querystring = qindex == (uri.Length - 1) ? "" : uri.Substring(qindex + 1);
			}
			
			MemoryStream requestBodyStream = new MemoryStream();
			byte[] buffer = new byte[8192];
			int bytesRead;
			while (0 != (bytesRead = reader.Read(buffer, 0, buffer.Length)))
			{
				requestBodyStream.Write(buffer, 0, bytesRead);
			}
			requestBodyStream.Seek(0, SeekOrigin.Begin);
			
			Stream readOnlyRequestBodyStream = new ReadOnlyStream(requestBodyStream);
			readOnlyRequestBodyStream.Seek(0, SeekOrigin.Begin);

			Request request = new Request(method, path, querystring, headers, readOnlyRequestBodyStream);
			Response response = new Response(new MemoryStream());
			if (StartFilter.Filter(request, response))
			{
				response.Stream.Seek(0, SeekOrigin.Begin);
				StreamHelper.Transfer(response.Stream, new SocketStream(socket, false));
				return true;
			}
			return false;
		}
		/// <returns>True if handled. If false, writes to the response will be reverted.</returns>
		public abstract bool Handle(Request request, Response response);
		/// <summary>Subclasses should override this to provide filtering behavior.</summary>
		/// <param name="httpMethod">e.g. "GET", "POST".</param>
		/// <param name="path">e.g. "/", "/foo/bar.png"</param>
		/// <param name="querystring">e.g. "foo=bar&x=y"</param>
		/// <param name="headers">The request headers.</param>
		/// <param name="requestBody">The data in the body of the request (does not include headers).</param>
		/// <param name="responseStream">The response stream. Use HttpHelper class to help form valid responses.</param>
		/// <returns>True if handled. If false, writes to the responseStream will be reverted.</returns>
		protected internal virtual bool Filter(Request request, Response response)
		{
			return NextFilter.Filter(request, response);
		}
		public override bool Handle(Request request, Response response)
		{
			if (request.HttpMethod != "GET")
				return false;
			
			string[] chunks = request.Path.Split(new char[] {'?'}, 2);
			string path1 = HttpUtility.UrlDecode(chunks[0]);
			if (!path1.StartsWith(_basePath))
				return false;
			string relativePath = path1.Substring(_basePath.Length).Replace('/', '\\');
			string filePath = Util.PathCanonicalize(Path.Combine(_rootDir, relativePath));
			if (!filePath.StartsWith(_rootDir))
			{
				Debug.Fail("Unexpected file path requested: " + filePath);
				return false;
			}
			
			if (Directory.Exists(filePath))
			{
				if (!request.Path.EndsWith("/"))
				{
					response.SendRedirect(request.Path + "/", false);
					return true;
				}
				
				if (File.Exists(Path.Combine(filePath, "index.htm")))
				{
					response.SendFile(Path.Combine(filePath, "index.htm"), "text/html");
					return true;
				}
				else if (File.Exists(Path.Combine(filePath, "index.html")))
				{
					response.SendFile(Path.Combine(filePath, "index.html"), "text/html");
					return true;
				}
				else if (_allowDirectoryBrowsing)
				{
					OutputDirectory(filePath, request, response);
					return true;
				}
				else
					return false;
			}
			
			if (!File.Exists(filePath))
				return false;
			
			string contentType;
			switch (Path.GetExtension(filePath).ToLower())
			{
				case ".htm":
				case ".html":
				case ".xhtml":
					contentType = "text/html";
					break;
				case ".txt":
					contentType = "text/plain";
					break;
				case ".css":
					contentType = "text/css";
					break;
				case ".jpg":
				case ".jpeg":
					contentType = "image/jpeg";
					break;
				case ".gif":
					contentType = "image/gif";
					break;
				case ".png":
					contentType = "image/png";
					break;
				case ".xml":
					contentType = "text/xml";
					break;
				default:
					response.SendErrorCode(403, "Access to the requested file type is forbidden");
					return true;
			}
			
			response.SendFile(filePath, contentType);
			return true;
		}
		private void OutputDirectory(string dir, Request request, Response response)
		{
			string[] dirs = Directory.GetDirectories(dir);
			Cleanup(dirs);
			string[] files = Directory.GetFiles(dir);
			Cleanup(files);
			
			response.SendHtml(_indexTemplate.Execute(request.Path, dirs, files));
		}
		private bool InvokeHandlers(Request request, Response response)
		{
			foreach (HttpRequestHandler handler in _handlers)
			{
				request.RequestBody.Seek(0, SeekOrigin.Begin);
				
				Response tempResponse = new Response(new MemoryStream());
				if (handler.Handle(request, tempResponse))
				{
					tempResponse.Stream.Seek(0, SeekOrigin.Begin);
					StreamHelper.Transfer(tempResponse.Stream, response.Stream);
					
					return true;
				}
			}
			return false;
		}
			protected internal override bool Filter(Request request, Response response)
			{
				return _parent.InvokeHandlers(request, response);
			}