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.1k
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
170
Wide Event Analytics (LISA19)
igorw
3
890
a day in the life of a request
igorw
0
98
production: an owner's manual
igorw
0
120
The Power of 2
igorw
0
190
LISP 1.5 Programmer's Manual: A Dramatic Reading
igorw
0
330
The Moral Character of Software
igorw
1
230
interdisciplinary computing (domcode)
igorw
0
230
miniKanren (clojure berlin)
igorw
1
220
Other Decks in Programming
See All in Programming
単体テストを書かない技術 #phpcon_odawara
o0h
PRO
26
8.2k
Goのエラースタックトレースの歴史と今後
sonatard
7
1.1k
try! Swift Tokyo 初参加報告LT
hinakko2
0
220
Amazon SQSコンシューマー疎結合への旅 - 出張! #DevelopersIO IT技術ブログの中の人が語る勉強会 #3
quiver
0
230
Prepare for Jakarta EE 11 - Performance and Developer Productivity
ivargrimstad
0
700
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
380
PHPの次期バージョンはこの時期どうなっているのか - Internalsの開発体制について - PHPカンファレンス小田原
youkidearitai
PRO
1
190
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
250
使ってみよう Azure AI Document Intelligence
kosmosebi
2
280
Milestoner
bkuhlmann
1
410
GraphQLサーバの構成要素を整理する #ハッカー鮨 #tsukijigraphql / graphql server technology selection
izumin5210
4
820
Elm 0.19.0 Changes
bkuhlmann
0
490
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1356
200k
How GitHub (no longer) Works
holman
304
140k
Principles of Awesome APIs and How to Build Them.
keavy
121
16k
Building an army of robots
kneath
300
41k
Adopting Sorbet at Scale
ufuk
68
8.6k
Designing with Data
zakiwarfel
96
4.8k
The Art of Programming - Codeland 2020
erikaheidi
42
12k
The Brand Is Dead. Long Live the Brand.
mthomps
49
28k
The Mythical Team-Month
searls
216
42k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
14
1.5k
Building a Scalable Design System with Sketch
lauravandoore
456
32k
[RailsConf 2023] Rails as a piece of cake
palkan
23
3.9k
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