public override int Read(byte[] buffer, int offset, int length) { if (this.currentField == null && this.fields.Count == 0) { return(-1); } if (this.currentField == null && this.fields.Count > 0) { this.currentField = this.fields.Dequeue(); } int readCount = 0; do { // read from the current stream int count = this.currentField.Read(buffer, offset + readCount, length - readCount); if (count > 0) { readCount += count; } else { // if the current field's stream is empty, go for the next one. // dispose the current one first try { this.currentField.Dispose(); } catch { } // no more fields/streams? exit if (this.fields.Count == 0) { break; } // grab the next one this.currentField = this.fields.Dequeue(); } // exit when we reach the length goal, or there's no more streams to read from } while (readCount < length && this.fields.Count > 0); return(readCount); }
public void AddStreamField(System.IO.Stream stream, string fieldName, string fileName, string mimeType) { var header = new BufferPoolMemoryStream(); header.WriteLine("--" + this.boundary); header.WriteLine("Content-Disposition: form-data; name=\"" + fieldName + "\"" + (!string.IsNullOrEmpty(fileName) ? "; filename=\"" + fileName + "\"" : string.Empty)); // Set up Content-Type head for the form. if (!string.IsNullOrEmpty(mimeType)) { header.WriteLine("Content-Type: " + mimeType); } header.WriteLine("Content-Length: " + stream.Length.ToString()); header.WriteLine(); header.Position = 0; var footer = new BufferPoolMemoryStream(); footer.Write(HTTPRequest.EOL, 0, HTTPRequest.EOL.Length); footer.Position = 0; // all wrapped streams going to be disposed by the StreamList wrapper. var wrapper = new StreamList(header, stream, footer); try { if (this._length >= 0) { this._length += wrapper.Length; } } catch { this._length = -1; } this.fields.Enqueue(wrapper); }
protected override void ThreadFunc(object param) { try { // Step 1 : create a stream with header information // Step 2 : create a stream from the file // Step 3 : create a StreamList // Step 4 : create a HTTPResponse object // Step 5 : call the Receive function of the response object using (System.IO.Stream fs = HTTPManager.IOService.CreateFileStream(this.CurrentRequest.CurrentUri.LocalPath, FileStreamModes.Open)) //using (Stream fs = AndroidFileHelper.GetAPKFileStream(this.CurrentRequest.CurrentUri.LocalPath)) using (StreamList stream = new StreamList(new System.IO.MemoryStream(), fs)) { // This will write to the MemoryStream stream.Write("HTTP/1.1 200 Ok\r\n"); stream.Write("Content-Type: application/octet-stream\r\n"); stream.Write("Content-Length: " + fs.Length.ToString() + "\r\n"); stream.Write("\r\n"); stream.Seek(0, System.IO.SeekOrigin.Begin); base.CurrentRequest.Response = new HTTPResponse(base.CurrentRequest, stream, base.CurrentRequest.UseStreaming, false); if (!CurrentRequest.Response.Receive()) { CurrentRequest.Response = null; } } } catch (Exception ex) { if (CurrentRequest != null) { // Something gone bad, Response must be null! CurrentRequest.Response = null; switch (State) { case HTTPConnectionStates.AbortRequested: CurrentRequest.State = HTTPRequestStates.Aborted; break; case HTTPConnectionStates.TimedOut: CurrentRequest.State = HTTPRequestStates.TimedOut; break; default: CurrentRequest.Exception = ex; CurrentRequest.State = HTTPRequestStates.Error; break; } } } finally { State = HTTPConnectionStates.Closed; if (CurrentRequest.State == HTTPRequestStates.Processing) { if (CurrentRequest.Response != null) { CurrentRequest.State = HTTPRequestStates.Finished; } else { CurrentRequest.State = HTTPRequestStates.Error; } } } }
protected override void ThreadFunc(object param) { try { // Step 1 : create a stream with header information // Step 2 : create a stream from the file // Step 3 : create a StreamList // Step 4 : create a HTTPResponse object // Step 5 : call the Receive function of the response object using (FileStream fs = new FileStream(this.CurrentRequest.CurrentUri.LocalPath, FileMode.Open)) //using (Stream fs = AndroidFileHelper.GetAPKFileStream(this.CurrentRequest.CurrentUri.LocalPath)) using (StreamList stream = new StreamList(new System.IO.MemoryStream(), fs)) { // This will write to the MemoryStream stream.Write("HTTP/1.1 200 Ok\r\n"); stream.Write("Content-Type: application/octet-stream\r\n"); stream.Write("Content-Length: " + fs.Length.ToString() + "\r\n"); stream.Write("\r\n"); stream.Seek(0, System.IO.SeekOrigin.Begin); base.CurrentRequest.Response = new HTTPResponse(base.CurrentRequest, stream, base.CurrentRequest.UseStreaming, false); if (!CurrentRequest.Response.Receive()) CurrentRequest.Response = null; } } catch(Exception ex) { if (CurrentRequest != null) { // Something gone bad, Response must be null! CurrentRequest.Response = null; switch (State) { case HTTPConnectionStates.AbortRequested: CurrentRequest.State = HTTPRequestStates.Aborted; break; case HTTPConnectionStates.TimedOut: CurrentRequest.State = HTTPRequestStates.TimedOut; break; default: CurrentRequest.Exception = ex; CurrentRequest.State = HTTPRequestStates.Error; break; } } } finally { State = HTTPConnectionStates.Closed; if (CurrentRequest.State == HTTPRequestStates.Processing) { if (CurrentRequest.Response != null) CurrentRequest.State = HTTPRequestStates.Finished; else CurrentRequest.State = HTTPRequestStates.Error; } } }
protected override void ThreadFunc(object param) { try { using (FileStream stream = new FileStream(base.CurrentRequest.CurrentUri.LocalPath, FileMode.Open, FileAccess.Read)) { Stream[] streams = new Stream[] { new MemoryStream(), stream }; using (StreamList list = new StreamList(streams)) { list.Write("HTTP/1.1 200 Ok\r\n"); list.Write("Content-Type: application/octet-stream\r\n"); list.Write("Content-Length: " + stream.Length.ToString() + "\r\n"); list.Write("\r\n"); list.Seek(0L, SeekOrigin.Begin); base.CurrentRequest.Response = new HTTPResponse(base.CurrentRequest, list, base.CurrentRequest.UseStreaming, false); if (!base.CurrentRequest.Response.Receive(-1, true)) { base.CurrentRequest.Response = null; } } } } catch (Exception exception) { if (base.CurrentRequest != null) { base.CurrentRequest.Response = null; HTTPConnectionStates state = base.State; if (state != HTTPConnectionStates.AbortRequested) { if (state == HTTPConnectionStates.TimedOut) { goto Label_0139; } goto Label_014A; } base.CurrentRequest.State = HTTPRequestStates.Aborted; } return; Label_0139: base.CurrentRequest.State = HTTPRequestStates.TimedOut; return; Label_014A: base.CurrentRequest.Exception = exception; base.CurrentRequest.State = HTTPRequestStates.Error; } finally { base.State = HTTPConnectionStates.Closed; if (base.CurrentRequest.State == HTTPRequestStates.Processing) { if (base.CurrentRequest.Response != null) { base.CurrentRequest.State = HTTPRequestStates.Finished; } else { base.CurrentRequest.State = HTTPRequestStates.Error; } } } }