Slide 1

Slide 1 text

HTTP { Hypertext Transfer Protocol }

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

HTML HTTP

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

https://speakerinnen.org/de/sign_up is translated to https://54.255.158.2:443/de/sign_up using the DNS protocol

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

4 Connect to the computer reachable under IP address 54.255.158.2 4 Use an SSL/TLS encrypted TCP socket on port 443 4 Speak HTTP 4 Tell the server we know it as speakerinnen.org 4 Access resource /de/sign_up

Slide 9

Slide 9 text

Server listens on TCP port

Slide 10

Slide 10 text

TCP socket 4 Is opened when client connects to port 4 Client and server can send each other messages

Slide 11

Slide 11 text

Client starts speaking GET /de/sign_up HTTP/1.1 Host: speakerinnen.org

Slide 12

Slide 12 text

Server replies HTTP/1.1 200 OK Content-Type: text/html Speakerinnen*-Liste

Slide 13

Slide 13 text

request vs response VERB path HTTP/1.1 HTTP/1.1 code description Header: Value Header: Value ... ... Body (if there is one) Body (if there is one)

Slide 14

Slide 14 text

Resources 4 identified by host and path 4 allow multiple operations 4 can have multiple representations speakerinnen.org/de/sign_up

Slide 15

Slide 15 text

HTTP methods

Slide 16

Slide 16 text

GET 4 Request a resource in its current state. 4 The standard operation. 4 Request does not include a body. 4 Request is safe (and idempotent).

Slide 17

Slide 17 text

Safe? Idempotent? Resource state? What??

Slide 18

Slide 18 text

Resource state 4 Representations, headers and availability associated with a resource. 4 /de/sign_up exists, is accessible, and has an HTML page with a form as its representation.

Slide 19

Slide 19 text

Safe requests 4 Safe requests do not change the state of a resource. 4 HTTP client does not need to ask the user for permission to perform request.

Slide 20

Slide 20 text

Idempotent requests 4 The resource state will be the same after performing the request once or multiple times. 4 HTTP client does not need to ask the user for permission to repeat the request.

Slide 21

Slide 21 text

Non-idempotent requests 4 Can't be sure about the resource state after multiple requests. 4 HTTP client should always ask the user for confirmation.

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

GET 4 Request a resource in its current state. 4 The standard operation. 4 Request does not include a body. 4 Request is safe (and idempotent).

Slide 24

Slide 24 text

HEAD 4 Same as GET, but there wont be a response body. 4 Useful if you only care about the headers. 4 Request is safe (and idempotent).

Slide 25

Slide 25 text

POST 4 "Do something dangerous." 4 Default for requests that change something. 4 Used for creating a new speakerinnen. 4 Unsafe and non-idempotent.

Slide 26

Slide 26 text

Slide 27

Slide 27 text

POST /de/profiles HTTP/1.1 Content-Type: application/x-www-form-urlencoded Content-Length: 49 profile[email]=me@rkh.im&profile[password]=abc123

Slide 28

Slide 28 text

PUT 4 Writing a resource to a given path. 4 Often used for uploads. 4 Unsafe, but idempotent (yay).

Slide 29

Slide 29 text

Other HTTP methods 4 DELETE: Remove a resource, idempotent. 4 OPTIONS: Learn about available methods and representations for a resource, safe. 4 PATCH: Update a resource from a partial representation, non-idempotent. 4 LINK and UNLINK: Create or destroy relations between resources, idempotent.

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Response Status 4 1xx - informational 4 2xx - success 4 3xx - redirection 4 4xx - client error 4 5xx - server error

Slide 32

Slide 32 text

If you don't know status xyz, treat it like x00.

Slide 33

Slide 33 text

Examples 4 303 See Other 4 403 Forbidden 4 404 File Not Found 4 405 Method Not Allowed 4 418 I'm a Teapot 4 500 Internal Server Error

Slide 34

Slide 34 text

Headers

Slide 35

Slide 35 text

Common request headers 4 Host: Domain name in URI. 4 User-Agent: Client software used. 4 Referer[sic]: Page user was on before.

Slide 36

Slide 36 text

Safari on iPad User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/ 531.21.10 (KHTML, like Gecko) Mobile/ 7B405

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Common response headers 4 Server: Server software used. 4 Last-Modified: The last time the resource state has changed.

Slide 39

Slide 39 text

Representations

Slide 40

Slide 40 text

The "file type" of the response or request body.

Slide 41

Slide 41 text

We have seen two so far 4 text/html 4 application/x-www-form-urlencoded

Slide 42

Slide 42 text

Other examples 4 image/gif, image/png, image/jpeg 4 text/plain, text/css, text/x-script.ruby 4 application/javascript, application/ json

Slide 43

Slide 43 text

A resource can have multiple representations But we need to tell it which one we want

Slide 44

Slide 44 text

We want a PNG image GET /resource HTTP/1.1 Host: example.org Accept: image/png Server gives us PNG image HTTP/1.1 200 OK Content-Type: image/png [ png data ]

Slide 45

Slide 45 text

We want a PNG image GET /resource HTTP/1.1 Host: example.org Accept: image/png Resource doesn't have a PNG representation HTTP/1.1 406 Not Acceptable Content-Type: text/plain No PNG, sorry.

Slide 46

Slide 46 text

We want any kind of image GET /resource HTTP/1.1 Host: example.org Accept: image/* Server gives us PNG image HTTP/1.1 200 OK Content-Type: image/png [ png data ]

Slide 47

Slide 47 text

We want a PNG or GIF image (but prefer PNG) GET /resource HTTP/1.1 Host: example.org Accept: image/png; q=1.0,image/gif; q=0.5 Server gives us PNG image HTTP/1.1 200 OK Content-Type: image/png [ png data ]

Slide 48

Slide 48 text

The same works for language. GET /resource HTTP/1.1 Host: example.org Accept-Language: en, de

Slide 49

Slide 49 text

Cookies

Slide 50

Slide 50 text

HTTP is stateless. Cookies are a way to attach state.

Slide 51

Slide 51 text

HTTP/1.1 200 OK Set-Cookie: mycookie=foobar; HttpOnly Content-Type: text/plain Just gave you a cookie!

Slide 52

Slide 52 text

GET /somepage HTTP/1.1 Host: example.org Cookie: mycookie=foobar

Slide 53

Slide 53 text

get '/' do name = session[:name] "Hello #{ name }!" end

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Thank You!