Example #1
0
        private void ReturnResponse(HttpRequestHeader request, int responseCode, string message,
                                    string method, HttpVersion version, IEnumerable <HttpHeader> 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 <HttpHeader> headers = new List <HttpHeader>(sendHeaders);

            headers.Add(new HttpHeader("X-Proxy-Server", "CANAPE"));

            if (response.Body.Length > 0)
            {
                headers.Add(new HttpHeader("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));
        }
Example #2
0
            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 (HttpHeader header in chunk.Headers)
                                {
                                    if (header.Name.Equals("content-length", StringComparison.OrdinalIgnoreCase))
                                    {
                                        hasContentLength = true;
                                        break;
                                    }
                                }

                                if (!hasContentLength)
                                {
                                    List <HttpHeader> headers = new List <HttpHeader>(chunk.Headers);
                                    headers.Add(new HttpHeader("Connection", "close"));
                                    chunk.Headers    = headers.ToArray();
                                    _closeConnection = true;
                                }
                            }
                        }
                    }
                }

                if (_removeChunked)
                {
                    chunk.ChunkedEncoding = false;
                }

                chunk.WriteChunk(_writer);

                if (chunk.FinalChunk)
                {
                    if (_closeConnection)
                    {
                        Dispose();
                    }
                }
            }