Comments (8)
No you shouldn't ignore the Content-Length header, if it is there you are supposed to use it. This is especially important for streaming larger respones where the iterable doesn't have a length of 1 (like in my example above). That said, my example is flawed since I do not implement support for len()
at all -- that said I do not think that granian checks this anywhere currently.
My example about closing the connection was mostly about setting content length to 300 when the actual payload was "hello world" (ie shorter than 300). In this case the server has no good option but to close the connection since it would already have written "hello world" to the wire (in an ideal case) when it realizes that the rest of the data does not come.
from granian.
Using
def app(env, start_response):
start_response('200 OK', [('Content-Type','text/html'), ('Content-Length', '300')])
return Response([b"Hello", b" ", b"World"])
should not hang the server, from https://peps.python.org/pep-3333/#handling-the-content-length-header
Of course, if the application does not provide enough data to meet its stated Content-Length, the server should close the connection and log or otherwise report the error.
from granian.
start_response
must return a write(body_data)
callable, currently it returns None. Note that this mostly for older frameworks but the spec still requires it afaik.
start_response
also need to support an optional exc_info
from granian.
If I am reading the code correctly, the whole response is also evaluated at once:
Line 56 in 653f93a
It would be better to write that back in a streaming fashion.
from granian.
Using
def app(env, start_response): start_response('200 OK', [('Content-Type','text/html'), ('Content-Length', '300')]) return Response([b"Hello", b" ", b"World"])should not hang the server, from https://peps.python.org/pep-3333/#handling-the-content-length-header
Of course, if the application does not provide enough data to meet its stated Content-Length, the server should close the connection and log or otherwise report the error.
https://peps.python.org/pep-3333/#handling-the-content-length-header also says
Under some circumstances, however, the server or gateway may be able to either generate a Content-Length header, or at least avoid the need to close the client connection. If the application does not call the write() callable, and returns an iterable whose len() is 1, then the server can automatically determine Content-Length by taking the length of the first bytestring yielded by the iterable.
So I think you can also ignore Content-Length and automatically determine Content-Length, instead of closing the connection (with is a more pleasing experience for everyone I think)
from granian.
No you shouldn't ignore the Content-Length header, if it is there you are supposed to use it. This is especially important for streaming larger respones where the iterable doesn't have a length of 1 (like in my example above). That said, my example is flawed since I do not implement support for
len()
at all -- that said I do not think that granian checks this anywhere currently.My example about closing the connection was mostly about setting content length to 300 when the actual payload was "hello world" (ie shorter than 300). In this case the server has no good option but to close the connection since it would already have written "hello world" to the wire (in an ideal case) when it realizes that the rest of the data does not come.
You are right.
from granian.
The issue with closing the response iterator is now fixed, so I'm closing this.
In regards to supporting iteration over responses, you can open up a dedicated issue.
from granian.
from granian.
Related Issues (20)
- Consider suport for custom `Server` header
- ASGI Flow Error on websockets unidirectional pong while awaiting response HOT 5
- Implement --pid HOT 2
- Strange error with starlette / fastapi HOT 6
- Add `__rsgi_del__` support to RSGI spec
- `--opt` in RSGI mode is incompatible with `asyncio.timeout` HOT 1
- Running wrk with Django 5 wsgi opens many db connections HOT 6
- Handling of Left-to-Right Mark (U+200E) in URLs HOT 3
- Memory leak when using Granian in Django HOT 6
- jemalloc and freebsd HOT 5
- Pathos for multiprocessing? HOT 4
- WSGI response iterator lagging with Flask when mimetype="text/event-stream" HOT 4
- WSGI headers with accented characters are not passed to the app HOT 5
- App factory HOT 1
- Stop responding after load testing HOT 5
- Log configurations access date formatter is ignored HOT 3
- Access log logs wrong status on file responses HOT 5
- Thread panic in FastAPI streaming response HOT 6
- Add CLI option to provide a factory function name
- Add Granian extended reloading options HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from granian.