Slide 1

Slide 1 text

HTTP in Ruby: Tips, Tricks and Techniques Andrey Deryabin

Slide 2

Slide 2 text

Andrey Deryabin

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

EVIL MARTIANS

Slide 5

Slide 5 text

EVIL MARTIANS

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

By Roman Shamin: https://twitter.com/romanshamin

Slide 8

Slide 8 text

¡Vamos allá!

Slide 9

Slide 9 text

HTTP Hypertext Transfer Protocol

Slide 10

Slide 10 text

History – HTTP/1.1 (v1) - 1999 (RFCs 2616, 7230, 7231, 7232, 7233, 7234 and 7235)

Slide 11

Slide 11 text

HTTP 1.1 – most popular protocol in WEB – a stateless – request => response

Slide 12

Slide 12 text

curl -v https: //google.com > GET / HTTP/1.1 > Host: google.com > User-Agent: curl/7.51.0 > Accept: */* > < HTTP/1.1 302 Found < Cache-Control: private < Content-Type: text/html; charset=UTF-8 < Location: https: // www.google.es/?gfe_rd=cr&dcr=0&ei=XIINWr2XIuis8wfFipPwDQ < Content-Length: 269 < 302 Moved

302 Moved

The document has moved here . Method Resource Protocol Request Response Body Headers

Slide 13

Slide 13 text

History – HTTP/1.1 (v1) - 1999 (RFCs 2616, 7230, 7231, 7232, 7233, 7234 and 7235) – HTTP/2.0 -2015 (RFC 7540)

Slide 14

Slide 14 text

HTTP/2 – Binary framing parsing and encoding – Query multiplexing – Headers Compression – Connection and stream management – And more and more

Slide 15

Slide 15 text

HTTP Evolution http://crowdhailer.me/talks/2017-08-17/working-with-http2-in-elixir/slides.html#37

Slide 16

Slide 16 text

What is about ?

Slide 17

Slide 17 text

How many Ruby HTTP clients do you know?

Slide 18

Slide 18 text

Ruby HTTP Clients Net::HTTP RestClient Faraday Ethcon Curb Typhoeus HTTP.rb HTTPClient HTTParty Patron EM-HTTP-Request Excon

Slide 19

Slide 19 text

What are the differences?

Slide 20

Slide 20 text

Engine?

Slide 21

Slide 21 text

Ruby HTTP Clients Faraday Ethcon Curb Typhoeus Patron Libcurl EM-HTTP-Request Excon Event machine HTTParty Net::HTTP RestClient HTTP.rb HTTPClient TCPSocket Net::HTTP

Slide 22

Slide 22 text

DSL?

Slide 23

Slide 23 text

Type of connection? – No Keep Alive – Keep Alive – Pipelining

Slide 24

Slide 24 text

What else?

Slide 25

Slide 25 text

Feature (Bugs)!!!

Slide 26

Slide 26 text

Net::HTTP # request uri = URI.parse('http: //localhost:4567/json') http = Net ::HTTP.new(uri.host, uri.port) request = Net ::HTTP ::Post.new(uri.request_uri, 'Content-Type' => 'text/json') request.body = { ‘first_name' => 'Leo', ‘last_name' => 'Messi' }.to_json http.request(request) # response [201, { "Сontent-length" => "7", 'Connection' => "Barca" }, "Created"]

Slide 27

Slide 27 text

Net::HTTP { "content-type" =>"text/html;charset=utf-8", "connection" =>"Barca, close", "x-xss-protection" =>"1; mode=block", "x-content-type-options" =>"nosniff", "x-frame-options" =>"SAMEORIGIN", "content-length" =>"7" } WHAT?

Slide 28

Slide 28 text

Net::HTTP # [201, { "Сontent-length" => "7", 'Connection' => "Barca" }, "Created"] { "content-type" =>"text/html;charset=utf-8", "connection" =>"Barca, close", "x-xss-protection" =>"1; mode=block", "x-content-type-options" =>"nosniff", "x-frame-options" =>"SAMEORIGIN", "content-length" =>"7" } WHAT?

Slide 29

Slide 29 text

RFC 2616 - "Hypertext Transfer Protocol -- HTTP/1.1", Section 4.2, "Message Headers" Each header field consists of a name followed by a colon (":") and the field value. Field names are case- insensitive

Slide 30

Slide 30 text

HTTPClient Documented? NO!

Slide 31

Slide 31 text

Typhoeus Typhoeus ::Request.new('localhost:4567/?lang=ruby&author=matz', method: :get).run

Slide 32

Slide 32 text

Typhoeus # request headers {"User-Agent" =>"Typhoeus - https: //github.com/typhoeus/typhoeus”} WHAT?

Slide 33

Slide 33 text

Typhoeus Default User-Agent?

Slide 34

Slide 34 text

Typhoeus

Slide 35

Slide 35 text

Typhoeus

Slide 36

Slide 36 text

Fail story 1

Slide 37

Slide 37 text

Groupon

Slide 38

Slide 38 text

# Simplifed ERB <%= Ipgeobase.lookup(current_ip).city %>

Slide 39

Slide 39 text

# https: //github.com/mokevnin/ipgeobase/blob/master/lib/ipgeobase.rb require 'uri' require 'open-uri' module Ipgeobase URL = 'http: //ipgeobase.ru:7020/geo' autoload 'IpMetaData', 'ipgeobase/ip_meta_data' def self.lookup(ip, params = {}) uri = URI.parse(URL) uri.query = URI.encode_ www_form :ip => ip resp = open(uri, params).read() IpMetaData.parse(resp.to_s) end end

