private TchResponseInfo _OnResourceRequest(TchRequestInfo request_info) { try { using (Frame frame = new Frame(request_info.RawBytes)) { var context = applicaition_.CreateContext(frame); applicaition_.ProcessRequestAsync(context).Wait(); var response_info = frame.GetResponseInfo(); return(response_info); } } catch (Exception ex) { TchResponseInfo tch_response_info = new TchResponseInfo(); tch_response_info.StatusCode = 200; tch_response_info.StatusText = ReasonPhrases.ToStatusPhrase(tch_response_info.StatusCode); tch_response_info.MimeType = "text/html"; tch_response_info.Body = Encoding.UTF8.GetBytes(ex.GetLast().Message); tch_response_info.Headers.Add("Content-Type", "text/html; charset=utf-8"); tch_response_info.Headers.Add("Content-Length", tch_response_info.Body.Length.ToString()); return(tch_response_info); } }
public TchResponseInfo GetResponseInfo() { this.FireOnStarting().Wait(); this.FireOnCompleted().Wait(); var info = new TchResponseInfo(); info.StatusCode = this.StatusCode; info.StatusText = ReasonPhrases.ToStatus(StatusCode, ReasonPhrase); var hasConnection = false; var hasTransferEncoding = false; var hasContentLength = false; foreach (var header in this.ResponseHeaders) { var isConnection = false; if (!hasConnection && string.Equals(header.Key, "Connection", StringComparison.OrdinalIgnoreCase)) { hasConnection = isConnection = true; } else if (!hasTransferEncoding && string.Equals(header.Key, "Transfer-Encoding", StringComparison.OrdinalIgnoreCase)) { hasTransferEncoding = true; } else if (!hasContentLength && string.Equals(header.Key, "Content-Length", StringComparison.OrdinalIgnoreCase)) { hasContentLength = true; } foreach (var value in header.Value) { info.Headers.Add(header.Key, value); if (isConnection && value.IndexOf("close", StringComparison.OrdinalIgnoreCase) != -1) { _keepAlive = false; } } } //if (_keepAlive && !hasTransferEncoding && !hasContentLength) //{ // if (HttpVersion == "HTTP/1.1") // { // _autoChunk = true; // response_builder.Append("Transfer-Encoding: chunked\r\n"); // } // else // { // _keepAlive = false; // } //} info.Headers.Add("Content-Length", this._responseStream.Length.ToString()); if (_keepAlive == false && hasConnection == false && HttpVersion == "HTTP/1.1") { info.Headers.Add("Connection", "close"); } else if (_keepAlive && hasConnection == false && HttpVersion == "HTTP/1.0") { info.Headers.Add("Connection", "keep-alive"); } if (this.ResponseHeaders.ContainsKey("Content-Type")) { info.MimeType = this.ResponseHeaders["Content-Type"]; int i = info.MimeType.IndexOf(';'); if (i > -1) { info.MimeType = info.MimeType.Substring(0, i); } } else { info.MimeType = "text/html"; } byte[] body_bytes = new byte[0]; if (this._responseStream.Length > 0) { body_bytes = new byte[this._responseStream.Length]; this._responseStream.Position = 0; this._responseStream.Read(body_bytes, 0, body_bytes.Length); } info.Body = body_bytes; return(info); }