本文整理汇总了C#中System.Net.HttpListenerContext.RequestHeader方法的典型用法代码示例。如果您正苦于以下问题:C# HttpListenerContext.RequestHeader方法的具体用法?C# HttpListenerContext.RequestHeader怎么用?C# HttpListenerContext.RequestHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.HttpListenerContext
的用法示例。
在下文中一共展示了HttpListenerContext.RequestHeader方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandleGet
private bool HandleGet(HttpListenerContext context, WebServer server, bool sendBuffer = true)
{
var urlPath = context.Request.Url.LocalPath.Replace('/', Path.DirectorySeparatorChar);
// adjust the path to see if we've got a default document
if (urlPath.Last() == Path.DirectorySeparatorChar)
urlPath = urlPath + DefaultDocument;
urlPath = urlPath.TrimStart(new char[] {Path.DirectorySeparatorChar});
var localPath = Path.Combine(FileSystemPath, urlPath);
var eTagValid = false;
byte[] buffer = null;
var fileDate = DateTime.Today;
var partialHeader = context.RequestHeader(Constants.HeaderRange);
var usingPartial = String.IsNullOrWhiteSpace(partialHeader) == false && partialHeader.StartsWith("bytes=");
if (string.IsNullOrWhiteSpace(DefaultExtension) == false && DefaultExtension.StartsWith(".") &&
File.Exists(localPath) == false)
{
var newPath = localPath + DefaultExtension;
if (File.Exists(newPath))
localPath = newPath;
}
if (File.Exists(localPath) == false) return false;
if (usingPartial == false)
{
fileDate = File.GetLastWriteTime(localPath);
var requestHash = context.RequestHeader(Constants.HeaderIfNotMatch);
if (RamCache.ContainsKey(localPath) && RamCache[localPath].LastModified == fileDate)
{
server.Log.DebugFormat("RAM Cache: {0}", localPath);
var currentHash = Extensions.ComputeMd5Hash(RamCache[localPath].Buffer) + '-' + fileDate.Ticks;
if (String.IsNullOrWhiteSpace(requestHash) || requestHash != currentHash)
{
buffer = RamCache[localPath].Buffer;
context.Response.AddHeader(Constants.HeaderETag, currentHash);
}
else
{
eTagValid = true;
}
}
else
{
server.Log.DebugFormat("File System: {0}", localPath);
if (sendBuffer)
{
buffer = File.ReadAllBytes(localPath);
var currentHash = Extensions.ComputeMd5Hash(buffer) + '-' + fileDate.Ticks;
if (String.IsNullOrWhiteSpace(requestHash) || requestHash != currentHash)
{
if (UseRamCache && buffer.Length <= MaxRamCacheFileSize)
{
RamCache[localPath] = new RamCacheEntry() {LastModified = fileDate, Buffer = buffer};
}
context.Response.AddHeader(Constants.HeaderETag, currentHash);
}
else
{
eTagValid = true;
}
}
}
}
// check to see if the file was modified or etag is the same
var utcFileDateString = fileDate.ToUniversalTime()
.ToString(Constants.BrowserTimeFormat, CultureInfo.InvariantCulture);
if (usingPartial == false &&
(eTagValid || context.RequestHeader(Constants.HeaderIfModifiedSince).Equals(utcFileDateString)))
{
context.Response.AddHeader(Constants.HeaderCacheControl, "private");
context.Response.AddHeader(Constants.HeaderPragma, string.Empty);
context.Response.AddHeader(Constants.HeaderExpires, string.Empty);
context.Response.ContentType = string.Empty;
context.Response.StatusCode = 304;
}
else
{
var extension = Path.GetExtension(localPath).ToLowerInvariant();
if (MimeTypes.ContainsKey(extension))
context.Response.ContentType = MimeTypes[extension];
context.Response.AddHeader(Constants.HeaderCacheControl, "private");
context.Response.AddHeader(Constants.HeaderPragma, string.Empty);
context.Response.AddHeader(Constants.HeaderExpires, string.Empty);
context.Response.AddHeader(Constants.HeaderLastModified, utcFileDateString);
context.Response.AddHeader(Constants.HeaderAcceptRanges, "bytes");
if (sendBuffer)
{
//.........这里部分代码省略.........
示例2: HandleGet
private bool HandleGet(HttpListenerContext context, WebServer server, bool sendBuffer = true)
{
var rootFs = FileSystemPath;
var urlPath = GetUrlPath(context, ref rootFs);
var localPath = Path.Combine(rootFs, urlPath);
var eTagValid = false;
Stream buffer = null;
var partialHeader = context.RequestHeader(Constants.HeaderRange);
var usingPartial = string.IsNullOrWhiteSpace(partialHeader) == false && partialHeader.StartsWith("bytes=");
if (ExistsLocalPath(urlPath, ref localPath) == false) return false;
var fileDate = File.GetLastWriteTime(localPath);
var requestHash = context.RequestHeader(Constants.HeaderIfNotMatch);
if (RamCache.ContainsKey(localPath) && RamCache[localPath].LastModified == fileDate)
{
#if COMPAT
server.Log.DebugFormat("RAM Cache: {0}", localPath);
#else
$"RAM Cache: {localPath}".Debug();
#endif
var currentHash = RamCache[localPath].Buffer.ComputeMD5().ToUpperHex() + '-' + fileDate.Ticks;
if (string.IsNullOrWhiteSpace(requestHash) || requestHash != currentHash)
{
buffer = new MemoryStream(RamCache[localPath].Buffer);
context.Response.AddHeader(Constants.HeaderETag, currentHash);
}
else
{
eTagValid = true;
}
}
else
{
#if COMPAT
server.Log.DebugFormat("File System: {0}", localPath);
#else
$"File System: {localPath}".Debug();
#endif
if (sendBuffer)
{
buffer = new FileStream(localPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
if (usingPartial == false)
{
eTagValid = UpdateFileCache(context, buffer, fileDate, requestHash, localPath);
}
}
}
// check to see if the file was modified or e-tag is the same
var utcFileDateString = fileDate.ToUniversalTime()
.ToString(Constants.BrowserTimeFormat, Constants.StandardCultureInfo);
if (usingPartial == false &&
(eTagValid || context.RequestHeader(Constants.HeaderIfModifiedSince).Equals(utcFileDateString)))
{
SetStatusCode304(context);
return true;
}
SetHeaders(context, localPath, utcFileDateString);
var fileSize = new FileInfo(localPath).Length;
if (sendBuffer == false)
{
context.Response.ContentLength64 = buffer?.Length ?? fileSize;
return true;
}
// If buffer is null something is really wrong
if (buffer == null) return false;
var lowerByteIndex = 0;
var upperByteIndex = 0;
long byteLength;
var isPartial = usingPartial &&
CalculateRange(partialHeader, fileSize, out lowerByteIndex, out upperByteIndex);
if (isPartial)
{
if (upperByteIndex > fileSize)
{
context.Response.StatusCode = 416;
context.Response.AddHeader(Constants.HeaderContentRanges,
$"bytes */{fileSize}");
return true;
}
if (upperByteIndex == fileSize)
{
byteLength = buffer.Length;
}
//.........这里部分代码省略.........