Comments (7)
Some adapters wrapping strings, byte-arrays & functions into Readers might be all that's needed, as issue #48 suggested.
Well, that's already the case. You can use Response::from_string
, Response::from_data
, etc.
from tiny-http.
Thanks for pointing those out. I see that I can use them .with_status_code and .with_header. It looks like I'm stuck with the provided Content-Type if I use from_string but not if I pass a String to from_data. Studying the code for from_data I've simplified my code to:
fn cursor_on<D>(data: D)->Cursor<Vec<u8>> where D: Into<Vec<u8>> {
Cursor::new(data.into())
}
fn str_response(
status_code: i32, headers: Vec<tiny_http::Header>, str_data: String
) -> tiny_http::Response<Cursor<Vec<u8>>> {
let data_len = str_data.len();
tiny_http::Response::new(
tiny_http::StatusCode::from(status_code), headers,
cursor_on(str_data), Some(data_len), None )
}
...
let some_headers: Vec<tiny_http::Header> = Vec::new();
let some_html: String = "...";
req.respond( str_response(200, some_headers, some_html) );
from tiny-http.
You shouldn't need to use Response::new()
.
For headers, there's a with_header
method:
req.respond(tiny_http::from_string("hello world")
.with_status_code(200)
.with_header(tiny_http::Header::from_bytes(&b"Content-Type"[..], &b"text/plain"[..]));
Just like Method
in the other issue, I agree that headers should be easier to create.
from tiny-http.
In my application I will be receiving the desired headers and putting them into a collection. Since the number of headers is unknown until runtime, I'll have to use new. I could avoid new if there were a with_headers (plural) method but calling new is in some ways still easier.
from tiny-http.
What about:
let mut response = tiny_http::from_string("hello world");
for header in headers_list {
response = response.with_header(header);
}
from tiny-http.
Note that this doesn't mean that with_headers
or some functions that make your life easier shouldn't be added.
from tiny-http.
My code examples above could certainly be restructured as you suggest tomaka. My real application gets all of the components for the response in one header/value table from the database, including the status code, headers & body (the status code and body are just special "headers"), so it therefore makes most sense for me to just call Response::new now that I'm more clear about what datatypes it requires. BTW I notice that it would be easy to accidentally send duplicate or conflicting headers using with_header.
from tiny-http.
Related Issues (20)
- Consider implementing Hash for Method HOT 1
- Panic when out of file descriptors HOT 7
- Maintenance Status HOT 5
- If could if why if is if could if server if could if unreachable if inside if of if codeblock? HOT 1
- Pushes to master are gated on Travis CI step HOT 1
- why four times thread are getting created for a single server? HOT 4
- Sending response before request is fully read HOT 1
- Add a feature or option to turn off the `Server: tiny-http (Rust)` header HOT 4
- tiny_http::Server::https (segmentation fault) HOT 1
- Continue serving requests after failing to spawn a thread HOT 3
- Use bounded thread pool HOT 10
- SSL configuration doesn't support certificate chains HOT 2
- Wrong dependency version of `log` (fails after `cargo update -Z minimal-versions`) HOT 1
- Custom transports
- feat(num_connections) : request HOT 1
- reqwest.get from main thread to tiny-http server in other thread HOT 16
- Why not use eprintln?
- How to setup a proxy?
- Rework HTTP response logic to avoid allocation
- Use rust-native-tls instead of openssl directly HOT 2
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 tiny-http.