#if NETFX using Apewer.Network; using System; using System.Collections.Generic; using System.IO; using System.Web; namespace Apewer.Web { /// 用于网站的服务程序。 public class WebsiteProvider : ApiProvider { private HttpContext context; private HttpRequest request; private HttpResponse response; /// HttpContext public override HttpContext Context { get => context; } /// 创建服务程序实例。 /// public WebsiteProvider(HttpContext context) { if (context == null) throw new ArgumentNullException(nameof(context)); this.context = context; request = context.Request; response = context.Response; } #region Implement /// 写入响应前的检查,返回错误信息。 public override string PreWrite() { // 从 Response 头中移除 Server 和 X-Powered-By 属性。 #if NETFX var keys = new List(response.Headers.AllKeys); #else var keys = new List(response.Headers.Keys); #endif if (keys.Contains("Server")) response.Headers.Remove("Server"); if (keys.Contains("X-Powered-By")) response.Headers.Remove("X-Powered-By"); return null; } /// public override void Sent() => response.Flush(); /// 停止并关闭响应流。 public override void End() => End(false); #endregion #region Request /// 获取 HTTP 方法。 public override Network.HttpMethod GetMethod() => ApiUtility.Method(request.HttpMethod); /// 获取客户端的 IP 地址。 public override string GetClientIP() => request.UserHostAddress; /// 获取请求的 URL。 public override Uri GetUrl() => request.Url; /// 获取请求的 Referrer。 public override string GetReferrer() => request.UrlReferrer == null ? null : request.UrlReferrer.OriginalString; /// 获取请求的头。 public override HttpHeaders GetHeaders() => new HttpHeaders(request.Headers); /// 获取请求的内容类型。 public override string GetContentType() => request.ContentType; /// 获取请求的内容长度。 public override long GetContentLength() => request.ContentLength; /// 获取请求的内容。 public override Stream RequestBody() => request.InputStream; #endregion #region Response /// 设置 HTTP 状态。 public override void SetStatus(int status, int subStatus = 0) { response.StatusCode = status; if (subStatus > 0) response.SubStatusCode = subStatus; } /// 设置响应的头。 public override string SetHeader(string name, string value) { if (response == null || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(value)) return "参数无效。"; try { #if NET20 response.AddHeader(name, value); #else response.Headers.Add(name, value); #endif return null; } catch (Exception ex) { return ex.Message; } } /// 设置响应的缓存秒数,设置为 0 即不允许缓存。 public override void SetCache(int seconds) { if (seconds > 0) { response.CacheControl = "public"; response.Cache.SetCacheability(HttpCacheability.Public); response.Cache.SetMaxAge(TimeSpan.FromSeconds(seconds)); response.Cache.SetProxyMaxAge(TimeSpan.FromSeconds(seconds)); response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); } else { response.CacheControl = "no-cache"; response.Cache.SetCacheability(HttpCacheability.NoCache); response.Cache.SetNoStore(); SetHeader("Pragma", "no-cache"); } } /// 设置响应的缓存秒数。 public override void SetContentType(string value) => response.ContentType = value; /// 设置响应的内容长度。 public override void SetContentLength(long value) => SetHeader("Content-Length", value.ToString()); /// 设置响应重定向。 /// 响应 302 状态。 public override void SetRedirect(string location) { try { response.Redirect(location, true); } catch { } } /// 获取 Response 流。 public override Stream ResponseBody() => response.OutputStream; #endregion #region This /// 停止并关闭响应流。可指定向发送缓冲区的数据。 public void End(bool flush) { try { if (flush) response.Flush(); } catch { } try { response.Close(); } catch { } // try { response.End(); } catch { } } /// 获取 Cookies。 public StringPairs GetCookies() { var sp = new StringPairs(); if (request != null && request.Cookies != null) { #if NETFX foreach (var key in request.Cookies.AllKeys) { try { var cookie = request.Cookies[key]; var value = cookie.Value ?? ""; sp.Add(new KeyValuePair(key, value)); } catch { } } #else foreach (var key in request.Cookies.Keys) { try { var value = request.Cookies[key] ?? ""; sp.Add(new KeyValuePair(key, value)); } catch { } } #endif } return sp; } /// 设置响应的 Cookie。 public static string SetCookie(HttpResponse response, string key, string value) { if (response == null) return null; var k = key.ToTrim(); var v = value.ToTrim(); if (k.IsEmpty()) return "参数 Key 无效。"; try { #if NETFX var cookie = new HttpCookie(k, v); var now = DateTime.Now; cookie.Expires = v.IsEmpty() ? now.AddDays(-1) : now.AddYears(1); response.SetCookie(cookie); #else // var options = new CookieOptions(); // response.Cookies.Append(key, value, options); response.Cookies.Append(key, value); #endif return null; } catch (Exception ex) { return ex.Message; } } internal static void TrimHeaders(HttpApplication context) { context.PreSendRequestHeaders += (s, e) => { var context = HttpContext.Current; if (context == null) return; var response = context.Response; // 从 Response 头中移除 Server 和 X-Powered-By 属性。 var keys = new List(response.Headers.AllKeys); if (keys.Contains("Server")) response.Headers.Remove("Server"); if (keys.Contains("X-Powered-By")) response.Headers.Remove("X-Powered-By"); }; } #endregion } } #endif