Share
Embed
Transcript
100 slides
-
▪ Encodings in Rails ▪ Tokaido (Rails.app) ▪ Bundler ▪
Ember.js HARD THINGS. -
A WILD REPLACEMENT CHARACTER APPEARS Identify symptoms Understand domain Identify
root cause Consider solutions Solve root cause Community work -
A WILD CORRUPTED CHARACTER APPEARS ümlaut Identify symptoms Understand domain
Identify root cause Consider solutions Solve root cause Community work -
Identify symptoms Understand domain Identify root cause Consider solutions Solve
root cause Community work ENCODINGS. -
"Yehüda".encode("ISO-8859-1") => "\x59\x65\x68\xFC\x64\x61" "Yehüda".encode("UTF-8") => "\x59\x65\x68\xC3xBC\x64\x61" "Yehüda".encode("UTF-16") => "\xFE\xFF\x00Y\x00e\x00h\x00\xFC\x00d \x00a"
BYTES. A String is a sequence of bytes. It doesn't have any marker indicating how those bytes should be interpreted. -
Y e h u d a 0x59 0x65 0x68 0x75
0x64 0x61 UTF-8 Y e h u d a 0x59 0x65 0x68 0x75 0x64 0x61 Latin-1 ASCII CHARS ARE REPRESENTED THE SAME. -
Y e h u d a 0x59 0x65 0x68 0x75
0x64 0x61 UTF-8 Y e h u d a 0x59 0x65 0x68 0x75 0x64 0x61 Latin-1 Y e h ü d a 0x59 0x65 0x68 0xc3 0xbc 0x64 0x61 UTF-8 Y e h ü d a 0x59 0x65 0x68 0xfc 0x64 0x61 Latin-1 ASCII CHARS ARE REPRESENTED THE SAME. OTHER CHARS ARE REPRESENTED DIFFERENTLY. -
Y e h ü d a 0x59 0x65 0x68 0xfc
0x64 0x61 Latin-1 -
Y e h ü d a 0x59 0x65 0x68 0xfc
0x64 0x61 UTF-8 ACCIDENTALLY INTERPRET INCORRECTLY. -
? ? ? ? ? ? 0x59 0x65 0x68 0xfc
0x64 0x61 UTF-8 -
Y ? ? ? ? ? 0x59 0x65 0x68 0xfc
0x64 0x61 UTF-8 -
Y e ? ? ? ? 0x59 0x65 0x68 0xfc
0x64 0x61 UTF-8 -
Y e h ? ? ? 0x59 0x65 0x68 0xfc
0x64 0x61 UTF-8 -
Y e h ? ? 0x59 0x65 0x68 0xfc 0x64
0x61 UTF-8 -
Y e h d ? 0x59 0x65 0x68 0xfc 0x64
0x61 UTF-8 -
Y e h d a 0x59 0x65 0x68 0xfc 0x64
0x61 UTF-8 -
Y e h ü d a 0x59 0x65 0x68 0xc3
0xbc 0x64 0x61 UTF-8 -
Y e h ü d a 0x59 0x65 0x68 0xc3
0xbc 0x64 0x61 Latin-1 -
Y e h ü d a 0x59 0x65 0x68 0xc3
0xbc 0x64 0x61 Latin-1 ACCIDENTALLY INTERPRET INCORRECTLY. -
? ? ? ? ? ? ? 0x59 0x65 0x68
0xc3 0xbc 0x64 0x61 Latin-1 -
Y ? ? ? ? ? ? 0x59 0x65 0x68
0xc3 0xbc 0x64 0x61 Latin-1 -
Y e ? ? ? ? ? 0x59 0x65 0x68
0xc3 0xbc 0x64 0x61 Latin-1 -
Y e h ? ? ? ? 0x59 0x65 0x68
0xc3 0xbc 0x64 0x61 Latin-1 -
Y e h à ? ? ? 0x59 0x65 0x68
0xc3 0xbc 0x64 0x61 Latin-1 -
Y e h à ¼ ? ? 0x59 0x65 0x68
0xc3 0xbc 0x64 0x61 Latin-1 -
Y e h à ¼ d ? 0x59 0x65 0x68
0xc3 0xbc 0x64 0x61 Latin-1 -
Y e h à ¼ d a 0x59 0x65 0x68
0xc3 0xbc 0x64 0x61 Latin-1 -
BOUNDARIES. Identify symptoms Understand domain Identify root cause Consider solutions
Solve root cause Community work -
rails UTF-8 HTTP MySQL GET params Nokogiri templates POST params
We would like to be able to work inside Rails as if everything is UTF-8. -
THE ENCODING OF THE PAGE! Identify symptoms Understand domain Identify
root cause Consider solutions Solve root cause Community work -
cattr_accessor(:default_charset) { "utf-8" } CONTENT_TYPE = "Content-Type".freeze def assign_default_content_type_and_charset! return
if headers[CONTENT_TYPE].present? @content_type ||= Mime::HTML @charset ||= self.class.default_charset type = @content_type.to_s.dup unless @sending_file type << "; charset=#{@charset}" end headers[CONTENT_TYPE] = type end PAGE'S ENCODING. -
form attribute: accept-charset This attribute speci es the list of
character encodings for input data that is accepted by the server processing this form “ HTML4 SPEC Identify symptoms Understand domain Identify root cause Consider solutions Solve root cause Community work -
form attribute: accept-charset This attribute speci es the list of
character encodings for input data that is accepted by the server processing this form “ HTML4 SPEC Identify symptoms Understand domain Identify root cause Consider solutions Solve root cause Community work -
form attribute: accept-charset Possible values: UTF-8 If the user enters
characters that are not in the character set of the document containing the form, the UTF-8 character set is used. “ MSDN -
form attribute: accept-charset Possible values: UTF-8 If the user enters
characters that are not in the character set of the document containing the form, the UTF-8 character set is used. “ MSDN -
form attribute: accept-charset Possible values: UTF-8 If the user enters
characters that are not in the character set of the document containing the form, the UTF-8 character set is used. “ MSDN -
form attribute: accept-charset Possible values: UTF-8 If the user enters
characters that are not in the character set of the document containing the form, the UTF-8 character set is used. “ MSDN -
<form accept-charset="utf-8"> <textarea> Latin-1 chárâctërs pasted from another application </textarea>
<button type="submit">Do it!</button> </form> PROBLEM. -
FAILURE. User changes encoding to Latin-1 User pastes chárâctërs from
Word IE sees that chárâctërs are in Latin-1 IE ignores accept-charset :( :( :( -
<form accept-charset="utf-8"> <input type="hidden" name="utf8" value="✓"> <textarea> Latin-1 chárâctërs pasted
from another application </textarea> <button type="submit">Do it!</button> </form> SNOWMAN HACK! -
SUCCESS. User changes encoding to Latin-1 User pastes chárâctërs from
Word IE sees that ✓ is not in Latin-1 IE honors accept-charset <3<3<3 -
IN CONTROL. Identify symptoms Understand domain Identify root cause Consider
solutions Solve root cause Community work rails UTF-8 POST params -
rails UTF-8 HTTP MySQL GET params Nokogiri templates POST params
Now rinse and repeat for the rest of these. REPEAT. Identify symptoms Understand domain Identify root cause Consider solutions Solve root cause Community work -
FORCE_ENCODING? Identify symptoms Understand domain Identify root cause Consider solutions
Solve root cause Community work -
DEFAULT_INTERNAL. Identify symptoms Understand domain Identify root cause Consider solutions
Solve root cause Community work -
I was keenly interested in an open source open government
project (Sunlight Foundation). I tried to get it running on my Mac, on my personal time. I had direct expert help (friends who dev using Ruby, Rails, etc.). I understand the tool stack was churning, something about mismatched versions, runtimes, whatever. I gave up after three weeks. “ HACKER NEWS. -
I was keenly interested in an open source open government
project (Sunlight Foundation). I tried to get it running on my Mac, on my personal time. I had direct expert help (friends who dev using Ruby, Rails, etc.). I understand the tool stack was churning, something about mismatched versions, runtimes, whatever. I gave up after three weeks. “ HACKER NEWS. Identify symptoms Understand domain Identify root cause Consider solutions Solve root cause Community work -
STATIC VS. DYNAMIC LINKING. Identify symptoms Understand domain Identify root
cause Consider solutions Solve root cause Community work -
$ DYLD_PRINT_BINDINGS=1 ruby -rpsych -e"" ... snip ... dyld: loaded:
<snip>psych.bundle dyld: loaded: <snip>libyaml-0.2.dylib DYNAMIC LOAD. -
// ext/psych/parser.c static VALUE allocate(VALUE klass) { yaml_parser_t * parser;
parser = xmalloc(sizeof(yaml_parser_t)); yaml_parser_initialize(parser); return Data_Wrap_Struct(klass, 0, dealloc, parser); } WHY? -
$ otool -L psych.bundle /Users/wycats/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/ x86_64-darwin11.3.0/psych.bundle: " /Users/wycats/.rvm/rubies/ruby-1.9.3-p194/lib/libruby. 1.9.1.dylib (compatibility
version 1.9.1, current version 1.9.1) " /Users/wycats/.rvm/usr/lib/libyaml-0.2.dylib (compatibility version 3.0.0, current version 3.2.0) " /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0) " /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0) HOW IT WORKS. -
$ otool -L psych.bundle /Users/wycats/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/ x86_64-darwin11.3.0/psych.bundle: " /Users/wycats/.rvm/rubies/ruby-1.9.3-p194/lib/libruby. 1.9.1.dylib (compatibility
version 1.9.1, current version 1.9.1) " /Users/wycats/.rvm/usr/lib/libyaml-0.2.dylib (compatibility version 3.0.0, current version 3.2.0) " /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0) " /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0) HOW IT WORKS. -
$ otool -L psych.bundle /Users/wycats/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/ x86_64-darwin11.3.0/psych.bundle: " /Users/wycats/.rvm/rubies/ruby-1.9.3-p194/lib/libruby. 1.9.1.dylib (compatibility
version 1.9.1, current version 1.9.1) " /Users/wycats/.rvm/usr/lib/libyaml-0.2.dylib (compatibility version 3.0.0, current version 3.2.0) " /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0) " /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0) HOW IT WORKS. -
ruby libyaml psych.bundle libruby Identify symptoms Understand domain Identify root
cause Consider solutions Solve root cause Community work -
$ otool -L psych.bundle /Users/wycats/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/ x86_64-darwin11.3.0/psych.bundle: " /Users/wycats/.rvm/rubies/ruby-1.9.3-p194/lib/libruby. 1.9.1.dylib (compatibility
version 1.9.1, current version 1.9.1) " /Users/wycats/.rvm/usr/lib/libyaml-0.2.dylib (compatibility version 3.0.0, current version 3.2.0) " /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0) " /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0) HIDDEN PROBLEMS. -
$ otool -L psych.bundle /Users/wycats/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/ x86_64-darwin11.3.0/psych.bundle: " /Users/wycats/.rvm/rubies/ruby-1.9.3-p194/lib/libruby. 1.9.1.dylib (compatibility
version 1.9.1, current version 1.9.1) " /Users/wycats/.rvm/usr/lib/libyaml-0.2.dylib (compatibility version 3.0.0, current version 3.2.0) " /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0) " /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0) HIDDEN PROBLEMS. -
AUTOMATE COMPILATION? Identify symptoms Understand domain Identify root cause Consider
solutions Solve root cause Community work -
MOST OF THE PROBLEMS HAPPEN IN COMPILATION. Identify symptoms Understand
domain Identify root cause Consider solutions Solve root cause Community work -
$ otool -L psych.bundle /Users/wycats/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/ x86_64-darwin11.3.0/psych.bundle: " /Users/wycats/.rvm/rubies/ruby-1.9.3-p194/lib/libruby. 1.9.1.dylib (compatibility
version 1.9.1, current version 1.9.1) " /Users/wycats/.rvm/usr/lib/libyaml-0.2.dylib (compatibility version 3.0.0, current version 3.2.0) " /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0) " /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0) HIDDEN PROBLEMS. Does the precompiled Ruby actually solve these problems? Is it actually portable? -
STATICALLY COMPILED RUBY AND DEPENDENCIES. Identify symptoms Understand domain Identify
root cause Consider solutions Solve root cause Community work -
./configure --static (libyaml) ./configure --enable-static (pkg-config) ./configure --disable-shared (others) pkg-config
(needed for openssl) STATICALLY COMPILE. -
./configure --static (libyaml) ./configure --enable-static (pkg-config) ./configure --disable-shared (others) pkg-config
(needed for openssl) STATICALLY COMPILE. -
$ man ld -search_paths_first This is now the default (in
Xcode4 tools). When processing -lx the linker now searches each directory in its library search paths for `libx.dylib' then `libx.a' before the moving on to the next path in the library search path. LINKER. Make sure that the linker finds the .a files before it finds system- installed .dylibs -
$ otool -L psych.bundle /Users/wycats/.sm/pkg/versions/tokaidoapp/lib/ruby/1.9.1/ x86_64-darwin11.3.0/psych.bundle: " /usr/lib/libSystem.B.dylib (compatibility version
1.0.0, current version 159.1.0) " /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0) PROBLEM SOLVED? Does the precompiled Ruby actually solve these problems? Is it actually portable? -
PRECOMPILED STATIC GEMS. Identify symptoms Understand domain Identify root cause
Consider solutions Solve root cause Community work -
Identify symptoms Understand domain Identify root cause Consider solutions Solve
root cause Community work A simple solution to a complex problem by definition means that much of the original complexity is hidden from you. That means, perversely, that the better a tool is at solving a complex problem, the less it seems necessary. Often complex problems seem simple to the naïve observer. If you are comparing two solutions and one is 10,000 lines of code and the other 1,000 lines of code, consider that the implementor of the second solution may not yet have a deep enough understanding of the problem to shield you the user from the complexity of the problem. Consider that the extra 9,000 lines that may seem like bloat to you may in fact be the full understanding of a complex problem, written down in code.