Esempio n. 1
0
		public void TestMultipartDecoder()
		{
			MultipartDecoder MultipartDecoder = new MultipartDecoder(new MemoryStream(rawData), "------WebKitFormBoundaryAGx5IxnXxZ0Paxm2");
			var Parts = MultipartDecoder.Parse();
			Assert.AreEqual(Parts[0].ContentType, "text/plain");
			Assert.AreEqual(Parts[1].ContentType, "text/plain");
			Assert.AreEqual(Parts[2].ContentType, "application/octet-stream");

			Assert.AreEqual(Parts[0].Name, "Title");
			Assert.AreEqual(Parts[1].Name, "Body");
			Assert.AreEqual(Parts[2].Name, "File");

			Assert.AreEqual(Parts[0].Content, "asdasdadas");
			Assert.AreEqual(Parts[1].Content, "dasdasd");
			Assert.AreEqual(Parts[2].Content, null);

			Assert.AreEqual(Parts[2].Stream.Length, 0);
		}
Esempio n. 2
0
		sealed protected override void HandleFascgiRequest(FastcgiRequest FastcgiRequest)
		{
			FastcgiRequest.StdinStream.SetPosition(0);
			Dictionary<string, string> Post = new Dictionary<string, string>();
			Dictionary<string, HttpFile> Files = new Dictionary<string, HttpFile>();
			bool HandledPost = false;

			//foreach (var Param in FastcgiRequest.Params) Console.WriteLine(Param);

			HttpHeader ContentType = new HttpHeader("Content-Type", FastcgiRequest.GetParam("CONTENT_TYPE"));

			var ContentTypeParts = ContentType.ParseValue("type");
			if (ContentTypeParts["type"] == "multipart/form-data")
			{
				string Boundary = ContentTypeParts["boundary"];
				//File.WriteAllBytes("Boundary.bin", Encoding.ASCII.GetBytes(Boundary));
				MultipartDecoder MultipartDecoder = new MultipartDecoder(FastcgiRequest.StdinStream, "--" + Boundary);
				var Parts = MultipartDecoder.Parse();

				foreach (var Part in Parts)
				{
					if (Part.IsFile)
					{
						Files.Add(Part.Name, new HttpFile()
						{
							TempFile = new FileInfo(Part.TempFilePath),
							FileName = Part.FileName,
							ContentType = Part.ContentType,
						});
					}
					else
					{
						Post[Part.Name] = Part.Content;
					}

					HandledPost = true;
				}
			}

			if (!HandledPost)
			{
				Post = HttpUtils.ParseUrlEncoded(Encoding.UTF8.GetString(FastcgiRequest.StdinStream.ReadAll()));
			}

			//CONTENT_TYPE: multipart/form-data; boundary=----WebKitFormBoundaryIMw3ByBOPx38V6Bd

			using (var OutputTextWriter = new StringWriter())
			{
				var HttpRequest = new HttpRequest()
				{
					Headers = new HttpHeaderList(),
					Output = OutputTextWriter,
					Enviroment = FastcgiRequest.Params,
					StdinStream = FastcgiRequest.StdinStream,
					Post = Post,
					Files = Files,
					Get = HttpUtils.ParseUrlEncoded(FastcgiRequest.GetParam("QUERY_STRING")),
					Cookies = new Dictionary<String, String>(),
				};

				HttpRequest.Headers.Set("X-Dynamic", "C#");
				HttpRequest.SetContentType("text/html", Encoding.UTF8);

				var Stopwatch = new Stopwatch();
				Stopwatch.Start();
				{
					HandleHttpRequest(HttpRequest);
				}
				Stopwatch.Stop();
				double GenerationTime = (double)Stopwatch.ElapsedTicks / (double)Stopwatch.Frequency;

				HttpRequest.Headers.Set("X-GenerationTime", String.Format("{0}", GenerationTime.ToString("F8")));

				using (var Stdout = new StreamWriter(FastcgiRequest.StdoutStream))
				{
					HttpRequest.Headers.WriteTo(Stdout);
				}

				using (var Stdout = new StreamWriter(FastcgiRequest.StdoutStream, HttpRequest.Encoding))
				{
					Stdout.Write(OutputTextWriter.ToString());
					if (HttpRequest.OutputDebug)
					{
						Stdout.WriteLine("<pre>");
						Stdout.WriteLine("Generation Time: {0}", GenerationTime);
						Stdout.WriteLine("</pre>");
					}
				}
			}
		}