Skip to content

Commit

Permalink
- fixed issues with file upload in PHP
Browse files Browse the repository at this point in the history
- added ImageResult (only to use with UImage which is really alpha)
- fixed FileResult
- removed controllerName and action from UrlHelper methods and derivatives (use the data object to pass controller info)
- added writeBytes to HttpResponse
  • Loading branch information
fponticelli committed Feb 8, 2011
1 parent 45c6141 commit 4612212
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 55 deletions.
49 changes: 31 additions & 18 deletions src/php/ufront/web/HttpRequest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class HttpRequest extends ufront.web.HttpRequest

public function new()
{
/*
php.Lib.print("<pre>");
php.Lib.dump(untyped __var__("_ENV") );
php.Lib.print("</pre>");
*/
_uploadHandler = new EmptyUploadHandler();
}

Expand All @@ -40,16 +45,12 @@ class HttpRequest extends ufront.web.HttpRequest
return "";
if (null == postString)
{
var h = untyped __call__("fopen", "php://input", "r");
var bsize = 8192;
var max = 32;
postString = null;
var counter = 0;
while (!untyped __call__("feof", h) && counter < max) {
postString += untyped __call__("fread", h, bsize);
counter++;
if (untyped __call__("isset", __var__('GLOBALS', 'HTTP_RAW_POST_DATA')))
{
postString = untyped __var__('GLOBALS', 'HTTP_RAW_POST_DATA');
} else {
postString = untyped __call__("file_get_contents", "php://input");
}
untyped __call__("fclose", h);
if (null == postString)
postString = "";
}
Expand All @@ -65,14 +66,6 @@ class HttpRequest extends ufront.web.HttpRequest
_parsed = true;
var post = getPost();
var handler = _uploadHandler;
untyped if (__call__("isset", __php__("$_POST")))
{
var na : php.NativeArray = __call__("array");
__php__("foreach($_POST as $k => $v) { $na[urldecode($k)] = $v; }");
var h = php.Lib.hashOfAssociativeArray(na);
for (k in h.keys())
post.set(k, h.get(k));
}
if(!untyped __call__("isset", __php__("$_FILES"))) return;
var parts : Array<String> = untyped __call__("new _hx_array",__call__("array_keys", __php__("$_FILES")));
untyped for(part in parts) {
Expand Down Expand Up @@ -125,7 +118,9 @@ class HttpRequest extends ufront.web.HttpRequest
override function getQuery()
{
if (null == query)
{
query = getHashFromString(queryString);
}
return query;
}

Expand All @@ -137,7 +132,25 @@ class HttpRequest extends ufront.web.HttpRequest
{
post = getHashFromString(postString);
if (!post.iterator().hasNext())
_parseMultipart();
{
untyped if (__call__("isset", __php__("$_POST")))
{
var na : php.NativeArray = __call__("array");
__php__("foreach($_POST as $k => $v) { $na[urldecode($k)] = $v; }");
var h = php.Lib.hashOfAssociativeArray(na);
for (k in h.keys())
post.set(k, h.get(k));
}
}
if (untyped __call__("isset", __php__("$_FILES")))
{
var parts : Array<String> = untyped __call__("new _hx_array",__call__("array_keys", __php__("$_FILES")));
untyped for (part in parts) {
var file : String = __php__("$_FILES[$part]['name']");
var name = StringTools.urldecode(part);
post.set(name, file);
}
}
}
return post;
}
Expand Down
2 changes: 1 addition & 1 deletion src/php/ufront/web/HttpResponse.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class HttpResponse extends ufront.web.HttpResponse
var k = null, v = null;

// set status
untyped __call__('header', 'X-Powered-By: ', true);
untyped __call__('header', 'X-Powered-By: ufront', true);
untyped __call__('header', "HTTP/1.1 " + statusDescription(status), true, status);

try {
Expand Down
6 changes: 6 additions & 0 deletions src/ufront/web/HttpResponse.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

package ufront.web;
import haxe.io.Bytes;
import haxe.io.BytesOutput;
import haxe.io.Output;
import thx.collections.HashList;
Expand Down Expand Up @@ -92,6 +93,11 @@ class HttpResponse
_buff.addChar(c);
}

public function writeBytes(b : Bytes)
{
_buff.add(b.readString(0, b.length));
}

public function setHeader(name : String, value : String)
{
NullArgument.throwIfNull(name, "name");
Expand Down
5 changes: 4 additions & 1 deletion src/ufront/web/mvc/FileResult.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package ufront.web.mvc;

class FileResult extends ActionResult
{
public var content : Bytes;
public var contentType : String;
public var fileDownloadName : String;

function new(contentType : String, fileDownloadName : String)
function new(content : Bytes, contentType : String, fileDownloadName : String)
{
this.content = content;
this.contentType = contentType;
this.fileDownloadName = fileDownloadName;
}
Expand All @@ -20,5 +22,6 @@ class FileResult extends ActionResult

if(null != fileDownloadName)
controllerContext.response.setHeader("content-disposition", "attachment; filename=" + fileDownloadName);
controllerContext.response.writeBytes(content);
}
}
52 changes: 52 additions & 0 deletions src/ufront/web/mvc/ImageResult.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ufront.web.mvc;

import thx.error.NullArgument;
import uimage.IImage<Dynamic>;

/**
* ...
* @author Franco Ponticelli
*/

class ImageResult extends ActionResult
{
public var format : ImageFormat;
public var image : IImage<Dynamic>;

public function new(image : IImage<Dynamic>, ?format : ImageFormat)
{
if (null == format)
format = Png;
this.format = format;
NullArgument.throwIfNull(image, "image");
this.image = image;
}

override function executeResult(controllerContext : ControllerContext)
{
NullArgument.throwIfNull(controllerContext, "controllerContext");
var response = controllerContext.response;
var bytes = switch(format)
{
case Jpeg:
response.contentType = "image/jpeg";
image.toBytesJpeg();
case Png:
response.contentType = "image/png";
image.toBytesPng();
case Gif:
response.contentType = "image/gif";
image.toBytesGif();
}
response.setHeader("Content-Length", "" + bytes.length);
// response.setHeader("Last-Modified", DateTools.format(Date.now(), '%a, %d %b %Y %H:%M:%S') + ' GMT');
response.writeBytes(bytes);
}
}

enum ImageFormat
{
Jpeg;
Png;
Gif;
}
14 changes: 7 additions & 7 deletions src/ufront/web/mvc/RedirectToControllerResult.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
package ufront.web.mvc;
import ufront.web.mvc.view.UrlHelper;
import thx.error.NullArgument;

import ufront.web.mvc.RedirectResult;
using thx.collections.UHash;

class RedirectToControllerResult extends ActionResult
{
var controllerName : String;
var actionName : String;
var params : Hash<Dynamic>;
public function new(controllerName : String, actionName = "index", ?params : Hash<Dynamic>)
public function new(?params : Hash<String>, ?o : Dynamic<String>)
{
this.controllerName = controllerName;
this.actionName = actionName;
this.params = null == params ? new Hash() : params;
if (null != o)
this.params.importObject(o);
if (null == this.params.get("action"))
this.params.set("action", "index");
}

override function executeResult(controllerContext : ControllerContext)
{
NullArgument.throwIfNull(controllerContext, "controllerContext");

var url = new UrlHelperInst(controllerContext.requestContext).route(controllerName, actionName, params);
var url = new UrlHelperInst(controllerContext.requestContext).route(params);
var redirect = new RedirectResult(url, false);
redirect.executeResult(controllerContext);
}
Expand Down
13 changes: 5 additions & 8 deletions src/ufront/web/mvc/view/HtmlHelper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ class HtmlHelperInst
return open("a", attrs) + text + close("a");
}

public function route(text : String, controllerName : String, ?action : String, ?data : Dynamic, ?attrs : Dynamic)
public function route(text : String, ?data : Dynamic, ?attrs : Dynamic)
{
return link(text, __url.route(controllerName, action, data), attrs);
return link(text, __url.route(data), attrs);
}

public function linkif(text : String, url : String, ?test : String, ?attrs : Dynamic)
public function linkif(test : String, text : String, url : String, ?attrs : Dynamic)
{
if(null == attrs)
attrs = {};
Expand All @@ -86,12 +86,9 @@ class HtmlHelperInst
}
}

