λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

CS

[Network] HTTP λž€ ?

 πŸ’‘ ν•˜λ£¨μ—λ„ λͺ‡ λ²ˆμ”©μ΄λ‚˜, 평생을 듀을 HTTP μ •λ¦¬ν•˜μž !

  λ…Έμ…˜μ—μ„œ 보기

 

 

🚚 HTTP

HTTP λž€?

 

 

  • HTTP(Hyper Text Transfer Protocol) λž€, μ„œλ²„ - ν΄λΌμ΄μ–ΈνŠΈ λͺ¨λΈμ„ 따라 데이터λ₯Ό μ£Όκ³  λ°›κΈ° μœ„ν•œ ν”„λ‘œν† μ½œ
  • 즉, HTTP λŠ” μΈν„°λ„·μ—μ„œ ν•˜μ΄νΌν…μŠ€νŠΈλ₯Ό κ΅ν™˜ν•˜κΈ° μœ„ν•œ 톡신 κ·œμ•½
  • 80번 포트 μ‚¬μš©
  • HTTP μ„œλ²„κ°€ 80 번 ν¬νŠΈμ—μ„œ μš”μ²­μ„ 기닀리고, ν΄λΌμ΄μ–ΈνŠΈλŠ” 80번 포트둜 μš”μ²­μ„ 보낸닀.

 


 

HTTP νŠΉμ§•

 

λΉ„μƒνƒœ μ—°κ²°(Stateless, Connectless)

  • μ„œλ²„μ— μ—°κ²°ν•΄ μš”μ²­ν•˜κ³  → 응닡을 λ°›μœΌλ©΄ : 연결을 λŠμ–΄λ²„λ¦°λ‹€.
  • μž₯점
    • 접속 μœ μ§€ μ΅œμ†Œν™”
    • λΆˆνŠΉμ • λ‹€μˆ˜λ₯Ό λŒ€μƒμœΌλ‘œ ν•˜λŠ” μ„œλΉ„μŠ€μ— 유리
  • 단점
    • 연결을 λŠμ–΄λ²„λ¦¬κΈ° λ•Œλ¬Έμ—, ν΄λΌμ΄μ–ΈνŠΈμ˜ 이전 μƒνƒœλ₯Ό μ•Œ 수 μ—†γ…‡,ㅁ
    • 이λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ μΏ ν‚€, μ„Έμ…˜ 등을 μ‚¬μš©ν•˜λŠ” 것!!!!!!!!!!!!!!

 

Keep Alive

  • HTTP 1.1 λΆ€ν„°λŠ” keep-alive κΈ°λŠ₯을 지원
  • HTTP λŠ” ν•˜λ‚˜μ˜ 연결에 - ν•˜λ‚˜μ˜ μš”μ²­μ„ ν•˜λŠ” 것을 κΈ°μ€€μœΌλ‘œ 섀계가 λ˜μ–΄μžˆμŒ
    • λ§Œμ•½, λ¬Έμ„œμ— λ‹€μˆ˜μ˜ 파일이 μžˆλ‹€λ©΄?
    • 계속 μ—°κ²°ν•˜κ³  → λ‹€μš΄ν•˜κ³  → 연결을 λŠμ–΄μ•Ό 함
    • μ΄λŸ¬ν•œ 방식은 TCP 톡신 κ³Όμ •μ—μ„œ λΉ„μš©μ΄ 많이 μ†Œλͺ¨λ¨!
  • Keep Alive κΈ°λŠ₯은, μ§€μ •λœ μ‹œκ°„λ™μ•ˆ 연결을 λŠμ§€ μ•Šκ³  μš”μ²­μ„ κ³„μ†ν•΄μ„œ 보낼 수 μžˆλ‹€.

 


 

 

 

 

 

HTTP Request ꡬ쑰

 

