Apache httpd
Introduction to
HTTP server
Úvod do problematiky
HTTP serveru

Lukáš Bařinka

© 2021

REV 2.10

TOC

Obsah

  1. X/HTML
  2. URL/URI/IRI
  3. HTTP Details
  4. Tools and Communication Testing
  5. HTTP Communication Overview
  6. LAB: HTTP Communication
  1. X/HTML
  2. URL/URI/IRI
  3. Protokol HTTP
  4. Nástroje a testování komunikace
  5. HTTP komunikace
  6. LAB: HTTP komunikace

X/HTML

HTML

HyperText Markup Language

  • Document Type Definition (DTD) Mandatory for HTML 4.01+, Directive <!DOCTYPE
  • Root element Element html represents whole document
  • Document header Contains metadata related to whole document, defines e.g. document name (title), language, encoding, keywords, description, stylesheet
  • Document body Contains (visible) document content (data)
  • Deklarace typu dokumentu (DTD) Je povinná pro 4.01+, direktiva <!DOCTYPE
  • Kořenový element Element html reprezentuje celý dokument
  • Hlavička dokumentu Obsahuje metadata, která se vztahují k celému dokumentu. Definují např. název dokumentu (title), jazyk, kódování, klíčová slova, popis, použitý styl zobrazení
  • Tělo dokumentu Obsahuje vlastní text dokumentu

HTML example

HTML ukázka


							<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
							                      "http://www.w3.org/TR/html4/loose.dtd">
							<html>
							    <head>
							        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
							        <title>HTML 4.01 Transitional</title>
							    </head>
							    <body>
							        …
							        <a href="http://www.example.com">
							            <img src="link.gif" alt="Link na www.example.com">
							        </a>
							        …
							    </body>
							</html>
						

XHTML

XHTML is XML version of HTML

XHTML je XML verze jazyka HTML

  • Should be valid XML document
  • Should be "well" displayed in web browser
  • Some web browsers could have problems to display document without DTD <!DOCTYPE at the beginning
  • Without XML declaration <?xml encoding=""?> is expected
    UTF-8/16 encoding
  • Měl by to být validní XML dokument
  • Měl by se „dobře“ zobrazit ve webovém prohlížeči
  • Některé webové prohlížeče mají problém se zobrazením, pokud není uvedena deklarace <!DOCTYPE jako první
  • Bez deklarace <?xml encoding=""?> se předpokládá
    UTF-8/16 kódování

							<?xml version="1.0" encoding="iso-8859-2"?>
							<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
							  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
							<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
						

URL/URI/IRI

URL, URI, IRI

  • URL = Uniform Resource Locator
    • Sequence of characters with defined structure
    • Specifies a location of source on the Internet
    • Subset of URI
  • URI = Uniform Resource Identifier
    • Names resource on the Internet
    • Syntax is schema-specific
  • IRI = Internationalized Resource Identifier
    • Generalization of URI
    • Allow to use Unicode instead of ASCII (resp. Latin1)
  • URL = Uniform Resource Locator
    • Řetězec znaků s definovanou strukturou
    • Specifikující umístění zdrojů na Internetu
    • Je podmnožinou URI
  • URI = Uniform Resource Identifier
    • Pojmenovává zdroje na Internetu
    • Syntaxe je specifická podle zvoleného schématu
  • IRI = Internationalized Resource Identifier
    • Zobecněním URI
    • Umožňuje využívat Unicode místo ASCII (resp. Latin1)
URI structure

URL

Simplified structure
generic-url ::= scheme:scheme-specific
scheme ::= http | ftp ...
scheme-specific ::= common-inet | ...
common-inet ::= //user:password@host:port/url-path
Examples
http://www.example.org
http://www.example.org:8080/path/filename
http://www.example.org/path/…
…/filename?parameter1=value1&parameter2=value2
ftp://anonymous:user%40example.org@ftp.example.org:21/…
…/path/filename;type=<typecode>
mailto:user@example.org
Zjednodušená struktura
generic-url ::= scheme:scheme-specific
scheme ::= http | ftp ...
scheme-specific ::= common-inet | ...
common-inet ::= //user:password@host:port/url-path
Příklady
http://www.example.org
http://www.example.org:8080/cesta/nazev
http://www.example.org/cesta/…
…/nazev?parametr1=hodnota1&parametr2=hodnota2
ftp://anonymous:user%40example.org@ftp.example.org:21/…
…/cesta/nazev;type=<typecode>
mailto:user@example.org

Character sets in URL