/**
* TODO fix issues with more than 5 argumetns (couple controllerName with action)
*/
public function routeif(text : String, controllerName : String, ?action : String, ?test : String, ?data : Dynamic)
public function routeif(test : String, text : String, ?data : Dynamic, ?attrs : Dynamic)
{
return linkif(text, __url.route(controllerName, action, data), test, null);
return linkif(test, text, __url.route(data), attrs);
}

public function open(name : String, attrs : Dynamic)
Expand Down
38 changes: 18 additions & 20 deletions src/ufront/web/mvc/view/UrlHelper.hx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ufront.web.mvc.view;
import thx.error.Error;
import thx.error.NullArgument;
import thx.util.UDynamicT;
import ufront.web.routing.RequestContext;
import ufront.web.routing.Route;

import thx.util.UDynamicT;
using thx.type.UType;
import thx.collections.UHash;
using thx.collections.UHash;

class UrlHelper implements IViewHelper
{
Expand Down Expand Up @@ -49,7 +50,7 @@ class UrlHelperInst
for(field in Reflect.fields(params))
{
var value = Reflect.field(params, field);
qs.push(field + "=" + StringTools.urlEncode(value));
qs.push(field + "=" + encode(value));
}
if(qs.length > 0)
url += (url.indexOf("?") >= 0 ? "&" : "?") + qs.join("&");
Expand All @@ -62,31 +63,28 @@ class UrlHelperInst
return StringTools.urlEncode(s);
}

public function route(?controllerName : String, ?action : String, ?data : Dynamic)
public function route(?data : Dynamic)
{
if (null == controllerName)
{
var route = __req.routeData.route.as(Route);
if (null == route)
throw new Error("action() method can't be used on this kind of route");
controllerName = route.defaults.get("controller");
}
NullArgument.throwIfNull(controllerName, "controllerName");
var hash = null;
if(null == data)
hash = new Hash();
else if(!Std.is(data, Hash))
hash = UDynamicT.toHash(data);
else if (Std.is(data, Hash))
hash = UHash.clone(data);
else
hash = data;
hash = UDynamicT.toHash(data);

if (null == hash.get("controller"))
{
var route = __req.routeData.route.as(Route);
if (null == route)
throw new Error("unable to find a suitable route for {0}", Std.string(hash));
hash.set("controller", route.defaults.get("controller"));
NullArgument.throwIfNull(hash.get("controller"), "controller");
}

hash.set("controller", controllerName);
if(null != action)
hash.set("action", action);

for(route in __req.routeData.route.routes.iterator())
{
var url = route.getPath(__req.httpContext, hash);
var url = route.getPath(__req.httpContext, hash.clone());
if(null != url)
return url;
}
Expand Down

0 comments on commit 4612212

Please sign in to comment.