Skip to content

Commit

Permalink
Cleanup statusCode/header codes
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinresol committed May 10, 2019
1 parent 5becd8d commit 90b0dce
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
32 changes: 12 additions & 20 deletions src/tink/web/macros/Routing.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<tink.web.routing.Response> {
Expand All @@ -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 = [];

Expand All @@ -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 ${{
Expand All @@ -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
));
}
Expand Down
8 changes: 4 additions & 4 deletions src/tink/web/routing/Response.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/tink/web/v2/Route.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 90b0dce

Please sign in to comment.