Giter VIP home page Giter VIP logo

llhttp-rs's Introduction

llhttp-rs

llhttp bindings for Rust

Quick starts

use llhttp_rs::*;

fn main() {
  #[derive(Default)]
  struct CallbackList {
      called_on_url: bool,
      called_on_header_field: bool,
      called_on_header_value: bool,
      called_on_body: bool,
  }

  impl Callbacks for CallbackList {
      fn on_url(&mut self, _: &mut Parser, url: &[u8]) -> ParserResult<()> {
          assert_eq!(b"/say_hello", url);
          self.called_on_url = true;
          Ok(())
      }

      fn on_header_field(&mut self, _: &mut Parser, hdr: &[u8]) -> ParserResult<()> {
          assert!(hdr == b"Host" || hdr == b"Content-Length");
          self.called_on_header_field = true;
          Ok(())
      }

      fn on_header_value(&mut self, _: &mut Parser, val: &[u8]) -> ParserResult<()> {
          assert!(val == b"localhost.localdomain" || val == b"11");
          self.called_on_header_value = true;
          Ok(())
      }

      fn on_body(&mut self, parser: &mut Parser, body: &[u8]) -> ParserResult<()> {
          assert_eq!(parser.get_method(), Some(http::method::Method::POST));
          assert_eq!(parser.get_version(), Some(http::version::Version::HTTP_11));
          assert_eq!(parser.get_status_code(), None);
          assert_eq!(parser.get_upgrade(), false);
          assert_eq!(body, b"Hello world");
          self.called_on_body = true;
          Ok(())
      }
  }

  let req = b"POST /say_hello HTTP/1.1\r\nContent-Length: 11\r\nHost: localhost.localdomain\r\n\r\nHello world";
  let mut handler = CallbackList::default();
  let mut parser = Parser::request();

  parser.parse(&mut handler, req).unwrap();
}

llhttp-rs's People

Contributors

dependabot[bot] avatar tcmits avatar

Watchers

 avatar

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.