src
Request.cs
using BeetleX.Buffers;
using BeetleX.Clients;
using BeetleX.Tasks;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
#if NETCOREAPP2_1
using BeetleX.Tracks;
#endif
namespace BeetleX.Http.Clients
{
public enum LoadedState : int
{
None = 1,
Method = 2,
Header = 4,
Completed = 8
}
#if NETCOREAPP2_1
public clast Request : IApiObject
#else
public clast Request
#endif
{
#if NETCOREAPP2_1
private CodeTrack mRequestTrack;
public string Name => this.Url;
public string[] Group
{
get
{
return new[] { "HTTPClient", "Request", $"{HttpHost.Host}" };
}
}
#endif
public const string CODE_TREAK_PARENTID = "parent-id";
public const string POST = "POST";
public const string GET = "GET";
public const string DELETE = "DELETE";
public const string PUT = "PUT";
public Request()
{
Method = GET;
this.HttpProtocol = "HTTP/1.1";
}
public string Boundary { get; set; }
public IBodyFormater Formater { get; set; } = new JsonFormater();
public Dictionary QuestryString { get; set; }
public Header Header { get; set; }
public string Url { get; set; }
public string Method { get; set; }
public string HttpProtocol { get; set; }
public Response Response { get; set; }
public Object Body { get; set; }
public Type BodyType { get; set; }
public int? TimeOut { get; set; }
public RequestStatus Status { get; set; }
public IClient Client { get; set; }
internal void Execute(PipeStream stream)
{
try
{
var buffer = HttpParse.GetByteBuffer();
int offset = 0;
offset += Encoding.ASCII.GetBytes(Method, 0, Method.Length, buffer, offset);
buffer[offset] = HeaderTypeFactory._SPACE_BYTE;
offset++;
offset += Encoding.ASCII.GetBytes(Url, 0, Url.Length, buffer, offset);
if (QuestryString != null && QuestryString.Count > 0)
{
int i = 0;
foreach (var item in this.QuestryString)
{
string key = item.Key;
string value = item.Value;
if (string.IsNullOrEmpty(value))
continue;
value = System.Net.WebUtility.UrlEncode(value);
if (i == 0)
{
buffer[offset] = HeaderTypeFactory._QMARK;
offset++;
}
else
{
buffer[offset] = HeaderTypeFactory._AND;
offset++;
}
offset += Encoding.ASCII.GetBytes(key, 0, key.Length, buffer, offset);
buffer[offset] = HeaderTypeFactory._EQ;
offset++;
offset += Encoding.ASCII.GetBytes(value, 0, value.Length, buffer, offset);
i++;
}
}
buffer[offset] = HeaderTypeFactory._SPACE_BYTE;
offset++;
offset += Encoding.ASCII.GetBytes(HttpProtocol, 0, HttpProtocol.Length, buffer, offset);
buffer[offset] = HeaderTypeFactory._LINE_R;
offset++;
buffer[offset] = HeaderTypeFactory._LINE_N;
offset++;
stream.Write(buffer, 0, offset);
Formater?.Setting(this);
if (Header != null)
Header.Write(stream);
if (Method == POST || Method == PUT)
{
if (Body != null)
{
stream.Write(HeaderTypeFactory.CONTENT_LENGTH_BYTES, 0, 16);
MemoryBlockCollection contentLength = stream.Allocate(10);
stream.Write(HeaderTypeFactory.TOW_LINE_BYTES, 0, 4);
int len = stream.CacheLength;
Formater.Serialization(this, Body, stream);
int count = stream.CacheLength - len;
contentLength.Full(count.ToString().PadRight(10), stream.Encoding);
}
else
{
stream.Write(HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES, 0, HeaderTypeFactory.NULL_CONTENT_LENGTH_BYTES.Length);
stream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2);
}
}
else
{
stream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2);
}
}
catch (Exception e)
{
Client?.DisConnect();
throw new HttpClientException($"http client write data error {e.Message}", e);
}
}
public HttpHost HttpHost { get; set; }
public Action GetConnection { get; set; }
public static event Action Executing;
public async Task Execute()
{
#if NETCOREAPP2_1
using (mRequestTrack = CodeTrackFactory.TrackReport(this, CodeTrackLevel.Module, null))
{
if (Activity.Current != null)
Header[CODE_TREAK_PARENTID] = Activity.Current.Id;
if (mRequestTrack.Enabled)
mRequestTrack.Activity?.AddTag("tag", "BeetleX HttpClient");
#endif
Executing?.Invoke(this);
mTaskCompletionSource = CompletionSourceFactory.Create(TimeOut != null ? TimeOut.Value : HttpHost.Pool.TimeOut); // new AnyCompletionSource();
mTaskCompletionSource.TimeOut = OnRequestTimeout;
OnExecute();
return await (Task)mTaskCompletionSource.GetTask();
#if NETCOREAPP2_1
}
#endif
}
private IAnyCompletionSource mTaskCompletionSource;
private void OnRequestTimeout(IAnyCompletionSource source)
{
if (requestResult != null)
{
requestResult.TrySetException(new TimeoutException($"request imeout!"));
}
else
{
Response response = new Response();
response.Code = "408";
response.CodeMsg = "Request timeout";
response.Exception = new HttpClientException(this, HttpHost.Uri, "Request timeout");
source?.Success(response);
}
}
private void onEventClientError(IClient c, ClientErrorArgs e)
{
requestResult.TrySetException(e.Error);
}
private void OnEventClientPacketCompleted(IClient client, object message)
{
requestResult.TrySetResult(message);
}
private TaskCompletionSource requestResult;
private async Task DisConnect(IClient client)
{
client.DisConnect();
await Task.Delay(1000);
}
private async void OnExecute()
{
HttpClientHandler client = null;
Response response;
bool closeClient = false;
try
{
object result = null;
requestResult = new TaskCompletionSource();
client = await HttpHost.Pool.Pop();
Client = client.Client;
AsyncTcpClient asyncClient = (AsyncTcpClient)client.Client;
asyncClient.ClientError = onEventClientError;
asyncClient.PacketReceive = OnEventClientPacketCompleted;
GetConnection?.Invoke(asyncClient);
#if NETCOREAPP2_1
using (CodeTrackFactory.Track(Url, CodeTrackLevel.Function, mRequestTrack?.Activity?.Id, "HTTPClient", "Protocol", "Write"))
{
asyncClient.Send(this);
Status = RequestStatus.SendCompleted;
}
#else
asyncClient.Send(this);
Status = RequestStatus.SendCompleted;
#endif
#if NETCOREAPP2_1
using (CodeTrackFactory.Track(Url, CodeTrackLevel.Function, mRequestTrack?.Activity?.Id, "HTTPClient", "Protocol", "Read"))
{
var a = requestResult.Task;
result = await a;
}
#else
var a = requestResult.Task;
result = await a;
#endif
if (result is Exception error)
{
response = new Response();
response.Exception = new HttpClientException(this, HttpHost.Uri, error.Message, error);
Status = RequestStatus.Error;
closeClient = true;
}
else
{
response = (Response)result;
Status = RequestStatus.Received;
}
if (response.Exception == null)
{
int code = int.Parse(response.Code);
if (response.Length > 0)
{
try
{
if (code >= 200 && code < 300)
response.Body = this.Formater.Deserialization(response, response.Stream, this.BodyType, response.Length);
else
response.Body = response.Stream.ReadString(response.Length);
}
finally
{
response.Stream.ReadFree(response.Length);
if (response.Chunked)
response.Stream.Dispose();
response.Stream = null;
}
}
if (!response.KeepAlive)
client.Client.DisConnect();
if (code >= 400)
{
response.Exception = new HttpClientException(this, HttpHost.Uri, $"{Url}({response.Code}) [{response.Body}]");
response.Exception.Code = code;
}
Status = RequestStatus.Completed;
}
}
catch (Exception e_)
{
HttpClientException clientException = new HttpClientException(this, HttpHost.Uri, e_.Message, e_);
response = new Response { Exception = clientException };
Status = RequestStatus.Error;
closeClient = true;
}
if (response.Exception != null)
HttpHost.AddError(response.Exception.SocketError);
else
HttpHost.AddSuccess();
Response.Current = response;
this.Response = response;
if (client != null)
{
if (client.Client is AsyncTcpClient asclient)
{
asclient.ClientError = null;
asclient.PacketReceive = null;
}
if (closeClient)
await DisConnect(client.Client);
HttpHost.Pool.Push(client);
client = null;
}
await Task.Run(() => mTaskCompletionSource.Success(response));
}
}
public enum RequestStatus
{
None,
SendCompleted,
Received,
Completed,
Error
}
}