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
Goで書いて学ぶ HTTP Server / Write and learn HTTP Ser...
Search
noyan
October 12, 2023
0
11
Goで書いて学ぶ HTTP Server / Write and learn HTTP Server in Go
noyan
October 12, 2023
Tweet
Share
More Decks by noyan
See All by noyan
利用者の生産性をどう上げるか 中規模モノレポの苦しみ
noyan_
0
320
React Fiber Architectureとレスポンス性の向上 / React fiber architecture and responsiveness
noyan_
0
9
React Server Componentsは誰のためのものか?
noyan_
0
390
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Automating Front-end Workflow
addyosmani
1366
200k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
Git: the NoSQL Database
bkeepers
PRO
427
64k
Become a Pro
speakerdeck
PRO
25
5k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Writing Fast Ruby
sferik
627
61k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Designing for Performance
lara
604
68k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Transcript
GoͰॻֶ͍ͯͿHTTP Server 2023 - 10 - 12 ൃදձ - Enablement
Bootcamp for Gopherizing #1 noyan
ϥΠϒϥϦΛଟ༻͢Δݱͷ։ൃ ↓ ࣮ͨ͠ػೳ ≠ ٕज़ྗ ↓ গ͠ਂ͘જͬͯΈΑ͏ʂ
ࠓճ͕ࣗHTTPαʔόʔͰ ֶΜͩ͜ͱΛ͠·͢
ࣗݾհ • ໊લɿnoyan • ϑϩϯτΤϯυք۾ʹੜଉ • Zenn: https://zenn.dev/noyan
HTTP αʔόʔΛ࣮͠Α͏ • ΫϥΠΞϯταΠυͷ࣮Λ͢ΔதͰɺ HTTP ϓϩτίϧͷετϦʔϛϯάΩϟογϡ ͳͲΛΔඞཁΛײͨ͡ • ୯७ͳϦΫΤετΛड͚औͬͯݻఆͷϨεϙϯεΛฦ͢؆қతͳHTTPαʔόʔΛ࣮͢Δ •
ඞཁͳ͜ͱ • ϦΫΤετͷύʔαʔΛॻ͘ • ϦΫΤετʹରͯ͠Ϩεϙϯε͕ฦͬͯ͘ΔΑ͏ʹ͢Δ
ύʔαʔ: ԿΛ͢Εྑ͍͔ • ؆୯ʹݴ͑ɺ͜ͷϦΫΤ ετΛύʔεͰ͖ΔΑ͏ͳ ύʔαʔΛ࣮͢Εྑ͍
ύʔαʔ: HTTPϦΫΤετͷߏจ • HTTP/1.1ͷߏจ- RFC 9112 sec 2.1 • ͭ·ΓɺϦΫΤετΛύʔε͢ΔʹɺελʔτϥΠϯɾϑΟʔ
ϧυϥΠϯɾϘσΟͷॲཧΛ࣮͢Εྑ͍ɻ HTTP-message = start-line CRLF *( fi eld-line CRLF ) CRLF [ message-body ]
ύʔαʔ: HTTP/1.1 ϦΫΤετ • ϦΫΤετϥΠϯʢϝιο υɺύεɺHTTPόʔδϣ ϯʣ • ϑΟʔϧυϥΠϯʢϔο μʔɺτϨΠϥʔʣ
• ϘσΟ HTTP-message = start-line CRLF *( fi eld-line CRLF ) CRLF [ message-body ]
ύʔαʔ: ࣮ํ 1. ࠷ॳͷߦΛ start-line ͱͯ͠ύʔ ε 2. ۭߦ͕ݱΕΔ·Ͱͷ֤ߦΛ ϑΟʔϧυϥΠϯͱͯ͠ύʔε
3. Γͷ෦ΛϘσΟͱͯ͠ύʔ ε
ύʔαʔ: ࣮ •
ύʔαʔ: ϦΫΤετϥΠϯͷߏจ • ϦΫΤετϥΠϯͷߏจ࣍ͷΑ͏ʹఆٛ͞Ε͍ͯΔ • request-line = method SP request-target
SP HTTP-version • ref. RFC 9112 sec. 3 • ͕ͨͬͯ͠ඞཁͳॲཧ • ϦΫΤετͷ࠷ॳͷߦΛಡΈࠐΉ • ۭനͰׂ͢Δ • ॱʹϝιουɾλʔήοτɾHTTPόʔδϣϯͱ͢Δ
ύʔαʔ: ϦΫΤετϥΠϯ࣮
ύʔαʔ: ϑΟʔϧυͷߏจ • ϦΫΤετϥΠϯͷߏจ࣍ͷΑ͏ʹఆٛ͞Ε͍ͯΔ • fi eld-line = fi eld-name
":" OWS fi eld-value OWS • ref. RFC 9112 sec. 5 • ͕ͨͬͯ͠ඞཁͳॲཧ • ϦΫΤετϥΠϯҎ߱ɺۭߦ·ͰಡΈਐΊΔ • ίϩϯͰׂ͠ɺϑΟʔϧυ໊ɾϑΟʔϧυͱ͢Δ
ϦΫΤετɿ ϥΠϯͷߏจ
ϦΫΤετ: ϘσΟͷߏจ • ϦΫΤετʹϘσΟؚ͕·ΕΔ͔ɺ Content-Length ͔ Transfer- Encoding ϔομʔϑΟʔϧυͰࣔ͞ΕΔ (sec.
6) • ϦΫΤετϥΠϯͷߏจ࣍ͷΑ͏ʹఆٛ͞Ε͍ͯΔ • message-body = *OCTET • ࠓճ୯७ԽͷͨΊɺTransfer-EncodingѻΘͳ͍
ϦΫΤετɿϘσΟ࣮ίʔυ •
Ϩεϙϯε: HTTPϨεϙϯεͷߏจ • HTTP/1.1ͷߏจ- RFC 9112 sec. 2.1, sec. 4
• ͭ·Γɺ͜ͷॱ൪Ͱॻ͖ग़͍͍ͤ HTTP-message = start-line CRLF *( fi eld-line CRLF ) CRLF [ message-body ] status-line = HTTP-version SP status-code SP [ reason-phrase ]
Ϩεϙϯε: HTTPϨεϙϯε࣮
٧·ͬͨͱ͜Ζ • Body ͷύʔεΛ࣮ͨ͠Βɺϋϯά͢ΔΑ͏ʹͳͬͨ • ݪҼ: Body ͷऴʹ CRLF ؚ͕·Εͳ͍
• αʔόʔΫϥΠΞϯτ͔Βऴه߸͕ૹΒΕͯ͜ͳ͍͔ظ͠ ͯͬͯ͠·͍ͬͯͨ • ରࡦ: Reader Λ bu fi o ͷ Scanner ͔Β Reader ʹมߋ͠ɺContent- Length ͚ͩಡΉΑ͏ʹมߋ
࣮ࡍʹ࡞ͬͨͷ • no-yan/go-http • https://github.com/no-yan/go-http
GoͰॻ͍ͯࢥͬͨ͜ͱ • Goݴޠ͔ͩΒ͔ɺRFCͷ࣮ͦͷͷʹूத͢Δ͜ͱ͕Ͱ͖ͨ • ωοτϫʔΫ io.Reader ͩͱߟ͍͑ͯΕྑ͍ • net/http ͕͔ͳΓಡΈ͍͢
• ඪ४ϥΠϒϥϦ͕ಉ͡ݴޠͰॻ͔Ε͍ͯΔͱɺؾܰʹಡΈʹߦ͚Δʂ • ंྠͷ࠶ൃ໌ʹͱͬͯخ͍͠
·ͱΊ • HTTP/1.1 ͷ؆୯ͳ࣮Λߦͳͬͨ • RFC४ڌΛ͍ٻΊͳ͚ΕɺҊ֎ؾܰʹ࡞Δ͜ͱ͕Ͱ͖Δ • গ͠ਂ͘જΔ࣌ʹɺGoݴޠڧ͍ຯํʹͳͬͯ͘ΕΔ