Git Product home page Git Product logo

hypp's Introduction

hypp

hypp is a client-oriented HTTP library for C++. It can parse and generate HTTP/1.1 messages and URIs.

To send or receive HTTP messages, you must use hypp along with another library such as hypr.

Conformance

hypp aims to conform to the syntax and semantics of HTTP/1.1 elements according to the following specification documents:

  • RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax
  • RFC 5234 - Augmented BNF for Syntax Specifications: ABNF
  • RFC 7230 - Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing
  • RFC 7231 - Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content

Exceptions

  • OPTIONAL requirements
  • Obsolete grammar rules (e.g. obsolete line folding)

Error Handling

hypp's parser functions return an Expected object, which contains either the expected result or an Error. For more information, see the std::expected proposal or the Usage section below.

hypp treats unsupported HTTP versions as errors, and does not attempt to recover usable protocol elements from invalid constructs.

Usage

This is a work in progress. Usage in public applications is not yet recommended.

#include <iostream>
#include <hypp.hpp>

int main() {
  // Example response from RFC 7230 Section 2.1
  constexpr auto example =
      "HTTP/1.1 200 OK\r\n"
      "Date: Mon, 27 Jul 2009 12:28:53 GMT\r\n"
      "Server: Apache\r\n"
      "Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT\r\n"
      "ETag: \"34aa387-d-1568eb00\"\r\n"
      "Accept-Ranges: bytes\r\n"
      "Content-Length: 51\r\n"
      "Vary: Accept-Encoding\r\n"
      "Content-Type: text/plain\r\n"
      "\r\n"
      "Hello World! My payload includes a trailing CRLF.\r\n";

  // Returns hypp::Expected<hypp::Response>
  const auto expected = hypp::ParseResponse(example);

  if (!expected) {
    std::cout << "Error: " << hypp::to_string(expected.error()) << '\n';
    return 1;
  }

  const auto& r = expected.value();
  std::cout << r.start_line.code << '\n';             // 200
  std::cout << r.header_fields.back().value << '\n';  // text/plain
  std::cout << r.body.substr(0, 12) << '\n';          // Hello World!

  return 0;
}

License

Licensed under the MIT License.

hypp's People

Contributors

erengy avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  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.