//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在RRQMCore.XREF命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using RRQMCore.ByteManager;
using RRQMCore.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace RRQMSocket.Http
{
///
/// Http基础头部
///
public abstract class HttpBase
{
///
/// 构造函数
///
public HttpBase()
{
this.headers = new Dictionary();
}
///
/// 服务器版本
///
public static readonly string ServerVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
private string requestLine;
///
/// 字符数据
///
public string Body { get { return this.content == null ? null : this.encoding.GetString(this.content); } }
private byte[] content;
///
/// 内容器
///
public byte[] Content
{
get { return content; }
}
///
/// 内容编码
///
public string Content_Encoding { get; set; }
///
/// 内容长度
///
public int Content_Length { get; set; }
///
/// 内容类型
///
public string Content_Type { get; set; }
///
/// 内容语言
///
public string ContentLanguage { get; set; }
private Encoding encoding = Encoding.UTF8;
///
/// 编码方式
///
public Encoding Encoding
{
get { return encoding; }
set { encoding = value; }
}
///
/// 传递标识
///
public object Flag { get; set; }
///
/// 请求头集合
///
public Dictionary Headers { get { return this.headers; } }
private Dictionary headers;
///
/// 协议名称
///
public string Protocols { get; set; }
///
/// HTTP协议版本
///
public string ProtocolVersion { get; set; } = "1.1";
///
/// 请求行
///
public string RequestLine
{
get { return requestLine; }
}
///
/// 构建数据
///
///
public abstract void Build(ByteBlock byteBlock);
///
/// 获取头值
///
///
///
public string GetHeader(HttpHeaders header)
{
return GetHeaderByKey(header);
}
///
/// 读取信息
///
public abstract void ReadFromBase();
///
/// 从内存中读取
///
///
///
///
public void ReadHeaders(byte[] buffer, int offset, int length)
{
string data = Encoding.UTF8.GetString(buffer, offset, length);
string[] rows = Regex.Split(data, Environment.NewLine);
//Request URL & Method & Version
this.requestLine = rows[0];
//Request Headers
GetRequestHeaders(rows);
string contentLength = this.GetHeader(HttpHeaders.ContentLength);
int.TryParse(contentLength, out int content_Length);
this.Content_Length = content_Length;
}
///
/// 获取头集合的值
///
///
///
protected string GetHeaderByKey(Enum header)
{
var fieldName = header.GetDescription();
if (fieldName == null) return null;
var hasKey = Headers.ContainsKey(fieldName);
if (!hasKey) return null;
return Headers[fieldName];
}
///
/// 获取头集合的值
///
///
///
protected string GetHeaderByKey(string fieldName)
{
if (string.IsNullOrEmpty(fieldName)) return null;
var hasKey = Headers.ContainsKey(fieldName);
if (!hasKey) return null;
return Headers[fieldName];
}
///
/// 设置头值
///
protected void SetHeaderByKey(Enum header, string value)
{
var fieldName = header.GetDescription();
if (fieldName == null) return;
var hasKey = Headers.ContainsKey(fieldName);
if (!hasKey) Headers.Add(fieldName, value);
Headers[fieldName] = value;
}
///
/// 设置头值
///
///
///
protected void SetHeaderByKey(string fieldName, string value)
{
if (string.IsNullOrEmpty(fieldName)) return;
var hasKey = Headers.ContainsKey(fieldName);
if (!hasKey) Headers.Add(fieldName, value);
Headers[fieldName] = value;
}
private void GetRequestHeaders(IEnumerable rows)
{
this.headers.Clear();
if (rows == null || rows.Count() <= 0)
{
return;
}
foreach (var item in rows)
{
string[] kv = item.SplitFirst(':');
if (kv.Length == 2)
{
if (!this.headers.ContainsKey(kv[0]))
{
this.headers.Add(kv[0], kv[1]);
}
}
}
}
///
/// 设置内容
///
///
///
public void SetContent(byte[] content)
{
this.content = content;
this.Content_Length = content.Length;
}
///
/// 设置内容
///
///
///
///
public void SetContent(string content, Encoding encoding = null)
{
//初始化内容
encoding = encoding != null ? encoding : Encoding.UTF8;
SetContent(encoding.GetBytes(content));
}
}
}