Giter Site home page Giter Site logo

gotham_formdata's Introduction


⚠️ This crate is still alpha. APIs are subject to change. ⚠️

gotham_formdata ✍️


This crate is an extension to the popular gotham web framework for Rust. It aims to reduce boilerplate necessary to read request bodies today as a stop-gap until gotham finally implements a body extractor.

✨ Features

  • Parse application/x-www-form-urlencoded request bodies
  • Parse multipart/form-data request bodies
  • Verify the parsed request body
  • #![forbid(unsafe_code)] ensures that all functionality is implemented in 100% safe Rust code

⚠️ Warning

This crate is asynchronous, but does not yet enforce uploads limits. YOU ARE RESPONSIBLE FOR ENFORCING UPLOAD LIMITS.

🗒️ Example

use gotham_formdata::FormData;
use validator::Validate;

#[derive(FormData, Validate)]
struct LoginData {
	#[validate(length(min = 5, max = 16))]
	username: String,
	#[validate(length(min = 8))]
	password: String
}

async fn login_handler(state: &mut State) -> Result<Response<Body>, HandlerError> {
	let login_data: LoginData = FormData::parse_form_data(state).await?;
	Ok(if login_data.password == "secret" {
		create_response(state, StatusCode::OK, TEXT_PLAIN, login_data.username)
	} else {
		create_empty_response(state, StatusCode::FORBIDDEN)
	})
}

🏷️ Versioning

Like all rust crates, this crate will follow semantic versioning guidelines. However, changing the MSRV (minimum supported rust version) is not considered a breaking change.

📃 License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

gotham_formdata's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar msrd0 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

gotham_formdata's Issues

Consider using serde

Currently, this crate parses raw key=value types where both key and value are strings, and then relies on a custom derive macro for the key and a FromStr implementation to parse the value into its Rust type.

Pro

  • feature-rich derive macros today, no self-brewed solution
  • easy support for other dataformats like JSON down the line

Con

  • implementing a custom Deserializer for a non-self-describing format is harder than using FromStr
  • serde is not built async-first, which will likely become a problem

Form Data Validation

Could look something like proposed here: gotham-rs/gotham#11 (comment)

Progress

  • Implement initial validation (#2)
  • Implement some common validators
    • min_length, max_length for string types (#5)
    • min, max for integer types (#6)
    • regex for string types (#8)
    • uppercase_only, lowercase_only, trimmed and similar for string types can be done with regex
    • expected for all types that implement PartialEq (#9)
  • Allow specifying custom error messages (#11)
  • Allow multiple validators to be combined (#12)

Support file uploads

Now that #14 has landed, we should support file uploads by simply streaming the bytes to a temporary file on the server.

Async body parsing

Currently, the part about this crate that is "async" is reading the async hyper body into memory. Everything else is synchronous. This is less of an issue with urlencoded bodies but a huge problem for multipart bodies, where large files might be uploaded as part of the body that shouldn't be accumulated in memory.

multipart-async is an effort of porting multipart to support async, but the current release is 3 years old and depends on the old futures 0.1.

Generic Types

While we use generics.split_for_impl() throughout the FormData derive macro, I'd be very supprised if that was enough to make generic types work properly. We need to determine whether generic types make sense, and either emit a hard error when a type has generics, or add several test cases with generic types and ensure they work as expected.

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.