Permalink
Browse files

fixing multipart

  • Loading branch information...
1 parent ac85d08 commit d214274ec53f8802eac3bf38435fbcd372abafb2 @gabriel gabriel committed Aug 8, 2008
@@ -8,6 +8,7 @@ package org.httpclient {
import flash.events.EventDispatcher;
import flash.events.Event;
+ import flash.errors.IllegalOperationError;
import org.httpclient.http.Put;
import org.httpclient.http.Post;
@@ -115,10 +116,8 @@ package org.httpclient {
else if (method == "POST") httpRequest = new Post();
else throw new ArgumentError("Method must be PUT or POST");
- // Comment out to compile under flash
- //httpRequest.multipart = new Multipart([ new FilePart(file) ]);
- throw new DefinitionError("Upload not supported; Rebuild with upload support.");
-
+ //httpRequest.setMultipart(new Multipart([ new FilePart(file) ]));
+ throw new IllegalOperationError("Not supported, comment out the line above");
request(uri, httpRequest);
}
@@ -148,7 +147,20 @@ package org.httpclient {
}
/**
- * Post with data.
+ * Post with multipart.
+ *
+ * @param uri
+ * @param multipart
+ */
+ public function postMultipart(uri:URI, multipart:Multipart):void {
+ var post:Post = new Post();
+ post.setMultipart(multipart);
+ request(uri, post);
+ }
+
+ /**
+ * Post with raw data.
+ *
* @param uri
* @param body
* @param contentType
@@ -7,6 +7,7 @@ package org.httpclient {
import com.adobe.net.URI;
import com.adobe.net.URIEncodingBitmap;
import com.hurlant.util.Base64;
+ import flash.errors.IllegalOperationError;
import flash.utils.ByteArray;
import org.httpclient.http.multipart.Multipart;
@@ -28,16 +29,18 @@ package org.httpclient {
/**
* Create request.
*
- * The request body can be anything but should respond to:
+ * The raw request body can be anything but should respond to:
* - readBytes(bytes:ByteArray, offset:uint, length:uint)
* - length
* - bytesAvailable
* - close
*
+ * To set multipart form data, don't pass in a body. Use request.setMultipart(...)
+ * To set form data, don't pass in a body. Use request.setFormData(...)
+ *
* @param method
* @param header
* @param body
- *
*/
public function HttpRequest(method:String, header:HttpHeader = null, body:* = null) {
_method = method;
@@ -91,6 +94,8 @@ package org.httpclient {
* - length
* - bytesAvailable
* - close
+ *
+ * @throws IllegalOperationError if content is already set
*/
public function set body(body:*):void {
_body = body;
@@ -106,16 +111,20 @@ package org.httpclient {
}
/**
- * Set form data.
+ * Set request body as form data content (type is x-www-form-urlencoded)
+ *
* @param params Array of key value pairs: [ { name: "Foo", value: "Bar" }, { name: "Baz", value: "Bewm" },... ]
* @param sep Separator
+ * @throws IllegalOperationError if content is already set
*/
public function setFormData(params:Array, sep:String = "&"):void {
+ if (_body) throw new IllegalOperationError("The body content was already set; You can only set using either setMultipart, setFormData or body=");
+
_header.replace("Content-Type", "application/x-www-form-urlencoded");
_body = new ByteArray();
_body.writeUTFBytes(params.map(function(item:*, index:int, array:Array):String {
- return encodeURI(item["name"]) + "=" + encodeURI(item["value"]);
+ return encodeURI(item.name) + "=" + encodeURI(item.value);
}).join(sep));
_body.position = 0;
@@ -124,9 +133,12 @@ package org.httpclient {
}
/**
- * Set multipart.
+ * Set request body as multipart content.
+ * @throws IllegalOperationError if content is already set
*/
- public function set multipart(multipart:Multipart):void {
+ public function setMultipart(multipart:Multipart):void {
+ if (_body) throw new IllegalOperationError("The body content was already set; You can only set using either setMultipart, setFormData or body=");
+
_header.replace("Content-Type", "multipart/form-data; boundary=" + Multipart.BOUNDARY);
_header.replace("Content-Length", String(multipart.length));
_body = multipart;
@@ -1,37 +0,0 @@
-/**
- * Copyright (c) 2007 Gabriel Handford
- * See LICENSE.txt for full license information.
- */
-package org.httpclient.http.multipart {
-
- import flash.utils.ByteArray;
-
- public class EndPart extends Part {
-
- /**
- * Create part section.
- *
- * @param none
- */
- public function EndPart() {
- super(new ByteArray());
- }
-
- /**
- * Build header.
- * @return Header as byte array
- */
- override protected function header():ByteArray {
- var bytes:ByteArray = new ByteArray();
-
- // Boundary
- bytes.writeUTFBytes("--" + Multipart.BOUNDARY + "--\r\n");
- bytes.position = 0;
- return bytes;
- }
-
- override protected function footer():ByteArray {
- return new ByteArray();
- }
- }
-}
@@ -15,7 +15,7 @@ package org.httpclient.http.multipart {
* @param contentType Content type
*/
public function FilePart(file:File, contentType:String = "application/octet-stream") {
- super(HttpFileStream.readFile(file), contentType, [ { name: "filename", value: file.name } ]);
+ super("file", HttpFileStream.readFile(file), contentType, "binary");
}
}
@@ -21,7 +21,6 @@ package org.httpclient.http.multipart {
*/
public function Multipart(parts:Array) {
_parts = parts;
- _parts.push(new EndPart());
}
/**
@@ -8,9 +8,9 @@ package org.httpclient.http.multipart {
public class Part {
- private var _contentType:String;
- private var _params:Array;
- private var _boundary:String;
+ private var _name:String;
+ private var _contentType:String;
+ private var _contentTransferEncoding:String;
private var _header:ByteArray;
private var _payload:*;
@@ -21,14 +21,23 @@ package org.httpclient.http.multipart {
*
* @param payload
* @param contentType
- * @param params [ { name: "Name", value: "Value" }, ... ]
- * @param boundary Boundary or null, and a random one is generated
+ * @param contentTransferEncoding
+ *
*/
- public function Part(payload:*, contentType:String = "application/octet-stream", params:Array = null) {
+ public function Part(name:String, payload:*, contentType:String = null, contentTransferEncoding:String = null) {
+
+ // Convert payload to UTF bytes if its a String
+ if (payload is String) {
+ var stringBytes:ByteArray = new ByteArray();
+ stringBytes.writeUTFBytes(payload);
+ stringBytes.position = 0;
+ payload = stringBytes;
+ }
+
+ _name = name;
_payload = payload;
_contentType = contentType;
- _params = params;
- if (!_params) _params = [];
+ _contentTransferEncoding = contentTransferEncoding;
_header = header();
_footer = footer();
@@ -79,12 +88,25 @@ package org.httpclient.http.multipart {
* Close payload.
*/
public function close():void {
- if (!(_payload is ByteArray))
- _payload.close();
+ if (!(_payload is ByteArray)) _payload.close(); // TODO: Can we check responds to?
}
/**
* Build header.
+ *
+ * Example,
+ * --BOUNDARY
+ * Content-Disposition: form-data; name="field1"
+ *
+ * <payload>
+ *
+ * Or,
+ * --BOUNDARY
+ * Content-Disposition: form-data; name="userfile"; filename="$filename"
+ * Content-Type: $mimetype
+ * Content-Transfer-Encoding: binary
+ *
+ * <payload>
* @return Header as byte array
*/
protected function header():ByteArray {
@@ -95,14 +117,14 @@ package org.httpclient.http.multipart {
// Content disposition
bytes.writeUTFBytes("Content-Disposition: form-data; ");
-
- // Params
- bytes.writeUTFBytes(_params.map(function(item:*, index:int, array:Array):String { return item["name"] + "=\"" + item["value"] + "\""; }).join("; "));
+ if (_name) bytes.writeUTFBytes("name=\"" + _name + "\"")
bytes.writeUTFBytes("\r\n");
// Content type
- bytes.writeUTFBytes("Content-Type: " + _contentType + "\r\n");
- //bytes.writeUTFBytes("Content-Length: " + _payload.length + "\r\n");
+ if (_contentType) bytes.writeUTFBytes("Content-Type: " + _contentType + "\r\n");
+
+ // Content transfer encoding
+ if (_contentTransferEncoding) bytes.writeUTFBytes("Content-Transfer-Encoding: " + _contentTransferEncoding + "\r\n");
// Empty line
bytes.writeUTFBytes("\r\n");
@@ -111,9 +133,14 @@ package org.httpclient.http.multipart {
return bytes;
}
+ /**
+ * Build footer
+ * Example,
+ * --BOUNDARY--
+ */
protected function footer():ByteArray {
var bytes:ByteArray = new ByteArray();
- bytes.writeUTFBytes("\r\n");
+ bytes.writeUTFBytes("--" + Multipart.BOUNDARY + "--\r\n");
bytes.position = 0;
return bytes;
}
@@ -0,0 +1,19 @@
+/**
+ * Copyright (c) 2007 Gabriel Handford
+ * See LICENSE.txt for full license information.
+ */
+package org.httpclient.http.multipart {
+
+ public class StringPart extends Part {
+
+ /**
+ * Create string name, value part.
+ * @param name
+ * @param value
+ */
+ public function StringPart(name:String, value:String) {
+ super(name, value);
+ }
+
+ }
+}
View
@@ -36,7 +36,7 @@
//ts.addTest(new DeliciousTests());
} else {
/* For single tests */
- ts.addTest(new UriEscapeTest("testEscapePath"));
+ ts.addTest(new S3PostTest("testPost"));
//ts.addTest(new TagsTest("testGetTags"));
}
@@ -46,11 +46,11 @@ package httpclient.http {
var request:HttpRequest = new HttpRequest("GET");
// /bar?action=getevent&token=KD/+c=|sau=|ted=
- var uri:URI = new URI("http://foo.com/bar?action=getevent&token=KD%2F%2Bc%3D%7Csau%3D%7Cted%3D");
+ var uri:URI = new URI("http://foo.com/bar?token=KD%2F%2Bc%3D%7Csau%3D%7Cted%3D");
var bytes:ByteArray = request.getHeader(uri);
var s:String = bytes.readUTFBytes(bytes.length);
// TODO: URI doesn't maintain order this test is even more brittle than before :O
- assertEquals("GET /bar?action=getevent&token=KD%2F%2Bc%3D%7Csau%3D%7Cted%3D HTTP/1.1\r\nHost: foo.com\r\nConnection: close\r\n\r\n", s)
+ assertEquals("GET /bar?token=KD%2F%2Bc%3D%7Csau%3D%7Cted%3D HTTP/1.1\r\nHost: foo.com\r\nConnection: close\r\n\r\n", s)
}
}
Oops, something went wrong.

0 comments on commit d214274

Please sign in to comment.