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
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
180
The Power of 2
igorw
0
320
LISP 1.5 Programmer's Manual: A Dramatic Reading
igorw
0
460
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
Coding as Prompting Since 2025
ragingwind
0
780
AIエージェントで業務改善してみた
taku271
0
500
[PHPerKaigi 2026]PHPerKaigi2025の企画CodeGolfが最高すぎて社内で内製して半年運営して得た内製と運営の知見
ikezoemakoto
0
340
AI時代の脳疲弊と向き合う ~言語学としてのPHP~
sakuraikotone
1
1.8k
의존성 주입과 모듈화
fornewid
0
120
おれのAgentic Coding 2026/03
tsukasagr
1
140
レガシーPHP転生 〜父がドメインエキスパートだったのでDDD+Claude Codeでチート開発します〜
panda_program
0
650
Linux Kernelの1文字のミスで 権限昇格ができた話
rqda
0
2.3k
Redox OS でのネームスペース管理と chroot の実現
isanethen
0
560
Oxlintとeslint-plugin-react-hooks 明日から始められそう?
t6adev
0
110
AWS re:Invent 2025の少し振り返り + DevOps AgentとBacklogを連携させてみた
satoshi256kbyte
3
150
「速くなった気がする」をデータで疑う
senleaf24
0
150
Featured
See All Featured
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
150
Building Flexible Design Systems
yeseniaperezcruz
330
40k
For a Future-Friendly Web
brad_frost
183
10k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
790
Building AI with AI
inesmontani
PRO
1
880
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
160
The Limits of Empathy - UXLibs8
cassininazir
1
290
Darren the Foodie - Storyboard
khoart
PRO
3
3.2k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
490
RailsConf 2023
tenderlove
30
1.4k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
The browser strikes back
jonoalderson
0
940
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