Znaky v URL

  • URL in HTTP scheme can contain #fragment
    • Is NOT part ofURL according to RFC
    • Parser should be able to recognize it (and ignore)
  • URL can safely contain (without encoding) only
    • Alphanumeric characters 0-9a-zA-Z
    • Special characters $-_.+!*'()
    • Other characters have to be URL encoded using %xx xx character code is according to ISO 8859-1
  • Pro schéma HTTP může URL obsahovat #fragment
    • Není součástí URL podle RFC
    • Parser by měl být schopen jej rozpoznat (a ignorovat)
  • V URL lze bezpečně (bez zakódování) používat pouze
    • Alfanumerické znaky 0-9a-zA-Z
    • Speciální znaky $-_.+!*'()
    • Ostatní musí být kódovány pomocí %xx kód je podle ISO 8859-1
  • Unsafe characters: <>"#%{}[]|\^~`
    • Can be considered as special characters during data transfer
    • Can be changed ⇒ Should be encoded
  • Reserved characters: ;/?:@=&
    • Have special meaning in URL
    • Have to be encoded for another purpose
  • Nebezpečné (unsafe) znaky: <>"#%{}[]|\^~`
    • Mohou být považovány za speciální při přenosu
    • Mohou být změněny, proto by měly být kódovány
  • Vyhrazené (reserved) znaky: ;/?:@=&
    • Speciální význam v URL
    • Pro jiné použití musí být kódovány

Character Encoding Example

Ukázka kódování znaků

CharCode
ZnakKód
&%2B
:%3A
=%3D
@%40
CharCode
ZnakKód
/%2F
;%3B
?%3F
%20

HTTP

HTTP/1.0 Basic features

Základní vlastnosti HTTP/1.0

HyperText Transfer Protocol

  • Text-based protocol (raw data)
  • Stateless protocol
  • Communication scheme: request – response
  • Application layer protocol (ISO OSI)
  • Based on TCP/IP protocols (default ports 80/443)
  • Textový protokol (raw data)
  • Bezestavový protokol
  • Fungování způsobem: požadavek – odpověď
  • Protokol aplikační vrstvy (ISO OSI)
  • Nad TCP/IP protokoly (obvykle port 80/443)

HTTP versions

Verze HTTP

HTTPS = encrypted version used for secure communication HTTP + SSL/TLS

HTTPS = šifrovaná verze protokolu HTTP + SSL/TLS

HTTP/1.1 Features

Vlastnosti HTTP/1.1

  • Same features as in HTTP/1.0
  • Exended version
    • Host: header in every request Ability to use virtual "servers" – virtual hosts
    • Ability to send response in several parts (chunks) Chunked encoding
    • Persistent connection support Optional header Connection: close to terminate connection
    • Proper handling of request Expect: 100-continue and response 100 Continue Usually ignored
    • Improved support of Cache-Control
  • Vlastnosti z HTTP/1.0 zůstávají
  • Rozšíření oproti HTTP/1.0
    • Hlavička Host: u každého požadavku Možnost používat virtuální „servery“ – virtual hosts
    • Možnost poslat odpověď ve více částech Chunked encoding
    • Podpora trvalého (persistent) spojení se serverem Případně hlavička Connection: close při ukončení spojení
    • Správné zpracování požadavku Expect: 100-continue a odpovědi 100 Continue Obvykle se ignoruje
    • Vylepšená podpora Cache-Control

HTTP Terminology

Pojmy HTTP

Message
The basic unit of HTTP communication, consisting of a structured sequence of octets transmitted via the connection
Request
A message from a client to a server
Response
A message from a server to a client after receiving and interpreting a request message
Zpráva (message)
Základní jednotka HTTP komunikace skládající se ze strukturované sekvence bytů (oktetů) a přenášená spojením
Požadavek (request)
HTTP zpráva od klienta serveru
Odpověď (response)
HTTP zpráva od serveru klientovi po obdržení a zpracování požadavku
Resource
A network data object or service that can be identified by a URI
Resources may be available in multiple representations E.g. multiple languages, data formats, size, and resolutions
Client
A program that establishes connections for the purpose of sending requests
User agent
The client which initiates a request Often browsers, editors, spiders (web-traversing robots), or other end user tools.
Zdroj (resource)
Datový o objekt nebo služba na síti, která může být identifikována pomocí URI
Zdroje mohou být k dispozici ve více reprezentacích Např. jazykové varianty, datové formáty
Klient (client)
Program, který navazuje spojení za účelem odeslání požadavku.
Uživatelský agent (user agent)
Klient, který inicioval požadavek Typicky jimi jsou prohlíže, editory, roboti atd.
Server
An application program that accepts connections in order to service requests by sending back responses
Any given program may be capable of being both a client and a server This term refers only to the role being performed by the program for a particular connection
Origin server
The server on which a given resource resides or is to be created
Server
Aplikace, která přijímá spojení a obsluhuje přes něj přijaté požadavky a odesílá zpět odpovědi
Program může být schopen chovat se jako klient i server Proto je toto označení vždy vztaženo ke konkrétnímu spojení
Origin server
Server, na kterém se zdroj nachází nebo bude vytvořen

