Giter VIP home page Giter VIP logo

Comments (7)

tomaka avatar tomaka commented on May 30, 2024

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.

GregDavidson avatar GregDavidson commented on May 30, 2024

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.

tomaka avatar tomaka commented on May 30, 2024

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.

GregDavidson avatar GregDavidson commented on May 30, 2024

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.

tomaka avatar tomaka commented on May 30, 2024

What about:

let mut response = tiny_http::from_string("hello world");

for header in headers_list {
    response = response.with_header(header);
}

from tiny-http.

tomaka avatar tomaka commented on May 30, 2024

Note that this doesn't mean that with_headers or some functions that make your life easier shouldn't be added.

from tiny-http.

GregDavidson avatar GregDavidson commented on May 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.