Upgrade to Pro — share decks privately, control downloads, hide ads and more …

http_basics

 http_basics

Node girls で話したhttp basics の話です

Yosuke Furukawa

February 21, 2017
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. yes !!! love http layer !!!! const http = require('http');

    const server = http.createServer((req, res) => { res.writeHead(200); res.end('Hello World'); }); server.listen(3000, () => { console.log('listening'); });
  2. ͦ΋ͦ΋ HTTP ͱ͸?? GET /hello.txt HTTP/1.1 User-Agent: curl/7.16.3 Host: www.example.com

    Accept-Language: en, mi HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT ETag: "34aa387-d-1568eb00" Accept-Ranges: bytes Content-Length: 51 Vary: Accept-Encoding Content-Type: text/plain Hello World! My payload includes a trailing CRLF. 3FRVFTU 3FTQPOTF
  3. ͦ΋ͦ΋ HTTP ͱ͸?? GET /hello.txt HTTP/1.1 User-Agent: curl/7.16.3 Host: www.example.com

    Accept-Language: en, mi HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT ETag: "34aa387-d-1568eb00" Accept-Ranges: bytes Content-Length: 51 Vary: Accept-Encoding Content-Type: text/plain Hello World! My payload includes a trailing CRLF. 3FRVFTU 3FTQPOTF 3FRVFTU-JOF શͯͷݩɺϝιουɺύεɺϓϩτ ίϧόʔδϣϯͰهड़ 3FRVFTU)FBEFS ϦΫΤετʹର͢Δϝλ৘ใ
  4. ͦ΋ͦ΋ HTTP ͱ͸?? GET /hello.txt HTTP/1.1 User-Agent: curl/7.16.3 Host: www.example.com

    Accept-Language: en, mi HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT ETag: "34aa387-d-1568eb00" Accept-Ranges: bytes Content-Length: 51 Vary: Accept-Encoding Content-Type: text/plain Hello World! My payload includes a trailing CRLF. 3FRVFTU 3FTQPOTF 3FTQPOTF-JOF ϓϩτίϧόʔδϣϯͱTUBUVTDPEF 3FTQPOTF)FBEFS Ϩεϙϯεͷϝλ৘ใɺαΠζ΍Β ίϯςϯτλΠϓ΍Βɻ 3FTQPOTF#PEZ Ϩεϙϯεͷຊମɺ͜͜ʹ)5.-΍ +40/ͳͲ͕৐Δ
  5. TCPͷϞδϡʔϧ͸netΛ࢖͏ const net = require('net'); const CRLF = '\r\n'; const

    server = net.createServer((connection) => { console.log('connect!!'); connection.write('HTTP/1.1 200 OK' + CRLF); connection.write('Set-Cookie: test=123' + CRLF); connection.write(CRLF); connection.write('Hello world'); connection.end(); }); server.listen(3000, () => { console.log('listen on 3000'); });
  6. Response header Λฦͯ͠ΈΔ const net = require('net'); const CRLF =

    '\r\n'; const server = net.createServer((connection) => { console.log('connect!!'); connection.write('HTTP/1.1 200 OK' + CRLF); connection.write('Set-Cookie: test=123' + CRLF); connection.write(CRLF); connection.write('Hello world'); connection.end(); }); server.listen(3000, () => { console.log('listen on 3000'); });
  7. TCPͷϞδϡʔϧ͸netΛ࢖͏ const net = require('net'); const CRLF = '\r\n'; const

    server = net.createServer((connection) => { console.log('connect!!'); connection.write('HTTP/1.1 200 OK' + CRLF); connection.write('Set-Cookie: test=123' + CRLF); connection.write(CRLF); connection.write('Hello world'); connection.end(); }); server.listen(3000, () => { console.log('listen on 3000'); }); 3FTQPOTF-JOF ϓϩτίϧόʔδϣϯͱTUBUVTDPEF 3FTQPOTF)FBEFS SFTQPOTFͷϝλ৘ใ 3FTQPOTF#PEZ SFTQPOTFͷ಺༰
  8. ͦΜͳ͜ͱ͸͠ͳ͍ Use express !!!! const app = express(); app.use('/static', express.static());

    app.use('/foo', (req, res, next) => { res.send('foo'); }); app.use('/bar', (req, res, next) => { res.send('foo'); });
  9. ͨͩHTTP͕Ͳ͏ͳͬͯΔ͔Λ ஌͓ͬͯ͘͜ͱ͸ॏཁ GET /hello.txt HTTP/1.1 User-Agent: curl/7.16.3 Host: www.example.com Accept-Language:

    en, mi HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT ETag: "34aa387-d-1568eb00" Accept-Ranges: bytes Content-Length: 51 Vary: Accept-Encoding Content-Type: text/plain Hello World! My payload includes a trailing CRLF.