Tools and Communication Testing

Nástroje a testování komunikace

General network tools

Obecné síťové nástroje

  • ping Discover IP address, connection test (ICMP) Zjištění IP adresy, kontrola spojení (ICMP)
  • nslookup, dig Domain information (from DNS) Informace o doméně (z DNS)
  • telnet Interactive network communication Interaktivní síťová komunikace
  • nc net-cat TCP+UDP communication (easy for scripting) net-cat TCP+UDP komunikace (dobře se skriptuje)
  • ping Discover IP address, connection test (ICMP) Zjištění IP adresy, kontrola spojení (ICMP)
    
    									ping www.google.com
    									PING www.google.com (172.217.23.196) 56(84) bytes of data.
    									64 bytes from prg03s05-in-f196.1e100.net (172.217.23.196): icmp_seq=1 ttl=115 time=6.90 ms
    									64 bytes from prg03s05-in-f4.1e100.net (172.217.23.196): icmp_seq=2 ttl=115 time=2.71 ms
    									64 bytes from prg03s05-in-f196.1e100.net (172.217.23.196): icmp_seq=3 ttl=115 time=3.05 ms
    								
  • nslookup Domain information (from DNS) Informace o doméně (z DNS)
    
    									nslookup www.google.com
    									Server:		192.168.0.1
    									Address:	192.168.0.1#53
    
    									Non-authoritative answer:
    									Name:	www.google.com
    									Address: 172.217.23.196
    									Name:	www.google.com
    									Address: 2a00:1450:4014:80c::2004
    								
  • dig Domain information (from DNS) Informace o doméně (z DNS)
    
    									dig www.google.com
    
    									; <<>> DiG 9.16.13-Debian <<>> www.google.com
    									;; global options: +cmd
    									;; Got answer:
    									;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46672
    									;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 8
    
    									;; OPT PSEUDOSECTION:
    									; EDNS: version: 0, flags:; udp: 4096
    									;; QUESTION SECTION:
    									;www.google.com.			IN	A
    
    									;; ANSWER SECTION:
    									www.google.com.		260	IN	A	172.217.23.196
    
    									;; AUTHORITY SECTION:
    									google.com.		313231	IN	NS	ns3.google.com.
    									google.com.		313231	IN	NS	ns2.google.com.
    									google.com.		313231	IN	NS	ns1.google.com.
    									google.com.		313231	IN	NS	ns4.google.com.
    
    									;; ADDITIONAL SECTION:
    									ns2.google.com.		144507	IN	A	216.239.34.10
    									ns2.google.com.		203036	IN	AAAA	2001:4860:4802:34::a
    									ns3.google.com.		144507	IN	A	216.239.36.10
    									ns3.google.com.		174240	IN	AAAA	2001:4860:4802:36::a
    									ns1.google.com.		144507	IN	A	216.239.32.10
    									ns4.google.com.		144507	IN	A	216.239.38.10
    									ns4.google.com.		174240	IN	AAAA	2001:4860:4802:38::a
    
    									;; Query time: 108 msec
    									;; SERVER: 192.168.0.1#53(192.168.0.1)
    									;; WHEN: Ne dub 04 20:05:37 CEST 2021
    									;; MSG SIZE  rcvd: 279
    								
  • telnet Interactive network communication
    Sends CR+LF as a newline terminator by default
    Interaktivní síťová komunikace
    Odesílá CR+LF jako konce řádků automaticky
    
    									telnet www.google.com 80
    									Trying 172.217.23.196...
    									Connected to www.google.com.
    									Escape character is '^]'.
    									GET / HTTP/1.0↵
    									↵
    									HTTP/1.0 200 OK
    									Date: Sun, 04 Apr 2021 18:12:56 GMT
    									Expires: -1
    									Cache-Control: private, max-age=0
    									Content-Type: text/html; charset=ISO-8859-1
    									P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
    									Server: gws
    									X-XSS-Protection: 0
    									X-Frame-Options: SAMEORIGIN
    									Set-Cookie: NID=212=Tuyy…; expires=Mon, …; path=/; domain=.google.com; HttpOnly
    									Accept-Ranges: none
    									Vary: Accept-Encoding
    
    									<!doctype html><html …
    								
  • nc net-cat TCP+UDP communication (easy for scripting)
    Sends LF as a line terminator by default
    Use -C to use CR+LF as a newline
    net-cat TCP+UDP komunikace (dobře se skriptuje)
    Odesílá LF jako konec řádku
    Při použití -C odesílá jako konce řádků CR+LF
    
    									nc -C www.google.com 80
    									GET / HTTP/1.0↵
    									↵
    									HTTP/1.0 200 OK
    									…
    								
    
    									printf '%s\r\n' "GET / HTTP/1.0" "" | nc www.google.com 80
    									HTTP/1.0 200 OK
    									…
    								
    
    									nc www.google.com 80 <<REQ
    									> GET / HTTP/1.0^M
    									> ^M
    									> REQ
    									HTTP/1.0 200 OK
    									…
    								

    Press CTRL+V followed by Enter to input CR character (^M)

    Stiskněte CTRL+V a Enter pro vložení znaku CR (^M)

