/// <summary> /// Writes the message's body to the socket /// </summary> /// <param name="socket"></param> /// <param name="message"></param> internal protected void SendBody(Socket socket, ManualResetEvent abortEvent, HttpMessage message, EventHandler <HttpMessageProgressEventArgs> onProgress, object stateObject) { // bail if there is no message body if (message.Body == null) { return; } // bail if there is a message body, with no length if (message.Body.Length == 0) { return; } /* * In order to properly send the body, we must first determine if the transfer-encoding is chunked or not * */ if (HttpUtils.Contains(message.TransferEncoding, HttpTransferEncodings.Chunked)) { // chunked this.SendBodyChunked(socket, abortEvent, message, onProgress, stateObject); } else { // non-chunked this.SendBodyNonChunked(socket, abortEvent, message, onProgress, stateObject); } }
/// <summary> /// Sends the specified response using the connection's socket /// </summary> /// <param name="response"></param> public virtual void SendResponseToRequest(ref HttpRequest request, ref HttpResponse response) { try { // if the request has asked that the connection be kept-alive, we'll abide by the request if (HttpUtils.Contains(request.Connection, HttpConnections.KeepAlive)) { // so instruct the response to notify the user-agent that the connection will be kept alive response.Connection = HttpConnections.KeepAlive; } Debug.WriteLineIf(_verbose, "Sending response...", MY_TRACE_CATEGORY); Debug.WriteIf(_verbose, response.ToString(false)); // lock the writer lock (_messageWriter) { // send the message _messageWriter.Write(_socket, null, response); } // next check the request to see if the connection should be closed after the response was sent if (HttpUtils.Contains(request.Connection, HttpConnections.Close)) { // yup, they wanted us to close the connection automatically so lets do that now this.Close(); return; } } catch (ThreadAbortException) { } catch (Exception ex) { this.OnException(this, new ExceptionEventArgs(ex)); // throw new Exception(ex.Message, ex); } }