Sha256: d06f83cf120f646346adf0f5beb8f4980800ee86db4551b95bfcf4ee5d9f933b
Contents?: true
Size: 1.32 KB
Versions: 2
Compression:
Stored size: 1.32 KB
Contents
require_relative 'allowlist_middleware' module Stitches # A middleware that requires all API calls to be for versioned JSON or Protobuf. # # This means that the Accept header (available to Rack apps as HTTP_ACCEPT) should be like so: # # application/json; version=1 # # This just checks that you've specified some numeric version. ApiVersionConstraint should be used # to "lock down" the versions you accept. # # Or in the case of a protobuf encoded payload the header should be like so: # # application/protobuf # # There isn't an accepted standard for protobuf encoded payloads but this form is common. class ValidMimeType < Stitches::AllowlistMiddleware protected def do_call(env) accept = String(env["HTTP_ACCEPT"]) if (accept =~ %r{application/json} && accept =~ %r{version=\d+}) || accept =~ %r{application/protobuf} @app.call(env) else not_acceptable_response(accept) end end private def not_acceptable_response(accept_header) status = 406 body = "Not Acceptable - '#{accept_header}' didn't have the right mime type or version number. We only accept application/json with a version or application/protobuf" header = { "WWW-Authenticate" => accept_header } Rack::Response.new(body, status, header).finish end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
stitches-4.1.0RC2 | lib/stitches/valid_mime_type.rb |
stitches-4.0.1 | lib/stitches/valid_mime_type.rb |