Start Line

  • HTTP λ©”μ†Œλ“œ
    • μš”μ²­μ˜ μ˜λ„μ— 따라 GET/POST/PUT/DELETE
      • GET : 쑰회 - μ‘΄μž¬ν•˜λŠ” μžμ›μ— λŒ€ν•œ μš”μ²­
      • POST : 등둝 - μƒˆλ‘œμš΄ μžμ›μ„ 생성
      • PUT : μˆ˜μ • - μ‘΄μž¬ν•˜λŠ” μžμ›μ— λŒ€ν•œ λ³€κ²½
        • PUT : λ¦¬μ†ŒμŠ€μ˜ λͺ¨λ“  것을 μ—…λ°μ΄νŠΈ ν•œλ‹€.
        • 보내지지 μ•Šμ€ 정보에 λŒ€ν•΄μ„œλŠ” nullκ°’μœΌλ‘œ μ—…λ°μ΄νŠΈ
        • PATCH : λ¦¬μ†ŒμŠ€μ˜ 일뢀λ₯Ό μ—…λ°μ΄νŠΈ ν•œλ‹€.
        • 보내지지 μ•Šμ€ 정보에 λŒ€ν•΄μ„œλŠ” κΈ°μ‘΄ 데이터λ₯Ό μœ μ§€
      • DELETE : μ‚­μ œ - μ‘΄μž¬ν•˜λŠ” μžμ›μ— λŒ€ν•œ μ‚­μ œ
  • Request Target Path(μš”μ²­ URL)
    • Request Target Path λŠ” HTTP Request κ°€ μ „μ†‘λ˜λŠ” λͺ©ν‘œ μ£Όμ†Œ
  • HTTP Version
    • version 에 따라 Request λ©”μ‹œμ§€ κ΅¬μ‘°λ‚˜ 데이터가 λ‹€λ₯Ό 수 있음

 

Header

  • HTTP Request κ·Έ μžμ²΄μ— λŒ€ν•œ 정보λ₯Ό λ‹΄κ³  있음
  • Key : Value ν˜•νƒœλ‘œ 이루어짐
  • Request 와 Response μ—λ§Œ μžˆλŠ” Header 외에 곡톡 Header 도 쑴재
  • Host : μš”μ²­ν•˜λ €λŠ” μ„œλ²„ 호슀트 이름과 포트 번호
  • User-agent : ν΄λΌμ΄μ–ΈνŠΈ ν”„λ‘œκ·Έλž¨ 정보
    • 이 정보λ₯Ό 톡해 μ„œλ²„λŠ”, ν΄λΌμ΄μ–ΈνŠΈ ν”„λ‘œκ·Έλž¨(λΈŒλΌμš°μ €)에 λ§žλŠ” 졜적의 데이터λ₯Ό 보낼 수 있음
  • Referer : λ°”λ‘œ 직전에 λ¨Έλ¬Όλ €λ˜ μ›Ή 링크 μ£Όμ†Œ
  • Accept : ν΄λΌμ΄μ–ΈνŠΈκ°€ 처리 κ°€λŠ₯ν•œ λ―Έλ””μ–΄ νƒ€μž… μ’…λ₯˜ λ‚˜μ—΄
  • If-Modified-Since : 여기에 쓰여진 μ‹œκ°„ μ΄ν›„λ‘œ λ³€κ²½λœ λ¦¬μ†ŒμŠ€ 취득
    • νŽ˜μ΄μ§€κ°€ μˆ˜μ •λœ 경우, μ΅œμ‹  νŽ˜μ΄μ§€λ‘œ ꡐ체
  • Authorization : 인증 토큰을 μ„œλ²„λ‘œ 보낼 λ•Œ μ‚¬μš©
  • Origin : μ„œλ²„λ‘œ POST μš”μ²­μ„ 보낼 λ•Œ μš”μ²­μ΄ μ–΄λŠ μ£Όμ†Œμ—μ„œ μ‹œμž‘λ˜μ—ˆλŠ”μ§€λ₯Ό λ‚˜νƒ€λ‚΄λŠ” κ°’
    • 이 κ°’μœΌλ‘œ μš”μ²­μ„ 보낸 μ£Όμ†Œμ™€ λ°›λŠ” μ£Όμ†Œκ°€ λ‹€λ₯Έ 경우, CORS(Cross-Origin Resource Sharing) μ—λŸ¬ λ°œμƒ
  • **Cookie** : μΏ ν‚€
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json
Content-Length: 257
Host: google.com
User-Agent: HTTPie/0.9.3

 

Body

  • HTTP Reuest κ°€ μ „μ†‘ν•˜λŠ” 데이터λ₯Ό λ‹΄κ³  μžˆλŠ” λΆ€λΆ„
  • μ „μ†‘ν•˜λŠ” 데이터가 μ—†λŠ” 경우 Body λŠ” λΉ„μ–΄μžˆμŒ
  • 보톡 POST μš”μ²­μΌ 경우, HTML 폼 데이터가 포함

 


 

HTTP Response ꡬ쑰

 

Start Line

  • HTTP Version
  • Status Code
    • Response 의 μƒνƒœλ₯Ό λ‚˜νƒ€λ‚΄λŠ” μ½”λ“œ
    • 200 : μš”μ²­ 정상 처리
    • 404 : 잘λͺ»λœ μš”μ²­
  • Status Text
    • Response 의 μƒνƒœλ₯Ό κ°„λž΅ν•˜κ²Œ κΈ€λ‘œ ν‘œν˜„
    HTTP/1.1 404 Not Found
    

 

