Beispiel #1
0
        internal static void WriteStatusLine(StreamWriter writer, HttpResponse response)
        {
            const string statusLineFormat = "HTTP/{0} {1} {2}";

            writer.WriteLine(statusLineFormat,
                response.ProtocolVersion,
                response.StatusCode,
                HttpReasonPhrase.GetStatusDescription(response.StatusCode));
            writer.Flush();
        }
Beispiel #2
0
        internal static void WriteHeaders(StreamWriter writer, HttpResponse response)
        {
            writer.WriteLine("Date: {0}", DateTime.Now.ToUniversalTime().ToString("R"));
            writer.WriteLine("Server: ShareIT");

            foreach (var header in response.Headers.AllKeys)
            {
                var value = response.Headers.Get(header);
                if (string.IsNullOrEmpty(value)) continue;
                writer.WriteLine("{0}: {1}", header, value);
            }

            writer.WriteLine();
            writer.Flush();
        }
Beispiel #3
0
        private void AcceptTcpClient(IAsyncResult asyncResult)
        {
            var l = asyncResult.AsyncState as TcpListener;
            if (l == null) return;

            TcpClient client;
            try
            {
                client = l.EndAcceptTcpClient(asyncResult);

                ThreadPool.QueueUserWorkItem(tcpClient =>
                {
                    HttpResponse response;

                    try
                    {
                        var context = new HttpContext(client);

                        if (RequestReceived != null)
                        {
                            RequestReceived(this, context);

                            if (!context.IsHandled)
                            {
                                context.Response.StatusCode = (int) HttpStatusCode.NotFound;
                            }
                        }

                        response = context.Response;
                    }
                    catch (HttpParseException e)
                    {
                        Logger.Error(e, "QueueUserWorkItem: Caught parsing exception with return code {0}", e.ReturnCode);
                        response = new HttpResponse(e.ReturnCode);
                    }
                    catch (Exception e)
                    {
                        Logger.Error(e, "QueueUserWorkItem: Caught unexpected exception.");
                        client.Close();
                        return;
                    }

                    if (response != null)
                    {
                        WriteResponse(response);
                    }

                    client.Close();
                }, client);
            }
            catch
            {
                // ignored
            }
            finally
            {
                l.BeginAcceptTcpClient(AcceptTcpClient, l);
            }
        }
Beispiel #4
0
        internal static void WriteContent(HttpResponse response)
        {
            if (response.OutputStream == null)
            {
                throw new ArgumentException("The underlying stream of response is null.", "response");
            }

            if (!response.OutputStream.CanRead)
            {
                throw new ArgumentException("The underlying stream of response cannot be read.", "response");
            }

            using (var reader = new BinaryReader(response.OutputStream))
            using (var writer = new BinaryWriter(response.UnderlyingStream))
            {
                var buffer = new byte[response.BlockSize];
                while (true)
                {
                    var read = reader.Read(buffer, 0, response.BlockSize);

                    if (read > 0)
                    {
                        writer.Write(buffer, 0, read);
                        writer.Flush();

                        if (response.ContentTransferredChanged != null)
                        {
                            response.ContentTransferredChanged(read);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Beispiel #5
0
        private void WriteResponse(HttpResponse response)
        {
            try
            {
                var stream = response.UnderlyingStream;

                if (stream == null)
                {
                    throw new ArgumentException("The underlying stream of response is null.", "response");
                }
                if (!stream.CanWrite)
                {
                    throw new ArgumentException("The underlying stream of response cannot be written.", "response");
                }

                var encoding = new UTF8Encoding(false);
                using (var writer = new StreamWriter(stream, encoding) { NewLine = "\r\n", AutoFlush = true })
                {
                    HttpResponseWriter.WriteStatusLine(writer, response);
                    HttpResponseWriter.WriteHeaders(writer, response);

                    if (response.TransferBegan != null)
                    {
                        response.TransferBegan();
                    }

                    if (response.ContentLength64 != -1)
                    {
                        HttpResponseWriter.WriteContent(response);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, "WriteResponse: Caught unexpected exception.");
            }

            if (response.TransferEnded != null)
            {
                try
                {
                    response.TransferEnded();
                }
                catch (Exception e)
                {
                    Logger.Error(e, "WriteResponse: TransferEnded: Caught unexpected exception.");
                }
            }
        }