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
1.2k
Build your own webserver with PHP
Igor Wiedler
November 20, 2013
Tweet
Share
More Decks by Igor Wiedler
See All by Igor Wiedler
Redis Bedtime Stories
igorw
1
270
Wide Event Analytics (LISA19)
igorw
4
900
a day in the life of a request
igorw
0
120
production: an owner's manual
igorw
0
140
The Power of 2
igorw
0
260
LISP 1.5 Programmer's Manual: A Dramatic Reading
igorw
0
390
The Moral Character of Software
igorw
1
260
interdisciplinary computing (domcode)
igorw
0
260
miniKanren (clojure berlin)
igorw
1
260
Other Decks in Programming
See All in Programming
Unlock the Potential of Swift Code Generation
rockname
0
190
SQL Server ベクトル検索
odashinsuke
0
150
趣味全開のAITuber開発
kokushin
0
180
サービスクラスのありがたみを発見したときの思い出 #phpcon_odawara
77web
4
400
Building a macOS screen saver with Kotlin (Android Makers 2025)
zsmb
1
110
Compose Hot Reload is here, stop re-launching your apps! (Android Makers 2025)
zsmb
1
360
ベクトル検索システムの気持ち
monochromegane
30
9.7k
エンジニア未経験が最短で戦力になるためのTips
gokana
0
250
新卒から4年間、20年もののWebサービスと 向き合って学んだソフトウェア考古学
oguri
8
7.1k
AI Coding Agent Enablement - エージェントを自走させよう
yukukotani
13
5.1k
gen_statem - OTP's Unsung Hero
whatyouhide
1
190
SLI/SLOの設定を進めるその前に アラート品質の改善に取り組んだ話
tanden
3
790
Featured
See All Featured
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
46
2.4k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
22
2.6k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
118
51k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.4k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.6k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.2k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
29
2k
Docker and Python
trallard
44
3.3k
How GitHub (no longer) Works
holman
314
140k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
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