mirror of https://gitee.com/apiumc/Gateway.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
362 lines
10 KiB
C#
362 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UMC.Data;
|
|
using UMC.Net;
|
|
|
|
namespace UMC.ITME
|
|
{
|
|
public class HttpMimeContext : UMC.Net.NetContext
|
|
{
|
|
public override string Server => Environment.MachineName;
|
|
HttpMimeRequest _request;
|
|
HttpMimeResponse _response;
|
|
public override long? ContentLength { get => _request.ContentLength; set => _response.ContentLength = value; }
|
|
public override void AppendCookie(string name, string value)
|
|
{
|
|
_response.AppendCookie(name, value);
|
|
|
|
}
|
|
public override void AppendCookie(string name, string value, string path)
|
|
{
|
|
|
|
_response.AppendCookie(name, value, path);
|
|
}
|
|
public override bool IsWebSocket => _request.IsWebSocket;
|
|
Net.TextWriter writer;
|
|
public override void RewriteUrl(string pathAndQuery)
|
|
{
|
|
_request.RewriteUrl(pathAndQuery);
|
|
_QueryString = null;
|
|
}
|
|
public HttpMimeContext(HttpMimeRequest request, HttpMimeResponse response)
|
|
{
|
|
this._request = request;
|
|
this._response = response;
|
|
writer = new Net.TextWriter(response.OutputStream.Write);
|
|
}
|
|
public override void ReadAsData(NetWriteData readData)
|
|
{
|
|
if (_request.ContentLength > 0 && _request.IsMimeFinish == false)
|
|
{
|
|
if (aseSynchronousIOEnd == null)
|
|
{
|
|
aseSynchronousIOEnd = () => { };
|
|
}
|
|
}
|
|
this._request.ReadAsData(readData);
|
|
}
|
|
Action aseSynchronousIOEnd;
|
|
public override bool AllowSynchronousIO => aseSynchronousIOEnd != null;
|
|
public override void UseSynchronousIO(Action action)
|
|
{
|
|
aseSynchronousIOEnd = action;
|
|
}
|
|
void UseSynchronousIOEnd()
|
|
{
|
|
try
|
|
{
|
|
aseSynchronousIOEnd();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
UMC.Data.Utility.Error("SynchronousIO", DateTime.Now, ex.ToString());
|
|
}
|
|
}
|
|
public override void OutputFinish()
|
|
{
|
|
|
|
if (aseSynchronousIOEnd != null)
|
|
{
|
|
this.Output.Flush();
|
|
if (_response.OutputFinish())
|
|
{
|
|
UseSynchronousIOEnd();
|
|
_request._context.OutputFinish();
|
|
}
|
|
else
|
|
{
|
|
_request._context.Dispose();
|
|
}
|
|
|
|
_request.Dispose();
|
|
}
|
|
WriteLog();
|
|
|
|
}
|
|
void WriteLog()
|
|
{
|
|
var logSetting = LogSetting.Instance();
|
|
if (logSetting.IsWriter)
|
|
{
|
|
if (_logs.Length == 0)
|
|
{
|
|
_logs = System.Buffers.ArrayPool<String>.Shared.Rent(25);
|
|
_logs[0] = "ITME";
|
|
_logSize = 1;
|
|
|
|
}
|
|
|
|
|
|
// IsLog = true;
|
|
int logSize = _logSize;
|
|
|
|
if (this.Token != null)
|
|
{
|
|
var username = this.Token.Username;
|
|
if (String.IsNullOrEmpty(username) || String.Equals(username, "?"))
|
|
{
|
|
username = $"G:{UMC.Data.Utility.IntParse((this.Token.UserId ?? this.Token.Device).Value)}";
|
|
}
|
|
_logs[logSize++] = "Username";
|
|
_logs[logSize++] = username;
|
|
}
|
|
|
|
|
|
var duration = UMC.Data.Reflection.TimeSpanMilli(DateTime.Now) - _request.startTime;
|
|
|
|
_logs[logSize++] = "Address";
|
|
_logs[logSize++] = this.UserHostAddress;
|
|
|
|
_logs[logSize++] = "Server";
|
|
_logs[logSize++] = this.Server;
|
|
|
|
_logs[logSize++] = "UserAgent";
|
|
_logs[logSize++] = this.UserAgent;
|
|
|
|
_logs[logSize++] = "Path";
|
|
_logs[logSize++] = this.RawUrl;
|
|
|
|
_logs[logSize++] = "Duration";
|
|
_logs[logSize++] = duration.ToString();
|
|
|
|
_logs[logSize++] = "Status";
|
|
_logs[logSize++] = this.StatusCode.ToString();
|
|
|
|
if (this.UrlReferrer != null)
|
|
{
|
|
_logs[logSize++] = "Referrer";
|
|
_logs[logSize++] = this.UrlReferrer.AbsoluteUri;
|
|
|
|
}
|
|
logSetting.Write(_logs, 0, logSize);
|
|
System.Buffers.ArrayPool<String>.Shared.Return(_logs);
|
|
|
|
}
|
|
}
|
|
int _logSize = 0;
|
|
String[] _logs = Array.Empty<String>();
|
|
public void WriteLog(String[] logs, int size)
|
|
{
|
|
if (_logs.Length > 0)
|
|
{
|
|
System.Buffers.ArrayPool<String>.Shared.Return(_logs);
|
|
}
|
|
this._logSize = size;
|
|
this._logs = logs;
|
|
}
|
|
// public void WriteLog(String[] logs, int size)
|
|
// {
|
|
// var logSetting = LogSetting.Instance();
|
|
// if (logSetting.IsWriter)
|
|
// {
|
|
// }
|
|
// }
|
|
// public bool IsLog
|
|
// {
|
|
// get; set;
|
|
// }
|
|
public override void Error(Exception ex)
|
|
{
|
|
this.Output.Flush();
|
|
_response.OutputError(ex);
|
|
if (aseSynchronousIOEnd != null)
|
|
{
|
|
UseSynchronousIOEnd();
|
|
|
|
}
|
|
_request._context.Dispose();
|
|
_request.Dispose();
|
|
WriteLog();
|
|
|
|
}
|
|
public override void ReadAsForm(Action<NameValueCollection> action)
|
|
{
|
|
if (_request.ContentLength > 0 && _request.IsMimeFinish == false)
|
|
{
|
|
if (aseSynchronousIOEnd == null)
|
|
{
|
|
aseSynchronousIOEnd = () => { };
|
|
}
|
|
}
|
|
_request.ReadAsForm(a =>
|
|
{
|
|
try
|
|
{
|
|
action(a);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Error(ex);
|
|
|
|
}
|
|
});
|
|
}
|
|
public virtual void ProcessRequest()
|
|
{
|
|
new UMC.ITME.WebServlet().ProcessRequest(this);
|
|
}
|
|
internal protected virtual void ProcessAfter()
|
|
{
|
|
if (this.IsWebSocket == false && aseSynchronousIOEnd == null)
|
|
{
|
|
this.Output.Flush();
|
|
_response.OutputFinish();
|
|
_request._context.OutputFinish();
|
|
|
|
_request.Dispose();
|
|
}
|
|
else
|
|
{
|
|
WriteLog();
|
|
}
|
|
}
|
|
public override int StatusCode
|
|
{
|
|
get
|
|
{
|
|
return this._response.StatusCode;
|
|
}
|
|
set
|
|
{
|
|
this._response.StatusCode = value;
|
|
}
|
|
}
|
|
public override void AddHeader(string name, string value)
|
|
{
|
|
this._response.AddHeader(name, value);
|
|
}
|
|
public override NameValueCollection Headers
|
|
{
|
|
get
|
|
{
|
|
return _request.Headers;
|
|
}
|
|
}
|
|
public override NameValueCollection Cookies
|
|
{
|
|
get
|
|
{
|
|
|
|
return _request.Cookies; ;
|
|
}
|
|
}
|
|
NameValueCollection _QueryString;
|
|
public override NameValueCollection QueryString
|
|
{
|
|
get
|
|
{
|
|
if (_QueryString == null)
|
|
{
|
|
var Query = this.Url.Query;
|
|
if (String.IsNullOrEmpty(Query) == false)
|
|
{
|
|
_QueryString = System.Web.HttpUtility.ParseQueryString(Query);
|
|
}
|
|
else
|
|
{
|
|
_QueryString = new NameValueCollection();
|
|
}
|
|
}
|
|
return _QueryString;
|
|
}
|
|
}
|
|
|
|
public override System.IO.TextWriter Output
|
|
{
|
|
get
|
|
{
|
|
return this.writer;
|
|
}
|
|
}
|
|
public override System.IO.Stream OutputStream
|
|
{
|
|
get
|
|
{
|
|
return this._response.OutputStream;
|
|
}
|
|
}
|
|
|
|
public override string ContentType
|
|
{
|
|
get
|
|
{
|
|
return this._request.ContentType;
|
|
}
|
|
set
|
|
{
|
|
this._response.ContentType = value;
|
|
}
|
|
}
|
|
|
|
public override string UserHostAddress
|
|
{
|
|
get { return this._request.UserHostAddress; }
|
|
}
|
|
|
|
public override string RawUrl
|
|
{
|
|
get { return _request.RawUrl; }
|
|
}
|
|
|
|
public override string UserAgent
|
|
{
|
|
get { return this._request.Headers["User-Agent"]; }
|
|
}
|
|
Uri _Referer;
|
|
public override Uri UrlReferrer
|
|
{
|
|
get
|
|
{
|
|
if (_Referer == null)
|
|
{
|
|
String referer = _request.Headers["Referer"];
|
|
if (String.IsNullOrEmpty(referer) == false)
|
|
{
|
|
try
|
|
{
|
|
_Referer = new Uri(referer);
|
|
}
|
|
catch
|
|
{
|
|
_Referer = new Uri(_request.Url, "/");
|
|
}
|
|
|
|
}
|
|
}
|
|
return _Referer;
|
|
}
|
|
}
|
|
|
|
public override Uri Url
|
|
{
|
|
get { return _request.Url; }
|
|
}
|
|
|
|
public override void Redirect(string url)
|
|
{
|
|
this._response.Redirect(url);
|
|
}
|
|
|
|
public override string HttpMethod
|
|
{
|
|
get { return this._request.HttpMethod; }
|
|
}
|
|
}
|
|
}
|