planetscale 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ci.yml +24 -0
- data/.github/workflows/gem-push.yml +38 -0
- data/.github/workflows/licensed.yml +43 -0
- data/.gitignore +10 -0
- data/.licensed.yml +9 -0
- data/.licenses/go/github.com/armon/circbuf.dep.yml +31 -0
- data/.licenses/go/github.com/gorilla/mux.dep.yml +41 -0
- data/.licenses/go/github.com/hashicorp/go-cleanhttp.dep.yml +375 -0
- data/.licenses/go/github.com/mitchellh/go-homedir.dep.yml +32 -0
- data/.licenses/go/github.com/pkg/errors.dep.yml +36 -0
- data/.licenses/go/github.com/planetscale/planetscale-go/planetscale.dep.yml +160 -0
- data/.licenses/go/github.com/planetscale/sql-proxy/proxy.dep.yml +180 -0
- data/.licenses/go/go.uber.org/atomic.dep.yml +31 -0
- data/.licenses/go/go.uber.org/multierr.dep.yml +30 -0
- data/.licenses/go/go.uber.org/zap.dep.yml +30 -0
- data/.licenses/go/go.uber.org/zap/buffer.dep.yml +30 -0
- data/.licenses/go/go.uber.org/zap/internal/bufferpool.dep.yml +30 -0
- data/.licenses/go/go.uber.org/zap/internal/color.dep.yml +30 -0
- data/.licenses/go/go.uber.org/zap/internal/exit.dep.yml +31 -0
- data/.licenses/go/go.uber.org/zap/zapcore.dep.yml +31 -0
- data/.licenses/go/golang.org/x/net/context/ctxhttp.dep.yml +63 -0
- data/.licenses/go/golang.org/x/oauth2.dep.yml +44 -0
- data/.licenses/go/golang.org/x/oauth2/internal.dep.yml +38 -0
- data/.ruby_version +1 -0
- data/Gemfile +8 -0
- data/LICENSE +201 -0
- data/README.md +88 -0
- data/Rakefile +10 -0
- data/bin/console +15 -0
- data/bin/setup +17 -0
- data/controller.go +255 -0
- data/go.mod +16 -0
- data/go.sum +428 -0
- data/lib/generators/planetscale/install_generator.rb +58 -0
- data/lib/planetscale.rb +137 -0
- data/lib/planetscale/version.rb +5 -0
- data/planetscale.gemspec +44 -0
- data/proxy.go +128 -0
- data/vendor/github.com/armon/circbuf/.gitignore +22 -0
- data/vendor/github.com/armon/circbuf/LICENSE +20 -0
- data/vendor/github.com/armon/circbuf/README.md +28 -0
- data/vendor/github.com/armon/circbuf/circbuf.go +92 -0
- data/vendor/github.com/armon/circbuf/go.mod +1 -0
- data/vendor/github.com/golang/protobuf/AUTHORS +3 -0
- data/vendor/github.com/golang/protobuf/CONTRIBUTORS +3 -0
- data/vendor/github.com/golang/protobuf/LICENSE +28 -0
- data/vendor/github.com/golang/protobuf/proto/buffer.go +324 -0
- data/vendor/github.com/golang/protobuf/proto/defaults.go +63 -0
- data/vendor/github.com/golang/protobuf/proto/deprecated.go +113 -0
- data/vendor/github.com/golang/protobuf/proto/discard.go +58 -0
- data/vendor/github.com/golang/protobuf/proto/extensions.go +356 -0
- data/vendor/github.com/golang/protobuf/proto/properties.go +306 -0
- data/vendor/github.com/golang/protobuf/proto/proto.go +167 -0
- data/vendor/github.com/golang/protobuf/proto/registry.go +317 -0
- data/vendor/github.com/golang/protobuf/proto/text_decode.go +801 -0
- data/vendor/github.com/golang/protobuf/proto/text_encode.go +560 -0
- data/vendor/github.com/golang/protobuf/proto/wire.go +78 -0
- data/vendor/github.com/golang/protobuf/proto/wrappers.go +34 -0
- data/vendor/github.com/gorilla/mux/AUTHORS +8 -0
- data/vendor/github.com/gorilla/mux/LICENSE +27 -0
- data/vendor/github.com/gorilla/mux/README.md +805 -0
- data/vendor/github.com/gorilla/mux/doc.go +306 -0
- data/vendor/github.com/gorilla/mux/go.mod +3 -0
- data/vendor/github.com/gorilla/mux/middleware.go +74 -0
- data/vendor/github.com/gorilla/mux/mux.go +606 -0
- data/vendor/github.com/gorilla/mux/regexp.go +388 -0
- data/vendor/github.com/gorilla/mux/route.go +736 -0
- data/vendor/github.com/gorilla/mux/test_helpers.go +19 -0
- data/vendor/github.com/hashicorp/go-cleanhttp/LICENSE +363 -0
- data/vendor/github.com/hashicorp/go-cleanhttp/README.md +30 -0
- data/vendor/github.com/hashicorp/go-cleanhttp/cleanhttp.go +58 -0
- data/vendor/github.com/hashicorp/go-cleanhttp/doc.go +20 -0
- data/vendor/github.com/hashicorp/go-cleanhttp/go.mod +3 -0
- data/vendor/github.com/hashicorp/go-cleanhttp/handlers.go +48 -0
- data/vendor/github.com/mitchellh/go-homedir/LICENSE +21 -0
- data/vendor/github.com/mitchellh/go-homedir/README.md +14 -0
- data/vendor/github.com/mitchellh/go-homedir/go.mod +1 -0
- data/vendor/github.com/mitchellh/go-homedir/homedir.go +167 -0
- data/vendor/github.com/pkg/errors/.gitignore +24 -0
- data/vendor/github.com/pkg/errors/.travis.yml +10 -0
- data/vendor/github.com/pkg/errors/LICENSE +23 -0
- data/vendor/github.com/pkg/errors/Makefile +44 -0
- data/vendor/github.com/pkg/errors/README.md +59 -0
- data/vendor/github.com/pkg/errors/appveyor.yml +32 -0
- data/vendor/github.com/pkg/errors/errors.go +288 -0
- data/vendor/github.com/pkg/errors/go113.go +38 -0
- data/vendor/github.com/pkg/errors/stack.go +177 -0
- data/vendor/github.com/planetscale/planetscale-go/planetscale/backups.go +139 -0
- data/vendor/github.com/planetscale/planetscale-go/planetscale/branches.go +258 -0
- data/vendor/github.com/planetscale/planetscale-go/planetscale/certs.go +142 -0
- data/vendor/github.com/planetscale/planetscale-go/planetscale/client.go +305 -0
- data/vendor/github.com/planetscale/planetscale-go/planetscale/databases.go +131 -0
- data/vendor/github.com/planetscale/planetscale-go/planetscale/deploy_requests.go +368 -0
- data/vendor/github.com/planetscale/planetscale-go/planetscale/organizations.go +78 -0
- data/vendor/github.com/planetscale/planetscale-go/planetscale/service_tokens.go +163 -0
- data/vendor/github.com/planetscale/sql-proxy/proxy/client.go +467 -0
- data/vendor/github.com/planetscale/sql-proxy/proxy/tls_cache.go +73 -0
- data/vendor/go.uber.org/atomic/.codecov.yml +19 -0
- data/vendor/go.uber.org/atomic/.gitignore +12 -0
- data/vendor/go.uber.org/atomic/.travis.yml +27 -0
- data/vendor/go.uber.org/atomic/CHANGELOG.md +76 -0
- data/vendor/go.uber.org/atomic/LICENSE.txt +19 -0
- data/vendor/go.uber.org/atomic/Makefile +78 -0
- data/vendor/go.uber.org/atomic/README.md +63 -0
- data/vendor/go.uber.org/atomic/bool.go +81 -0
- data/vendor/go.uber.org/atomic/bool_ext.go +53 -0
- data/vendor/go.uber.org/atomic/doc.go +23 -0
- data/vendor/go.uber.org/atomic/duration.go +82 -0
- data/vendor/go.uber.org/atomic/duration_ext.go +40 -0
- data/vendor/go.uber.org/atomic/error.go +51 -0
- data/vendor/go.uber.org/atomic/error_ext.go +39 -0
- data/vendor/go.uber.org/atomic/float64.go +76 -0
- data/vendor/go.uber.org/atomic/float64_ext.go +47 -0
- data/vendor/go.uber.org/atomic/gen.go +26 -0
- data/vendor/go.uber.org/atomic/go.mod +8 -0
- data/vendor/go.uber.org/atomic/go.sum +9 -0
- data/vendor/go.uber.org/atomic/int32.go +102 -0
- data/vendor/go.uber.org/atomic/int64.go +102 -0
- data/vendor/go.uber.org/atomic/nocmp.go +35 -0
- data/vendor/go.uber.org/atomic/string.go +54 -0
- data/vendor/go.uber.org/atomic/string_ext.go +43 -0
- data/vendor/go.uber.org/atomic/uint32.go +102 -0
- data/vendor/go.uber.org/atomic/uint64.go +102 -0
- data/vendor/go.uber.org/atomic/value.go +31 -0
- data/vendor/go.uber.org/multierr/.codecov.yml +15 -0
- data/vendor/go.uber.org/multierr/.gitignore +4 -0
- data/vendor/go.uber.org/multierr/.travis.yml +23 -0
- data/vendor/go.uber.org/multierr/CHANGELOG.md +60 -0
- data/vendor/go.uber.org/multierr/LICENSE.txt +19 -0
- data/vendor/go.uber.org/multierr/Makefile +42 -0
- data/vendor/go.uber.org/multierr/README.md +23 -0
- data/vendor/go.uber.org/multierr/error.go +449 -0
- data/vendor/go.uber.org/multierr/glide.yaml +8 -0
- data/vendor/go.uber.org/multierr/go.mod +8 -0
- data/vendor/go.uber.org/multierr/go.sum +11 -0
- data/vendor/go.uber.org/multierr/go113.go +52 -0
- data/vendor/go.uber.org/zap/.codecov.yml +17 -0
- data/vendor/go.uber.org/zap/.gitignore +32 -0
- data/vendor/go.uber.org/zap/.readme.tmpl +109 -0
- data/vendor/go.uber.org/zap/.travis.yml +23 -0
- data/vendor/go.uber.org/zap/CHANGELOG.md +432 -0
- data/vendor/go.uber.org/zap/CODE_OF_CONDUCT.md +75 -0
- data/vendor/go.uber.org/zap/CONTRIBUTING.md +81 -0
- data/vendor/go.uber.org/zap/FAQ.md +156 -0
- data/vendor/go.uber.org/zap/LICENSE.txt +19 -0
- data/vendor/go.uber.org/zap/Makefile +63 -0
- data/vendor/go.uber.org/zap/README.md +134 -0
- data/vendor/go.uber.org/zap/array.go +320 -0
- data/vendor/go.uber.org/zap/buffer/buffer.go +123 -0
- data/vendor/go.uber.org/zap/buffer/pool.go +49 -0
- data/vendor/go.uber.org/zap/checklicense.sh +17 -0
- data/vendor/go.uber.org/zap/config.go +264 -0
- data/vendor/go.uber.org/zap/doc.go +113 -0
- data/vendor/go.uber.org/zap/encoder.go +79 -0
- data/vendor/go.uber.org/zap/error.go +80 -0
- data/vendor/go.uber.org/zap/field.go +539 -0
- data/vendor/go.uber.org/zap/flag.go +39 -0
- data/vendor/go.uber.org/zap/glide.yaml +34 -0
- data/vendor/go.uber.org/zap/global.go +168 -0
- data/vendor/go.uber.org/zap/global_go112.go +26 -0
- data/vendor/go.uber.org/zap/global_prego112.go +26 -0
- data/vendor/go.uber.org/zap/go.mod +13 -0
- data/vendor/go.uber.org/zap/go.sum +56 -0
- data/vendor/go.uber.org/zap/http_handler.go +81 -0
- data/vendor/go.uber.org/zap/internal/bufferpool/bufferpool.go +31 -0
- data/vendor/go.uber.org/zap/internal/color/color.go +44 -0
- data/vendor/go.uber.org/zap/internal/exit/exit.go +64 -0
- data/vendor/go.uber.org/zap/level.go +132 -0
- data/vendor/go.uber.org/zap/logger.go +344 -0
- data/vendor/go.uber.org/zap/options.go +140 -0
- data/vendor/go.uber.org/zap/sink.go +161 -0
- data/vendor/go.uber.org/zap/stacktrace.go +85 -0
- data/vendor/go.uber.org/zap/sugar.go +304 -0
- data/vendor/go.uber.org/zap/time.go +27 -0
- data/vendor/go.uber.org/zap/writer.go +99 -0
- data/vendor/go.uber.org/zap/zapcore/console_encoder.go +161 -0
- data/vendor/go.uber.org/zap/zapcore/core.go +113 -0
- data/vendor/go.uber.org/zap/zapcore/doc.go +24 -0
- data/vendor/go.uber.org/zap/zapcore/encoder.go +443 -0
- data/vendor/go.uber.org/zap/zapcore/entry.go +264 -0
- data/vendor/go.uber.org/zap/zapcore/error.go +115 -0
- data/vendor/go.uber.org/zap/zapcore/field.go +227 -0
- data/vendor/go.uber.org/zap/zapcore/hook.go +68 -0
- data/vendor/go.uber.org/zap/zapcore/increase_level.go +66 -0
- data/vendor/go.uber.org/zap/zapcore/json_encoder.go +534 -0
- data/vendor/go.uber.org/zap/zapcore/level.go +175 -0
- data/vendor/go.uber.org/zap/zapcore/level_strings.go +46 -0
- data/vendor/go.uber.org/zap/zapcore/marshaler.go +61 -0
- data/vendor/go.uber.org/zap/zapcore/memory_encoder.go +179 -0
- data/vendor/go.uber.org/zap/zapcore/sampler.go +208 -0
- data/vendor/go.uber.org/zap/zapcore/tee.go +81 -0
- data/vendor/go.uber.org/zap/zapcore/write_syncer.go +123 -0
- data/vendor/golang.org/x/net/AUTHORS +3 -0
- data/vendor/golang.org/x/net/CONTRIBUTORS +3 -0
- data/vendor/golang.org/x/net/LICENSE +27 -0
- data/vendor/golang.org/x/net/PATENTS +22 -0
- data/vendor/golang.org/x/net/context/context.go +56 -0
- data/vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go +71 -0
- data/vendor/golang.org/x/net/context/go17.go +73 -0
- data/vendor/golang.org/x/net/context/go19.go +21 -0
- data/vendor/golang.org/x/net/context/pre_go17.go +301 -0
- data/vendor/golang.org/x/net/context/pre_go19.go +110 -0
- data/vendor/golang.org/x/oauth2/.travis.yml +13 -0
- data/vendor/golang.org/x/oauth2/AUTHORS +3 -0
- data/vendor/golang.org/x/oauth2/CONTRIBUTING.md +26 -0
- data/vendor/golang.org/x/oauth2/CONTRIBUTORS +3 -0
- data/vendor/golang.org/x/oauth2/LICENSE +27 -0
- data/vendor/golang.org/x/oauth2/README.md +36 -0
- data/vendor/golang.org/x/oauth2/go.mod +9 -0
- data/vendor/golang.org/x/oauth2/go.sum +361 -0
- data/vendor/golang.org/x/oauth2/internal/client_appengine.go +14 -0
- data/vendor/golang.org/x/oauth2/internal/doc.go +6 -0
- data/vendor/golang.org/x/oauth2/internal/oauth2.go +37 -0
- data/vendor/golang.org/x/oauth2/internal/token.go +294 -0
- data/vendor/golang.org/x/oauth2/internal/transport.go +33 -0
- data/vendor/golang.org/x/oauth2/oauth2.go +381 -0
- data/vendor/golang.org/x/oauth2/token.go +178 -0
- data/vendor/golang.org/x/oauth2/transport.go +89 -0
- data/vendor/google.golang.org/appengine/LICENSE +202 -0
- data/vendor/google.golang.org/appengine/internal/api.go +678 -0
- data/vendor/google.golang.org/appengine/internal/api_classic.go +169 -0
- data/vendor/google.golang.org/appengine/internal/api_common.go +123 -0
- data/vendor/google.golang.org/appengine/internal/app_id.go +28 -0
- data/vendor/google.golang.org/appengine/internal/base/api_base.pb.go +308 -0
- data/vendor/google.golang.org/appengine/internal/base/api_base.proto +33 -0
- data/vendor/google.golang.org/appengine/internal/datastore/datastore_v3.pb.go +4367 -0
- data/vendor/google.golang.org/appengine/internal/datastore/datastore_v3.proto +551 -0
- data/vendor/google.golang.org/appengine/internal/identity.go +55 -0
- data/vendor/google.golang.org/appengine/internal/identity_classic.go +61 -0
- data/vendor/google.golang.org/appengine/internal/identity_flex.go +11 -0
- data/vendor/google.golang.org/appengine/internal/identity_vm.go +134 -0
- data/vendor/google.golang.org/appengine/internal/internal.go +110 -0
- data/vendor/google.golang.org/appengine/internal/log/log_service.pb.go +1313 -0
- data/vendor/google.golang.org/appengine/internal/log/log_service.proto +150 -0
- data/vendor/google.golang.org/appengine/internal/main.go +16 -0
- data/vendor/google.golang.org/appengine/internal/main_common.go +7 -0
- data/vendor/google.golang.org/appengine/internal/main_vm.go +69 -0
- data/vendor/google.golang.org/appengine/internal/metadata.go +60 -0
- data/vendor/google.golang.org/appengine/internal/net.go +56 -0
- data/vendor/google.golang.org/appengine/internal/regen.sh +40 -0
- data/vendor/google.golang.org/appengine/internal/remote_api/remote_api.pb.go +361 -0
- data/vendor/google.golang.org/appengine/internal/remote_api/remote_api.proto +44 -0
- data/vendor/google.golang.org/appengine/internal/transaction.go +115 -0
- data/vendor/google.golang.org/appengine/internal/urlfetch/urlfetch_service.pb.go +527 -0
- data/vendor/google.golang.org/appengine/internal/urlfetch/urlfetch_service.proto +64 -0
- data/vendor/google.golang.org/appengine/urlfetch/urlfetch.go +210 -0
- data/vendor/google.golang.org/protobuf/AUTHORS +3 -0
- data/vendor/google.golang.org/protobuf/CONTRIBUTORS +3 -0
- data/vendor/google.golang.org/protobuf/LICENSE +27 -0
- data/vendor/google.golang.org/protobuf/PATENTS +22 -0
- data/vendor/google.golang.org/protobuf/encoding/prototext/decode.go +773 -0
- data/vendor/google.golang.org/protobuf/encoding/prototext/doc.go +7 -0
- data/vendor/google.golang.org/protobuf/encoding/prototext/encode.go +371 -0
- data/vendor/google.golang.org/protobuf/encoding/protowire/wire.go +538 -0
- data/vendor/google.golang.org/protobuf/internal/descfmt/stringer.go +318 -0
- data/vendor/google.golang.org/protobuf/internal/descopts/options.go +29 -0
- data/vendor/google.golang.org/protobuf/internal/detrand/rand.go +69 -0
- data/vendor/google.golang.org/protobuf/internal/encoding/defval/default.go +213 -0
- data/vendor/google.golang.org/protobuf/internal/encoding/messageset/messageset.go +241 -0
- data/vendor/google.golang.org/protobuf/internal/encoding/tag/tag.go +207 -0
- data/vendor/google.golang.org/protobuf/internal/encoding/text/decode.go +665 -0
- data/vendor/google.golang.org/protobuf/internal/encoding/text/decode_number.go +190 -0
- data/vendor/google.golang.org/protobuf/internal/encoding/text/decode_string.go +161 -0
- data/vendor/google.golang.org/protobuf/internal/encoding/text/decode_token.go +373 -0
- data/vendor/google.golang.org/protobuf/internal/encoding/text/doc.go +29 -0
- data/vendor/google.golang.org/protobuf/internal/encoding/text/encode.go +265 -0
- data/vendor/google.golang.org/protobuf/internal/errors/errors.go +89 -0
- data/vendor/google.golang.org/protobuf/internal/errors/is_go112.go +39 -0
- data/vendor/google.golang.org/protobuf/internal/errors/is_go113.go +12 -0
- data/vendor/google.golang.org/protobuf/internal/filedesc/build.go +158 -0
- data/vendor/google.golang.org/protobuf/internal/filedesc/desc.go +631 -0
- data/vendor/google.golang.org/protobuf/internal/filedesc/desc_init.go +471 -0
- data/vendor/google.golang.org/protobuf/internal/filedesc/desc_lazy.go +704 -0
- data/vendor/google.golang.org/protobuf/internal/filedesc/desc_list.go +450 -0
- data/vendor/google.golang.org/protobuf/internal/filedesc/desc_list_gen.go +356 -0
- data/vendor/google.golang.org/protobuf/internal/filedesc/placeholder.go +107 -0
- data/vendor/google.golang.org/protobuf/internal/filetype/build.go +297 -0
- data/vendor/google.golang.org/protobuf/internal/flags/flags.go +24 -0
- data/vendor/google.golang.org/protobuf/internal/flags/proto_legacy_disable.go +9 -0
- data/vendor/google.golang.org/protobuf/internal/flags/proto_legacy_enable.go +9 -0
- data/vendor/google.golang.org/protobuf/internal/genid/any_gen.go +34 -0
- data/vendor/google.golang.org/protobuf/internal/genid/api_gen.go +106 -0
- data/vendor/google.golang.org/protobuf/internal/genid/descriptor_gen.go +829 -0
- data/vendor/google.golang.org/protobuf/internal/genid/doc.go +11 -0
- data/vendor/google.golang.org/protobuf/internal/genid/duration_gen.go +34 -0
- data/vendor/google.golang.org/protobuf/internal/genid/empty_gen.go +19 -0
- data/vendor/google.golang.org/protobuf/internal/genid/field_mask_gen.go +31 -0
- data/vendor/google.golang.org/protobuf/internal/genid/goname.go +25 -0
- data/vendor/google.golang.org/protobuf/internal/genid/map_entry.go +16 -0
- data/vendor/google.golang.org/protobuf/internal/genid/source_context_gen.go +31 -0
- data/vendor/google.golang.org/protobuf/internal/genid/struct_gen.go +116 -0
- data/vendor/google.golang.org/protobuf/internal/genid/timestamp_gen.go +34 -0
- data/vendor/google.golang.org/protobuf/internal/genid/type_gen.go +184 -0
- data/vendor/google.golang.org/protobuf/internal/genid/wrappers.go +13 -0
- data/vendor/google.golang.org/protobuf/internal/genid/wrappers_gen.go +175 -0
- data/vendor/google.golang.org/protobuf/internal/impl/api_export.go +177 -0
- data/vendor/google.golang.org/protobuf/internal/impl/checkinit.go +141 -0
- data/vendor/google.golang.org/protobuf/internal/impl/codec_extension.go +223 -0
- data/vendor/google.golang.org/protobuf/internal/impl/codec_field.go +830 -0
- data/vendor/google.golang.org/protobuf/internal/impl/codec_gen.go +5637 -0
- data/vendor/google.golang.org/protobuf/internal/impl/codec_map.go +388 -0
- data/vendor/google.golang.org/protobuf/internal/impl/codec_map_go111.go +37 -0
- data/vendor/google.golang.org/protobuf/internal/impl/codec_map_go112.go +11 -0
- data/vendor/google.golang.org/protobuf/internal/impl/codec_message.go +217 -0
- data/vendor/google.golang.org/protobuf/internal/impl/codec_messageset.go +123 -0
- data/vendor/google.golang.org/protobuf/internal/impl/codec_reflect.go +209 -0
- data/vendor/google.golang.org/protobuf/internal/impl/codec_tables.go +557 -0
- data/vendor/google.golang.org/protobuf/internal/impl/codec_unsafe.go +17 -0
- data/vendor/google.golang.org/protobuf/internal/impl/convert.go +496 -0
- data/vendor/google.golang.org/protobuf/internal/impl/convert_list.go +141 -0
- data/vendor/google.golang.org/protobuf/internal/impl/convert_map.go +121 -0
- data/vendor/google.golang.org/protobuf/internal/impl/decode.go +276 -0
- data/vendor/google.golang.org/protobuf/internal/impl/encode.go +201 -0
- data/vendor/google.golang.org/protobuf/internal/impl/enum.go +21 -0
- data/vendor/google.golang.org/protobuf/internal/impl/extension.go +156 -0
- data/vendor/google.golang.org/protobuf/internal/impl/legacy_enum.go +219 -0
- data/vendor/google.golang.org/protobuf/internal/impl/legacy_export.go +92 -0
- data/vendor/google.golang.org/protobuf/internal/impl/legacy_extension.go +176 -0
- data/vendor/google.golang.org/protobuf/internal/impl/legacy_file.go +81 -0
- data/vendor/google.golang.org/protobuf/internal/impl/legacy_message.go +558 -0
- data/vendor/google.golang.org/protobuf/internal/impl/merge.go +176 -0
- data/vendor/google.golang.org/protobuf/internal/impl/merge_gen.go +209 -0
- data/vendor/google.golang.org/protobuf/internal/impl/message.go +276 -0
- data/vendor/google.golang.org/protobuf/internal/impl/message_reflect.go +465 -0
- data/vendor/google.golang.org/protobuf/internal/impl/message_reflect_field.go +543 -0
- data/vendor/google.golang.org/protobuf/internal/impl/message_reflect_gen.go +249 -0
- data/vendor/google.golang.org/protobuf/internal/impl/pointer_reflect.go +178 -0
- data/vendor/google.golang.org/protobuf/internal/impl/pointer_unsafe.go +174 -0
- data/vendor/google.golang.org/protobuf/internal/impl/validate.go +576 -0
- data/vendor/google.golang.org/protobuf/internal/impl/weak.go +74 -0
- data/vendor/google.golang.org/protobuf/internal/order/order.go +89 -0
- data/vendor/google.golang.org/protobuf/internal/order/range.go +115 -0
- data/vendor/google.golang.org/protobuf/internal/pragma/pragma.go +29 -0
- data/vendor/google.golang.org/protobuf/internal/set/ints.go +58 -0
- data/vendor/google.golang.org/protobuf/internal/strs/strings.go +196 -0
- data/vendor/google.golang.org/protobuf/internal/strs/strings_pure.go +27 -0
- data/vendor/google.golang.org/protobuf/internal/strs/strings_unsafe.go +94 -0
- data/vendor/google.golang.org/protobuf/internal/version/version.go +79 -0
- data/vendor/google.golang.org/protobuf/proto/checkinit.go +71 -0
- data/vendor/google.golang.org/protobuf/proto/decode.go +278 -0
- data/vendor/google.golang.org/protobuf/proto/decode_gen.go +603 -0
- data/vendor/google.golang.org/protobuf/proto/doc.go +94 -0
- data/vendor/google.golang.org/protobuf/proto/encode.go +319 -0
- data/vendor/google.golang.org/protobuf/proto/encode_gen.go +97 -0
- data/vendor/google.golang.org/protobuf/proto/equal.go +167 -0
- data/vendor/google.golang.org/protobuf/proto/extension.go +92 -0
- data/vendor/google.golang.org/protobuf/proto/merge.go +139 -0
- data/vendor/google.golang.org/protobuf/proto/messageset.go +93 -0
- data/vendor/google.golang.org/protobuf/proto/proto.go +43 -0
- data/vendor/google.golang.org/protobuf/proto/proto_methods.go +19 -0
- data/vendor/google.golang.org/protobuf/proto/proto_reflect.go +19 -0
- data/vendor/google.golang.org/protobuf/proto/reset.go +43 -0
- data/vendor/google.golang.org/protobuf/proto/size.go +97 -0
- data/vendor/google.golang.org/protobuf/proto/size_gen.go +55 -0
- data/vendor/google.golang.org/protobuf/proto/wrappers.go +29 -0
- data/vendor/google.golang.org/protobuf/reflect/protodesc/desc.go +276 -0
- data/vendor/google.golang.org/protobuf/reflect/protodesc/desc_init.go +248 -0
- data/vendor/google.golang.org/protobuf/reflect/protodesc/desc_resolve.go +286 -0
- data/vendor/google.golang.org/protobuf/reflect/protodesc/desc_validate.go +374 -0
- data/vendor/google.golang.org/protobuf/reflect/protodesc/proto.go +252 -0
- data/vendor/google.golang.org/protobuf/reflect/protoreflect/methods.go +77 -0
- data/vendor/google.golang.org/protobuf/reflect/protoreflect/proto.go +504 -0
- data/vendor/google.golang.org/protobuf/reflect/protoreflect/source.go +128 -0
- data/vendor/google.golang.org/protobuf/reflect/protoreflect/source_gen.go +461 -0
- data/vendor/google.golang.org/protobuf/reflect/protoreflect/type.go +665 -0
- data/vendor/google.golang.org/protobuf/reflect/protoreflect/value.go +285 -0
- data/vendor/google.golang.org/protobuf/reflect/protoreflect/value_pure.go +59 -0
- data/vendor/google.golang.org/protobuf/reflect/protoreflect/value_union.go +411 -0
- data/vendor/google.golang.org/protobuf/reflect/protoreflect/value_unsafe.go +98 -0
- data/vendor/google.golang.org/protobuf/reflect/protoregistry/registry.go +869 -0
- data/vendor/google.golang.org/protobuf/runtime/protoiface/legacy.go +15 -0
- data/vendor/google.golang.org/protobuf/runtime/protoiface/methods.go +167 -0
- data/vendor/google.golang.org/protobuf/runtime/protoimpl/impl.go +44 -0
- data/vendor/google.golang.org/protobuf/runtime/protoimpl/version.go +56 -0
- data/vendor/google.golang.org/protobuf/types/descriptorpb/descriptor.pb.go +4039 -0
- data/vendor/modules.txt +79 -0
- metadata +495 -0
@@ -0,0 +1,306 @@
|
|
1
|
+
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
2
|
+
// Use of this source code is governed by a BSD-style
|
3
|
+
// license that can be found in the LICENSE file.
|
4
|
+
|
5
|
+
/*
|
6
|
+
Package mux implements a request router and dispatcher.
|
7
|
+
|
8
|
+
The name mux stands for "HTTP request multiplexer". Like the standard
|
9
|
+
http.ServeMux, mux.Router matches incoming requests against a list of
|
10
|
+
registered routes and calls a handler for the route that matches the URL
|
11
|
+
or other conditions. The main features are:
|
12
|
+
|
13
|
+
* Requests can be matched based on URL host, path, path prefix, schemes,
|
14
|
+
header and query values, HTTP methods or using custom matchers.
|
15
|
+
* URL hosts, paths and query values can have variables with an optional
|
16
|
+
regular expression.
|
17
|
+
* Registered URLs can be built, or "reversed", which helps maintaining
|
18
|
+
references to resources.
|
19
|
+
* Routes can be used as subrouters: nested routes are only tested if the
|
20
|
+
parent route matches. This is useful to define groups of routes that
|
21
|
+
share common conditions like a host, a path prefix or other repeated
|
22
|
+
attributes. As a bonus, this optimizes request matching.
|
23
|
+
* It implements the http.Handler interface so it is compatible with the
|
24
|
+
standard http.ServeMux.
|
25
|
+
|
26
|
+
Let's start registering a couple of URL paths and handlers:
|
27
|
+
|
28
|
+
func main() {
|
29
|
+
r := mux.NewRouter()
|
30
|
+
r.HandleFunc("/", HomeHandler)
|
31
|
+
r.HandleFunc("/products", ProductsHandler)
|
32
|
+
r.HandleFunc("/articles", ArticlesHandler)
|
33
|
+
http.Handle("/", r)
|
34
|
+
}
|
35
|
+
|
36
|
+
Here we register three routes mapping URL paths to handlers. This is
|
37
|
+
equivalent to how http.HandleFunc() works: if an incoming request URL matches
|
38
|
+
one of the paths, the corresponding handler is called passing
|
39
|
+
(http.ResponseWriter, *http.Request) as parameters.
|
40
|
+
|
41
|
+
Paths can have variables. They are defined using the format {name} or
|
42
|
+
{name:pattern}. If a regular expression pattern is not defined, the matched
|
43
|
+
variable will be anything until the next slash. For example:
|
44
|
+
|
45
|
+
r := mux.NewRouter()
|
46
|
+
r.HandleFunc("/products/{key}", ProductHandler)
|
47
|
+
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
|
48
|
+
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
|
49
|
+
|
50
|
+
Groups can be used inside patterns, as long as they are non-capturing (?:re). For example:
|
51
|
+
|
52
|
+
r.HandleFunc("/articles/{category}/{sort:(?:asc|desc|new)}", ArticlesCategoryHandler)
|
53
|
+
|
54
|
+
The names are used to create a map of route variables which can be retrieved
|
55
|
+
calling mux.Vars():
|
56
|
+
|
57
|
+
vars := mux.Vars(request)
|
58
|
+
category := vars["category"]
|
59
|
+
|
60
|
+
Note that if any capturing groups are present, mux will panic() during parsing. To prevent
|
61
|
+
this, convert any capturing groups to non-capturing, e.g. change "/{sort:(asc|desc)}" to
|
62
|
+
"/{sort:(?:asc|desc)}". This is a change from prior versions which behaved unpredictably
|
63
|
+
when capturing groups were present.
|
64
|
+
|
65
|
+
And this is all you need to know about the basic usage. More advanced options
|
66
|
+
are explained below.
|
67
|
+
|
68
|
+
Routes can also be restricted to a domain or subdomain. Just define a host
|
69
|
+
pattern to be matched. They can also have variables:
|
70
|
+
|
71
|
+
r := mux.NewRouter()
|
72
|
+
// Only matches if domain is "www.example.com".
|
73
|
+
r.Host("www.example.com")
|
74
|
+
// Matches a dynamic subdomain.
|
75
|
+
r.Host("{subdomain:[a-z]+}.domain.com")
|
76
|
+
|
77
|
+
There are several other matchers that can be added. To match path prefixes:
|
78
|
+
|
79
|
+
r.PathPrefix("/products/")
|
80
|
+
|
81
|
+
...or HTTP methods:
|
82
|
+
|
83
|
+
r.Methods("GET", "POST")
|
84
|
+
|
85
|
+
...or URL schemes:
|
86
|
+
|
87
|
+
r.Schemes("https")
|
88
|
+
|
89
|
+
...or header values:
|
90
|
+
|
91
|
+
r.Headers("X-Requested-With", "XMLHttpRequest")
|
92
|
+
|
93
|
+
...or query values:
|
94
|
+
|
95
|
+
r.Queries("key", "value")
|
96
|
+
|
97
|
+
...or to use a custom matcher function:
|
98
|
+
|
99
|
+
r.MatcherFunc(func(r *http.Request, rm *RouteMatch) bool {
|
100
|
+
return r.ProtoMajor == 0
|
101
|
+
})
|
102
|
+
|
103
|
+
...and finally, it is possible to combine several matchers in a single route:
|
104
|
+
|
105
|
+
r.HandleFunc("/products", ProductsHandler).
|
106
|
+
Host("www.example.com").
|
107
|
+
Methods("GET").
|
108
|
+
Schemes("http")
|
109
|
+
|
110
|
+
Setting the same matching conditions again and again can be boring, so we have
|
111
|
+
a way to group several routes that share the same requirements.
|
112
|
+
We call it "subrouting".
|
113
|
+
|
114
|
+
For example, let's say we have several URLs that should only match when the
|
115
|
+
host is "www.example.com". Create a route for that host and get a "subrouter"
|
116
|
+
from it:
|
117
|
+
|
118
|
+
r := mux.NewRouter()
|
119
|
+
s := r.Host("www.example.com").Subrouter()
|
120
|
+
|
121
|
+
Then register routes in the subrouter:
|
122
|
+
|
123
|
+
s.HandleFunc("/products/", ProductsHandler)
|
124
|
+
s.HandleFunc("/products/{key}", ProductHandler)
|
125
|
+
s.HandleFunc("/articles/{category}/{id:[0-9]+}"), ArticleHandler)
|
126
|
+
|
127
|
+
The three URL paths we registered above will only be tested if the domain is
|
128
|
+
"www.example.com", because the subrouter is tested first. This is not
|
129
|
+
only convenient, but also optimizes request matching. You can create
|
130
|
+
subrouters combining any attribute matchers accepted by a route.
|
131
|
+
|
132
|
+
Subrouters can be used to create domain or path "namespaces": you define
|
133
|
+
subrouters in a central place and then parts of the app can register its
|
134
|
+
paths relatively to a given subrouter.
|
135
|
+
|
136
|
+
There's one more thing about subroutes. When a subrouter has a path prefix,
|
137
|
+
the inner routes use it as base for their paths:
|
138
|
+
|
139
|
+
r := mux.NewRouter()
|
140
|
+
s := r.PathPrefix("/products").Subrouter()
|
141
|
+
// "/products/"
|
142
|
+
s.HandleFunc("/", ProductsHandler)
|
143
|
+
// "/products/{key}/"
|
144
|
+
s.HandleFunc("/{key}/", ProductHandler)
|
145
|
+
// "/products/{key}/details"
|
146
|
+
s.HandleFunc("/{key}/details", ProductDetailsHandler)
|
147
|
+
|
148
|
+
Note that the path provided to PathPrefix() represents a "wildcard": calling
|
149
|
+
PathPrefix("/static/").Handler(...) means that the handler will be passed any
|
150
|
+
request that matches "/static/*". This makes it easy to serve static files with mux:
|
151
|
+
|
152
|
+
func main() {
|
153
|
+
var dir string
|
154
|
+
|
155
|
+
flag.StringVar(&dir, "dir", ".", "the directory to serve files from. Defaults to the current dir")
|
156
|
+
flag.Parse()
|
157
|
+
r := mux.NewRouter()
|
158
|
+
|
159
|
+
// This will serve files under http://localhost:8000/static/<filename>
|
160
|
+
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))
|
161
|
+
|
162
|
+
srv := &http.Server{
|
163
|
+
Handler: r,
|
164
|
+
Addr: "127.0.0.1:8000",
|
165
|
+
// Good practice: enforce timeouts for servers you create!
|
166
|
+
WriteTimeout: 15 * time.Second,
|
167
|
+
ReadTimeout: 15 * time.Second,
|
168
|
+
}
|
169
|
+
|
170
|
+
log.Fatal(srv.ListenAndServe())
|
171
|
+
}
|
172
|
+
|
173
|
+
Now let's see how to build registered URLs.
|
174
|
+
|
175
|
+
Routes can be named. All routes that define a name can have their URLs built,
|
176
|
+
or "reversed". We define a name calling Name() on a route. For example:
|
177
|
+
|
178
|
+
r := mux.NewRouter()
|
179
|
+
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
|
180
|
+
Name("article")
|
181
|
+
|
182
|
+
To build a URL, get the route and call the URL() method, passing a sequence of
|
183
|
+
key/value pairs for the route variables. For the previous route, we would do:
|
184
|
+
|
185
|
+
url, err := r.Get("article").URL("category", "technology", "id", "42")
|
186
|
+
|
187
|
+
...and the result will be a url.URL with the following path:
|
188
|
+
|
189
|
+
"/articles/technology/42"
|
190
|
+
|
191
|
+
This also works for host and query value variables:
|
192
|
+
|
193
|
+
r := mux.NewRouter()
|
194
|
+
r.Host("{subdomain}.domain.com").
|
195
|
+
Path("/articles/{category}/{id:[0-9]+}").
|
196
|
+
Queries("filter", "{filter}").
|
197
|
+
HandlerFunc(ArticleHandler).
|
198
|
+
Name("article")
|
199
|
+
|
200
|
+
// url.String() will be "http://news.domain.com/articles/technology/42?filter=gorilla"
|
201
|
+
url, err := r.Get("article").URL("subdomain", "news",
|
202
|
+
"category", "technology",
|
203
|
+
"id", "42",
|
204
|
+
"filter", "gorilla")
|
205
|
+
|
206
|
+
All variables defined in the route are required, and their values must
|
207
|
+
conform to the corresponding patterns. These requirements guarantee that a
|
208
|
+
generated URL will always match a registered route -- the only exception is
|
209
|
+
for explicitly defined "build-only" routes which never match.
|
210
|
+
|
211
|
+
Regex support also exists for matching Headers within a route. For example, we could do:
|
212
|
+
|
213
|
+
r.HeadersRegexp("Content-Type", "application/(text|json)")
|
214
|
+
|
215
|
+
...and the route will match both requests with a Content-Type of `application/json` as well as
|
216
|
+
`application/text`
|
217
|
+
|
218
|
+
There's also a way to build only the URL host or path for a route:
|
219
|
+
use the methods URLHost() or URLPath() instead. For the previous route,
|
220
|
+
we would do:
|
221
|
+
|
222
|
+
// "http://news.domain.com/"
|
223
|
+
host, err := r.Get("article").URLHost("subdomain", "news")
|
224
|
+
|
225
|
+
// "/articles/technology/42"
|
226
|
+
path, err := r.Get("article").URLPath("category", "technology", "id", "42")
|
227
|
+
|
228
|
+
And if you use subrouters, host and path defined separately can be built
|
229
|
+
as well:
|
230
|
+
|
231
|
+
r := mux.NewRouter()
|
232
|
+
s := r.Host("{subdomain}.domain.com").Subrouter()
|
233
|
+
s.Path("/articles/{category}/{id:[0-9]+}").
|
234
|
+
HandlerFunc(ArticleHandler).
|
235
|
+
Name("article")
|
236
|
+
|
237
|
+
// "http://news.domain.com/articles/technology/42"
|
238
|
+
url, err := r.Get("article").URL("subdomain", "news",
|
239
|
+
"category", "technology",
|
240
|
+
"id", "42")
|
241
|
+
|
242
|
+
Mux supports the addition of middlewares to a Router, which are executed in the order they are added if a match is found, including its subrouters. Middlewares are (typically) small pieces of code which take one request, do something with it, and pass it down to another middleware or the final handler. Some common use cases for middleware are request logging, header manipulation, or ResponseWriter hijacking.
|
243
|
+
|
244
|
+
type MiddlewareFunc func(http.Handler) http.Handler
|
245
|
+
|
246
|
+
Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed to it, and then calls the handler passed as parameter to the MiddlewareFunc (closures can access variables from the context where they are created).
|
247
|
+
|
248
|
+
A very basic middleware which logs the URI of the request being handled could be written as:
|
249
|
+
|
250
|
+
func simpleMw(next http.Handler) http.Handler {
|
251
|
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
252
|
+
// Do stuff here
|
253
|
+
log.Println(r.RequestURI)
|
254
|
+
// Call the next handler, which can be another middleware in the chain, or the final handler.
|
255
|
+
next.ServeHTTP(w, r)
|
256
|
+
})
|
257
|
+
}
|
258
|
+
|
259
|
+
Middlewares can be added to a router using `Router.Use()`:
|
260
|
+
|
261
|
+
r := mux.NewRouter()
|
262
|
+
r.HandleFunc("/", handler)
|
263
|
+
r.Use(simpleMw)
|
264
|
+
|
265
|
+
A more complex authentication middleware, which maps session token to users, could be written as:
|
266
|
+
|
267
|
+
// Define our struct
|
268
|
+
type authenticationMiddleware struct {
|
269
|
+
tokenUsers map[string]string
|
270
|
+
}
|
271
|
+
|
272
|
+
// Initialize it somewhere
|
273
|
+
func (amw *authenticationMiddleware) Populate() {
|
274
|
+
amw.tokenUsers["00000000"] = "user0"
|
275
|
+
amw.tokenUsers["aaaaaaaa"] = "userA"
|
276
|
+
amw.tokenUsers["05f717e5"] = "randomUser"
|
277
|
+
amw.tokenUsers["deadbeef"] = "user0"
|
278
|
+
}
|
279
|
+
|
280
|
+
// Middleware function, which will be called for each request
|
281
|
+
func (amw *authenticationMiddleware) Middleware(next http.Handler) http.Handler {
|
282
|
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
283
|
+
token := r.Header.Get("X-Session-Token")
|
284
|
+
|
285
|
+
if user, found := amw.tokenUsers[token]; found {
|
286
|
+
// We found the token in our map
|
287
|
+
log.Printf("Authenticated user %s\n", user)
|
288
|
+
next.ServeHTTP(w, r)
|
289
|
+
} else {
|
290
|
+
http.Error(w, "Forbidden", http.StatusForbidden)
|
291
|
+
}
|
292
|
+
})
|
293
|
+
}
|
294
|
+
|
295
|
+
r := mux.NewRouter()
|
296
|
+
r.HandleFunc("/", handler)
|
297
|
+
|
298
|
+
amw := authenticationMiddleware{tokenUsers: make(map[string]string)}
|
299
|
+
amw.Populate()
|
300
|
+
|
301
|
+
r.Use(amw.Middleware)
|
302
|
+
|
303
|
+
Note: The handler chain will be stopped if your middleware doesn't call `next.ServeHTTP()` with the corresponding parameters. This can be used to abort a request if the middleware writer wants to.
|
304
|
+
|
305
|
+
*/
|
306
|
+
package mux
|
@@ -0,0 +1,74 @@
|
|
1
|
+
package mux
|
2
|
+
|
3
|
+
import (
|
4
|
+
"net/http"
|
5
|
+
"strings"
|
6
|
+
)
|
7
|
+
|
8
|
+
// MiddlewareFunc is a function which receives an http.Handler and returns another http.Handler.
|
9
|
+
// Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed
|
10
|
+
// to it, and then calls the handler passed as parameter to the MiddlewareFunc.
|
11
|
+
type MiddlewareFunc func(http.Handler) http.Handler
|
12
|
+
|
13
|
+
// middleware interface is anything which implements a MiddlewareFunc named Middleware.
|
14
|
+
type middleware interface {
|
15
|
+
Middleware(handler http.Handler) http.Handler
|
16
|
+
}
|
17
|
+
|
18
|
+
// Middleware allows MiddlewareFunc to implement the middleware interface.
|
19
|
+
func (mw MiddlewareFunc) Middleware(handler http.Handler) http.Handler {
|
20
|
+
return mw(handler)
|
21
|
+
}
|
22
|
+
|
23
|
+
// Use appends a MiddlewareFunc to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router.
|
24
|
+
func (r *Router) Use(mwf ...MiddlewareFunc) {
|
25
|
+
for _, fn := range mwf {
|
26
|
+
r.middlewares = append(r.middlewares, fn)
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
// useInterface appends a middleware to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router.
|
31
|
+
func (r *Router) useInterface(mw middleware) {
|
32
|
+
r.middlewares = append(r.middlewares, mw)
|
33
|
+
}
|
34
|
+
|
35
|
+
// CORSMethodMiddleware automatically sets the Access-Control-Allow-Methods response header
|
36
|
+
// on requests for routes that have an OPTIONS method matcher to all the method matchers on
|
37
|
+
// the route. Routes that do not explicitly handle OPTIONS requests will not be processed
|
38
|
+
// by the middleware. See examples for usage.
|
39
|
+
func CORSMethodMiddleware(r *Router) MiddlewareFunc {
|
40
|
+
return func(next http.Handler) http.Handler {
|
41
|
+
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
42
|
+
allMethods, err := getAllMethodsForRoute(r, req)
|
43
|
+
if err == nil {
|
44
|
+
for _, v := range allMethods {
|
45
|
+
if v == http.MethodOptions {
|
46
|
+
w.Header().Set("Access-Control-Allow-Methods", strings.Join(allMethods, ","))
|
47
|
+
}
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
51
|
+
next.ServeHTTP(w, req)
|
52
|
+
})
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
// getAllMethodsForRoute returns all the methods from method matchers matching a given
|
57
|
+
// request.
|
58
|
+
func getAllMethodsForRoute(r *Router, req *http.Request) ([]string, error) {
|
59
|
+
var allMethods []string
|
60
|
+
|
61
|
+
for _, route := range r.routes {
|
62
|
+
var match RouteMatch
|
63
|
+
if route.Match(req, &match) || match.MatchErr == ErrMethodMismatch {
|
64
|
+
methods, err := route.GetMethods()
|
65
|
+
if err != nil {
|
66
|
+
return nil, err
|
67
|
+
}
|
68
|
+
|
69
|
+
allMethods = append(allMethods, methods...)
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
return allMethods, nil
|
74
|
+
}
|
@@ -0,0 +1,606 @@
|
|
1
|
+
// Copyright 2012 The Gorilla Authors. All rights reserved.
|
2
|
+
// Use of this source code is governed by a BSD-style
|
3
|
+
// license that can be found in the LICENSE file.
|
4
|
+
|
5
|
+
package mux
|
6
|
+
|
7
|
+
import (
|
8
|
+
"context"
|
9
|
+
"errors"
|
10
|
+
"fmt"
|
11
|
+
"net/http"
|
12
|
+
"path"
|
13
|
+
"regexp"
|
14
|
+
)
|
15
|
+
|
16
|
+
var (
|
17
|
+
// ErrMethodMismatch is returned when the method in the request does not match
|
18
|
+
// the method defined against the route.
|
19
|
+
ErrMethodMismatch = errors.New("method is not allowed")
|
20
|
+
// ErrNotFound is returned when no route match is found.
|
21
|
+
ErrNotFound = errors.New("no matching route was found")
|
22
|
+
)
|
23
|
+
|
24
|
+
// NewRouter returns a new router instance.
|
25
|
+
func NewRouter() *Router {
|
26
|
+
return &Router{namedRoutes: make(map[string]*Route)}
|
27
|
+
}
|
28
|
+
|
29
|
+
// Router registers routes to be matched and dispatches a handler.
|
30
|
+
//
|
31
|
+
// It implements the http.Handler interface, so it can be registered to serve
|
32
|
+
// requests:
|
33
|
+
//
|
34
|
+
// var router = mux.NewRouter()
|
35
|
+
//
|
36
|
+
// func main() {
|
37
|
+
// http.Handle("/", router)
|
38
|
+
// }
|
39
|
+
//
|
40
|
+
// Or, for Google App Engine, register it in a init() function:
|
41
|
+
//
|
42
|
+
// func init() {
|
43
|
+
// http.Handle("/", router)
|
44
|
+
// }
|
45
|
+
//
|
46
|
+
// This will send all incoming requests to the router.
|
47
|
+
type Router struct {
|
48
|
+
// Configurable Handler to be used when no route matches.
|
49
|
+
NotFoundHandler http.Handler
|
50
|
+
|
51
|
+
// Configurable Handler to be used when the request method does not match the route.
|
52
|
+
MethodNotAllowedHandler http.Handler
|
53
|
+
|
54
|
+
// Routes to be matched, in order.
|
55
|
+
routes []*Route
|
56
|
+
|
57
|
+
// Routes by name for URL building.
|
58
|
+
namedRoutes map[string]*Route
|
59
|
+
|
60
|
+
// If true, do not clear the request context after handling the request.
|
61
|
+
//
|
62
|
+
// Deprecated: No effect, since the context is stored on the request itself.
|
63
|
+
KeepContext bool
|
64
|
+
|
65
|
+
// Slice of middlewares to be called after a match is found
|
66
|
+
middlewares []middleware
|
67
|
+
|
68
|
+
// configuration shared with `Route`
|
69
|
+
routeConf
|
70
|
+
}
|
71
|
+
|
72
|
+
// common route configuration shared between `Router` and `Route`
|
73
|
+
type routeConf struct {
|
74
|
+
// If true, "/path/foo%2Fbar/to" will match the path "/path/{var}/to"
|
75
|
+
useEncodedPath bool
|
76
|
+
|
77
|
+
// If true, when the path pattern is "/path/", accessing "/path" will
|
78
|
+
// redirect to the former and vice versa.
|
79
|
+
strictSlash bool
|
80
|
+
|
81
|
+
// If true, when the path pattern is "/path//to", accessing "/path//to"
|
82
|
+
// will not redirect
|
83
|
+
skipClean bool
|
84
|
+
|
85
|
+
// Manager for the variables from host and path.
|
86
|
+
regexp routeRegexpGroup
|
87
|
+
|
88
|
+
// List of matchers.
|
89
|
+
matchers []matcher
|
90
|
+
|
91
|
+
// The scheme used when building URLs.
|
92
|
+
buildScheme string
|
93
|
+
|
94
|
+
buildVarsFunc BuildVarsFunc
|
95
|
+
}
|
96
|
+
|
97
|
+
// returns an effective deep copy of `routeConf`
|
98
|
+
func copyRouteConf(r routeConf) routeConf {
|
99
|
+
c := r
|
100
|
+
|
101
|
+
if r.regexp.path != nil {
|
102
|
+
c.regexp.path = copyRouteRegexp(r.regexp.path)
|
103
|
+
}
|
104
|
+
|
105
|
+
if r.regexp.host != nil {
|
106
|
+
c.regexp.host = copyRouteRegexp(r.regexp.host)
|
107
|
+
}
|
108
|
+
|
109
|
+
c.regexp.queries = make([]*routeRegexp, 0, len(r.regexp.queries))
|
110
|
+
for _, q := range r.regexp.queries {
|
111
|
+
c.regexp.queries = append(c.regexp.queries, copyRouteRegexp(q))
|
112
|
+
}
|
113
|
+
|
114
|
+
c.matchers = make([]matcher, len(r.matchers))
|
115
|
+
copy(c.matchers, r.matchers)
|
116
|
+
|
117
|
+
return c
|
118
|
+
}
|
119
|
+
|
120
|
+
func copyRouteRegexp(r *routeRegexp) *routeRegexp {
|
121
|
+
c := *r
|
122
|
+
return &c
|
123
|
+
}
|
124
|
+
|
125
|
+
// Match attempts to match the given request against the router's registered routes.
|
126
|
+
//
|
127
|
+
// If the request matches a route of this router or one of its subrouters the Route,
|
128
|
+
// Handler, and Vars fields of the the match argument are filled and this function
|
129
|
+
// returns true.
|
130
|
+
//
|
131
|
+
// If the request does not match any of this router's or its subrouters' routes
|
132
|
+
// then this function returns false. If available, a reason for the match failure
|
133
|
+
// will be filled in the match argument's MatchErr field. If the match failure type
|
134
|
+
// (eg: not found) has a registered handler, the handler is assigned to the Handler
|
135
|
+
// field of the match argument.
|
136
|
+
func (r *Router) Match(req *http.Request, match *RouteMatch) bool {
|
137
|
+
for _, route := range r.routes {
|
138
|
+
if route.Match(req, match) {
|
139
|
+
// Build middleware chain if no error was found
|
140
|
+
if match.MatchErr == nil {
|
141
|
+
for i := len(r.middlewares) - 1; i >= 0; i-- {
|
142
|
+
match.Handler = r.middlewares[i].Middleware(match.Handler)
|
143
|
+
}
|
144
|
+
}
|
145
|
+
return true
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
if match.MatchErr == ErrMethodMismatch {
|
150
|
+
if r.MethodNotAllowedHandler != nil {
|
151
|
+
match.Handler = r.MethodNotAllowedHandler
|
152
|
+
return true
|
153
|
+
}
|
154
|
+
|
155
|
+
return false
|
156
|
+
}
|
157
|
+
|
158
|
+
// Closest match for a router (includes sub-routers)
|
159
|
+
if r.NotFoundHandler != nil {
|
160
|
+
match.Handler = r.NotFoundHandler
|
161
|
+
match.MatchErr = ErrNotFound
|
162
|
+
return true
|
163
|
+
}
|
164
|
+
|
165
|
+
match.MatchErr = ErrNotFound
|
166
|
+
return false
|
167
|
+
}
|
168
|
+
|
169
|
+
// ServeHTTP dispatches the handler registered in the matched route.
|
170
|
+
//
|
171
|
+
// When there is a match, the route variables can be retrieved calling
|
172
|
+
// mux.Vars(request).
|
173
|
+
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
174
|
+
if !r.skipClean {
|
175
|
+
path := req.URL.Path
|
176
|
+
if r.useEncodedPath {
|
177
|
+
path = req.URL.EscapedPath()
|
178
|
+
}
|
179
|
+
// Clean path to canonical form and redirect.
|
180
|
+
if p := cleanPath(path); p != path {
|
181
|
+
|
182
|
+
// Added 3 lines (Philip Schlump) - It was dropping the query string and #whatever from query.
|
183
|
+
// This matches with fix in go 1.2 r.c. 4 for same problem. Go Issue:
|
184
|
+
// http://code.google.com/p/go/issues/detail?id=5252
|
185
|
+
url := *req.URL
|
186
|
+
url.Path = p
|
187
|
+
p = url.String()
|
188
|
+
|
189
|
+
w.Header().Set("Location", p)
|
190
|
+
w.WriteHeader(http.StatusMovedPermanently)
|
191
|
+
return
|
192
|
+
}
|
193
|
+
}
|
194
|
+
var match RouteMatch
|
195
|
+
var handler http.Handler
|
196
|
+
if r.Match(req, &match) {
|
197
|
+
handler = match.Handler
|
198
|
+
req = requestWithVars(req, match.Vars)
|
199
|
+
req = requestWithRoute(req, match.Route)
|
200
|
+
}
|
201
|
+
|
202
|
+
if handler == nil && match.MatchErr == ErrMethodMismatch {
|
203
|
+
handler = methodNotAllowedHandler()
|
204
|
+
}
|
205
|
+
|
206
|
+
if handler == nil {
|
207
|
+
handler = http.NotFoundHandler()
|
208
|
+
}
|
209
|
+
|
210
|
+
handler.ServeHTTP(w, req)
|
211
|
+
}
|
212
|
+
|
213
|
+
// Get returns a route registered with the given name.
|
214
|
+
func (r *Router) Get(name string) *Route {
|
215
|
+
return r.namedRoutes[name]
|
216
|
+
}
|
217
|
+
|
218
|
+
// GetRoute returns a route registered with the given name. This method
|
219
|
+
// was renamed to Get() and remains here for backwards compatibility.
|
220
|
+
func (r *Router) GetRoute(name string) *Route {
|
221
|
+
return r.namedRoutes[name]
|
222
|
+
}
|
223
|
+
|
224
|
+
// StrictSlash defines the trailing slash behavior for new routes. The initial
|
225
|
+
// value is false.
|
226
|
+
//
|
227
|
+
// When true, if the route path is "/path/", accessing "/path" will perform a redirect
|
228
|
+
// to the former and vice versa. In other words, your application will always
|
229
|
+
// see the path as specified in the route.
|
230
|
+
//
|
231
|
+
// When false, if the route path is "/path", accessing "/path/" will not match
|
232
|
+
// this route and vice versa.
|
233
|
+
//
|
234
|
+
// The re-direct is a HTTP 301 (Moved Permanently). Note that when this is set for
|
235
|
+
// routes with a non-idempotent method (e.g. POST, PUT), the subsequent re-directed
|
236
|
+
// request will be made as a GET by most clients. Use middleware or client settings
|
237
|
+
// to modify this behaviour as needed.
|
238
|
+
//
|
239
|
+
// Special case: when a route sets a path prefix using the PathPrefix() method,
|
240
|
+
// strict slash is ignored for that route because the redirect behavior can't
|
241
|
+
// be determined from a prefix alone. However, any subrouters created from that
|
242
|
+
// route inherit the original StrictSlash setting.
|
243
|
+
func (r *Router) StrictSlash(value bool) *Router {
|
244
|
+
r.strictSlash = value
|
245
|
+
return r
|
246
|
+
}
|
247
|
+
|
248
|
+
// SkipClean defines the path cleaning behaviour for new routes. The initial
|
249
|
+
// value is false. Users should be careful about which routes are not cleaned
|
250
|
+
//
|
251
|
+
// When true, if the route path is "/path//to", it will remain with the double
|
252
|
+
// slash. This is helpful if you have a route like: /fetch/http://xkcd.com/534/
|
253
|
+
//
|
254
|
+
// When false, the path will be cleaned, so /fetch/http://xkcd.com/534/ will
|
255
|
+
// become /fetch/http/xkcd.com/534
|
256
|
+
func (r *Router) SkipClean(value bool) *Router {
|
257
|
+
r.skipClean = value
|
258
|
+
return r
|
259
|
+
}
|
260
|
+
|
261
|
+
// UseEncodedPath tells the router to match the encoded original path
|
262
|
+
// to the routes.
|
263
|
+
// For eg. "/path/foo%2Fbar/to" will match the path "/path/{var}/to".
|
264
|
+
//
|
265
|
+
// If not called, the router will match the unencoded path to the routes.
|
266
|
+
// For eg. "/path/foo%2Fbar/to" will match the path "/path/foo/bar/to"
|
267
|
+
func (r *Router) UseEncodedPath() *Router {
|
268
|
+
r.useEncodedPath = true
|
269
|
+
return r
|
270
|
+
}
|
271
|
+
|
272
|
+
// ----------------------------------------------------------------------------
|
273
|
+
// Route factories
|
274
|
+
// ----------------------------------------------------------------------------
|
275
|
+
|
276
|
+
// NewRoute registers an empty route.
|
277
|
+
func (r *Router) NewRoute() *Route {
|
278
|
+
// initialize a route with a copy of the parent router's configuration
|
279
|
+
route := &Route{routeConf: copyRouteConf(r.routeConf), namedRoutes: r.namedRoutes}
|
280
|
+
r.routes = append(r.routes, route)
|
281
|
+
return route
|
282
|
+
}
|
283
|
+
|
284
|
+
// Name registers a new route with a name.
|
285
|
+
// See Route.Name().
|
286
|
+
func (r *Router) Name(name string) *Route {
|
287
|
+
return r.NewRoute().Name(name)
|
288
|
+
}
|
289
|
+
|
290
|
+
// Handle registers a new route with a matcher for the URL path.
|
291
|
+
// See Route.Path() and Route.Handler().
|
292
|
+
func (r *Router) Handle(path string, handler http.Handler) *Route {
|
293
|
+
return r.NewRoute().Path(path).Handler(handler)
|
294
|
+
}
|
295
|
+
|
296
|
+
// HandleFunc registers a new route with a matcher for the URL path.
|
297
|
+
// See Route.Path() and Route.HandlerFunc().
|
298
|
+
func (r *Router) HandleFunc(path string, f func(http.ResponseWriter,
|
299
|
+
*http.Request)) *Route {
|
300
|
+
return r.NewRoute().Path(path).HandlerFunc(f)
|
301
|
+
}
|
302
|
+
|
303
|
+
// Headers registers a new route with a matcher for request header values.
|
304
|
+
// See Route.Headers().
|
305
|
+
func (r *Router) Headers(pairs ...string) *Route {
|
306
|
+
return r.NewRoute().Headers(pairs...)
|
307
|
+
}
|
308
|
+
|
309
|
+
// Host registers a new route with a matcher for the URL host.
|
310
|
+
// See Route.Host().
|
311
|
+
func (r *Router) Host(tpl string) *Route {
|
312
|
+
return r.NewRoute().Host(tpl)
|
313
|
+
}
|
314
|
+
|
315
|
+
// MatcherFunc registers a new route with a custom matcher function.
|
316
|
+
// See Route.MatcherFunc().
|
317
|
+
func (r *Router) MatcherFunc(f MatcherFunc) *Route {
|
318
|
+
return r.NewRoute().MatcherFunc(f)
|
319
|
+
}
|
320
|
+
|
321
|
+
// Methods registers a new route with a matcher for HTTP methods.
|
322
|
+
// See Route.Methods().
|
323
|
+
func (r *Router) Methods(methods ...string) *Route {
|
324
|
+
return r.NewRoute().Methods(methods...)
|
325
|
+
}
|
326
|
+
|
327
|
+
// Path registers a new route with a matcher for the URL path.
|
328
|
+
// See Route.Path().
|
329
|
+
func (r *Router) Path(tpl string) *Route {
|
330
|
+
return r.NewRoute().Path(tpl)
|
331
|
+
}
|
332
|
+
|
333
|
+
// PathPrefix registers a new route with a matcher for the URL path prefix.
|
334
|
+
// See Route.PathPrefix().
|
335
|
+
func (r *Router) PathPrefix(tpl string) *Route {
|
336
|
+
return r.NewRoute().PathPrefix(tpl)
|
337
|
+
}
|
338
|
+
|
339
|
+
// Queries registers a new route with a matcher for URL query values.
|
340
|
+
// See Route.Queries().
|
341
|
+
func (r *Router) Queries(pairs ...string) *Route {
|
342
|
+
return r.NewRoute().Queries(pairs...)
|
343
|
+
}
|
344
|
+
|
345
|
+
// Schemes registers a new route with a matcher for URL schemes.
|
346
|
+
// See Route.Schemes().
|
347
|
+
func (r *Router) Schemes(schemes ...string) *Route {
|
348
|
+
return r.NewRoute().Schemes(schemes...)
|
349
|
+
}
|
350
|
+
|
351
|
+
// BuildVarsFunc registers a new route with a custom function for modifying
|
352
|
+
// route variables before building a URL.
|
353
|
+
func (r *Router) BuildVarsFunc(f BuildVarsFunc) *Route {
|
354
|
+
return r.NewRoute().BuildVarsFunc(f)
|
355
|
+
}
|
356
|
+
|
357
|
+
// Walk walks the router and all its sub-routers, calling walkFn for each route
|
358
|
+
// in the tree. The routes are walked in the order they were added. Sub-routers
|
359
|
+
// are explored depth-first.
|
360
|
+
func (r *Router) Walk(walkFn WalkFunc) error {
|
361
|
+
return r.walk(walkFn, []*Route{})
|
362
|
+
}
|
363
|
+
|
364
|
+
// SkipRouter is used as a return value from WalkFuncs to indicate that the
|
365
|
+
// router that walk is about to descend down to should be skipped.
|
366
|
+
var SkipRouter = errors.New("skip this router")
|
367
|
+
|
368
|
+
// WalkFunc is the type of the function called for each route visited by Walk.
|
369
|
+
// At every invocation, it is given the current route, and the current router,
|
370
|
+
// and a list of ancestor routes that lead to the current route.
|
371
|
+
type WalkFunc func(route *Route, router *Router, ancestors []*Route) error
|
372
|
+
|
373
|
+
func (r *Router) walk(walkFn WalkFunc, ancestors []*Route) error {
|
374
|
+
for _, t := range r.routes {
|
375
|
+
err := walkFn(t, r, ancestors)
|
376
|
+
if err == SkipRouter {
|
377
|
+
continue
|
378
|
+
}
|
379
|
+
if err != nil {
|
380
|
+
return err
|
381
|
+
}
|
382
|
+
for _, sr := range t.matchers {
|
383
|
+
if h, ok := sr.(*Router); ok {
|
384
|
+
ancestors = append(ancestors, t)
|
385
|
+
err := h.walk(walkFn, ancestors)
|
386
|
+
if err != nil {
|
387
|
+
return err
|
388
|
+
}
|
389
|
+
ancestors = ancestors[:len(ancestors)-1]
|
390
|
+
}
|
391
|
+
}
|
392
|
+
if h, ok := t.handler.(*Router); ok {
|
393
|
+
ancestors = append(ancestors, t)
|
394
|
+
err := h.walk(walkFn, ancestors)
|
395
|
+
if err != nil {
|
396
|
+
return err
|
397
|
+
}
|
398
|
+
ancestors = ancestors[:len(ancestors)-1]
|
399
|
+
}
|
400
|
+
}
|
401
|
+
return nil
|
402
|
+
}
|
403
|
+
|
404
|
+
// ----------------------------------------------------------------------------
|
405
|
+
// Context
|
406
|
+
// ----------------------------------------------------------------------------
|
407
|
+
|
408
|
+
// RouteMatch stores information about a matched route.
|
409
|
+
type RouteMatch struct {
|
410
|
+
Route *Route
|
411
|
+
Handler http.Handler
|
412
|
+
Vars map[string]string
|
413
|
+
|
414
|
+
// MatchErr is set to appropriate matching error
|
415
|
+
// It is set to ErrMethodMismatch if there is a mismatch in
|
416
|
+
// the request method and route method
|
417
|
+
MatchErr error
|
418
|
+
}
|
419
|
+
|
420
|
+
type contextKey int
|
421
|
+
|
422
|
+
const (
|
423
|
+
varsKey contextKey = iota
|
424
|
+
routeKey
|
425
|
+
)
|
426
|
+
|
427
|
+
// Vars returns the route variables for the current request, if any.
|
428
|
+
func Vars(r *http.Request) map[string]string {
|
429
|
+
if rv := r.Context().Value(varsKey); rv != nil {
|
430
|
+
return rv.(map[string]string)
|
431
|
+
}
|
432
|
+
return nil
|
433
|
+
}
|
434
|
+
|
435
|
+
// CurrentRoute returns the matched route for the current request, if any.
|
436
|
+
// This only works when called inside the handler of the matched route
|
437
|
+
// because the matched route is stored in the request context which is cleared
|
438
|
+
// after the handler returns.
|
439
|
+
func CurrentRoute(r *http.Request) *Route {
|
440
|
+
if rv := r.Context().Value(routeKey); rv != nil {
|
441
|
+
return rv.(*Route)
|
442
|
+
}
|
443
|
+
return nil
|
444
|
+
}
|
445
|
+
|
446
|
+
func requestWithVars(r *http.Request, vars map[string]string) *http.Request {
|
447
|
+
ctx := context.WithValue(r.Context(), varsKey, vars)
|
448
|
+
return r.WithContext(ctx)
|
449
|
+
}
|
450
|
+
|
451
|
+
func requestWithRoute(r *http.Request, route *Route) *http.Request {
|
452
|
+
ctx := context.WithValue(r.Context(), routeKey, route)
|
453
|
+
return r.WithContext(ctx)
|
454
|
+
}
|
455
|
+
|
456
|
+
// ----------------------------------------------------------------------------
|
457
|
+
// Helpers
|
458
|
+
// ----------------------------------------------------------------------------
|
459
|
+
|
460
|
+
// cleanPath returns the canonical path for p, eliminating . and .. elements.
|
461
|
+
// Borrowed from the net/http package.
|
462
|
+
func cleanPath(p string) string {
|
463
|
+
if p == "" {
|
464
|
+
return "/"
|
465
|
+
}
|
466
|
+
if p[0] != '/' {
|
467
|
+
p = "/" + p
|
468
|
+
}
|
469
|
+
np := path.Clean(p)
|
470
|
+
// path.Clean removes trailing slash except for root;
|
471
|
+
// put the trailing slash back if necessary.
|
472
|
+
if p[len(p)-1] == '/' && np != "/" {
|
473
|
+
np += "/"
|
474
|
+
}
|
475
|
+
|
476
|
+
return np
|
477
|
+
}
|
478
|
+
|
479
|
+
// uniqueVars returns an error if two slices contain duplicated strings.
|
480
|
+
func uniqueVars(s1, s2 []string) error {
|
481
|
+
for _, v1 := range s1 {
|
482
|
+
for _, v2 := range s2 {
|
483
|
+
if v1 == v2 {
|
484
|
+
return fmt.Errorf("mux: duplicated route variable %q", v2)
|
485
|
+
}
|
486
|
+
}
|
487
|
+
}
|
488
|
+
return nil
|
489
|
+
}
|
490
|
+
|
491
|
+
// checkPairs returns the count of strings passed in, and an error if
|
492
|
+
// the count is not an even number.
|
493
|
+
func checkPairs(pairs ...string) (int, error) {
|
494
|
+
length := len(pairs)
|
495
|
+
if length%2 != 0 {
|
496
|
+
return length, fmt.Errorf(
|
497
|
+
"mux: number of parameters must be multiple of 2, got %v", pairs)
|
498
|
+
}
|
499
|
+
return length, nil
|
500
|
+
}
|
501
|
+
|
502
|
+
// mapFromPairsToString converts variadic string parameters to a
|
503
|
+
// string to string map.
|
504
|
+
func mapFromPairsToString(pairs ...string) (map[string]string, error) {
|
505
|
+
length, err := checkPairs(pairs...)
|
506
|
+
if err != nil {
|
507
|
+
return nil, err
|
508
|
+
}
|
509
|
+
m := make(map[string]string, length/2)
|
510
|
+
for i := 0; i < length; i += 2 {
|
511
|
+
m[pairs[i]] = pairs[i+1]
|
512
|
+
}
|
513
|
+
return m, nil
|
514
|
+
}
|
515
|
+
|
516
|
+
// mapFromPairsToRegex converts variadic string parameters to a
|
517
|
+
// string to regex map.
|
518
|
+
func mapFromPairsToRegex(pairs ...string) (map[string]*regexp.Regexp, error) {
|
519
|
+
length, err := checkPairs(pairs...)
|
520
|
+
if err != nil {
|
521
|
+
return nil, err
|
522
|
+
}
|
523
|
+
m := make(map[string]*regexp.Regexp, length/2)
|
524
|
+
for i := 0; i < length; i += 2 {
|
525
|
+
regex, err := regexp.Compile(pairs[i+1])
|
526
|
+
if err != nil {
|
527
|
+
return nil, err
|
528
|
+
}
|
529
|
+
m[pairs[i]] = regex
|
530
|
+
}
|
531
|
+
return m, nil
|
532
|
+
}
|
533
|
+
|
534
|
+
// matchInArray returns true if the given string value is in the array.
|
535
|
+
func matchInArray(arr []string, value string) bool {
|
536
|
+
for _, v := range arr {
|
537
|
+
if v == value {
|
538
|
+
return true
|
539
|
+
}
|
540
|
+
}
|
541
|
+
return false
|
542
|
+
}
|
543
|
+
|
544
|
+
// matchMapWithString returns true if the given key/value pairs exist in a given map.
|
545
|
+
func matchMapWithString(toCheck map[string]string, toMatch map[string][]string, canonicalKey bool) bool {
|
546
|
+
for k, v := range toCheck {
|
547
|
+
// Check if key exists.
|
548
|
+
if canonicalKey {
|
549
|
+
k = http.CanonicalHeaderKey(k)
|
550
|
+
}
|
551
|
+
if values := toMatch[k]; values == nil {
|
552
|
+
return false
|
553
|
+
} else if v != "" {
|
554
|
+
// If value was defined as an empty string we only check that the
|
555
|
+
// key exists. Otherwise we also check for equality.
|
556
|
+
valueExists := false
|
557
|
+
for _, value := range values {
|
558
|
+
if v == value {
|
559
|
+
valueExists = true
|
560
|
+
break
|
561
|
+
}
|
562
|
+
}
|
563
|
+
if !valueExists {
|
564
|
+
return false
|
565
|
+
}
|
566
|
+
}
|
567
|
+
}
|
568
|
+
return true
|
569
|
+
}
|
570
|
+
|
571
|
+
// matchMapWithRegex returns true if the given key/value pairs exist in a given map compiled against
|
572
|
+
// the given regex
|
573
|
+
func matchMapWithRegex(toCheck map[string]*regexp.Regexp, toMatch map[string][]string, canonicalKey bool) bool {
|
574
|
+
for k, v := range toCheck {
|
575
|
+
// Check if key exists.
|
576
|
+
if canonicalKey {
|
577
|
+
k = http.CanonicalHeaderKey(k)
|
578
|
+
}
|
579
|
+
if values := toMatch[k]; values == nil {
|
580
|
+
return false
|
581
|
+
} else if v != nil {
|
582
|
+
// If value was defined as an empty string we only check that the
|
583
|
+
// key exists. Otherwise we also check for equality.
|
584
|
+
valueExists := false
|
585
|
+
for _, value := range values {
|
586
|
+
if v.MatchString(value) {
|
587
|
+
valueExists = true
|
588
|
+
break
|
589
|
+
}
|
590
|
+
}
|
591
|
+
if !valueExists {
|
592
|
+
return false
|
593
|
+
}
|
594
|
+
}
|
595
|
+
}
|
596
|
+
return true
|
597
|
+
}
|
598
|
+
|
599
|
+
// methodNotAllowed replies to the request with an HTTP status code 405.
|
600
|
+
func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
|
601
|
+
w.WriteHeader(http.StatusMethodNotAllowed)
|
602
|
+
}
|
603
|
+
|
604
|
+
// methodNotAllowedHandler returns a simple request handler
|
605
|
+
// that replies to each request with a status code 405.
|
606
|
+
func methodNotAllowedHandler() http.Handler { return http.HandlerFunc(methodNotAllowed) }
|