-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Emil Persson
committed
Nov 7, 2015
1 parent
6211c50
commit 28877db
Showing
4 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
part of bridge.http; | ||
|
||
class FormMethodMiddleware { | ||
shelf.Handler innerHandler; | ||
|
||
call(shelf.Handler innerHandler) { | ||
this.innerHandler = innerHandler; | ||
return _handleRequests; | ||
} | ||
|
||
_handleRequests(shelf.Request request) async { | ||
shelf.Response response = await innerHandler(request); | ||
|
||
return new shelf.Response( | ||
response.statusCode, | ||
body: _injectFormMethods(await response.readAsString()), | ||
headers: response.headers, | ||
context: response.context); | ||
} | ||
|
||
String _injectFormMethods(String body) { | ||
const pattern = r'''(<form[^>]*?method=)(['"])(put|patch|update|delete)\2([^>]*>)'''; | ||
return body.replaceAllMapped( | ||
new RegExp(pattern, caseSensitive: false), (m) { | ||
return '${m[1]}${m[2]}POST${m[2]}${m[4]}' | ||
"<input type='hidden' name='_method' value='${m[3].toUpperCase()}'>"; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters