private void OnSocketError(IClient c, ClientErrorArgs e) { HttpApiServer httpApiServer = Server.Gateway.HttpServer; if (httpApiServer.EnableLog(BeetleX.EventArgs.LogType.Error)) { httpApiServer.Log(BeetleX.EventArgs.LogType.Error, $"gateway request {Server.Host}:{Server.Port} error {e.Message}@{e.Error.InnerException?.Message} status {Status}"); } if (Status == RequestStatus.Requesting) { BadGateway result = new BadGateway(e.Error); EventResponseErrorArgs erea; if (e.Error is SocketException) { Code = ServerAgent.SOCKET_ERROR_CODE; erea = new EventResponseErrorArgs(Request, Response, result, BadGateway.SERVER_NET_ERROR); } else { Code = ServerAgent.PROCESS_ERROR_CODE; erea = new EventResponseErrorArgs(Request, Response, result, BadGateway.SERVER_AGENT_PROCESS_ERROR); } OnCompleted(erea); } else { Code = ServerAgent.OTHRER_ERROR_CODE; if (Status > RequestStatus.None) { OnCompleted(null); } } }
public EventResponseErrorArgs(HttpRequest request, HttpResponse response, Gateway gateway, string message, int errorCode) : base(request, response, gateway) { Result = new BadGateway(message, errorCode); ErrorCode = errorCode; Message = message; }
public void Execute(HttpRequest request, HttpResponse response) { if (ExecuteFilter(request, response)) { var agent = UrlRoute.GetServerAgent(request); if (agent == null) { BadGateway result = new BadGateway($"The {Url} url route server unavailable"); Events.EventResponseErrorArgs erea = new Events.EventResponseErrorArgs( request, response, result, BadGateway.URL_NODE_SERVER_UNAVAILABLE); UrlRoute.Gateway.OnResponseError(erea); } else { agent.Execute(request, response); } } }
public void Execute(HttpRequest request, HttpResponse response) { TcpClientAgent clientAgent = PopClient(); if (clientAgent == null) { string error = $"Unable to reach {Host}:{Port} HTTP request, exceeding maximum number of connections"; if (Gateway.HttpServer.EnableLog(LogType.Error)) { Gateway.HttpServer.Log(LogType.Error, error); } BadGateway result = new BadGateway(error); Events.EventResponseErrorArgs erea = new Events.EventResponseErrorArgs(request, response, result, BadGateway.SERVER_MAX_OF_CONNECTIONS); Gateway.OnResponseError(erea); } else { RequestAgent agent = new RequestAgent(clientAgent, this, request, response); agent.Completed = OnCompleted; agent.Execute(); } }
protected bool ExecuteFilter(HttpRequest request, HttpResponse response) { bool result = true; try { for (int i = 0; i < UrlRoute.RequestFilters.Length; i++) { result = UrlRoute.RequestFilters[i].Execute(request, response); if (!result) { break; } } return(result); } catch (Exception e_) { BadGateway badGateway = new BadGateway($"execute url filter error {e_.Message}"); UrlRoute.Gateway.OnResponseError(new Events.EventResponseErrorArgs(request, response, badGateway, BadGateway.URL_FILTER_ERROR)); return(false); } }
public void Execute() { var request = Request; var response = Response; Status = RequestStatus.Requesting; mClientAgent.Client.Connect(); if (mClientAgent.Client.IsConnected) { try { if (Request.Server.EnableLog(BeetleX.EventArgs.LogType.Info)) { Request.Server.Log(BeetleX.EventArgs.LogType.Info, $"gateway {Request.RemoteIPAddress} {Request.Method} {Request.Url} request {Server.Host}:{Server.Port} netstream writing"); } PipeStream pipeStream = mClientAgent.Client.Stream.ToPipeStream(); byte[] buffer = mBuffer; int offset = 0; var len = Encoding.UTF8.GetBytes(request.Method, 0, request.Method.Length, buffer, offset); offset += len; buffer[offset] = HeaderTypeFactory._SPACE_BYTE; offset++; len = Encoding.UTF8.GetBytes(request.Url, 0, request.Url.Length, buffer, offset); offset += len; buffer[offset] = HeaderTypeFactory._SPACE_BYTE; offset++; for (int i = 0; i < HeaderTypeFactory.HTTP_V11_BYTES.Length; i++) { buffer[offset + i] = HeaderTypeFactory.HTTP_V11_BYTES[i]; } offset += HeaderTypeFactory.HTTP_V11_BYTES.Length; buffer[offset] = HeaderTypeFactory._LINE_R; offset++; buffer[offset] = HeaderTypeFactory._LINE_N; offset++; pipeStream.Write(buffer, 0, offset); request.Header.Write(pipeStream); //if (request.Cookies.Items.Count > 0) //{ // HeaderTypeFactory.Write(HeaderTypeFactory.COOKIE, pipeStream); // int _i = 0, _iLast = request.Cookies.Items.Count - 1; // foreach (var _item in request.Cookies.Items) // { // if (_i == _iLast) // { // pipeStream.Write( // Encoding.ASCII.GetBytes( // $"{_item.Key}={_item.Value};".TrimEnd(';') // ) // ); // } // else // { // pipeStream.Write( // Encoding.ASCII.GetBytes( // $"{_item.Key}={_item.Value};" // ) // ); // } // _i++; // } ////// pipeStream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2); //} //if (request.Cookies.Items.Count > 0) //{ // StringBuilder _sbuilder = new StringBuilder(); // foreach (var _item in request.Cookies.Items) // { // _sbuilder.Append($"{_item.Key}={_item.Value};"); // } // new HeaderValue( // HeaderTypeFactory.Find(HeaderTypeFactory.COOKIE), // _sbuilder.ToString().TrimEnd(';') // ) // .Write(pipeStream); //} //HTTP报文Head结尾需要再补个/r/n,也就是说报文Head结尾是两个/r/n pipeStream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2); int bodylength = request.Length; while (bodylength > 0) { len = request.Stream.Read(buffer, 0, buffer.Length); pipeStream.Write(buffer, 0, len); bodylength -= len; } Status = RequestStatus.Responding; /* BeetleX.dll!BeetleX.Buffers.SocketAsyncEventArgsX.InvokeCompleted() * BeetleX.dll!BeetleX.Buffers.SocketAsyncEventArgsX.AsyncTo(System.Net.Sockets.Socket socket, object userToken, int length)=>socket.SendAsyn * BeetleX.dll!BeetleX.Buffers.Buffer.AsyncTo(BeetleX.Buffers.SocketAsyncEventArgsX argsX, System.Net.Sockets.Socket socket) * BeetleX.dll!BeetleX.Clients.AsyncTcpClient.CommitBuffer(BeetleX.Buffers.IBuffer buffer) * BeetleX.dll!BeetleX.Clients.AsyncTcpClient.ProcessSendMessages() * BeetleX.dll!BeetleX.Clients.AsyncTcpClient.Send(object data) * BeetleX.dll!BeetleX.Clients.AsyncTcpClient.OnWriterFlash(BeetleX.Buffers.IBuffer data) * BeetleX.dll!BeetleX.Buffers.PipeStream.Flush() * */ mClientAgent.Client.Stream.Flush(); ///<see cref="AsyncTcpClient.OnWriterFlash"/> if (Request.Server.EnableLog(BeetleX.EventArgs.LogType.Info)) { Request.Server.Log(BeetleX.EventArgs.LogType.Info, $"gateway {Request.RemoteIPAddress} {Request.Method} {Request.Url} request {Server.Host}:{Server.Port} netstream writed"); } } catch (Exception e_) { string error = $"gateway {request.RemoteIPAddress} {request.Method} {request.Url} to {Server.Host}:{Server.Port} error {e_.Message}@{e_.StackTrace}"; if (request.Server.EnableLog(BeetleX.EventArgs.LogType.Error)) { request.Server.Log(BeetleX.EventArgs.LogType.Error, error); } BadGateway result = new BadGateway(error); EventResponseErrorArgs eventResponseErrorArgs = new EventResponseErrorArgs(request, response, result, BadGateway.SERVER_NET_ERROR); try { if (mClientAgent.Client != null) { mClientAgent.Client.DisConnect(); } } finally { OnCompleted(eventResponseErrorArgs); } return; } } }
public void Execute() { var request = Request; var response = Response; Status = RequestStatus.Requesting; mClientAgent.Client.Connect(); if (mClientAgent.Client.IsConnected) { try { PipeStream pipeStream = mClientAgent.Client.Stream.ToPipeStream(); byte[] buffer = mBuffer; int offset = 0; var len = Encoding.UTF8.GetBytes(request.Method, 0, request.Method.Length, buffer, offset); offset += len; buffer[offset] = HeaderTypeFactory._SPACE_BYTE; offset++; len = Encoding.UTF8.GetBytes(request.Url, 0, request.Url.Length, buffer, offset); offset += len; buffer[offset] = HeaderTypeFactory._SPACE_BYTE; offset++; for (int i = 0; i < HeaderTypeFactory.HTTP_V11_BYTES.Length; i++) { buffer[offset + i] = HeaderTypeFactory.HTTP_V11_BYTES[i]; } offset += HeaderTypeFactory.HTTP_V11_BYTES.Length; buffer[offset] = HeaderTypeFactory._LINE_R; offset++; buffer[offset] = HeaderTypeFactory._LINE_N; offset++; pipeStream.Write(buffer, 0, offset); request.Header.Write(pipeStream); pipeStream.Write(HeaderTypeFactory.LINE_BYTES, 0, 2); while (request.Length > 0) { len = request.Stream.Read(buffer, 0, buffer.Length); pipeStream.Write(buffer, 0, len); } Status = RequestStatus.Responding; mClientAgent.Client.Stream.Flush(); } catch (Exception e_) { string error = $"gateway {request.RemoteIPAddress} {request.Method} {request.Url} to {Server.Host}:{Server.Port} error {e_.Message}@{e_.StackTrace}"; if (request.Server.EnableLog(BeetleX.EventArgs.LogType.Error)) { request.Server.Log(BeetleX.EventArgs.LogType.Error, error); } BadGateway result = new BadGateway(error); EventResponseErrorArgs eventResponseErrorArgs = new EventResponseErrorArgs(request, response, result, BadGateway.SERVER_NET_ERROR); try { if (mClientAgent.Client != null) { mClientAgent.Client.DisConnect(); } } finally { OnCompleted(eventResponseErrorArgs); } return; } } }