Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Build your own webserver with PHP
Search
Igor Wiedler
November 20, 2013
Programming
1.3k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Build your own webserver with PHP
Igor Wiedler
November 20, 2013
More Decks by Igor Wiedler
See All by Igor Wiedler
Redis Bedtime Stories
igorw
1
350
Wide Event Analytics (LISA19)
igorw
4
940
a day in the life of a request
igorw
0
170
production: an owner's manual
igorw
0
190
The Power of 2
igorw
0
340
LISP 1.5 Programmer's Manual: A Dramatic Reading
igorw
0
480
The Moral Character of Software
igorw
1
310
interdisciplinary computing (domcode)
igorw
0
320
miniKanren (clojure berlin)
igorw
1
330
Other Decks in Programming
See All in Programming
dRuby over BLE
makicamel
2
330
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.5k
A2UI という光を覗いてみる
satohjohn
1
130
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
110
AutonomyとControlのあいだ:Graflowで記述するAIエージェント協調
myui
0
120
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
230
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
110
OSもどきOS
arkw
0
560
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
230
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
2
620
RTSPクライアントを自作してみた話
simotin13
0
600
Agentic UI
manfredsteyer
PRO
0
150
Featured
See All Featured
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
470
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
The Curse of the Amulet
leimatthew05
1
13k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
230
Context Engineering - Making Every Token Count
addyosmani
9
960
Abbi's Birthday
coloredviolet
2
8k
GitHub's CSS Performance
jonrohan
1033
470k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
55k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.9k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
62
44k
Code Review Best Practice
trishagee
74
20k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
1.7k
Transcript
Build your own web server with
@igorwesome
Event-driven, non-blocking I/O with PHP.
None
When you open your web browser… ! What actually happens?
None
http://igor.io/
DNS Where is igor.io?
DNS 176.58.127.253
176.58.127.253
2^16! 65535! TCP ports
TCP port 80
SYN
SYN/ACK
ACK
None
GET / HTTP/1.1! Host: igor.io
HTTP/1.1 200 OK! Content-Type: text/html! ! …
HTTP TCP IP
Layers +---+--------------+----------------+ | 7 | Application | HTTP, FTP, DNS
| | 6 | Presentation | TLS | | 5 | Session | TCP | | 4 | Transport | TCP, UDP | | 3 | Network | IP | | 2 | Data Link | ARP, PPP | | 1 | Physical | | +---+--------------+----------------+
http://igor.io/
http://igor.io:80/
http://igor.io:80/ } DNS (layer 7)! resolves to! IP (layer 3)
http://igor.io:80/ } TCP (layer 4)
http://igor.io:80/ } TLS (layer 6)! HTTP (layer 7) } HTTP
(layer 7)
The Socket API
________ < socket > -------- \ ^__^ \ (oo)\_______ (__)\
)\/\ ||----w | || ||
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> socket() -------- bind() listen()
accept() -------- connect()
Syscalls Process socket! bind! listen! accept! connect! ! read! write!
close! !
Stream Sockets
I will show you Les Codes 00
HTTP: The Protocol
Request GET / HTTP/1.1 <--- request line Host: igor.io <---
header <--- line feed <--- body (blank)
Response HTTP/1.1 200 OK <--- status line Content-Length: 2 <---
headers <--- line feed Hi <--- body
Verbs •GET •POST
Verbs •GET •POST •PUT •DELETE
Verbs •GET •POST •PUT •DELETE ! •HEAD •OPTIONS •TRACE •CONNECT
• 1xx - Special protocol stuff • 2xx - All
good • 3xx - Redirection • 4xx - You messed up • 5xx - I messed up Status codes
Keep-Alive
Streaming • Transfer-Encoding: chunked • dechex(strlen($chunk)) \r\n $chunk \r\n •
0 \r\n \r\n
General weirdness • Trailers • Repeated headers • 100 Continue
ABNF is very permissive
HTTP/1.1 RFC 2616 read it.
Parsing
Request GET / HTTP/1.1 <--- request line Host: igor.io <---
header <--- line feed <--- body (blank)
More codes 01, 02
Problem? • Incomplete HTTP parser • Missing error handling •
Assumption: request can be read in one go • Assumption: request no larger than 512 bytes • Assumption: request/response body fits in memory • Blocking I/O
Non-blocking I/O
Even more codes 03-08
Unreadable
LOL error handling
Writing your own HTTP server is hard
Don’t do it.
None
Questions? joind.in/9277 ! reactphp.org ! @igorwesome ! ! github.com/igorw/webserver-zceu
Bonus • Memory leaks • Too fast reads / too
slow writes • libevent / libev / libuv • Promises • Generators • pthreads • WebSockets
Logo credits • Netscape • Apache • FreeBSD • Erlang