private void finishInitialization10() { var transferEnc = _headers.GetOne("Transfer-Encoding"); if (transferEnc != null) { _context.ErrorMessage = "Invalid Transfer-Encoding header"; return; } if (_httpMethod == "POST") { if (_contentLength == -1) { _context.ErrorMessage = "Content-Length header required"; return; } if (_contentLength == 0) { _context.ErrorMessage = "Invalid Content-Length header"; return; } } }
internal WebHeaderCollection WriteHeadersTo(MemoryStream destination) { var headers = new WebHeaderCollection(HttpHeaderType.Response, true); if (_headers != null) { headers.Add(_headers); } if (_contentType != null) { var type = _contentType.IndexOf("charset=", StringComparison.Ordinal) == -1 && _contentEncoding != null ? String.Format("{0}; charset={1}", _contentType, _contentEncoding.WebName) : _contentType; headers.InternalSet("Content-Type", type, true); } if (headers.GetOne("Server") == null) { headers.InternalSet("Server", "websocket-sharp/1.0", true); } var prov = CultureInfo.InvariantCulture; if (headers.GetOne("Date") == null) { headers.InternalSet("Date", DateTime.UtcNow.ToString("r", prov), true); } if (!_sendChunked) { headers.InternalSet("Content-Length", _contentLength.ToString(prov), true); } else { headers.InternalSet("Transfer-Encoding", "chunked", true); } /* * Apache forces closing the connection for these status codes: * - 400 Bad Request * - 408 Request Timeout * - 411 Length Required * - 413 Request Entity Too Large * - 414 Request-Uri Too Long * - 500 Internal Server Error * - 503 Service Unavailable */ var closeConn = !_context.Request.KeepAlive || !_keepAlive || _statusCode == 400 || _statusCode == 408 || _statusCode == 411 || _statusCode == 413 || _statusCode == 414 || _statusCode == 500 || _statusCode == 503; var reuses = _context.Connection.Reuses; if (closeConn || reuses >= 100) { headers.InternalSet("Connection", "close", true); } else { headers.InternalSet( "Keep-Alive", String.Format("timeout=15,max={0}", 100 - reuses), true); if (_context.Request.ProtocolVersion < HttpVersion.Version11) { headers.InternalSet("Connection", "keep-alive", true); } } if (_location != null) { headers.InternalSet("Location", _location, true); } if (_cookies != null) { foreach (Cookie cookie in _cookies) { headers.InternalSet("Set-Cookie", cookie.ToResponseString(), true); } } var enc = _contentEncoding ?? Encoding.Default; var writer = new StreamWriter(destination, enc, 256); writer.Write("HTTP/{0} {1} {2}\r\n", _version, _statusCode, _statusDescription); writer.Write(headers.ToStringMultiValue(true)); writer.Flush(); // Assumes that the destination was at position 0. destination.Position = enc.GetPreamble().Length; return(headers); }