Tools for HTTP communication

Nástroje pro HTTP komunikaci

  • curl Tool for URL manipulation (no redirect by default, -L) Nástroj pro práci s URL (standardně neřeší přesměrování, -L)
  • wget Download data from web server (redirect by default) Stahování dat z webu (řeší přesměrování)
  • ab Apache benchmark - performance statistics Apache benchmark - statistiky výkonu
  • web-browser User testing (+ lighthouse) Beware of cache! Uživatelské testování (+ lighthouse) Pozor na cache!

HTTP communication

HTTP komunikace

Client — Server

Klient — Server

Network client-server communication scheme / Schéma síťové komunikace klinet-server

HTTP messages

HTTP zprávy

  • Request simplified structure Zjednodušená struktura požadavku
    Request ::= Request-Line CRLF [Header CRLF]* CRLF [body] Request-Line ::= Method Request-Uri HTTP-Version CRLF ::= CR LF Header ::= Host | ... Method ::= GET | HEAD | ...
  • Response simplified structure Zjednodušená struktura odpovědi
    Response ::= Status-Line CRLF [Header CRLF]* CRLF [body] Status-Line ::= HTTP-Version Status-Code Reason-Phrase

Server must implement GET and HEAD methods, other methods are optional.

Server musí podporovat metody GET a HEAD, ostatní metody volitelně.

Status codes

Stavové kódy

  • Indicates response result
    • Status-Code 3-digit integer for automata
    • Reason-Phrase Short textual description for the human user
  • First digit indicates status class
    • 1xx - Informational Request received, processing continues
    • 2xx - Success Request was successfully received, understood, and accepted
    • 3xx - Redirection Further action needs to be taken by the user agent in order to fulfill the request
    • 4xx - Client Error Client seems to have erred (incorrect syntax, permissions, …)
    • 5xx - Server Error Server is aware that it has erred or is incapable of performing the request
  • Určují výsledek požadavku
    • Číselně Pro automatické zpracování
    • Textově Pro snazší porozumění člověkem
  • První číslice kódu určuje třídu
    • 1xx - Informational (informace) Požadavek přijat, zpracování pokračuje
    • 2xx - Success (úspěch) Požadavek přijat a plně zpracován
    • 3xx - Redirection (přesměrování) Další akce musí být provedeny pro dokončení požadavku
    • 4xx - Client Error (chyba klienta) Požadavek nemůže být zpracován (špatná syntaxe, …)
    • 5xx - Server Error (chyba serveru) Serveru se nepodařilo zpracovat validní požadavek

Frequent status codes

Časté stavové kódy

  • 100 – Continue Client should continue with request Zpracování pokračuje
  • 200 – OK
  • 300 – Multiple Choices User can select a preferred representation and redirect its request E.g. in content negotiation Výběr z možností Např. při dojednávání obsahu
  • 301 – Moved Permanently Redirection is cacheable unless indicated otherwise Trvale přesunuto
  • 400 – Bad Request Malformed syntax Neplatný požadavek
  • 401 – Unauthorized Request requires user authentication Vyžadována autentizace
  • 403 – Forbidden Server understood the request, but is refusing to fulfill it Přístup zakázán
  • 404 – Not Found Server has not found anything matching the Request-URI Zdroj nenalezen
  • 500 – Internal Server Error The server encountered an unexpected condition Vnitřní chyba serveru

Unrecognized status code must be interpreted as x00

Pokud klient některému kódu nerozumí,
považuje jej za kód x00

HTTP request example

Ukázka HTTP požadavku

  • nc
    
    									nc -C www.google.com 80
    									GET / HTTP/1.0↵
    									↵
    								
  • telnet
    
    								telnet www.google.com 80
    								Trying 172.217.23.196...
    								Connected to www.google.com.
    								Escape character is '^]'.
    								GET / HTTP/1.0↵
    								↵
    								

HTTP response example

Ukázka HTTP odpovědi


							HTTP/1.0 200 OK
							Date: Mon, 05 Apr 2021 07:35:04 GMT
							Expires: -1
							Cache-Control: private, max-age=0
							Content-Type: text/html; charset=ISO-8859-1
							P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
							Server: gws
							X-XSS-Protection: 0
							X-Frame-Options: SAMEORIGIN
							Set-Cookie: NID=212=4LgD…; expires=Tue, … GMT; path=/; domain=.google.com; HttpOnly
							Accept-Ranges: none
							Vary: Accept-Encoding

							<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="cs"><head>
								<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
						