Slide 40

Slide 40 text

Use timeouts

Slide 41

Slide 41 text

Fail story 2

Slide 42

Slide 42 text

Useless box

Slide 43

Slide 43 text

TopSecretProjectMlApi ::AdaptiveFeed .perform({id:"15234", date: "2017-09-07"})

Slide 44

Slide 44 text

What is it? API call? What JSON do you send?

Slide 45

Slide 45 text

Epic thinking face

Slide 46

Slide 46 text

Control HTTP traffic

Slide 47

Slide 47 text

Command line tools

Slide 48

Slide 48 text

Tcpdump

Slide 49

Slide 49 text

GUI applications

Slide 50

Slide 50 text

Charles (Web debug proxy application)

Slide 51

Slide 51 text

Developer friendly?

Slide 52

Slide 52 text

puts "no"

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Sniffer – Log all requests – Debug requests – Understand what is under the hood

Slide 55

Slide 55 text

gem install sniffer

Slide 56

Slide 56 text

Sniffer supports most popular HTTP clients: Net::HTTP HTTP HTTPClient Patron Curb Ethan Typhoeus

Slide 57

Slide 57 text

Disabled by default

Slide 58

Slide 58 text

Sniffer.enable!

Slide 59

Slide 59 text

Easy configuration

Slide 60

Slide 60 text

Sniffer.config do logger: Logger.new($stdout), severity: Logger ::Severity ::DEBUG, # HTTP options to log log: { request_url: true, request_headers: true, request_body: true, request_method: true, response_status: true, response_headers: true, response_body: true, timing: true }, store: true, # save requests/responses to Sniffer.data enabled: false # Sniffer disabled by default end

Slide 61

Slide 61 text

Time to play

Slide 62

Slide 62 text

pry(main)> Sniffer.enable!; pry(main)> client = Elasticsearch ::Client.new; pry(main)> client.search q: 'Barcelona on Rails'; D, [2017-11-16T01:19:45.455444 #45358] DEBUG -- : {"port": 9200,"host":"localhost","query":"/_search? q=Barcelona+on+Rails","rq_content_type":"application/ json","rq_user_agent":"Faraday v0.11.0","rq_accept_encoding":"gzip;q=1.0,deflate;q=0.6,identity ;q=0.3","rq_accept":"*/ *","rq_connection":"close","method":"GET","request_body":"","sta tus":200,"rs_content_type":"application/json; charset=UTF-8","rs_content_length":"124","timing": 1.6817439999431372,"response_body":"{\"took\": 989,\"timed_out\":false,\"_shards\":{\"total\":5,\"successful\": 5,\"failed\":0},\"hits\":{\"total\":0,\"max_score\":null, \"hits\":[]}}"} Example pry(main)> Sniffer.enable!; pry(main)> client = Elasticsearch ::Client.new; pry(main)> client.search q: 'Barcelona on Rails'; D, [2017-11-16T01:19:45.455444 #45358] DEBUG -- : {"port": 9200,"host":"localhost","query":"/_search? q=Barcelona+on+Rails","rq_content_type":"application/ json","rq_user_agent":"Faraday v0.11.0","rq_accept_encoding":"gzip;q=1.0,deflate;q=0.6,identity ;q=0.3","rq_accept":"*/ *","rq_connection":"close","method":"GET","request_body":"","sta tus":200,"rs_content_type":"application/json; charset=UTF-8","rs_content_length":"124","timing": 1.6817439999431372,"response_body":"{\"took\": 989,\"timed_out\":false,\"_shards\":{\"total\":5,\"successful\": 5,\"failed\":0},\"hits\":{\"total\":0,\"max_score\":null, \"hits\":[]}}"}

Slide 63

Slide 63 text

{"port":9200,"host":"localhost","query":"/_search? q=Barcelona+on+Rails","rq_content_type":"application/ json","rq_user_agent":"Faraday v0.11.0","rq_accept_encoding":"gzip;q=1.0,deflate;q=0.6,identity ;q=0.3","rq_accept":"*/ *","rq_connection":"close","method":"GET","request_body":"","sta tus":200,"rs_content_type":"application/json; charset=UTF-8","rs_content_length":"124","timing": 1.6817439999431372,"response_body":"{\"took\": 989,\"timed_out\":false,\"_shards\":{\"total\":5,\"successful\": 5,\"failed\":0},\"hits\":{\"total\":0,\"max_score\":null, \"hits\":[]}}"} Logging

Slide 64

Slide 64 text

Sniffer.data.first.request => #"application/json", "user-agent" =>"Faraday v0.11.0", "accept- encoding" =>"gzip;q=1.0,deflate;q=0.6,identity;q=0.3", "accept" =>"*/*", "connection" =>"close"}, @host="localhost", @method="GET", @port=9200, @query="/_search?q=Barcelona+on+Rails"> Debug request

Slide 65

Slide 65 text

Sniffer.data.first.response => #"application/json; charset=UTF-8", "content-length" =>"124"}, @status=200, @timing=1.6817439999431372> Debug response

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Resume

Slide 68

Slide 68 text

Read a source code

Slide 69

Slide 69 text

Write open source

Slide 70

Slide 70 text

Use Sniffer!

Slide 71

Slide 71 text

Thanks! aderyabin @aderyabin @evilmartians evilmartians.com [email protected]