Code Monkey home page Code Monkey logo

Comments (5)

SergioBenitez avatar SergioBenitez commented on May 24, 2024

Currently, a Data can be only created from inside rocket. This has some restrictions. In my particular case, I cannot read part of the data and then wrap the rest in another Data object. This pattern is, however, needed by many protocols that built ontop of HTTP and have their own "header+payload" structure within the HTTP body (in my case, it's the IPP protocol).

This is very much on purpose. What we want to guarantee is that any Data is valid and generated from an incoming request. This is particular important when trying to assert application security properties.

Chainable data guards, as you point out, was indeed designed to solve the kind of problem you illustrate here without giving up the above premise. I believe it should suffice to implement what you're suggesting here. Can you take a look at the api and hash example? The latter takes hash in the prefix of the data body and compares it to a hash of the body itself without copying anywhere.

I would be in favor of a generalized data guard that implements a kind of transformer of the sort you refer to (header + body). Something like:

struct PrefixedData<'r; P: Prefix> {
  prefix: P,
  data: Data<'r>
}

And make implementing Prefix as trivial as possible for a variety of usa cases.

from rocket.

ARogovskyy avatar ARogovskyy commented on May 24, 2024

Thanks! Took me a bit to understand the API Transformer provides, but now I think I get it.

It works well with various "push/write" based parsers/transformers such as flate2 's types in the write module, or as with the hash-example in the repo. Basically everything where you have to push data to yourself. However, I think this API does not work quite as smoothly with read/pull-based ones. In the simplest case, a read-based parser for a "prefix" P as you wrote above would be a function

pub async fn some_pull_parser(read: &mut impl AsyncRead) -> P  {
   // do things above to parse the a prefix of "read" into P, by calling the various read.read_..() functions
}

Adapting such a function to the transformer API turns out to be not that easy. While playing with it, my current attempt would be having a transformer that diverts the fresh() part to an internal buffer from which it can be read using an AsyncRead impl. Then, once reading the prefix is done, it can go back to just transparently passing the rest of the data downstream.

However, such an approach has several quirks, such as the fact that a transformer is only triggered once the DataStream is actively read. So, to trigger parsing the prefix, one would have to spawn a background tokio task calling the prefix parser, then peek one byte of the DataStream or so.

from rocket.

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.