Files
TouchSocket/RRQMSocket.Http/Control/HttpResponse.cs
御风踏青岚 78335d76f8 合并项目
2021-05-07 08:21:06 +08:00

152 lines
4.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//------------------------------------------------------------------------------
// 此代码版权归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议若本仓库没有设置则按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.Text;
namespace RRQMSocket.Http
{
/// <summary>
/// Http响应
/// </summary>
public class HttpResponse : BaseHeader
{
/// <summary>
/// 构造函数
/// </summary>
public HttpResponse()
{
this.Headers = new Dictionary<string, string>();
}
/// <summary>
/// 状态码
/// </summary>
public string StatusCode { get; set; }
/// <summary>
/// 内容
/// </summary>
public byte[] Content { get; private set; }
/// <summary>
/// 设置内容
/// </summary>
/// <param name="content"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public HttpResponse SetContent(byte[] content, Encoding encoding = null)
{
this.Content = content;
this.Encoding = encoding != null ? encoding : Encoding.UTF8;
this.Content_Length = content.Length;
return this;
}
/// <summary>
/// 设置内容
/// </summary>
/// <param name="content"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public HttpResponse SetContent(string content, Encoding encoding = null)
{
//初始化内容
encoding = encoding != null ? encoding : Encoding.UTF8;
return SetContent(encoding.GetBytes(content), encoding);
}
/// <summary>
/// 获取头数据
/// </summary>
/// <param name="header"></param>
/// <returns></returns>
public string GetHeader(ResponseHeader header)
{
return GetHeaderByKey(header);
}
/// <summary>
/// 获取头数据
/// </summary>
/// <param name="fieldName"></param>
/// <returns></returns>
public string GetHeader(string fieldName)
{
return GetHeaderByKey(fieldName);
}
/// <summary>
/// 设置头数据
/// </summary>
/// <param name="header"></param>
/// <param name="value"></param>
public void SetHeader(ResponseHeader header, string value)
{
SetHeaderByKey(header, value);
}
/// <summary>
/// 设置头数据
/// </summary>
/// <param name="fieldName"></param>
/// <param name="value"></param>
public void SetHeader(string fieldName, string value)
{
SetHeaderByKey(fieldName, value);
}
/// <summary>
/// 构建响应头部
/// </summary>
/// <returns></returns>
private void BuildHeader(ByteBlock byteBlock)
{
StringBuilder stringBuilder = new StringBuilder();
if (!string.IsNullOrEmpty(StatusCode))
stringBuilder.AppendLine($"HTTP/1.1 {StatusCode}");
if (!string.IsNullOrEmpty(this.Content_Type))
stringBuilder.AppendLine("Content-Type: " + this.Content_Type);
if (this.Content_Length > 0)
stringBuilder.AppendLine("Content-Length: " + this.Content_Length);
foreach (var headerkey in this.Headers.Keys)
{
stringBuilder.Append($"{headerkey}: ");
stringBuilder.AppendLine(this.Headers[headerkey]);
}
stringBuilder.AppendLine($"Server: RRQMSocket.Http {ServerVersion}");
stringBuilder.AppendLine("Date: " + DateTime.Now.ToGMTString("r"));
stringBuilder.AppendLine();
byteBlock.Write(Encoding.UTF8.GetBytes(stringBuilder.ToString()));
}
private void BuildContent(ByteBlock byteBlock)
{
if (this.Content_Length > 0)
{
byteBlock.Write(this.Content);
}
}
/// <summary>
/// 构建响应数据
/// </summary>
/// <param name="byteBlock"></param>
public void Build(ByteBlock byteBlock)
{
BuildHeader(byteBlock);
BuildContent(byteBlock);
}
}
}