The charset is specified in HTTP header (ISO-8859-1 [ISO Latin 1]) and in HTML header (UTF-8) as well. The HTTP header information takes precendece over content information.

V HTTP hlavičce je uvedeno kódování ISO-8859-1 (ISO Latin 1) a v HTML hlavičce pak UTF-8. Platí informace z HTTP, skutečně je kódování ISO Latin 1.

HTTP virtualisation

HTTP virtualizace

  • Virtual hosting (VH) allows to host multiple domains on single server (e.g. webserver)
  • IP-Based virtual hosting
    • Every domain uses one unshared IP address/port
    • Can be used with HTTP/1.0 or HTTP/1.1
  • Name-Based virtual hosting
    • Multiple domains can share one IP address/port
    • Used protocols must implement methods to distinguish individual domain
    • Can be used with HTTP/1.1+
  • It is possible to combine IP-Based and Name-based VH
  • Virtual hosting (VH) je technika umožňující na jednom serveru (např. webserveru) hostit více domén
  • IP-based virtual hosting
    • Pro každou hostovanou doménu je vyhrazena jedna nesdílená IP adresa/port
    • Lze použít s HTTP/1.0 i HTTP/1.1
  • Name-based virtual hosting
    • Na jedné IP adrese/portu je provozováno více domén
    • Používané protokoly pak musejí implementovat mechanismus umožňující rozlišení domén
    • Lze použít pouze s HTTP/1.1+
  • Obě techniky lze kombinovat

Host determination

Určení hostitele

  • HTTP/1.1
    1. If the URI is an absolute, the host is is part of the URI and Host header must be ignored
    2. If the URI is not an absolute, and the request includes a Host header field, the host is determined by the Host header
    3. Else the response must be a 400 Bad Request error message
  • HTTP/1.0
    1. If request lacks a Host header field, the server may attempt to use heuristics E.g., examination of the URI path for something unique to a particular host
  • Request to proxy server must be absolute URI
  • Path in URI must not to be empty ⇒ must be at least /
  • HTTP/1.1
    1. Pokud server dostane absolutní URI, musí hlavičku Host ignorovat a použít název hostitele z URI
    2. Pokud dostane relativní URI a požadavek obsahuje hlavičku Host, použije název hostitele z hlavičky
    3. Jinak odpovídá: 400 Bad Request
  • HTTP/1.0
    1. Pokud požadavek neobsahuje hlavičku Host, může se pokusit určit hostitele jinak
  • Požadavek na proxy ⇒ absolutní URI
  • Absolutní cesta v URI nesmí být prázdná ⇒ alespoň /

Host header examples

Ukázky hlavičky Host

  • HTTP/1.0 It does not work (but it could work) Nefunguje (ale mohlo by)
    
    									nc -C www.sony.com 80 <<REQ
    									GET / HTTP/1.0
    									User-Agent: netcat
    
    									REQ
    								
    
    									HTTP/1.0 400 Bad Request
    									Server: AkamaiGHost
    									Mime-Version: 1.0
    									Content-Type: text/html
    									Content-Length: 209
    									Expires: Mon, 05 Apr 2021 08:40:16 GMT
    									Date: Mon, 05 Apr 2021 08:40:16 GMT
    									Connection: close
    
    									<HTML><HEAD>
    									<TITLE>Invalid URL</TITLE>…
    								
  • HTTP/1.0 with absolute URL It works (as expected) HTTP/1.0 s absolutní URL Funguje
    
    									nc -C www.sony.com 80 <<REQ
    									GET http://www.sony.com/ HTTP/1.0
    									User-Agent: netcat
    
    									REQ
    								
    
    									HTTP/1.0 302 Moved Temporarily
    									Server: AkamaiGHost
    									Content-Length: 0
    									Location: http://www.sony.com/en/
    									…
    								
  • HTTP/1.0 with Host: header It works (Host here is optional but thanks to that it works) HTTP/1.0 s hlavičkou Host: Funguje (Host hlavička je nepovinná, ale díky ní to funguje)
    
    									nc -C www.sony.com 80 <<REQ
    									GET / HTTP/1.0
    									Host: www.sony.com
    									User-Agent: netcat
    
    									REQ
    								
    
    									HTTP/1.0 302 Moved Temporarily
    									Server: AkamaiGHost
    									Content-Length: 0
    									Location: http://www.sony.com/en/
    									…
    								
  • HTTP/1.1 with Host: header It works according to this mandatory Host header HTTP/1.1 s hlavičkou Host: Funguje podle adresy
    
    									nc -C www.sony.com 80 <<REQ
    									GET / HTTP/1.1
    									Host: www.sony.cz
    									User-Agent: netcat
    
    									REQ
    								
    
    									HTTP/1.1 301 Moved Permanently
    									Server: AkamaiGHost
    									Content-Length: 0
    									Location: https://www.sony.cz/
    									…
    								

