Build your own webserver with PHP

A4b95be2145cc46f891707b6db9dd82d?s=47 Igor Wiedler
November 20, 2013

Build your own webserver with PHP

A4b95be2145cc46f891707b6db9dd82d?s=128

Igor Wiedler

November 20, 2013
Tweet

Transcript

  1. Build your own web server with

  2. @igorwesome

  3. Event-driven, non-blocking I/O with PHP.

  4. None
  5. When you open your web browser… ! What actually happens?

  6. None
  7. http://igor.io/

  8. DNS Where is igor.io?

  9. DNS 176.58.127.253

  10. 176.58.127.253

  11. 2^16! 65535! TCP ports

  12. TCP port 80

  13. SYN

  14. SYN/ACK

  15. ACK

  16. None
  17. GET / HTTP/1.1! Host: igor.io

  18. HTTP/1.1 200 OK! Content-Type: text/html! ! …

  19. HTTP TCP IP

  20. 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 | | +---+--------------+----------------+
  21. http://igor.io/

  22. http://igor.io:80/

  23. http://igor.io:80/ } DNS (layer 7)! resolves to! IP (layer 3)

  24. http://igor.io:80/ } TCP (layer 4)

  25. http://igor.io:80/ } TLS (layer 6)! HTTP (layer 7) } HTTP

    (layer 7)
  26. The Socket API

  27. ________ < socket > -------- \ ^__^ \ (oo)\_______ (__)\

    )\/\ ||----w | || ||
  28. #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> socket() -------- bind() listen()

    accept() -------- connect()
  29. Syscalls Process socket! bind! listen! accept! connect! ! read! write!

    close! !
  30. Stream Sockets

  31. I will show you Les Codes 00

  32. HTTP: The Protocol

  33. Request GET / HTTP/1.1 <--- request line Host: igor.io <---

    header <--- line feed <--- body (blank)
  34. Response HTTP/1.1 200 OK <--- status line Content-Length: 2 <---

    headers <--- line feed Hi <--- body
  35. Verbs •GET •POST

  36. Verbs •GET •POST •PUT •DELETE

  37. Verbs •GET •POST •PUT •DELETE ! •HEAD •OPTIONS •TRACE •CONNECT

  38. • 1xx - Special protocol stuff • 2xx - All

    good • 3xx - Redirection • 4xx - You messed up • 5xx - I messed up Status codes
  39. Keep-Alive

  40. Streaming • Transfer-Encoding: chunked • dechex(strlen($chunk)) \r\n $chunk \r\n •

    0 \r\n \r\n
  41. General weirdness • Trailers • Repeated headers • 100 Continue

  42. ABNF is very permissive

  43. HTTP/1.1 RFC 2616 read it.

  44. Parsing

  45. Request GET / HTTP/1.1 <--- request line Host: igor.io <---

    header <--- line feed <--- body (blank)
  46. More codes 01, 02

  47. 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
  48. Non-blocking I/O

  49. Even more codes 03-08

  50. Unreadable

  51. LOL error handling

  52. Writing your own HTTP server is hard

  53. Don’t do it.

  54. None
  55. Questions? joind.in/9277 ! reactphp.org ! @igorwesome ! ! github.com/igorw/webserver-zceu

  56. Bonus • Memory leaks • Too fast reads / too

    slow writes • libevent / libev / libuv • Promises • Generators • pthreads • WebSockets
  57. Logo credits • Netscape • Apache • FreeBSD • Erlang