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
How GitHub (no longer) Works
holman
310
140k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Why Our Code Smells
bkeepers
PRO
334
57k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
Practical Orchestrator
shlominoach
186
10k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
It's Worth the Effort
3n
183
27k
A Tale of Four Properties
chriscoyier
156
23k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
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ݴޠڧ͍ຯํʹͳͬͯ͘ΕΔ