1 module soup.Multipart;
2 
3 private import glib.ConstructionException;
4 private import glib.Str;
5 private import gobject.ObjectG;
6 private import soup.Buffer;
7 private import soup.MessageBody;
8 private import soup.MessageHeaders;
9 private import soup.c.functions;
10 public  import soup.c.types;
11 
12 
13 /**
14  * Represents a multipart HTTP message body, parsed according to the
15  * syntax of RFC 2046. Of particular interest to HTTP are
16  * <literal>multipart/byte-ranges</literal> and
17  * <literal>multipart/form-data</literal>.
18  * 
19  * Although the headers of a #SoupMultipart body part will contain the
20  * full headers from that body part, libsoup does not interpret them
21  * according to MIME rules. For example, each body part is assumed to
22  * have "binary" Content-Transfer-Encoding, even if its headers
23  * explicitly state otherwise. In other words, don't try to use
24  * #SoupMultipart for handling real MIME multiparts.
25  *
26  * Since: 2.26
27  */
28 public class Multipart
29 {
30 	/** the main Gtk struct */
31 	protected SoupMultipart* soupMultipart;
32 	protected bool ownedRef;
33 
34 	/** Get the main Gtk struct */
35 	public SoupMultipart* getMultipartStruct(bool transferOwnership = false)
36 	{
37 		if (transferOwnership)
38 			ownedRef = false;
39 		return soupMultipart;
40 	}
41 
42 	/** the main Gtk struct as a void* */
43 	protected void* getStruct()
44 	{
45 		return cast(void*)soupMultipart;
46 	}
47 
48 	/**
49 	 * Sets our main struct and passes it to the parent class.
50 	 */
51 	public this (SoupMultipart* soupMultipart, bool ownedRef = false)
52 	{
53 		this.soupMultipart = soupMultipart;
54 		this.ownedRef = ownedRef;
55 	}
56 
57 	~this ()
58 	{
59 		if ( ownedRef )
60 			soup_multipart_free(soupMultipart);
61 	}
62 
63 
64 	/** */
65 	public static GType getType()
66 	{
67 		return soup_multipart_get_type();
68 	}
69 
70 	/**
71 	 * Creates a new empty #SoupMultipart with a randomly-generated
72 	 * boundary string. Note that @mime_type must be the full MIME type,
73 	 * including "multipart/".
74 	 *
75 	 * Params:
76 	 *     mimeType = the MIME type of the multipart to create.
77 	 *
78 	 * Returns: a new empty #SoupMultipart of the given @mime_type
79 	 *
80 	 * Since: 2.26
81 	 *
82 	 * Throws: ConstructionException GTK+ fails to create the object.
83 	 */
84 	public this(string mimeType)
85 	{
86 		auto __p = soup_multipart_new(Str.toStringz(mimeType));
87 
88 		if(__p is null)
89 		{
90 			throw new ConstructionException("null returned by new");
91 		}
92 
93 		this(cast(SoupMultipart*) __p);
94 	}
95 
96 	/**
97 	 * Parses @headers and @body to form a new #SoupMultipart
98 	 *
99 	 * Params:
100 	 *     headers = the headers of the HTTP message to parse
101 	 *     body_ = the body of the HTTP message to parse
102 	 *
103 	 * Returns: a new #SoupMultipart (or %NULL if the
104 	 *     message couldn't be parsed or wasn't multipart).
105 	 *
106 	 * Since: 2.26
107 	 *
108 	 * Throws: ConstructionException GTK+ fails to create the object.
109 	 */
110 	public this(MessageHeaders headers, MessageBody body_)
111 	{
112 		auto __p = soup_multipart_new_from_message((headers is null) ? null : headers.getMessageHeadersStruct(), (body_ is null) ? null : body_.getMessageBodyStruct());
113 
114 		if(__p is null)
115 		{
116 			throw new ConstructionException("null returned by new_from_message");
117 		}
118 
119 		this(cast(SoupMultipart*) __p);
120 	}
121 
122 	/**
123 	 * Adds a new MIME part containing @body to @multipart, using
124 	 * "Content-Disposition: form-data", as per the HTML forms
125 	 * specification. See soup_form_request_new_from_multipart() for more
126 	 * details.
127 	 *
128 	 * Params:
129 	 *     controlName = the name of the control associated with this file
130 	 *     filename = the name of the file, or %NULL if not known
131 	 *     contentType = the MIME type of the file, or %NULL if not known
132 	 *     body_ = the file data
133 	 *
134 	 * Since: 2.26
135 	 */
136 	public void appendFormFile(string controlName, string filename, string contentType, Buffer body_)
137 	{
138 		soup_multipart_append_form_file(soupMultipart, Str.toStringz(controlName), Str.toStringz(filename), Str.toStringz(contentType), (body_ is null) ? null : body_.getBufferStruct());
139 	}
140 
141 	/**
142 	 * Adds a new MIME part containing @data to @multipart, using
143 	 * "Content-Disposition: form-data", as per the HTML forms
144 	 * specification. See soup_form_request_new_from_multipart() for more
145 	 * details.
146 	 *
147 	 * Params:
148 	 *     controlName = the name of the control associated with @data
149 	 *     data = the body data
150 	 *
151 	 * Since: 2.26
152 	 */
153 	public void appendFormString(string controlName, string data)
154 	{
155 		soup_multipart_append_form_string(soupMultipart, Str.toStringz(controlName), Str.toStringz(data));
156 	}
157 
158 	/**
159 	 * Adds a new MIME part to @multipart with the given headers and body.
160 	 * (The multipart will make its own copies of @headers and @body, so
161 	 * you should free your copies if you are not using them for anything
162 	 * else.)
163 	 *
164 	 * Params:
165 	 *     headers = the MIME part headers
166 	 *     body_ = the MIME part body
167 	 *
168 	 * Since: 2.26
169 	 */
170 	public void appendPart(MessageHeaders headers, Buffer body_)
171 	{
172 		soup_multipart_append_part(soupMultipart, (headers is null) ? null : headers.getMessageHeadersStruct(), (body_ is null) ? null : body_.getBufferStruct());
173 	}
174 
175 	/**
176 	 * Frees @multipart
177 	 *
178 	 * Since: 2.26
179 	 */
180 	public void free()
181 	{
182 		soup_multipart_free(soupMultipart);
183 		ownedRef = false;
184 	}
185 
186 	/**
187 	 * Gets the number of body parts in @multipart
188 	 *
189 	 * Returns: the number of body parts in @multipart
190 	 *
191 	 * Since: 2.26
192 	 */
193 	public int getLength()
194 	{
195 		return soup_multipart_get_length(soupMultipart);
196 	}
197 
198 	/**
199 	 * Gets the indicated body part from @multipart.
200 	 *
201 	 * Params:
202 	 *     part = the part number to get (counting from 0)
203 	 *     headers = return location for the MIME part
204 	 *         headers
205 	 *     body_ = return location for the MIME part
206 	 *         body
207 	 *
208 	 * Returns: %TRUE on success, %FALSE if @part is out of range (in
209 	 *     which case @headers and @body won't be set)
210 	 *
211 	 * Since: 2.26
212 	 */
213 	public bool getPart(int part, out MessageHeaders headers, out Buffer body_)
214 	{
215 		SoupMessageHeaders* outheaders = null;
216 		SoupBuffer* outbody_ = null;
217 
218 		auto __p = soup_multipart_get_part(soupMultipart, part, &outheaders, &outbody_) != 0;
219 
220 		headers = ObjectG.getDObject!(MessageHeaders)(outheaders);
221 		body_ = ObjectG.getDObject!(Buffer)(outbody_);
222 
223 		return __p;
224 	}
225 
226 	/**
227 	 * Serializes @multipart to @dest_headers and @dest_body.
228 	 *
229 	 * Params:
230 	 *     destHeaders = the headers of the HTTP message to serialize @multipart to
231 	 *     destBody = the body of the HTTP message to serialize @multipart to
232 	 *
233 	 * Since: 2.26
234 	 */
235 	public void toMessage(MessageHeaders destHeaders, MessageBody destBody)
236 	{
237 		soup_multipart_to_message(soupMultipart, (destHeaders is null) ? null : destHeaders.getMessageHeadersStruct(), (destBody is null) ? null : destBody.getMessageBodyStruct());
238 	}
239 }