private void ReturnResponse(HttpRequestHeader request, int responseCode, string message, string method, HttpVersion version, IEnumerable<KeyDataPair<string>> sendHeaders, DataAdapterToStream stm) { if (request != null) { FlushRequest(request); } HttpResponseDataChunk response = new HttpResponseDataChunk(); if (_config.Version10Proxy && !version.IsVersionUnknown) { response.Version = HttpVersion.Version10; } else { response.Version = version; } response.ResponseCode = responseCode; response.Message = message; response.FinalChunk = true; response.Body = new byte[0]; List<KeyDataPair<string>> headers = new List<KeyDataPair<string>>(sendHeaders); headers.Add(new KeyDataPair<string>("X-Proxy-Server", "CANAPE")); if (response.Body.Length > 0) { headers.Add(new KeyDataPair<string>("Content-Type", "text/html")); } response.Headers = headers.ToArray(); if (method.Equals("HEAD", StringComparison.OrdinalIgnoreCase)) { response.HeadResponse = true; } else if (method.Equals("CONNECT", StringComparison.OrdinalIgnoreCase)) { response.ConnectResponse = true; } response.WriteChunk(new DataWriter(stm)); }
private void Write(HttpResponseDataChunk chunk) { // Add proxy specific headers if (chunk.ChunkNumber == 0) { ProcessProxyResponseHeaders(chunk); lock (_requestQueue) { HttpRequestHeader request = _requestQueue.Dequeue(); // Fix up version to match the request if (_server._config.Version10Proxy && !request.Version.IsVersionUnknown) { chunk.Version = HttpVersion.Version10; } else { chunk.Version = request.Version; } if (!chunk.Version.IsVersion11) { // Remove chunked encoding for sending back 1.0 _removeChunked = true; _closeConnection = true; } else { // If not chunk encoding and no content-length then set close of end if (!chunk.ChunkedEncoding) { bool hasContentLength = false; foreach (KeyDataPair<string> header in chunk.Headers) { if (header.Name.Equals("content-length", StringComparison.OrdinalIgnoreCase)) { hasContentLength = true; break; } } if (!hasContentLength) { List<KeyDataPair<string>> headers = new List<KeyDataPair<string>>(chunk.Headers); headers.Add(new KeyDataPair<string>("Connection", "close")); chunk.Headers = headers.ToArray(); _closeConnection = true; } } } } } if (_removeChunked) { chunk.ChunkedEncoding = false; } chunk.WriteChunk(_writer); if (chunk.FinalChunk) { if (_closeConnection) { Close(); } } }