Header

  • Location : μƒνƒœ μ½”λ“œκ°€ 301, 302 일 λ•Œλ§Œ λ³Ό 수 μžˆλŠ” Heder
    • μ„œλ²„μ˜ 응닡이 λ‹€λ₯Έ 곳에 μžˆλ‹€κ³  μ•Œλ €μ£Όλ©΄μ„œ → ν•΄λ‹Ή μœ„μΉ˜ URI μ €μž₯
    • URI 와 URL (URI > URL, URN)
      • URI(Uniform resource Identifier) : λ„€νŠΈμ›Œν¬ μƒμ—μ„œ μžμ›μ˜ μœ„μΉ˜λ₯Ό μ•Œλ €μ£ΌκΈ° μœ„ν•œ κ·œμ•½
      • URL(Uniform Resource Locator) : 톡합 μžμ› μ‹λ³„μž → 인터넷에 μžˆλŠ” μžμ›μ„ λ‚˜νƒ€λ‚΄λŠ” μœ μΌν•œ μ£Όμ†Œ
    • URI 와 URL ꡬ뢄
      • https://example.com μ˜ 경우 https://example.com μ΄λΌλŠ” μ„œλ²„λ₯Ό λ‚˜νƒ€λ‚΄κΈ° λ•Œλ¬Έμ— URLμ΄λ©΄μ„œ URI
      • https://example.com/skin 의 경우 example μ„œλ²„μ˜ skinμ΄λΌλŠ” μΈν„°λ„·μƒμ˜ μžμ›μ˜ μœ„μΉ˜λ₯Ό μ˜λ―Έν•˜κΈ°μ— URL μ΄λ©΄μ„œ URI
      • https://example.com/123 의 경우
        • URL : https://example.com κΉŒμ§€
        • μ›ν•˜λŠ” 정보에 λ„λ‹¬ν•˜κΈ° μœ„ν•΄μ„œλŠ” 123μ΄λΌλŠ” μ‹λ³„μžκ°€ ν•„μš”
        • UIRI μ΄μ§€λ§Œ URL 은 μ•„λ‹˜
  • Server : μ›Ή μ„œλ²„μ˜ μ’…λ₯˜
  • **Age** : max-age(μœ νš¨μ‹œκ°„, μ΄ˆλ‹¨μœ„) μ‹œκ°„ λ‚΄μ—μ„œ μ–Όλ§ˆλ‚˜ ν˜λ €λŠ”μ§€λ₯Ό 초 λ‹¨μœ„λ‘œ μ•Œλ €μ£ΌλŠ” κ°’
  • Referrer-policy : μ„œλ²„ referrer 정책을 μ•Œλ €μ£ΌλŠ” κ°’
    • origin, no-referrer, unsafe-url
  • WWW-Authenticate : μ‚¬μš©μž 인증이 ν•„μš”ν•œ μžμ›μ„ μš”κ΅¬ν•  경우, μ„œλ²„κ°€ μ œκ³΅ν•˜λŠ” 인증 방식
  • Proxy-Authenticate : μš”μ²­ν•œ μ„œλ²„κ°€ ν”„λ‘μ‹œ μ„œλ²„μΈ 경우 μœ μ € 인증을 μœ„ν•œ κ°’

 

Body

  • μ „μ†‘λ°›λŠ” 데이터 → μ—†λŠ” 경우 λΉ„μ–΄μžˆμŒ
  • HTML, JSON, Octet Stream

 

 

HTTP/1.1 404 Not Found

Connection: close
Content-Length: 1573
Content-Type: text/html; charset=UTF-8
Date: Mon, 20 Aug 2018 07:59:05 GMT

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 404 (Not Found)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>404.</b> <ins>That’s an error.</ins>
  <p>The requested URL <code>/payment-sync</code> was not found on this server.  <ins>That’s all we know.</ins>

 

