2010-05-27

schemeのないURLは相対URL

知らなかった。もしかして Web を支える技術には載ってました?(未読)

問題

  • GoogleAjaxAPI を使って jQuery を読み込んでいた
  • 開発、検証環境では http だが本番では https で動く
  • http での読み込みを決め打ちしてたら IE で怒られた><

要件

  • schemeの変化に自動で追随してほしい

Google Analytics とかどうしてんの? なんか scheme の判別して URL を組み立て直してるよ。あーじゃあこれ真似すればいいか、なんかでも面倒だな。

ここで神降臨!

Twitter / Yosuke HASEGAWA: @wtnabe script src="//exam …

@wtnabe <script src="//example.jp/foo.js">と書けば現在のschemeで読んでくれます。

……。ほんとだ。

RFC 2396

しかし実はこの根拠が分からなかった。なんか言われればそれでいいような気がするけど、scheme だけ欠けている URL を見たことがなかったから。でもちゃんと定義されていた。

RFC 2396 - Uniform Resource Identifiers (URI): Generic Syntax

     absoluteURI   = scheme ":" ( hier_part | opaque_part )

  URI that are hierarchical in nature use the slash "/" character for
  separating hierarchical components.  For some file systems, a "/"
  character (used to denote the hierarchical structure of a URI) is the
  delimiter used to construct a file name hierarchy, and thus the URI
  path will look similar to a file pathname.  This does NOT imply that
  the resource is a file or that the URI maps to an actual filesystem
  pathname.

     hier_part     = ( net_path | abs_path ) [ "?" query ]

     net_path      = "//" authority [ abs_path ]

     abs_path      = "/"  path_segments

(snip

  The syntax for relative URI takes advantage of the <hier_part> syntax
  of <absoluteURI> (Section 3) in order to express a reference that is
  relative to the namespace of another hierarchical URI.

     relativeURI   = ( net_path | abs_path | rel_path ) [ "?" query ]

  A relative reference beginning with two slash characters is termed a
  network-path reference, as defined by <net_path> in Section 3.  Such
  references are rarely used.

えーと、これで見ると

//example.com/path/to/resource

net_path として定義されていて、net_path だけの URL は relativeURI と定義されていることになる。なるほどー。

※ ちなみにちゃんと読んだら Google Analytics の tracking code が scheme 判別をしているのは scheme によって net_path が異なるからだった。確かにこの場合は判別して書き換えないとダメだね。

About

例によって個人のなんちゃらです

Recent Posts

Categories

Tool 日々 Web Biz Net Apple MS ことば News Unix howto Food PHP Movie Edu Community Book Security Text TV Perl Ruby Music Pdoc 生き方 RDoc ViewCVS CVS Rsync Disk Mail FreeBSD Cygwin PDF Photo Zebedee Debian OSX Comic Cron Sysadmin Font Analog iCal Sunbird DNS Linux Wiki Emacs Thunderbird Sitecopy Terminal Drawing tDiary AppleScript Life Money Omni PukiWiki Xen XREA Zsh Screen CASL Firefox Fink zsh haXe Ecmascript PATH_INFO SQLite PEAR Lighttpd FastCGI Subversion au prototype.js jsUnit Apache Trac Template Java Rhino Mochikit Feed Bloglines CSS del.icio.us SBS qwikWeb gettext Ajax JSDoc Rails HTML CHM EPWING NDTP EB IE CLI ck ThinkPad Toy WSH RFC readline rlwrap ImageMagick epeg Frenzy sysprep Ubuntu MeCab DTP ERD DBMS eclipse Eclipse Awk RD Diigo XAMPP RubyGems PHPDoc iCab DOM YAML Camino Geekmonkey w3m Scheme Gauche Lisp JSAN Google VMware DSL SLAX Safari Markdown Textile IRC Jabber Fastladder MacPorts LLSpirit CPAN Mozilla Twitter OpenFL Rswatch ITS NTP GUI Pragger Yapra XML Mobile Git Study JSON VirtualBox Samba Pear Growl Mercurial Rack Capistrano Rake Win RSS Mechanize Sitemaps Android JavaScript Python RTM OOo iPod Yahoo Unicode Github iTunes God SBM friendfeed Friendfeed HokuUn Sinatra TDD Test Project Evernote iPad Geohash Location Map Search Simplenote Image WebKit RSpec Phone CSV WiMAX USB Chrome RubyKaigi RubyKaigi2011 Space CoffeeScript Nokogiri Hpricot Rubygems jQuery Node GTD CI UX Design VCS Kanazawa.rb Kindle Amazon Agile Vagrant Chef Windows Composer Dotenv PaaS Itamae SaaS Docker Swagger Grape WebAPI Microservices OmniAuth HTTP 分析基盤 CDN Terraform IaaS HCL Webpack Vue.js BigQuery Middleman CMS AWS PNG Laravel Selenium OAuth OpenAPI GitHub UML GCP TypeScript SQL Hanami Document SVG AsciiDoc Pandoc DocBook Develop Jekyll macOS Node.js Vite Heroku Transformer AI Data Cloud Wasm