diff --git a/src/tink/web/macros/Routing.hx b/src/tink/web/macros/Routing.hx index 587c8e6..11e60b7 100644 --- a/src/tink/web/macros/Routing.hx +++ b/src/tink/web/macros/Routing.hx @@ -385,23 +385,16 @@ class Routing { case None: } - // var isResponse = !isNoise && t.unifiesWith(Context.getType('tink.web.Response')); - for (fmt in route.produces) formats.push( - macro @:pos(pos) if (ctx.accepts($v{fmt})) return ${{ - var e = macro tink.web.routing.Response.textual( + macro @:pos(pos) if (ctx.accepts($v{fmt})) + return tink.web.routing.Response.textual( $statusCode, - $v{fmt}, ${MimeType.writers.get([fmt], t, pos).generator}(__data__) - ); - - if(headers.length > 0) - e = macro { - var res = $e; - new tink.http.Response.OutgoingResponse(res.header.concat(${macro $a{headers}}), res.body); - } - e; - }}); + $v{fmt}, + ${MimeType.writers.get([fmt], t, pos).generator}(__data__), + $a{headers} + ) + ); macro @:pos(pos) tink.core.Promise.lift($result).next( function (__data__:$ct):tink.core.Promise { @@ -411,6 +404,7 @@ class Routing { ); case ROpaque(OParsed(res, t)): + // @:statusCode and @:header is ignored here, we should probably error/warn var ct = res.toComplex(); var formats = []; @@ -425,8 +419,6 @@ class Routing { case None: } - // var isResponse = !isNoise && t.unifiesWith(Context.getType('tink.web.Response')); - for (fmt in route.produces) formats.push( macro @:pos(pos) if (ctx.accepts($v{fmt})) return ${{ @@ -447,21 +439,21 @@ class Routing { var e = macro @:pos(pos) tink.core.Promise.lift($result) .next(function (v:$t):tink.web.routing.Response return v); switch [statusCode, headers] { - case [macro null, []]: + case [macro 200, []]: e; - case [macro null, _]: + case [macro 200, _]: macro $e.next(function(res) return new tink.http.Response.OutgoingResponse( res.header.concat(${macro $a{headers}}), res.body )); case [_, []]: macro $e.next(function (res) return new tink.http.Response.OutgoingResponse( - new tink.http.Response.ResponseHeader($statusCode, res.header.reason, @:privateAccess res.header.fields, res.header.protocol), + new tink.http.Response.ResponseHeader($statusCode, $statusCode, @:privateAccess res.header.fields, res.header.protocol), res.body )); case _: macro $e.next(function (res) return new tink.http.Response.OutgoingResponse( - new tink.http.Response.ResponseHeader($statusCode, res.header.reason, @:privateAccess res.header.fields.concat(${macro $a{headers}}), res.header.protocol), + new tink.http.Response.ResponseHeader($statusCode, $statusCode, @:privateAccess res.header.fields.concat(${macro $a{headers}}), res.header.protocol), res.body )); } diff --git a/src/tink/web/routing/Response.hx b/src/tink/web/routing/Response.hx index 6ab6345..e211567 100644 --- a/src/tink/web/routing/Response.hx +++ b/src/tink/web/routing/Response.hx @@ -23,15 +23,15 @@ abstract Response(OutgoingResponse) from OutgoingResponse to OutgoingResponse { return new OutgoingResponse(new ResponseHeader(Found, Found, [new HeaderField('location', u)]), Chunk.EMPTY); } - static public function binary(?code, contentType:String, bytes:Bytes):Response { + static public function binary(?code, contentType:String, bytes:Bytes, ?headers):Response { //TODO: calculate ETag - return OutgoingResponse.blob(code, bytes, contentType); + return OutgoingResponse.blob(code, bytes, contentType, headers); } static public function empty(?code = OK):Response { return new OutgoingResponse(new ResponseHeader(code, code, [new HeaderField(CONTENT_LENGTH, '0')]), Chunk.EMPTY); } - static public function textual(?code, contentType:String, string:String):Response - return binary(code, contentType, Bytes.ofString(string)); + static public function textual(?code, contentType:String, string:String, ?headers):Response + return binary(code, contentType, Bytes.ofString(string), headers); } diff --git a/src/tink/web/v2/Route.hx b/src/tink/web/v2/Route.hx index 9ac5ce9..4a92066 100644 --- a/src/tink/web/v2/Route.hx +++ b/src/tink/web/v2/Route.hx @@ -37,7 +37,7 @@ class Route { variants: call, statusCode: switch field.meta.extract(':statusCode') { - case []: macro null; + case []: macro 200; case [{params: [v]}]: v; case [v]: v.pos.error('@:statusCode must have one argument exactly'); case v: v[1].pos.error('Cannot have multiple @:statusCode directives');