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
14
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
Navigating Team Friction
lara
183
15k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.9k
Facilitating Awesome Meetings
lara
50
6.2k
Optimising Largest Contentful Paint
csswizardry
33
3k
A better future with KSS
kneath
238
17k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.5k
Building an army of robots
kneath
302
44k
Git: the NoSQL Database
bkeepers
PRO
427
64k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
Agile that works and the tools we love
rasmusluckow
328
21k
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ݴޠڧ͍ຯํʹͳͬͯ͘ΕΔ