Request redirection

Přesměrování požadavku

http://www.sony.com (HTTP/1.1) → http://www.sony.com/en/ (HTTP/1.1) → https://www.sony.com/en/ (HTTP/2)


							curl -sL -i http://www.sony.com
						

							HTTP/1.1 302 Moved Temporarily
							Server: AkamaiGHost
							Content-Length: 0
							Location: http://www.sony.com/en/
							…

							HTTP/1.1 301 Moved Permanently
							Server: AkamaiGHost
							Content-Length: 0
							Location: https://www.sony.com/en/
							…

							HTTP/2 200
							accept-ranges: bytes
							content-type: text/html
							last-modified: Wed, 31 Mar 2021 15:51:55 GMT
							server: Apache
							content-length: 121443
							…

							<!DOCTYPE html>
							<html lang="en">
							…
						

-i = include headers
-s = silent (no progress bar)
-L = location redirect

(Open) proxy server example

Použití (open) proxy serveru


							nc 51.75.147.44 3128 <<REQ
							GET http://www.google.com/ HTTP/1.0

							REQ
						

							HTTP/1.1 200 OK
							Server: nginx/1.16.1
							Date: Mon, 05 Apr 2021 09:51:55 GMT
							Content-Type: text/html; charset=ISO-8859-1
							Connection: close
							Expires: -1
							Cache-Control: private, max-age=0
							P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
							X-XSS-Protection: 0
							X-Frame-Options: SAMEORIGIN
							Set-Cookie: NID=212=NhTY…; path=/; domain=.google.com; HttpOnly
							Accept-Ranges: none
							Vary: Accept-Encoding

							<!doctype html><html … lang="de"><head>
						

http://www.xroxy.com/proxylist.htm

HTTP authentication

HTTP autentizace

  • Client must add Proxy-Authorization header with login credentials when proxy server requires authentication
  • The login credentials format: Basic base64(user:pass)
    
    									echo -n 'lukas:secretpassword' | base64
    									bHVrYXM6c2VjcmV0cGFzc3dvcmQ=
    								
    
    									Proxy-Autorization: Basic bHVrYXM6c2VjcmV0cGFzc3dvcmQ=
    								
    
    									base64 -d <<< 'bHVrYXM6c2VjcmV0cGFzc3dvcmQ='
    									lukas:secretpassword
    								
  • The HTTP Basic authentication works exactly the same way
  • Při požadavku přes proxy server s autentizací, je potřeba do hlavičky požadavku přidat položku Proxy-Authorization s přihlašovacími údaji
  • Formát přihlašovacích údajů: Basic base64(user:pass)
    
    									echo -n 'lukas:tajneheslo' | base64
    									bHVrYXM6dGFqbmVoZXNsbw==
    								
    
    									Proxy-Autorization: Basic bHVrYXM6dGFqbmVoZXNsbw==
    								
    
    									base64 -d <<< 'bHVrYXM6dGFqbmVoZXNsbw=='
    									lukas:tajneheslo
    								
  • Stejně funguje také HTTP Basic autentizace

Data transfer

Přenos dat

  • Client-Server connection can be
    • Separate
    • Persistent
  • Transmitted data can be included in single response or divided into series of parts (chunks):
    • For dynamically generated content The content size is not known yet
    • For headers generated later
  • Chunked encoding (only HTTP/1.1)
    • Head must contain Transfer-Encoding: chunked
    • Body contains sequence of chunks
    • Every chunk starts with its content length in bytes (hex)
  • Spojení klienta se serverem může být
    • Separátní
    • Perzistentní
  • Data mohou být obsažena v jedné odpovědi, nebo rozdělena do několika odpovědí (chunked):
    • Generování obsahu Ještě není známa velikost
    • Pro hlavičky, které budou generovány později
  • Chunked encoding
    • Hlavička musí obsahovat Transfer-Encoding: chunked
    • Tělo obsahuje jednotlivé bloky
    • Každý blok začíná svoji délkou zapsanou hexadecimálně

Chunked encoding

  • Syntax Syntaxe
    Chunked-Body ::= chunk* last-chunk trailer CRLF chunk ::= chunk-size CRLF chunk-data CRLF last-chunk ::= "0" CRLF trailer ::= (entity-header CRLF)*
  • Every HTTP/1.1 application must be able to receive and decode chunks encoding Proxy server can join data chunks into single content
  • Každá HTTP/1.1 aplikace musí umět přijmout a dekódovat chunks encoding Proxy server může spojit jednotlivé části do jedné

Chunked encoding response example

