pub repository
mirror.p-hatan.irshelf
0.5.4+3Web Server Middleware for Dart
Readme
Web Server Middleware for Dart
Introduction
Shelf makes it easy to create and compose web servers and parts of web servers. How?
- Expose a small set of simple types.
- Map server logic into a simple function: a single argument for the request, the response is the return value.
- Trivially mix and match synchronous and asynchronous processing.
- Flexibliity to return a simple string or a byte stream with the same model.
Example
See example/example_server.dart
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
void main() {
var handler = const shelf.Pipeline().addMiddleware(shelf.logRequests())
.addHandler(_echoRequest);
io.serve(handler, 'localhost', 8080).then((server) {
print('Serving at http://${server.address.host}:${server.port}');
});
}
shelf.Response _echoRequest(shelf.Request request) {
return new shelf.Response.ok('Request for "${request.url}"');
}
Handlers and Middleware
A handler is any function that handles a shelf.Request and returns a shelf.Response. It can either handle the request itself--for example, a static file server that looks up the requested URI on the filesystem--or it can do some processing and forward it to another handler--for example, a logger that prints information about requests and responses to the command line.
The latter kind of handler is called "middleware", since it sits in the middle of the server stack. Middleware can be thought of as a function that takes a handler and wraps it in another handler to provide additional functionality. A Shelf application is usually composed of many layers of middleware with one or more handlers at the very center; the shelf.Pipeline class makes this sort of application easy to construct.
Some middleware can also take multiple handlers and call one or more of them for each request. For example, a routing middleware might choose which handler to call based on the request's URI or HTTP method, while a cascading middleware might call each one in sequence until one returns a successful response.
Adapters
An adapter is any code that creates shelf.Request objects, passes them to a
handler, and deals with the resulting shelf.Response. For the most part,
adapters forward requests from and responses to an underlying HTTP server;
shelf_io.serve is this sort of adapter. An adapter might also synthesize
HTTP requests within the browser using window.location and window.history,
or it might pipe requests directly from an HTTP client to a Shelf handler.
When implementing an adapter, some rules must be followed. The adapter must not
pass the url or scriptName parameters to new shelf.Request; it should
only pass requestedUri. If it passes the context parameter, all keys must
begin with the adapter's package name followed by a period. If multiple headers
with the same name are received, the adapter must collapse them into a single
header separated by commas as per RFC 2616 section 4.2.
An adapter must handle all errors from the handler, including the handler
returning a null response. It should print each error to the console if
possible, then act as though the handler returned a 500 response. The adapter
may include body data for the 500 response, but this body data must not include
information about the error that occurred. This ensures that unexpected errors
don't result in exposing internal information in production by default; if the
user wants to return detailed error descriptions, they should explicitly include
middleware to do so.
An adapter should include information about itself in the Server header of the response by default. If the handler returns a response with the Server header set, that must take precedence over the adapter's default header.
An adapter should include the Date header with the time the handler returns a response. If the handler returns a response with the Date header set, that must take precedence.
An adapter should ensure that asynchronous errors thrown by the handler don't cause the application to crash, even if they aren't reported by the future chain. Specifically, these errors shouldn't be passed to the root zone's error handler; however, if the adapter is run within another error zone, it should allow these errors to be passed to that zone. The following function can be used to capture only errors that would otherwise be top-leveled:
/// Run [callback] and capture any errors that would otherwise be top-leveled.
///
/// If [this] is called in a non-root error zone, it will just run [callback]
/// and return the result. Otherwise, it will capture any errors using
/// [runZoned] and pass them to [onError].
catchTopLevelErrors(callback(), void onError(error, StackTrace stackTrace)) {
if (Zone.current.inSameErrorZone(Zone.ROOT)) {
return runZoned(callback, onError: onError);
} else {
return callback();
}
}
Inspiration
- Connect for NodeJS.
- Read this great write-up to understand the overall philosophy of all of these models.
- Rack for Ruby.
- WSGI for Python.
Changelog
0.5.4+3
- Widen the version constraint on the
collectionpackage.
0.5.4+2
- Updated headers map to use a more efficient case-insensitive backing store.
0.5.4+1
- Widen the version constraint for
stack_trace.
0.5.4
-
The
shelf_ioadapter now sends theDateHTTP header by default. -
Fixed logic for setting Server header in
shelf_io.
0.5.3
- Add new named parameters to
Request.change:scriptNameandurl.
0.5.2
-
Add a
Cascadehelper that runs handlers in sequence until one returns a response that's neither a 404 nor a 405. -
Add a
Request.changemethod that copies a request with new header values. -
Add a
Request.hijackmethod that allows handlers to gain access to the underlying HTTP socket.
0.5.1+1
-
Capture all asynchronous errors thrown by handlers if they would otherwise be top-leveled.
-
Add more detail to the README about handlers, middleware, and the rules for implementing an adapter.
0.5.1
- Add a
contextmap toRequestandResponsefor passing data among handlers and middleware.
0.5.0+1
- Allow
scheduled_testdevelopment dependency up to v0.12.0
0.5.0
- Renamed
StacktoPipeline.
0.4.0
-
Access to headers for
RequestandResponseis now case-insensitive. -
The constructor for
Requesthas been simplified. -
Requestnow exposesurlwhich replacespathInfo,queryString, andpathSegments.
0.3.0+9
-
Removed old testing infrastructure.
-
Updated documentation address.
0.3.0+8
- Added a dependency on the
http_parserpackage.
0.3.0+7
- Removed unused dependency on the
mimepackage.
0.3.0+6
- Added a dependency on the
string_scannerpackage.
0.3.0+5
- Updated
pubspecdetails for move to Dart SDK.
0.3.0 2014-03-25
Response- NEW!
int get contentLength - NEW!
DateTime get expires - NEW!
DateTime get lastModified
- NEW!
Request- BREAKING
contentLengthis now read fromheaders. The constructor argument has been removed. - NEW! supports an optional
Stream<List<int>> bodyconstructor argument. - NEW!
Stream<List<int>> read()andFuture<String> readAsString([Encoding encoding]) - NEW!
DateTime get ifModifiedSince - NEW!
String get mimeType - NEW!
Encoding get encoding
- BREAKING
0.2.0 2014-03-06
- BREAKING Removed
Shelfprefix from all classes. - BREAKING
Responsehas drastically different constructors. - NEW!
Responsenow accepts a body of eitherStringorStream<List<int>>. - NEW!
Responsenow exposesencodingandmimeType.
0.1.0 2014-03-02
- First reviewed release
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add shelfpubspec.yaml
name: shelf
version: 0.5.4+3
author: Dart Team <misc@dartlang.org>
description: Web Server Middleware for Dart
homepage: http://www.dartlang.org
environment:
sdk: '>=1.4.0 <2.0.0'
dependencies:
collection: '>=0.9.4 <2.0.0'
http_parser: '>=0.0.0 <0.1.0'
path: '>=1.0.0 <2.0.0'
stack_trace: '>=0.9.2 <2.0.0'
dev_dependencies:
http: '>=0.9.2 <0.12.0'
scheduled_test: '>=0.10.0 <0.12.0'
unittest: '>=0.10.0 <0.12.0'
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.0.0-dev | 2014-01-02T04:21:30.895900Z | Download | 4afe71670b99facba517bec1f61284a898f4f6e9b08f775ebfbd6385271b7018 |
| 0.0.0-dev+1 | 2014-01-03T19:23:52.752120Z | Download | bf52b1ade3612fc8525da0c8b48c9ea01f98b65d4b2a4405f3df904db29134c7 |
| 0.0.1 | 2014-03-02T21:47:55.269770Z | Download | bff176f0e52173cfba29c3f36fa5bf5ba195899253c42341a49c952edb3215a2 |
| 0.1.0 | 2014-03-03T04:17:39.447740Z | Download | 568505d6d06567bbf59b6884ae55c578b161a3bde049760402a3efc97e2af414 |
| 0.2.0 | 2014-03-06T19:30:43.874240Z | Download | 2acad0d25ea5d9ab171b629560afdbc647638af4a3e13f3147f078395aa59f3a |
| 0.3.0 | 2014-03-25T22:54:04.048770Z | Download | a2ab589e45ae87f52d92fc6e8dc4f98a1c9da73820b2407e4bbd07bf2452ae28 |
| 0.3.0+1 | 2014-03-29T04:57:59.469200Z | Download | e90d8386034d8ac3be45587a14382429e3a5f441e64f7ec304a191f7398a7ff6 |
| 0.3.0+2 | 2014-03-29T17:20:37.346960Z | Download | 3597889d2712d6ec1583f85d03b735ee5cfe8d171084028cd338b805ad91ec5f |
| 0.3.0+3 | 2014-03-29T17:22:36.825620Z | Download | c0074eb5b6c7c2415f6f0c84efa4e04a686c43bbbf5b6b463969c8cbc7085beb |
| 0.3.0+4 | 2014-03-31T23:55:15.696990Z | Download | ea625f245bc7aa691f851326fac15e33b5c690246bf201dfb7ae7b55360344bb |
| 0.3.0+6 | 2014-04-02T21:00:31.644650Z | Download | d25f50dc4d8a07a4dce67a62ae8b1a20d396693d2ab3ca7eb1fa6bce116dbaec |
| 0.3.0+7 | 2014-04-02T22:17:43.059830Z | Download | ef72a7801b5d9360224c6bdc64727d4d7e3b6fe0ac714fe72610e8fcec0c0290 |
| 0.3.0+8 | 2014-04-03T00:55:31.518580Z | Download | 9c1e66ae752a2ad0c65ba1ef0975393dbc06cd48c87775a20d30fa9960219e65 |
| 0.4.0 | 2014-04-09T01:04:14.555070Z | Download | 3b08fb62b25d73ec0c83f85ebae64312de7d8286aca1cf3eb3d36e34eddf8108 |
| 0.5.0 | 2014-04-15T21:36:46.815050Z | Download | a3ee1d6497a55e615463753ef87d4b0abd26c3238aa7d06ba8ba33f06fe49b8c |
| 0.5.0+1 | 2014-04-15T23:28:42.888530Z | Download | 278b49838c82f1e382129a1d26960e91cb6dbb579cb85e69a8c1efb4cefee4b0 |
| 0.5.1 | 2014-04-19T20:23:23.664390Z | Download | d9f531b2abc38e41566b527ceb9888907042bfe6e1eb0fcf6249f712847e93f8 |
| 0.5.1+1 | 2014-04-25T18:18:29.944920Z | Download | 022aaed807297854b5633c788d9697732bada6fa4447a746f35af50b5cccd73c |
| 0.5.2 | 2014-05-19T23:06:58.011930Z | Download | 74ce1e60a632481ecda152c93971c32bbaf625fc7350aa6b18851adb69ab62d5 |
| 0.5.3 | 2014-05-24T00:35:48.768220Z | Download | 4b34cf05d5bcfa259ea947c87013d8cb0e5700c697bed6a409babd3bfdc2e046 |
| 0.5.4 | 2014-05-28T00:50:56.801980Z | Download | 1d03f958c69f79b41848961cf20ac00ef6887837cc9ca2220edc601009235fda |
| 0.5.4+1 | 2014-06-02T18:42:13.778200Z | Download | 9369e89d137d2cabb24a8ac0e81f439b0a882adfbb09fd9ebbca8d12d442555e |
| 0.5.4+2 | 2014-07-07T23:54:22.795470Z | Download | 82366dae4eae3d62e0a2e234aa903820ade568e09c21f6d3efc53570847f3743 |
| 0.5.4+3 | 2014-08-26T19:51:23.025180Z | Download | 38e64be014fcd16e9081182decdaa923f7136854ca7cd9310f52feb41c9924cf |
| 0.5.5 | 2014-09-23T21:49:55.098930Z | Download | e1813b8a976a108fdaacb777f1f5931f2a5698f319bc6c9a1338b220cf3988f1 |
| 0.5.5+1 | 2014-10-14T17:39:16.515670Z | Download | 2d75f5b2fd4e911b27fa5374785825aea5cbc1be607570b0dff1543f2e516e72 |
| 0.5.6 | 2014-11-24T21:25:56.562160Z | Download | 2034faee5635e240b776c3e89086eac6da8af782e3b139d5fd811c63db0d91df |
| 0.5.7 | 2015-01-21T02:16:45.987590Z | Download | ceb5c0cc2fb6a816e7feaad7cad2a3e9431842fe98c7a0ee050df8662c22f68e |
| 0.6.0 | 2015-03-04T23:48:16.805910Z | Download | 6f5c80218e702533845d1ad52193d135ba2b3a3ac5f90c1e1de62b23c5faca7b |
| 0.6.1 | 2015-03-25T22:14:19.013280Z | Download | 84755ef640744b706f9053877665140d40f2c88f7781d1bd3968b529bca6a1d6 |
| 0.6.1+1 | 2015-03-25T22:32:47.282770Z | Download | 1f53eb9d75a548906041dd954af4efe5959949d8da31a6d804d4e30fc674c22d |
| 0.6.1+2 | 2015-05-11T18:06:37.080810Z | Download | 313d3c272e2e86f53ca222e8907af8269744a4d680cf157c4c66eb54c808116a |
| 0.6.1+3 | 2015-07-06T20:08:45.815Z | Download | c00be6a3b3f76d358eb9d3a0578c4c98a5ef9ae25b80b9f4fee2cedf7422b311 |
| 0.6.2 | 2015-07-06T21:58:07.268Z | Download | c7a20d3de8c59b6adf002e75c45ea67c946e21f9cfd16d3722862948e53ee237 |
| 0.6.2+1 | 2015-08-26T19:16:57.427Z | Download | 8de626251fceae4520da38de3cf50e4ada6b0f6a2c845b6ca097614ca406ef8a |
| 0.6.3 | 2015-09-01T02:02:04.879Z | Download | 93bf93b28820d22f522b30cc615f07327dfa93a3fc64aace4d44b03c54341d4a |
| 0.6.3+1 | 2015-10-21T00:21:38.315Z | Download | 59b4d09c05c9960531d29564aab4f439f788453117dbfe1076f67124e05838ba |
| 0.6.4 | 2015-10-26T23:03:14.853Z | Download | a15637837af36947dad463901fafe31f2b0854111f7f33c78a71f4a183364942 |
| 0.6.4+1 | 2015-10-28T23:24:44.383Z | Download | c2fbffc4a9a4f3fb8afe25555cfff0de6abdff38d17125dfbad72a56b26385b3 |
| 0.6.4+2 | 2015-11-18T00:24:11.914Z | Download | 02945044d2a1fe0b68fc8ba3a24c38a2b21fd9875a09e7b89cfe4bf137cbed43 |
| 0.6.4+3 | 2015-12-14T22:22:21.287Z | Download | 3cb4475add687430d03510ce4da56a533193ad3354270c054186e4680a19c1af |
| 0.6.5 | 2016-01-28T22:37:40.524Z | Download | 52177a1e99a0f857281cd428a55304fbf9f46ade764e8780efa2300e37c7b28d |
| 0.6.5+1 | 2016-05-04T18:11:49.132Z | Download | cb7ba37351f542cf03586bdf21951401c5d2f6ce78de2d0263f8292253ca4744 |
| 0.6.5+2 | 2016-05-05T17:21:08.202Z | Download | 9d70d71e679f344a4c9744e0edce54815c01cf9e3268d5f27868d655e2b5b8ee |
| 0.6.5+3 | 2016-08-25T20:51:11.151Z | Download | 70f564e70216fda2f6acdc5cd6eee9225c3cca8ec6c0c4d84b5ac5b0fb5fd45e |
| 0.6.6 | 2016-10-24T22:29:47.317Z | Download | 22e2aa035b95542a95bfa9fa62c88e38ca6024f2844c10471be707c5e6954d92 |
| 0.6.7 | 2016-10-26T20:23:15.178Z | Download | 3c1000c28b58f8b829c3b65acf474dec680fc789062336a44f694f33a14237c9 |
| 0.6.7+1 | 2016-10-26T20:58:19.817Z | Download | bff1098421f3d91f17ec7f6dfeaab94167b90b88f14acbeb1c275629aa175e95 |
| 0.6.7+2 | 2016-12-06T23:27:18.653Z | Download | e5abb9e0731acc24480df302a9b4a0c2bc46b976dd24f24cf4347be1833b8f03 |
| 0.6.8 | 2017-06-23T00:27:45.164869Z | Download | 9818547d45dbde101628d14f4be3e3c100412c2ce993077b8ff8eae448ff1acb |
| 0.7.0 | 2017-08-29T22:04:21.858822Z | Download | 654dc74da4ad9fe1179d0b7f4fbf3fd43564bf7b57486ee057bca1c0e8c7e98e |
| 0.7.1 | 2017-10-26T19:17:52.532829Z | Download | b6f70437bb743937198ad1107e49af2f1adf54d193a83d8c20a7662b3c1a3808 |
| 0.7.2 | 2018-01-04T20:38:48.380923Z | Download | 18842d1020dcc041a7e347e6183be6854a3f44b93c400cdb24386e863a6511d1 |
| 0.7.3 | 2018-05-02T23:15:13.592126Z | Download | c0043920f19d4329109562f4d57f7598ae98f4f3894a1fec0ea77def98a9bead |
| 0.7.3+1 | 2018-05-22T15:25:18.538698Z | Download | 32ddaacee31749472382d3feff300f25224a8a22f4f8a0daae34bb7745bcc0c6 |
| 0.7.3+2 | 2018-07-10T00:10:58.426334Z | Download | 52ecabc1b0197162c0736f90877d1d834007a46c1c0004dec016a3c2a9ddfd5f |
| 0.7.3+3 | 2018-07-16T16:40:59.854134Z | Download | dd4a28b16310c3501a573716bd3025710823b2728909c4856f96bd36901d84e5 |
| 0.7.4 | 2018-12-17T17:13:38.380190Z | Download | 10b9973865473afb3665f279551e2844c2828738a5aa4b4a0b08f9262b99171e |
| 0.7.4+1 | 2019-03-27T20:29:28.837278Z | Download | e51ec952cbe2e93eaa3a848d0291e1e5ce9b229c15f9cc1656521c572c531b28 |
| 0.7.5 | 2019-04-01T16:03:31.169159Z | Download | c256a07581278075dc1ccd0f8c63c4177f74280dded1e70107371125242486ff |
| 0.7.6 | 2020-06-18T16:47:47.249142Z | Download | 86888c6295f3b6c6e29054c45b389da7b8389b5c8da88cb17a508f91bbea7b89 |
| 0.7.7 | 2020-06-19T17:13:37.434539Z | Download | 739463c850fd26279ce763fa883c685a26fcf7600d7b8c6f9e72253f3dc485cf |
| 0.7.8 | 2020-08-12T15:59:44.584570Z | Download | 257f7b850c9278dee6d63a1f553e8ff080f0d95e6b37f30ae39303f4d030988a |
| 0.7.9 | 2020-08-14T03:32:37.050758Z | Download | d16d1197f877e814f767be03c1c5940a397ae0ddb46dcfb9b5465074bb80ee37 |
| 1.0.0-nullsafety.0 | 2021-01-29T16:27:07.654638Z | Download | e6536cccce2ed77e0f5a3913ba60a181e6be5d73ba1ddeb39edf1e372818f367 |
| 1.0.0 | 2021-02-05T20:51:35.173441Z | Download | c0710a5ca61f1a96e9aae37081040f537c1b96265040edeb70c8817a10d361c6 |
| 1.1.0 | 2021-03-17T19:41:46.781390Z | Download | c2f658d28ec86857657dec3579e2db4dc5a6c477b6aecde870e77f0682258901 |
| 1.1.1 | 2021-04-23T23:45:40.691720Z | Download | cb24b18e3d32d08418e82daeef644c80ed616288de5e34cbae5c05879bfafe55 |
| 1.1.2 | 2021-05-07T20:51:34.677176Z | Download | 83ad9c33171d30672abd22f6bb449205e83fcf6dc4f13a4bd5801c388590f0d7 |
| 1.1.3 | 2021-05-13T15:48:06.050824Z | Download | 85a7ff4073f37b55033702fffb6d2ab33978ac1d1fee07ce1e00cd143f8d0a1f |
| 1.1.4 | 2021-05-13T19:24:52.844929Z | Download | 26d22c68cf9c349fb1adb4a830da3e014cb5f7ed9cc57f721d9d9e20cf8d6de5 |
| 1.2.0 | 2021-07-08T19:24:05.365533Z | Download | c240984c924796e055e831a0a36db23be8cb04f170b26df572931ab36418421d |
| 1.3.0 | 2022-03-28T18:29:51.395586Z | Download | 4592f6cb6c417632ebdfb63e4db42a7e3ad49d1bd52d9f93b6eb883035ddc0c3 |
| 1.3.1 | 2022-06-16T01:11:10.130313Z | Download | f27c6406c89ab3921ed4a3bbdc38d48676f8b78fe537cbdbeb0d2c0f661026d7 |
| 1.3.2 | 2022-07-28T18:57:56.641660Z | Download | 8ec607599dd0a78931a5114cdac7d609b6dbbf479a38acc9a6dba024b2a30ea0 |
| 1.4.0 | 2022-09-20T23:40:25.736807Z | Download | c24a96135a2ccd62c64b69315a14adc5c3419df63b4d7c05832a346fdb73682c |
| 1.4.1 | 2023-05-03T19:28:35.057465Z | Download | ad29c505aee705f41a4d8963641f91ac4cee3c8fad5947e033390a7bd8180fa4 |
| 1.4.2 | 2024-06-21T17:47:04.083038Z | Download | e7dd780a7ffb623c57850b33f43309312fc863fb6aa3d276a754bb299839ef12 |
| 1.4.3 | 2026-10-16T22:20:00.002809Z | Download | e75e9cf311d7fd712048038cc17c25199c38536c20330d53e7db058b543d5a48 |
Dependencies
collection "\u003e=0.9.4 \u003c2.0.0"
http_parser "\u003e=0.0.0 \u003c0.1.0"
path "\u003e=1.0.0 \u003c2.0.0"
stack_trace "\u003e=0.9.2 \u003c2.0.0"