Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build your own webserver with PHP

Build your own webserver with PHP

Igor Wiedler

November 20, 2013
Tweet

More Decks by Igor Wiedler

Other Decks in Programming

Transcript

  1. Build your own
    web server
    with

    View Slide

  2. @igorwesome

    View Slide

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

    View Slide

  4. View Slide

  5. When you open your web
    browser…

    !
    What actually happens?

    View Slide

  6. View Slide

  7. http://igor.io/

    View Slide

  8. DNS
    Where is igor.io?

    View Slide

  9. DNS
    176.58.127.253

    View Slide

  10. 176.58.127.253

    View Slide

  11. 2^16!
    65535!
    TCP ports

    View Slide

  12. TCP port 80

    View Slide

  13. SYN

    View Slide

  14. SYN/ACK

    View Slide

  15. ACK

    View Slide

  16. View Slide

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

    View Slide

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

    View Slide

  19. HTTP
    TCP
    IP

    View Slide

  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 | |
    +---+--------------+----------------+

    View Slide

  21. http://igor.io/

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  26. The Socket API

    View Slide

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

    View Slide

  28. #include
    #include
    #include
    socket()
    --------
    bind()
    listen()
    accept()
    --------
    connect()

    View Slide

  29. Syscalls
    Process
    socket!
    bind!
    listen!
    accept!
    connect!
    !
    read!
    write!
    close!
    !

    View Slide

  30. Stream Sockets

    View Slide

  31. I will show you

    Les Codes
    00

    View Slide

  32. HTTP: The Protocol

    View Slide

  33. Request
    GET / HTTP/1.1 Host: igor.io

    View Slide

  34. Response
    HTTP/1.1 200 OK Content-Length: 2 Hi

    View Slide

  35. Verbs
    •GET

    •POST

    View Slide

  36. Verbs
    •GET

    •POST

    •PUT

    •DELETE

    View Slide

  37. Verbs
    •GET

    •POST

    •PUT

    •DELETE

    !
    •HEAD

    •OPTIONS

    •TRACE

    •CONNECT

    View Slide

  38. • 1xx - Special protocol stuff

    • 2xx - All good

    • 3xx - Redirection

    • 4xx - You messed up

    • 5xx - I messed up
    Status codes

    View Slide

  39. Keep-Alive

    View Slide

  40. Streaming
    • Transfer-Encoding: chunked

    • dechex(strlen($chunk)) \r\n $chunk \r\n

    • 0 \r\n \r\n

    View Slide

  41. General weirdness
    • Trailers

    • Repeated headers

    • 100 Continue

    View Slide

  42. ABNF is
    very permissive

    View Slide

  43. HTTP/1.1
    RFC 2616
    read it.

    View Slide

  44. Parsing

    View Slide

  45. Request
    GET / HTTP/1.1 Host: igor.io

    View Slide

  46. More codes
    01, 02

    View Slide

  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

    View Slide

  48. Non-blocking I/O

    View Slide

  49. Even more codes
    03-08

    View Slide

  50. Unreadable

    View Slide

  51. LOL error handling

    View Slide

  52. Writing your own
    HTTP server is hard

    View Slide

  53. Don’t do it.

    View Slide

  54. View Slide

  55. Questions?
    joind.in/9277

    !
    reactphp.org

    !
    @igorwesome

    !
    !
    github.com/igorw/webserver-zceu

    View Slide

  56. Bonus
    • Memory leaks

    • Too fast reads / too slow writes

    • libevent / libev / libuv

    • Promises

    • Generators

    • pthreads

    • WebSockets

    View Slide

  57. Logo credits
    • Netscape

    • Apache

    • FreeBSD

    • Erlang

    View Slide