Ukázka chunked encoding odpovědi


						nc www.google.com 80 <<REQ
						> GET / HTTP/1.1
						> Host: www.google.com
						> Connection: Close
						>
						> REQ
						

							HTTP/1.1 200 OK
							Date: Mon, 05 Apr 2021 11:30:32 GMT
							Expires: -1
							Cache-Control: private, max-age=0
							Content-Type: text/html; charset=ISO-8859-1
							P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
							Server: gws
							X-XSS-Protection: 0
							X-Frame-Options: SAMEORIGIN
							Set-Cookie: NID=212=Fr7p …; path=/; domain=.google.com; HttpOnly
							Accept-Ranges: none
							Vary: Accept-Encoding
							Connection: close
							Transfer-Encoding: chunked

							51ef
							<!doctype html><html … }},ea=da(1
							f9
							),fa=da(2);p("sb",ea);p … ;ia=c
							6e70
							+1},ja=[],ia=0;p("logger",{ … ;})();</script> </body></html>
							0
						

HTTP methods

HTTP metody

  • GET* Safe = not changing server state
    • Retrieves whatever information represented by URI The user did not request the side-effects
    • If the Request-URI refers to a data-producing process Only produced data, not the source code of process
    • Request can be conditional (cache, e.g. If-Modified-Since) or partial (byte range, Range)
  • HEAD Safe
    • Identical to GET except … Identical response headers
    • Server must not return a message-body in the response Serves mainly for testing purpose
  • GET* Bezpečná = nemění stav serveru (Uživatel nepožaduje žádné vedlejší efekty požadavku)
    • Požadavek o zdroj reprezentován URI Bez vedlejších efektů
    • V případě žádosti o zpracování Pouze výsledek, nikoli zdrojový kód
    • Požadavek může být podmíněný (cache) nebo částečný (partial)
  • HEAD Bezpečná
    • Identická metoda jako GET Server odesílá stejné informace
    • Odpověď bez těla Slouží především pro testování

* part of REST (Representational State Transfer)

* součást REST (Representational State Transfer)

  • POST*
    • Provides (sends) a block of data to server (in message body) to be processed
  • PUT* Idempotent = repetition does not change result The side-effects of N > 0 identical requests are the same as for a single request
    • Changes or creates new resource on the origin server
  • DELETE* Idempotent
    • Deletes server resource
  • POST*
    • Odeslání informací (v těle požadavku) na server ke zpracování
  • PUT* Idempotentní = opakování nezmění stav výsledku
    • Mění nebo nahrává data zdroje na server
  • DELETE* Idempotentní
    • Maže zdroj na serveru
  • OPTIONS* Safe
    • Requests for information about the communication options available (for URI or server)
    • If the URI is an asterisk ("*"), the request is intended to apply to the server in general (ping)
  • TRACE Safe
    • Invokes application-layer loop-back of the request message (echo)Information about changes during data transfer for testing or diagnostic
  • CONNECT
    • Used for proxy that can dynamically switch to being a tunnelE.g. Transparent TCP/IP SSL tunnel
  • OPTIONS* Bezpečná
    • Požadavek na informace o možnostech zdroje (URI) nebo serveru
    • Pokud je zaslán požadavek na *, jedná se o spíš o ping, než žádost o informace
  • TRACE Bezpečná
    • Kopie požadavku (echo)Informace o změnách provedených na serverech vedoucích k cíli
  • CONNECT
    • Překládá požadavek na spojení na transparentní TCP/IP tunel Obvykle při SSL komunikaci přes proxy server

Configuration notes

Poznámky ke konfiguraci

  • Directive AllowMethods (mod_allowmethods) Except TRACE method
    
    									<Location />
    									  AllowMethods GET POST OPTIONS
    									</Location>
    								
  • Directive TraceEnable (core)
    
    									TraceEnable on
    								
  • Direktiva AllowMethods (mod_allowmethods) Kromě metody TRACE
    
    									<Location />
    									  AllowMethods GET POST OPTIONS
    									</Location>
    								
  • Direktiva TraceEnable (core)
    
    									TraceEnable on
    								

Options and Trace methods examples

Ukázka metod Options a Trace


							OPTIONS / HTTP/1.0
							 
						

							HTTP/1.1 200 OK
							Date: Tue, 25 Oct 2011 15:01:38 GMT
							Server: Apache
							Allow: GET,HEAD,POST,OPTIONS
							Content-Length: 0
							Connection: close
							Content-Type: text/html
						

							TRACE / HTTP/1.0
							 
						

							HTTP/1.1 200 OK
							Date: Tue, 25 Oct 2011 15:28:09 GMT
							Server: Apache
							Connection: close
							Content-Type: message/http

							TRACE / HTTP/1.0
						

LAB: HTTP communication

LAB: HTTP komunikace

HTTP connection and messages

HTTP spojení a zprávy

The goal is to get familiar with basic network tools and tools to communicate using HTTP. It is good to use different tools; ones from low-level up to hi-level (web browser). You will use those tool during this course as well as in real life.

