Skip to content

Commit

Permalink
feat: 405 Method not allowed. feat: handleCors
Browse files Browse the repository at this point in the history
  • Loading branch information
iyifr committed Oct 2, 2024
1 parent d3ff228 commit 9674290
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
14 changes: 7 additions & 7 deletions bin/run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import 'dart:math';

import 'package:h4/create.dart';
import 'package:h4/utils/body_utils.dart';
import 'package:h4/utils/request_utils.dart';
import 'package:h4/utils/req_utils.dart';
// import 'package:h4/utils/request_utils.dart';

void main(List<String> arguments) async {
var app = createApp(
port: 5173,
onRequest: (event) => {},
afterResponse: (event) => {},
onRequest: (event) {
handleCors(event, origin: "https://example.re");
},
);

var router = createRouter();
app.use(router);

router.post("/vamos/:id/**", (event) async {
var body = await readRequestBody(event);
print(body["map"]);
var header = getHeader(event, HttpHeaders.userAgentHeader);
var query = getQueryParams(event);
return [header, body, query, event.params];
var header = getRequestUrl(event);
return [header, body];
});

Future<String> unreliableFunction() async {
Expand Down
44 changes: 40 additions & 4 deletions lib/src/h4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,15 @@ class H4 {

if (match != null) {
handler = match[request.method];
} else {
// Return 404 Not found.
return404(request)(middlewares, null);
}

// If we find no match for the request signature - 404.
if (handler == null || match == null) {
return404(request)(middlewares, null);
// If we find no match for the request method - 405 (Not allowed).
if (handler == null) {
print(match?.keys.join(','));
return405(request)(middlewares, null, match);
}

// We've found a match - handle the request.
Expand Down Expand Up @@ -260,7 +264,10 @@ typedef NotFoundHandler = dynamic Function(
MiddlewareStack stack, Map<String, String>? params);

NotFoundHandler return404(HttpRequest request) {
return (stack, params) {
return (
stack,
params,
) {
return defineEventHandler(
(event) {
event.statusCode = 404;
Expand All @@ -277,3 +284,32 @@ NotFoundHandler return404(HttpRequest request) {
)(request);
};
}

typedef MethodNotAllowedHandler = dynamic Function(
MiddlewareStack stack,
Map<String, String>? params,
Map<String, dynamic Function(H4Event)?>? match);

MethodNotAllowedHandler return405(HttpRequest request) {
return (stack, params, match) {
return defineEventHandler(
(event) {
event.statusCode = 405;
setResponseHeader(event,
header: HttpHeaders.contentTypeHeader, value: 'application/json');

setResponseHeader(event,
header: HttpHeaders.allowHeader,
value: match?.keys.join(',') ?? "");
return {
"statusCode": 405,
"statusMessage": "Method Not Allowed",
"message":
"Cannot ${event.method.toUpperCase()} - ${event.path}. Reason: Method not allowed, ${match?.keys.join(',').toUpperCase()} available."
};
},
stack,
params,
)(request);
};
}
12 changes: 12 additions & 0 deletions lib/utils/req_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ String? getRequestHost(H4Event event) {
return event.node["value"]?.headers.value(HttpHeaders.hostHeader);
}

String? getRequestUrl2(H4Event event) {
return event.node["value"]?.headers.value("Origin");
}

String? getRequestUrl(H4Event event) {
return '${getRequestProtocol(event)}://${getRequestHost(event)}${event.path}';
}
Expand All @@ -35,6 +39,14 @@ String? getRouterParams(H4Event event, {required String name}) {
return event.params.containsKey(name) ? event.params[name] : null;
}

void handleCors(H4Event event, {String origin = "*", String methods = "*"}) {
event.node["value"]!.response.headers
.set(HttpHeaders.accessControlAllowOriginHeader, origin);

event.node["value"]!.response.headers
.set(HttpHeaders.accessControlAllowMethodsHeader, methods);
}

class FormData {
final Map<String, List<String>> _data = {};

Expand Down

0 comments on commit 9674290

Please sign in to comment.