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
13
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
340
React Fiber Architectureとレスポンス性の向上 / React fiber architecture and responsiveness
noyan_
0
11
React Server Componentsは誰のためのものか?
noyan_
0
390
Featured
See All Featured
A Philosophy of Restraint
colly
203
16k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Building Adaptive Systems
keathley
38
2.3k
A Tale of Four Properties
chriscoyier
157
23k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Rails Girls Zürich Keynote
gr2m
94
13k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
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ݴޠڧ͍ຯํʹͳͬͯ͘ΕΔ