protected override void ParseResponse() { switch (_response.StatusLine.StatusCode) { case 200: if (_response.Body.ReceiveStream.GetType() != typeof(Networking.Protocols.Http.HttpNetworkStream)) throw new OpenDMS.Networking.Protocols.Http.HttpNetworkStreamException("Invalid stream type."); Logger.Storage.Debug("Received a successful response from CouchDB."); ResponseMessage = _200; ContentType = _response.ContentType; if (!_response.ContentLength.HasValue) throw new Http.Message.HeaderException("Content-Length header does not exist."); Length = (ulong)_response.ContentLength.Value; Stream = (Networking.Protocols.Http.HttpNetworkStream)_response.Body.ReceiveStream; Ok = true; Logger.Storage.Debug("GetAttachmentReply loaded."); break; case 404: Logger.Storage.Debug("Received a failure response from CouchDB: " + _404); ResponseMessage = _404; Ok = false; Logger.Storage.Debug("GetAttachmentReply loaded."); break; default: Logger.Storage.Error("GetAttachmentReply received an unknown response code: " + _response.StatusLine.StatusCode.ToString()); Ok = false; throw new UnsupportedException("The response code " + _response.StatusLine.StatusCode.ToString() + " is not supported."); } }
protected override void ParseAndAttachToBody_Callback(Tcp.TcpConnection sender, Tcp.TcpConnectionAsyncEventArgs e) { HttpNetworkStream ns; AsyncCallback callback = (AsyncCallback)e.UserToken; AppendAndParse(e.Buffer, 0, e.Length); if (AllHeadersReceived) { if (!Request.ContentLength.HasValue) { throw new HttpNetworkStreamException("A Content-Length header was not found."); } ulong temp = (ulong)Request.ContentLength.Value; if (_remainingBufferAppendPosition > 0) { // We need to take the left over buffer from _responseBuilder and prepend that // to an HttpNetworkStream wrapping the _tcpConnection and then give the user // that HttpNetworkStream... cake byte[] newBuffer = new byte[_remainingBufferAppendPosition]; BytesReceived += e.BytesTransferred - newBuffer.Length; MessageSize = BytesReceived + Request.ContentLength.Value; Buffer.BlockCopy(_remainingBuffer, 0, newBuffer, 0, newBuffer.Length); ns = new HttpNetworkStream(HttpNetworkStream.DirectionType.Upload, temp, newBuffer, sender.Socket, System.IO.FileAccess.Write, false); } else { BytesReceived += e.BytesTransferred; MessageSize = BytesReceived + Response.ContentLength.Value; ns = new HttpNetworkStream(HttpNetworkStream.DirectionType.Upload, temp, sender.Socket, System.IO.FileAccess.Write, false); } if (Request.Headers.ContainsKey(new Message.ChunkedTransferEncodingHeader())) { Request.Body.IsChunked = true; } Response.Body.ReceiveStream = ns; callback(this, Response); } else { BytesReceived += e.Length; sender.ReceiveAsync(ParseAndAttachToBody_Callback, callback); } }
protected override void ParseResponse() { switch (_response.StatusLine.StatusCode) { case 200: if (_response.Body.ReceiveStream.GetType() != typeof(Networking.Protocols.Http.HttpNetworkStream)) { throw new OpenDMS.Networking.Protocols.Http.HttpNetworkStreamException("Invalid stream type."); } Logger.Storage.Debug("Received a successful response from CouchDB."); ResponseMessage = _200; ContentType = _response.ContentType; if (!_response.ContentLength.HasValue) { throw new Http.Message.HeaderException("Content-Length header does not exist."); } Length = (ulong)_response.ContentLength.Value; Stream = (Networking.Protocols.Http.HttpNetworkStream)_response.Body.ReceiveStream; Ok = true; Logger.Storage.Debug("GetAttachmentReply loaded."); break; case 404: Logger.Storage.Debug("Received a failure response from CouchDB: " + _404); ResponseMessage = _404; Ok = false; Logger.Storage.Debug("GetAttachmentReply loaded."); break; default: Logger.Storage.Error("GetAttachmentReply received an unknown response code: " + _response.StatusLine.StatusCode.ToString()); Ok = false; throw new UnsupportedException("The response code " + _response.StatusLine.StatusCode.ToString() + " is not supported."); } }
protected override void ParseAndAttachToBody_Callback(Tcp.TcpConnection sender, Tcp.TcpConnectionAsyncEventArgs e) { HttpNetworkStream ns; byte[] newBuffer = null; AsyncCallback callback = (AsyncCallback)e.UserToken; AppendAndParse(e.Buffer, 0, e.BytesTransferred); if (AllHeadersReceived) { // Sets up the buffer to prepend if (_remainingBufferAppendPosition > 0) { // We need to take the left over buffer from _responseBuilder and prepend that // to an HttpNetworkStream wrapping the _tcpConnection and then give the user // that HttpNetworkStream... cake newBuffer = new byte[_remainingBufferAppendPosition]; BytesReceived += e.BytesTransferred - newBuffer.Length; Buffer.BlockCopy(_remainingBuffer, 0, newBuffer, 0, newBuffer.Length); } else { newBuffer = null; } if (!Response.ContentLength.HasValue) { // If content length is not set if (!Response.Headers.ContainsKey(new Message.ChunkedTransferEncodingHeader())) { throw new HttpNetworkStreamException("A Content-Length header was not found."); } MessageSize = 0; // NetworkStream needs modified to handle undetermined length ns = new HttpNetworkStream(HttpNetworkStream.DirectionType.Download, newBuffer, sender.Socket, System.IO.FileAccess.Read, false); Response.Body.IsChunked = true; Response.Body.ReceiveStream = new Interceptors.InterceptorStream(new Interceptors.ChunkedEncodingInterceptor(ns)); } else { // Content length is set if (Response.Headers.ContainsKey(new Message.ChunkedTransferEncodingHeader())) { throw new HttpNetworkStreamException("A Content-Length header was found in a chunked transfer."); } ulong temp = (ulong)Response.ContentLength.Value; MessageSize = BytesReceived + Response.ContentLength.Value; ns = new HttpNetworkStream(HttpNetworkStream.DirectionType.Download, temp, newBuffer, sender.Socket, System.IO.FileAccess.Read, false); Response.Body.ReceiveStream = ns; } callback(this, Response); } else { BytesReceived += e.BytesTransferred; sender.ReceiveAsync(ParseAndAttachToBody_Callback, callback); } }
protected override void ParseAndAttachToBody_Callback(Tcp.TcpConnection sender, Tcp.TcpConnectionAsyncEventArgs e) { HttpNetworkStream ns; AsyncCallback callback = (AsyncCallback)e.UserToken; AppendAndParse(e.Buffer, 0, e.Length); if (AllHeadersReceived) { if (!Request.ContentLength.HasValue) throw new HttpNetworkStreamException("A Content-Length header was not found."); ulong temp = (ulong)Request.ContentLength.Value; if (_remainingBufferAppendPosition > 0) { // We need to take the left over buffer from _responseBuilder and prepend that // to an HttpNetworkStream wrapping the _tcpConnection and then give the user // that HttpNetworkStream... cake byte[] newBuffer = new byte[_remainingBufferAppendPosition]; BytesReceived += e.BytesTransferred - newBuffer.Length; MessageSize = BytesReceived + Request.ContentLength.Value; Buffer.BlockCopy(_remainingBuffer, 0, newBuffer, 0, newBuffer.Length); ns = new HttpNetworkStream(HttpNetworkStream.DirectionType.Upload, temp, newBuffer, sender.Socket, System.IO.FileAccess.Write, false); } else { BytesReceived += e.BytesTransferred; MessageSize = BytesReceived + Response.ContentLength.Value; ns = new HttpNetworkStream(HttpNetworkStream.DirectionType.Upload, temp, sender.Socket, System.IO.FileAccess.Write, false); } if (Request.Headers.ContainsKey(new Message.ChunkedTransferEncodingHeader())) Request.Body.IsChunked = true; Response.Body.ReceiveStream = ns; callback(this, Response); } else { BytesReceived += e.Length; sender.ReceiveAsync(ParseAndAttachToBody_Callback, callback); } }