NodeNetAsync.Net.Http.HttpResponse.EndAsync C# (CSharp) Méthode

EndAsync() public méthode

public EndAsync ( ) : System.Threading.Tasks.Task
Résultat System.Threading.Tasks.Task
		async public Task EndAsync()
		{
			//if (!IsWebSocket)
			{
				await WriteAsync("");

				if (Buffering)
				{
					await Socket.WriteAsync(Buffer.GetBuffer(), 0, (int)Buffer.Length);
				}
			}
		}

Usage Example

Exemple #1
0
		async private Task TcpServer_HandleClient(TcpSocket Client)
		{
			Exception YieldedException = null;

			//Console.WriteLine("HandleClient");
			// Create Request and Response
			int ConnectionId = LastConnectionId++;

			await InitializeConnectionAsync(Client);

			bool KeepAlive = true;
			int KeepAliveCount = 0;
			//bool KeepAlive = false;
			try
			{
				do
				{
					var Request = new HttpRequest(this.Port, ConnectionId, KeepAliveCount++);
					var Response = new HttpResponse(Client);

					try
					{
						try
						{
							await ReadHeadersAsync(Client, Request, Response);
						}
						catch (SequenceTooLongException)
						{
							KeepAlive = false;
							//throw(new HttpException(HttpCode.REQUEST_URI_TOO_LONG_412));
							throw (new HttpException(HttpCode.REQUEST_HEADER_FIELDS_TOO_LARGE_431));
						}
						catch (Exception Exception)
						{
							KeepAlive = false;
							throw Exception;
						}

						Response.Headers["Content-Type"] = "text/html";

						switch (Request.Headers["Connection"].ToLowerInvariant())
						{
							case "keep-alive":
								Response.Headers["Connection"] = "keep-alive";
								break;
							default:
							case "close":
								Response.Headers["Connection"] = "close";
								KeepAlive = false;
								break;
						}

						// Reached maximum KeepAlive Requests.
						if (KeepAliveCount >= MaxKeepAliveRequests)
						{
							Response.Headers["Connection"] = "close";
							KeepAlive = false;
						}

						Request.Content = new byte[0];
						var ContentLengthString = Request.Headers["Content-Length"];
						if (ContentLengthString != "" && ContentLengthString != null)
						{
							int ContentLength = 0;
							if (!int.TryParse(ContentLengthString, out ContentLength))
							{
								throw(new HttpException(HttpCode.BAD_REQUEST_400));
							}
							if (ContentLength > 128 * 1024)
							{
								throw (new HttpException(HttpCode.BAD_REQUEST_400));
							}
							Request.Content = await Client.ReadBytesAsync(ContentLength);
						}

						// Apply Pre Request filters
						foreach (var Filter in FilterList)
						{
							await Filter.FilterAsync(Request, Response);
						}

						// Main HandleRequest
						if (HandleRequest != null)
						{
							await HandleRequest(Request, Response);
						}
					}
					catch (HttpException HttpException)
					{
						Response.Buffering = true;
						Response.ChunkedTransferEncoding = true;
						//Response.Buffering = false;
						//Response.ChunkedTransferEncoding = true;
						Response.SetHttpCode(HttpException.HttpCode);
						YieldedException = HttpException;
					}
					catch (IOException)
					{
					}
					catch (Exception Exception)
					{
						YieldedException = Exception;
					}

					if (YieldedException != null)
					{
						if (YieldedException is HttpException) {
							var HttpException = YieldedException as HttpException;
							if ((int)HttpException.HttpCode >= 400)
							{
								await Response.WriteAsync("<h1>" + HttpException.HttpCode + "</h1>");
							}
						}
						else
						{
							if (Debugger.IsAttached)
							{
								await Response.WriteAsync("--><pre>" + Html.Quote(YieldedException.ToString()) + "</pre>");
							}

							//await Console.Out.WriteLineAsync("Exception : " + YieldedException.ToString());
							Console.WriteLine("Exception : " + YieldedException.ToString());
						}
						YieldedException = null;
					}

					// Finalize response
					await Response.EndAsync();
				} while (KeepAlive);
			}
			catch (IOException)
			{
			}
			catch (Exception Exception)
			{
				YieldedException = Exception;
			}

			if (YieldedException != null)
			{
				//Console.WriteLine("YIELD!!!!!!!!!!!!!!!!!! : " + YieldedException.ToString());
				if (Debugger.IsAttached)
				{
					Console.WriteLine("YIELD!!!!!!!!!!!!!!!!!! : " + YieldedException.ToString());
				}
				YieldedException = null;
			}

			await Client.CloseAsync();
		}