diff --git a/src/php/ufront/web/HttpRequest.hx b/src/php/ufront/web/HttpRequest.hx index c47c977..b715151 100644 --- a/src/php/ufront/web/HttpRequest.hx +++ b/src/php/ufront/web/HttpRequest.hx @@ -24,6 +24,11 @@ class HttpRequest extends ufront.web.HttpRequest public function new() { + /* + php.Lib.print("
");
+		php.Lib.dump(untyped __var__("_ENV") );
+		php.Lib.print("
"); + */ _uploadHandler = new EmptyUploadHandler(); } @@ -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 = ""; } @@ -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 = untyped __call__("new _hx_array",__call__("array_keys", __php__("$_FILES"))); untyped for(part in parts) { @@ -125,7 +118,9 @@ class HttpRequest extends ufront.web.HttpRequest override function getQuery() { if (null == query) + { query = getHashFromString(queryString); + } return query; } @@ -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 = 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; } diff --git a/src/php/ufront/web/HttpResponse.hx b/src/php/ufront/web/HttpResponse.hx index 1a06a1c..804494a 100644 --- a/src/php/ufront/web/HttpResponse.hx +++ b/src/php/ufront/web/HttpResponse.hx @@ -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 { diff --git a/src/ufront/web/HttpResponse.hx b/src/ufront/web/HttpResponse.hx index fc443a7..1d92281 100644 --- a/src/ufront/web/HttpResponse.hx +++ b/src/ufront/web/HttpResponse.hx @@ -4,6 +4,7 @@ */ package ufront.web; +import haxe.io.Bytes; import haxe.io.BytesOutput; import haxe.io.Output; import thx.collections.HashList; @@ -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"); diff --git a/src/ufront/web/mvc/FileResult.hx b/src/ufront/web/mvc/FileResult.hx index 0429859..1f1b7ac 100644 --- a/src/ufront/web/mvc/FileResult.hx +++ b/src/ufront/web/mvc/FileResult.hx @@ -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; } @@ -20,5 +22,6 @@ class FileResult extends ActionResult if(null != fileDownloadName) controllerContext.response.setHeader("content-disposition", "attachment; filename=" + fileDownloadName); + controllerContext.response.writeBytes(content); } } \ No newline at end of file diff --git a/src/ufront/web/mvc/ImageResult.hx b/src/ufront/web/mvc/ImageResult.hx new file mode 100644 index 0000000..d6e2e49 --- /dev/null +++ b/src/ufront/web/mvc/ImageResult.hx @@ -0,0 +1,52 @@ +package ufront.web.mvc; + +import thx.error.NullArgument; +import uimage.IImage; + +/** + * ... + * @author Franco Ponticelli + */ + +class ImageResult extends ActionResult +{ + public var format : ImageFormat; + public var image : IImage; + + public function new(image : IImage, ?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; +} \ No newline at end of file diff --git a/src/ufront/web/mvc/RedirectToControllerResult.hx b/src/ufront/web/mvc/RedirectToControllerResult.hx index 99c1cbc..611e433 100644 --- a/src/ufront/web/mvc/RedirectToControllerResult.hx +++ b/src/ufront/web/mvc/RedirectToControllerResult.hx @@ -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; - public function new(controllerName : String, actionName = "index", ?params : Hash) + public function new(?params : Hash, ?o : Dynamic) { - 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); } diff --git a/src/ufront/web/mvc/view/HtmlHelper.hx b/src/ufront/web/mvc/view/HtmlHelper.hx index a895604..b9ee60b 100644 --- a/src/ufront/web/mvc/view/HtmlHelper.hx +++ b/src/ufront/web/mvc/view/HtmlHelper.hx @@ -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 = {}; @@ -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) diff --git a/src/ufront/web/mvc/view/UrlHelper.hx b/src/ufront/web/mvc/view/UrlHelper.hx index 3cfb94c..60b757e 100644 --- a/src/ufront/web/mvc/view/UrlHelper.hx +++ b/src/ufront/web/mvc/view/UrlHelper.hx @@ -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 { @@ -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("&"); @@ -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; }