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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Igor Wiedler
November 20, 2013
Programming
1.3k
1
Share
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
340
Wide Event Analytics (LISA19)
igorw
4
940
a day in the life of a request
igorw
0
160
production: an owner's manual
igorw
0
190
The Power of 2
igorw
0
320
LISP 1.5 Programmer's Manual: A Dramatic Reading
igorw
0
470
The Moral Character of Software
igorw
1
300
interdisciplinary computing (domcode)
igorw
0
310
miniKanren (clojure berlin)
igorw
1
320
Other Decks in Programming
See All in Programming
セグメントとターゲットを意識するプロポーザルの書き方 〜採択の鍵は、誰に刺すかを見極めるマーケティング戦略にある〜
m3m0r7
PRO
0
710
2026年のソフトウェア開発を考える(2026/05版) / Software Engineering Scrum Fest Niigata 2026 Edition
twada
PRO
18
8k
Structured Concurrency, Scoped Values and Joiners in the JDK 25 26 27
josepaumard
1
120
From Formal Specification to Property Based Test
ohbarye
0
590
Road to RubyKaigi: Play Hard(ware)
makicamel
1
520
Making the RBS Parser Faster
soutaro
0
640
Kingdom of the Machine
yui_knk
2
1.3k
AIエージェントで業務改善してみた
taku271
0
550
【26新卒研修】OpenAPI/Swagger REST API研修
dip_tech
PRO
0
110
의존성 주입과 모듈화
fornewid
0
150
PHPer、Cloudflare に引っ越す
suguruooki
1
120
Back to the roots of date
jinroq
0
600
Featured
See All Featured
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.4k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
350
Raft: Consensus for Rubyists
vanstee
141
7.4k
Evolving SEO for Evolving Search Engines
ryanjones
0
180
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.2k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
780
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.2k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
290
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
140
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.7k
Balancing Empowerment & Direction
lara
6
1.1k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
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