public void MultiFileUploadInMixedModeNoName() { var request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, "http://localhost"); var encoder = new HttpPostRequestEncoder(request, true); FileStream fileStream1 = File.Open("./Multipart/file-01.txt", FileMode.Open, FileAccess.Read); this.files.Add(fileStream1); FileStream fileStream2 = File.Open("./Multipart/file-02.txt", FileMode.Open, FileAccess.Read); this.files.Add(fileStream2); encoder.AddBodyAttribute("foo", "bar"); encoder.AddBodyFileUpload("quux", "", fileStream1, "text/plain", false); encoder.AddBodyFileUpload("quux", "", fileStream2, "text/plain", false); // We have to query the value of these two fields before finalizing // the request, which unsets one of them. string multipartDataBoundary = encoder.MultipartDataBoundary; string multipartMixedBoundary = encoder.MultipartMixedBoundary; string content = GetRequestBody(encoder); string expected = "--" + multipartDataBoundary + "\r\n" + HttpHeaderNames.ContentDisposition + ": form-data; name=\"foo\"" + "\r\n" + HttpHeaderNames.ContentLength + ": 3" + "\r\n" + HttpHeaderNames.ContentType + ": text/plain; charset=utf-8" + "\r\n" + "\r\n" + "bar" + "\r\n" + "--" + multipartDataBoundary + "\r\n" + HttpHeaderNames.ContentDisposition + ": form-data; name=\"quux\"" + "\r\n" + HttpHeaderNames.ContentType + ": multipart/mixed; boundary=" + multipartMixedBoundary + "\r\n" + "\r\n" + "--" + multipartMixedBoundary + "\r\n" + HttpHeaderNames.ContentDisposition + ": attachment\r\n" + HttpHeaderNames.ContentLength + ": " + fileStream1.Length + "\r\n" + HttpHeaderNames.ContentType + ": text/plain" + "\r\n" + HttpHeaderNames.ContentTransferEncoding + ": binary" + "\r\n" + "\r\n" + "File 01" + /*StringUtil.*/ Newline + "\r\n" + "--" + multipartMixedBoundary + "\r\n" + HttpHeaderNames.ContentDisposition + ": attachment\r\n" + HttpHeaderNames.ContentLength + ": " + fileStream2.Length + "\r\n" + HttpHeaderNames.ContentType + ": text/plain" + "\r\n" + HttpHeaderNames.ContentTransferEncoding + ": binary" + "\r\n" + "\r\n" + "File 02" + /*StringUtil.*/ Newline + "\r\n" + "--" + multipartMixedBoundary + "--" + "\r\n" + "--" + multipartDataBoundary + "--" + "\r\n"; Assert.Equal(expected, content); }
public void SingleFileUploadInHtml5Mode() { var request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, "http://localhost"); var factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MinSize); var encoder = new HttpPostRequestEncoder( factory, request, true, //Encoding.UTF8, HttpPostRequestEncoder.EncoderMode.HTML5); FileStream fileStream1 = File.Open("./Multipart/file-01.txt", FileMode.Open, FileAccess.Read); this.files.Add(fileStream1); FileStream fileStream2 = File.Open("./Multipart/file-02.txt", FileMode.Open, FileAccess.Read); this.files.Add(fileStream2); encoder.AddBodyAttribute("foo", "bar"); encoder.AddBodyFileUpload("quux", fileStream1, "text/plain", false); encoder.AddBodyFileUpload("quux", fileStream2, "text/plain", false); string multipartDataBoundary = encoder.MultipartDataBoundary; string content = GetRequestBody(encoder); string expected = "--" + multipartDataBoundary + "\r\n" + HttpHeaderNames.ContentDisposition + ": form-data; name=\"foo\"" + "\r\n" + HttpHeaderNames.ContentLength + ": 3" + "\r\n" + HttpHeaderNames.ContentType + ": text/plain; charset=utf-8" + "\r\n" + "\r\n" + "bar" + "\r\n" + "--" + multipartDataBoundary + "\r\n" + HttpHeaderNames.ContentDisposition + ": form-data; name=\"quux\"; filename=\"file-01.txt\"" + "\r\n" + HttpHeaderNames.ContentLength + ": " + fileStream1.Length + "\r\n" + HttpHeaderNames.ContentType + ": text/plain" + "\r\n" + HttpHeaderNames.ContentTransferEncoding + ": binary" + "\r\n" + "\r\n" + "File 01" + /*StringUtil.*/ Newline + "\r\n" + "--" + multipartDataBoundary + "\r\n" + HttpHeaderNames.ContentDisposition + ": form-data; name=\"quux\"; filename=\"file-02.txt\"" + "\r\n" + HttpHeaderNames.ContentLength + ": " + fileStream2.Length + "\r\n" + HttpHeaderNames.ContentType + ": text/plain" + "\r\n" + HttpHeaderNames.ContentTransferEncoding + ": binary" + "\r\n" + "\r\n" + "File 02" + /*StringUtil.*/ Newline + "\r\n" + "--" + multipartDataBoundary + "--" + "\r\n"; Assert.Equal(expected, content); }
static void ShouldThrowExceptionIfNotAllowed(HttpMethod method, FileStream fileStream) { fileStream.Position = 0; // Reset to the begining var request = new DefaultFullHttpRequest(HttpVersion.Http11, method, "http://localhost"); var encoder = new HttpPostRequestEncoder(request, true); encoder.AddBodyAttribute("foo", "bar"); encoder.AddBodyFileUpload("quux", fileStream, "text/plain", false); string multipartDataBoundary = encoder.MultipartDataBoundary; string content = GetRequestBody(encoder); string expected = "--" + multipartDataBoundary + "\r\n" + HttpHeaderNames.ContentDisposition + ": form-data; name=\"foo\"" + "\r\n" + HttpHeaderNames.ContentLength + ": 3" + "\r\n" + HttpHeaderNames.ContentType + ": text/plain; charset=utf-8" + "\r\n" + "\r\n" + "bar" + "\r\n" + "--" + multipartDataBoundary + "\r\n" + HttpHeaderNames.ContentDisposition + ": form-data; name=\"quux\"; filename=\"file-01.txt\"" + "\r\n" + HttpHeaderNames.ContentLength + ": " + fileStream.Length + "\r\n" + HttpHeaderNames.ContentType + ": text/plain" + "\r\n" + HttpHeaderNames.ContentTransferEncoding + ": binary" + "\r\n" + "\r\n" + "File 01" + /*StringUtil.*/ Newline + "\r\n" + "--" + multipartDataBoundary + "--" + "\r\n"; Assert.Equal(expected, content); }
/** * Standard post without multipart but already support on Factory (memory management) * * @return the list of HttpData object (attribute and file) to be reused on next post */ private static async Task <List <IInterfaceHttpData> > FormpostAsync(Bootstrap bootstrap, Uri uriSimple, FileStream file, IHttpDataFactory factory, IList <HeaderEntry <AsciiString, ICharSequence> > headers) { // XXX /formpost // Start the connection attempt. IChannel channel = await bootstrap.ConnectAsync(new IPEndPoint(ClientSettings.Host, ClientSettings.Port)); // Prepare the HTTP request. IHttpRequest request = new DefaultHttpRequest(DotNetty.Codecs.Http.HttpVersion.Http11, HttpMethod.Post, uriSimple.ToString()); // Use the PostBody encoder HttpPostRequestEncoder bodyRequestEncoder = new HttpPostRequestEncoder(factory, request, false); // false => not multipart // it is legal to add directly header or cookie into the request until finalize foreach (var entry in headers) { request.Headers.Set(entry.Key, entry.Value); } // add Form attribute bodyRequestEncoder.AddBodyAttribute("getform", "POST"); bodyRequestEncoder.AddBodyAttribute("info", "first value"); bodyRequestEncoder.AddBodyAttribute("secondinfo", "secondvalue ���&"); bodyRequestEncoder.AddBodyAttribute("thirdinfo", TextArea); bodyRequestEncoder.AddBodyAttribute("fourthinfo", TextAreaLong); bodyRequestEncoder.AddBodyFileUpload("myfile", file, "application/x-zip-compressed", false); // finalize request request = bodyRequestEncoder.FinalizeRequest(); // Create the bodylist to be reused on the last version with Multipart support var bodylist = bodyRequestEncoder.GetBodyListAttributes(); var list = new List <object>(); // send request list.Add(request); // test if request was chunked and if so, finish the write if (bodyRequestEncoder.IsChunked) { // could do either request.isChunked() // either do it through ChunkedWriteHandler list.Add(bodyRequestEncoder); } await channel.WriteAndFlushManyAsync(list); // Do not clear here since we will reuse the InterfaceHttpData on the next request // for the example (limit action on client side). Take this as a broadcast of the same // request on both Post actions. // // On standard program, it is clearly recommended to clean all files after each request // bodyRequestEncoder.cleanFiles(); // Wait for the server to close the connection. await channel.CloseCompletion; return(bodylist); }
public void SingleFileUploadNoName() { var request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, "http://localhost"); var encoder = new HttpPostRequestEncoder(request, true); FileStream fileStream = File.Open("./Multipart/file-01.txt", FileMode.Open, FileAccess.Read); this.files.Add(fileStream); encoder.AddBodyAttribute("foo", "bar"); encoder.AddBodyFileUpload("quux", "", fileStream, "text/plain", false); string multipartDataBoundary = encoder.MultipartDataBoundary; string content = GetRequestBody(encoder); string expected = "--" + multipartDataBoundary + "\r\n" + HttpHeaderNames.ContentDisposition + ": form-data; name=\"foo\"" + "\r\n" + HttpHeaderNames.ContentLength + ": 3" + "\r\n" + HttpHeaderNames.ContentType + ": text/plain; charset=utf-8" + "\r\n" + "\r\n" + "bar" + "\r\n" + "--" + multipartDataBoundary + "\r\n" + HttpHeaderNames.ContentDisposition + ": form-data; name=\"quux\"\r\n" + HttpHeaderNames.ContentLength + ": " + fileStream.Length + "\r\n" + HttpHeaderNames.ContentType + ": text/plain" + "\r\n" + HttpHeaderNames.ContentTransferEncoding + ": binary" + "\r\n" + "\r\n" + "File 01" + StringUtil.Newline + "\r\n" + "--" + multipartDataBoundary + "--" + "\r\n"; Assert.Equal(expected, content); }
public void HttpPostRequestEncoderSlicedBuffer() { var request = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, "http://localhost"); var encoder = new HttpPostRequestEncoder(request, true); // add Form attribute encoder.AddBodyAttribute("getform", "POST"); encoder.AddBodyAttribute("info", "first value"); encoder.AddBodyAttribute("secondinfo", "secondvalue a&"); encoder.AddBodyAttribute("thirdinfo", "short text"); const int Length = 100000; var array = new char[Length]; array.Fill('a'); string longText = new string(array); encoder.AddBodyAttribute("fourthinfo", longText.Substring(0, 7470)); FileStream fileStream1 = File.Open("./Multipart/file-01.txt", FileMode.Open, FileAccess.Read); this.files.Add(fileStream1); encoder.AddBodyFileUpload("myfile", fileStream1, "application/x-zip-compressed", false); encoder.FinalizeRequest(); while (!encoder.IsEndOfInput) { IHttpContent httpContent = encoder.ReadChunk(null); IByteBuffer content = httpContent.Content; int refCnt = content.ReferenceCount; Assert.True( (ReferenceEquals(content.Unwrap(), content) || content.Unwrap() == null) && refCnt == 1 || !ReferenceEquals(content.Unwrap(), content) && refCnt == 2, "content: " + content + " content.unwrap(): " + content.Unwrap() + " refCnt: " + refCnt); httpContent.Release(); } encoder.CleanFiles(); encoder.Close(); }