All Projects → socketry → http-accept

socketry / http-accept

Licence: other
Parse Accept and Accept-Language HTTP headers in Ruby.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to http-accept

cpphttpstack
c++ api for http client & server
Stars: ✭ 30 (-56.52%)
Mutual labels:  http-client, http-server
http4ts
Server as a Function http toolkit for TypeScript & JavaScript
Stars: ✭ 30 (-56.52%)
Mutual labels:  http-client, http-server
EthernetWebServer SSL
Simple TLS/SSL Ethernet WebServer, HTTP Client and WebSocket Client library for for AVR, Portenta_H7, Teensy, SAM DUE, SAMD21, SAMD51, STM32F/L/H/G/WB/MP1, nRF52 and RASPBERRY_PI_PICO boards using Ethernet shields W5100, W5200, W5500, ENC28J60 or Teensy 4.1 NativeEthernet/QNEthernet. It now supports Ethernet TLS/SSL Client. The library supports …
Stars: ✭ 40 (-42.03%)
Mutual labels:  http-client, http-server
hunt-http
http library for D, support http 1.1 / http 2.0 (http2) / websocket server and client.
Stars: ✭ 29 (-57.97%)
Mutual labels:  http-client, http-server
http4s-good-practices
Collection of what I consider good practices in Http4s (WIP)
Stars: ✭ 74 (+7.25%)
Mutual labels:  http-client, http-server
Http Kit
http-kit is a minimalist, event-driven, high-performance Clojure HTTP server/client library with WebSocket and asynchronous support
Stars: ✭ 2,234 (+3137.68%)
Mutual labels:  http-client, http-server
matador
Take your appclication by the horns
Stars: ✭ 59 (-14.49%)
Mutual labels:  http-client, http-server
Httpp
Micro http server and client written in C++
Stars: ✭ 144 (+108.7%)
Mutual labels:  http-client, http-server
BCA-Phantom
A multi-platform HTTP(S) Reverse Shell Server and Client in Python 3
Stars: ✭ 80 (+15.94%)
Mutual labels:  http-client, http-server
waspy
WASP framework for Python
Stars: ✭ 43 (-37.68%)
Mutual labels:  http-client, http-server
Zio Http
A scala library to write Http apps.
Stars: ✭ 162 (+134.78%)
Mutual labels:  http-client, http-server
zenith
⚡ Functional Scala HTTP server, client, and toolkit.
Stars: ✭ 15 (-78.26%)
Mutual labels:  http-client, http-server
Http4s
A minimal, idiomatic Scala interface for HTTP
Stars: ✭ 2,173 (+3049.28%)
Mutual labels:  http-client, http-server
Donkey
Modern Clojure HTTP server and client built for ease of use and performance
Stars: ✭ 199 (+188.41%)
Mutual labels:  http-client, http-server
Libhv
🔥 比libevent、libuv更易用的国产网络库。A c/c++ network library for developing TCP/UDP/SSL/HTTP/WebSocket client/server.
Stars: ✭ 3,355 (+4762.32%)
Mutual labels:  http-client, http-server
go-sse
Fully featured, spec-compliant HTML5 server-sent events library
Stars: ✭ 165 (+139.13%)
Mutual labels:  http-client, http-server
Http4k
The Functional toolkit for Kotlin HTTP applications. http4k provides a simple and uniform way to serve, consume, and test HTTP services.
Stars: ✭ 1,883 (+2628.99%)
Mutual labels:  http-client, http-server
Aiohttp
Asynchronous HTTP client/server framework for asyncio and Python
Stars: ✭ 11,972 (+17250.72%)
Mutual labels:  http-client, http-server
rawhttp
HTTP library to make it easy to deal with raw HTTP.
Stars: ✭ 156 (+126.09%)
Mutual labels:  http-client, http-server
aiohttp-json-rpc
Implements JSON-RPC 2.0 using aiohttp
Stars: ✭ 54 (-21.74%)
Mutual labels:  http-client, http-server

HTTP::Accept

Provides a robust set of parsers for dealing with HTTP Accept, Accept-Language, Accept-Encoding, Accept-Charset headers.

Build Status Coverage Status

Motivation

I've been developing some tools for building RESTful endpoints and part of that involved versioning. After reviewing the options, I settled on using the Accept: application/json;version=1 method as outlined here.

The version=1 part of the media-type is a parameter as defined by RFC7231 Section 3.1.1.1. After reviewing several existing different options for parsing the Accept: header, I noticed a disturbing trend: header.split(','). Because parameters may contain quoted strings which contain commas, this is clearly not an appropriate way to parse the header.

I am concerned about correctness, security and performance. As such, I implemented this gem to provide a simple high level interface for both parsing and correctly interpreting these headers.

Installation

Add this line to your application's Gemfile:

gem 'http-accept'

And then execute:

$ bundle

Or install it yourself as:

$ gem install http-accept

Usage

Here are some examples of how to parse various headers.

Parsing Accept: headers

You can parse the incoming Accept: header:

media_types = HTTP::Accept::MediaTypes.parse("text/html;q=0.5, application/json; version=1")

expect(media_types[0].mime_type).to be == "application/json"
expect(media_types[0].parameters).to be == {'version' => '1'}
expect(media_types[1].mime_type).to be == "text/html"
expect(media_types[1].parameters).to be == {'q' => '0.5'}

Normally, you'd want to match the media types against some set of available mime types:

module ToJSON
  def content_type
    HTTP::Accept::ContentType.new("application", "json", charset: 'utf-8')
  end

  # Used for inserting into map.
  def split(*args)
    content_type.split(*args)
  end

  def convert(object, options)
    object.to_json
  end
end

module ToXML
  # Are you kidding?
end

map = HTTP::Accept::MediaTypes::Map.new
map << ToJSON
map << ToXML

object, media_range = map.for(media_types)
content = object.convert(model, media_range.parameters)
response = [200, {'Content-Type' => object.content_type}, [content]]

Parsing Accept-Language: headers

You can parse the incoming Accept-Language: header:

languages = HTTP::Accept::Languages.parse("da, en-gb;q=0.8, en;q=0.7")

expect(languages[0].locale).to be == "da"
expect(languages[1].locale).to be == "en-gb"
expect(languages[2].locale).to be == "en"

Normally, you'd want to match the languages against some set of available localizations:

available_localizations = HTTP::Accept::Languages::Locales.new(["en-nz", "en-us"])

# Given the languages that the user wants, and the localizations available, compute the set of desired localizations.
desired_localizations = available_localizations & languages

The desired_localizations in the example above is a subset of available_localizations.

HTTP::Accept::Languages::Locales provides an efficient data-structure for matching the Accept-Languages header to set of available localizations according to https://tools.ietf.org/html/rfc7231#section-5.3.5 and https://tools.ietf.org/html/rfc4647#section-2.3

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Released under the MIT license.

Copyright, 2016, by Samuel G. D. Williams.
Copyright, 2016, by Matthew Kerwin.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].