HTTP Request / Response 곡톡 Header

  • Date : ν˜„μž¬ μ‹œκ°„
  • Cache-Control : μΊμ‹œ μ œμ–΄
  • Transfer-Encoding : Body λ‚΄μš© 자체 μ••μΆ• 방식 지정
  • Content-Encoding : Body 의 λ¦¬μ†ŒμŠ€ μ••μΆ• 방식
  • Cotent-type : Body 의 λ―Έλ””μ–΄ νƒ€μž…
    • Ex) application/json, text/html
  • Content-Length : Body 의 길이
  • Content-language : Body λ₯Ό μ΄ν•΄ν•˜λŠ”λ° κ°€μž₯ μ μ ˆν•œ μ–Έμ–΄
    • Ex) ko
  • Connection : ν΄λΌμ΄μ–ΈνŠΈμ™€ μ„œλ²„μ˜ μ—°κ²° 방식 μ„€μ •
    • HTTP/1.1 은 keep-alive 둜 연결을 μœ μ§€ν•˜λŠ” 것이 λ””ν΄νŠΈ

 


 

Request Header 의 Content-type 과 Body

  • form ν˜•νƒœ : URL Emcoded 방식
    • application/x-www-form-urlencoded
    • λ©”μ‹œμ§€ λ°”λ”” : 쿼리 λ¬Έμžμ—΄
  • json ν˜•νƒœ
    • application/json
  • **multipart ν˜•νƒœ** : 이진 νŒŒμΌμ„ λ„˜κΈΈ λ•Œ, ν•˜λ‚˜μ˜ λ©”μ‹œμ§€ 바디에 파트λ₯Ό λ‚˜λˆ„μ–΄ μž‘μ„±
    • boundary λŠ” 파트 κ΅¬λΆ„μž
    • multipart/form-data : boundary=frontier

 

Response Header 의 Content-type 과 Body

  • text/plain, text/html
  • application/xml, applicaion/json
  • image/png, image/jpg
  • audio/mp3, video/mp4

 


 

Satatus Code

  • 1xx : 정보
  • 2xx : 성곡
    • 200 : OK. μš”μ²­ 성곡
    • 201 : Created. 생성 μš”μ²­ 성곡
    • 202 : Accepted. μš”μ²­ 수락(처리 보μž₯ X)
    • 204 : μ„±κ³΅ν–ˆμœΌλ‚˜ λŒλ €μ€„ 것이 μ—†μŒ
  • 3xx : λ¦¬λ‹€μ΄λ ‰μ…˜
    • 300 : Multiple choices. μ—¬λŸ¬ λ¦¬μ†ŒμŠ€μ— λŒ€ν•œ μš”μ²­ κ²°κ³Ό λͺ©λ‘
    • 301, 302, 303 : Redirect. λ¦¬μ†ŒμŠ€ μœ„μΉ˜κ°€ λ³€κ²½λœ μƒνƒœ
    • 304 : Not modified. λ¦¬μ†ŒμŠ€κ°€ μˆ˜μ •λ˜μ§€ μ•ŠμŒ
  • 4xx : ν΄λΌμ΄μ–ΈνŠΈ 였λ₯˜
    • 400 : Bad Request. μš”μ²­ 였λ₯˜(문법상 였λ₯˜, 잘λͺ» μž…λ ₯ν•œ url)
    • 401 : Unauthorized. κΆŒν•œ μ—†μŒ
    • 403 : Forbidden. μš”μ²­ κ±°λΆ€
    • 404 : Not Found. λ¦¬μ†ŒμŠ€κ°€ μ—†λŠ” μƒνƒœ(μš”μ²­ν•œ νŽ˜μ΄μ§€λ₯Ό 찾을 수 μ—†λŠ” 경우)
  • 5xx : μ„œλ²„ 였λ₯˜
    • 500 : Internal Server Error. μ„œλ²„κ°€ μš”μ²­μ„ μ²˜λ¦¬ν•˜μ§€ λͺ»ν•¨
    • 501 : Not Implemented. μ„œλ²„κ°€ μ§€μ›ν•˜μ§€ μ•ŠλŠ” μš”μ²­
    • 503 : Service Unavailable. κ³ΌλΆ€ν•˜ λ“±μœΌλ‘œ λ‹Ήμž₯ μ„œλΉ„μŠ€κ°€ λΆˆκ°€λŠ₯ν•œ μƒνƒœ

 


 

HTTP 의 문제점

  • μ„œλ²„μ—μ„œ → λΈŒλΌμš°μ €λ‘œ μ „μ†‘λ˜λŠ” 정보가 μ•”ν˜Έν™”λ˜μ§€ μ•ŠλŠ”λ‹€λŠ” 것!
  • 즉, 데이터가 μ‰½κ²Œ λ„λ‚œλ‹Ήν•  수 μžˆλ‹€λŠ” 것이닀.
  • 이λ₯Ό HTTPS λŠ” SSL(λ³΄μ•ˆ μ†ŒμΌ“ 계측) 을 μ‚¬μš©ν•΄ ν•΄κ²°ν–ˆλ‹€.

 

 

참고자료