Cílem je vyzkoušet základní síťové nástroje a nástroje pro HTTP komunikaci. Je dobré se postupně seznámit s různými nástroji, od nejnižší úrovně až po webový prohlížeč. Tyto nástroje se vám budou hodit v celém kurzu i v praxi.

  • Start testing webserver
  • Discover if the webserver is running
  • Retrieve resources from webserver using various tools
  • Stop testing webserver
  • Nastartovat testovací instanci webserveru
  • Zjistit, zda webserver běží
  • Získat zdroje z webserveru pomocí různých nástrojů
  • Ukončit testovací instanci webserveru

Tips

Tipy

  • To start/stop service you can use commands
    
    									systemctl start service
    									systemctl stop service
    								
  • The service for testing webserver instance is apache2-test
  • List of open files [lsof] (TCP ports) you can get using
    
    									lsof -i tcp
    								
    Port 80 = http, Port 8080 = http-alt
  • Pro start/stop služby je možné použít příkaz
    
    									systemctl start service
    									systemctl stop service
    								
  • Testovací webserver poskytuje služba (service) apache2-test
  • Seznam poslouchajících TCP portů lze vypsat příkazem
    
    									lsof -i tcp
    								
    Port 80 = http, Port 8080 = http-alt

Start of Testing webserver

Start testovacího webserveru

  1. Discover if the testing webserver is running and listening on 8080 TCP port
  2. Start testing webserver
  3. Discover if the testing webserver is running and listening on 8080 TCP port
  1. Zjistěte, zda testovací instance webserveru poslouchá na TCP portu 8080
  2. Nastartujte testovací instanci webserveru
  3. Zjistěte, zda testovací instance webserveru poslouchá na portu 8080

Various net/HTTP tools

Různé nástroje pro síť/HTTP

  1. Display web page http://localhost:8080/ using web browser (e.g. firefox)
    • See request/response headers and status codes
    • See status codes during refresh/reload page CTRL+R, CTRL+SHIFT+R
    • Try to try to omit trailing / from URL
  2. Display the same page and headers using wget
  3. Display the same page and headers using curl
  4. Display the same page and headers using nc
  5. Display the same page and headers using telnet
  1. Zobrazte webovou stránku na adrese http://localhost:8080/ pomocí webového prohlížeče (např. firefox)
  2. Zobrazte tutéž stránku a hlavičky pomocí wget
  3. Zobrazte tutéž stránku a hlavičky pomocí curl
  4. Zobrazte tutéž stránku a hlavičky pomocí nc
  5. Zobrazte tutéž stránku a hlavičky pomocí telnet

Redirection and chunk encoding

Přesměrování a chunk encoding

  1. Display response headers to request for http://localhost:8080/redir using firefox, wget, and curl tools
  2. Display communication during request for http://localhost:8080/chunk using curl, nc, and telnet tools Warning! Chunk encoding is supported in HTTP/1.1 only
  3. Get just part of resource (bytes from 79 to 99) of resource http://localhost:8080 using curl and nc tools Header Range: bytes=79-99
  1. Zobrazte hlavičky odpovědí při žádosti o http://localhost:8080/redir pomocí nástrojů firefox, wget, curl
  2. Zobrazte záznam komunikace při dotazu na http://localhost:8080/chunk pomocí nástrojů curl, nc a telnet Pozor, chunk encoding je podporován pouze v HTTP/1.1
  3. Požádejte pouze o část (bajty 79 až 99) zdroje http://localhost:8080 pomocí nástrojů curl a nc Hlavička Range: bytes=79-99

HTTP methods

Metody HTTP

  1. Send data to server using POST method to /echo URI Data "a=10" and "b=text" as URI parameters Text "hello" as request body (5 bytes)
  2. Upload /etc/issue file using PUT method with /upload/issue address
    
    									curl -X PUT --data-binary @/etc/issue …
    								
  3. Discover uploaded file size from response headers without downloading it
  4. Download that file from http://localhost:8080/upload/issue
  1. Pošlete data na server pomocí metody POST na /echo URI Data "a=10" a "b=text" jako parametry v URI Text "hello" v těle požadavku (5 B)
  2. Pomocí metody PUT nahrajte na server soubor /etc/issue pod adresou /upload/issue
    
    									curl -X PUT --data-binary @/etc/issue …
    								
  3. Zjistěte velikost nahraného souboru z hlaviček odpovědi, aniž byste jej stahovali
  4. Stáhněte nahraný soubor z adresy http://localhost:8080/upload/issue
  1. Delete uploaded file from http://localhost:8080/upload/issue
  2. Try to delete that file once more and see response status code
  3. Send arbitrary request to server using TRACE method and see full response
  1. Smažte nahraný soubor z adresy http://localhost:8080/upload/issue
  2. Pokuste se jej smazat ještě jednou a podívejte se na stavový kód odpovědi
  3. Pomocí metody TRACE pošlete libovolný požadavek na server podívejte se na odpověď