1 
2 deprecated("Using hunt.http.HttpBody instead.")
3 module hunt.http.client.RequestBody;
4 
5 import hunt.http.HttpBody;
6 
7 deprecated("Using HttpBody instead.")
8 alias RequestBody = HttpBody;
9 
10 
11 // import hunt.http.HttpOutputStream;
12 // import hunt.io.HeapByteBuffer;
13 // import hunt.io.ByteBuffer;
14 // import hunt.io.BufferUtils;
15 
16 // import hunt.Exceptions;
17 // import hunt.logging;
18 // import hunt.util.MimeType;
19 // import hunt.util.MimeTypeUtils;
20 
21 // import std.range;
22 // import std.file;
23 // import std.path;
24 // import std.range;
25 // import std.stdio;
26 
27 // /** 
28 //  * 
29 //  */
30 // abstract class RequestBody {
31 
32 // 	string contentType();
33 
34 //     /**
35 //      * Returns the number of bytes that will be written to {@code sink} in a call to {@link #writeTo},
36 //      * or -1 if that count is unknown.
37 //      */
38 // 	long contentLength() {
39 // 		return -1;
40 // 	}
41 
42 //     /** Writes the content of this request to {@code sink}. */
43 //     void writeTo(HttpOutputStream sink);
44 
45 	
46 //     /**
47 //      * Returns a new request body that transmits {@code content}. If {@code contentType} is non-null
48 //      * and lacks a charset, this will use UTF-8.
49 //      */
50 //     static RequestBody create(string contentType, string content) {
51 //         // Charset charset = UTF_8;
52 //         if (contentType !is null) {
53 //             // charset = contentType.charset();
54 // 			string charset = new MimeType(contentType).getCharset();
55 //             if (charset.empty()) {
56 //                 // charset = UTF_8;
57 //                 contentType = contentType ~ "; charset=utf-8";
58 //             }
59 //         }
60 //         byte[] bytes = cast(byte[])content; // content.getBytes(charset);
61 //         return create(contentType, bytes);
62 //     }
63 
64 	
65 //     /** Returns a new request body that transmits {@code content}. */
66 //     static RequestBody create(string contentType, byte[] content) {
67 //         return create(contentType, content, 0, cast(int)content.length);
68 //     }
69 
70 //     /** Returns a new request body that transmits {@code content}. */
71 //     static RequestBody create(string type, byte[] content,
72 //             int offset, int byteCount) {
73 
74 //         if (content.empty()) throw new NullPointerException("content is null");
75 //         // Util.checkOffsetAndCount(content.length, offset, byteCount);
76 // 		assert(offset + byteCount <= content.length);
77 
78 //         return new class RequestBody {
79 
80 //             override string contentType() {
81 //                 return type;
82 //             }
83 
84 //             override long contentLength() {
85 //                 return byteCount;
86 //             }
87 
88 //             override void writeTo(HttpOutputStream sink) {
89 //                 sink.write(content, offset, byteCount);
90 //             }
91 //         };
92 //     }
93 
94 //     /** Returns a new request body that transmits the content of {@code file}. */
95 //     static RequestBody createFromFile(string type, string fileName) {
96 //         if (fileName.empty()) throw new NullPointerException("fileName is null");
97 		
98 // 		string rootPath = dirName(thisExePath);
99 // 		string abslutePath = buildPath(rootPath, fileName);
100 // 		version(HUNT_HTTP_DEBUG) info("generate request body from file: ", abslutePath);
101 // 		if(!abslutePath.exists) throw new FileNotFoundException(fileName);
102 
103 // 		DirEntry entry = DirEntry(abslutePath);
104 // 		if(entry.isDir()) throw new FileException("Can't handle a direcotry: ", abslutePath);
105 
106 // 		long total = cast(long)entry.size();
107 // 		if(total > 10*1024*1024) {
108 // 			// throw new FileException("The file is too big to upload (< 10MB).");
109 // 			debug warning("uploading a big file: %d MB", total/1024/1024);
110 // 		}
111 
112 //         return new class RequestBody {
113 //             override string contentType() {
114 //                 return type;
115 //             }
116 
117 //             override long contentLength() {
118 //                 return total;
119 //             }
120 
121 // 			override void writeTo(HttpOutputStream sink)  {
122 // 				version(HUNT_HTTP_DEBUG) infof("loading data from: %s, size: %d", abslutePath, total);
123 // 				enum MaxBufferSize = 16*1024;
124 // 				ubyte[] buffer;
125 				
126 // 				File f = File(abslutePath, "r");
127 // 				scope(exit) f.close();
128 
129 // 				size_t remaining = cast(size_t)total;
130 // 				while(remaining > 0 && !f.eof()) {
131 
132 // 					if(remaining > MaxBufferSize) 
133 // 						buffer = new ubyte[MaxBufferSize];
134 // 					else 
135 // 						buffer = new ubyte[remaining];
136 // 					ubyte[] data = f.rawRead(buffer);
137                     
138 //                     if(data.length > 0) {
139 // 					    sink.write(cast(byte[])data);
140 //                         remaining -= cast(int)data.length;
141 //                     }
142 //                     version(HUNT_HTTP_DEBUG_MORE) {
143 //                         tracef("read: %s, remaining: %d, eof: %s", 
144 //                             data.length, remaining, f.eof());
145 //                     }
146 // 				}
147 // 			}
148 //         };
149 //     }		
150 // }