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

はてなリモートインターンシップ2022 Web API 講義資料

Hatena
December 19, 2022

はてなリモートインターンシップ2022 Web API 講義資料

Hatena

December 19, 2022
Tweet

More Decks by Hatena

Other Decks in Programming

Transcript

  1. URL Uniform Resource Locator WHATWG URL Living Standard RFC :

    Uniform Resource Identi er (URI): Generic Syntax
  2. HTTP HTTP/ . HTTP/ . HTTP/ . Google SPDY Google

    QUIC SPDY HTTP/ HTTP-over-QUIC HTTP/ QUIC / / HTTP/
  3. HTTP RFCs - RFC : HTTP Semantics - RFC :

    HTTP Caching - RFC : HTTP/ . - RFC : HTTP/ - RFC : HTTP/ - RFC : Expect-CT Extension for HTTP - RFC : QPACK: Field Compression for HTTP/ - RFC : Building Protocols with HTTP - RFC : The Proxy-Status HTTP Response Header Field - RFC : The Cache-Status HTTP Response Header Field - RFC : Targeted HTTP Cache Control - RFC : Extensible Prioritization Scheme for HTTP - RFC : Bootstrapping WebSockets with HTTP/ - RFC : Oblivious DNS over HTTPS
  4. HTTP/ . RFCs RFC Hypertext Transfer Protocol -- HTTP/ .

    Obsoleted by: , , , , , RFC Hypertext Transfer Protocol -- HTTP/ . Obsoleted by: , , , , , RFC Hypertext Transfer Protocol (HTTP/ . ): Message Syntax and Routing Obsoleted by: , RFC Hypertext Transfer Protocol (HTTP/ . ): Semantics and Content Obsoleted by: RFC Hypertext Transfer Protocol (HTTP/ . ): Conditional Requests Obsoleted by: RFC Hypertext Transfer Protocol (HTTP/ . ): Range Requests Obsoleted by: RFC Hypertext Transfer Protocol (HTTP/ . ): Caching Obsoleted by: RFC Hypertext Transfer Protocol (HTTP/ . ): Authentication Obsoleted by: RFC HTTP Semantics RFC HTTP Caching RFC HTTP/ .
  5. HTTP ْخشغכذ٭أشع تط٭ذت ىشر٭ RFC Field (RFC Section ) ٍظؔ

    RFC Content HTTP/ . Content Message Body (RFC Section )
  6. TLS . RFC : The Transport Layer Security (TLS) Protocol

    Version . HTTP HTTP https TLS RFC . . ALPN: RFC Transport Layer Security (TLS) Application-Layer Protocol Negotiation Extension TLS HTTP Clinet Hello(TLS ) - : Encrypted Extensions - :
  7. TLS HTTP TLS HTTP : $ echo -e "HEAD /

    HTTP/1.1\r\nHost: hatenablog.com \r\n" | openssl s_client -ign_eof -connect hatenablog.com:443 ALPN http/ . http/ $ echo | openssl s_client -alpn h2,http/1.1 -connect hatenablog.com:443 http/ . h - IANAIANA ALPN Protocol IDs HTTP IANA Internet Assigned Numbers Authority: Web ID
  8. GitHub ˝ GET /repos/:owner/:repo/issues/ comments/:comment_id ˝ POST /repos/:owner/:repo/issues/:issue_number/ comments ˝

    PATCH /repos/:owner/:repo/issues/ comments/:comment_id ˝ DELETE /repos/:owner/:repo/issues/ comments/:comment_id
  9. ىشر٭ס❆ Host: HTTP/ . User-Agent sec-ch-ua: Content Negotiation Fields: RFC

    . https://wicg.github.io/ua-client-hints/ https://www.rfc-editor.org/rfc/ rfc #section- . . A client MUST send a Host header field (Section 7.2 of [HTTP]) in all HTTP/1.1 request messages. https://www.rfc-editor.org/rfc/rfc .html#section- . -
  10. ( ) http://www.example.com ٛؠؙتع GET / HTTP/1.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Upgrade-Insecure-Requests:

    1 Host: www.example.com User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15 Accept-Language: ja Accept-Encoding: gzip, deflate Connection: keep-alive ٝتَ٤ت HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Vary: Accept-Encoding Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT Age: 573292 Content-Encoding: gzip Expires: Fri, 26 Aug 2022 02:22:20 GMT Cache-Control: max-age=604800 Date: Fri, 19 Aug 2022 02:22:20 GMT Content-Length: 648 ETag: "3147526947" Accept-Ranges: bytes Server: ECS (oxr/8315) X-Cache: HIT
  11. Content Negotiation Fields: Accept Content-Type - Content-Type: text/html HTML -

    Content-Type: application/JSON JSON - Content-Type: application/x-www-form-urlencoded application/x-www-form-urlencodedס䎬䌋 key=value&another_key=another_value
  12. Connection: keep- alive, TCP, HoL Connection: keep-alive TCP TLS TLS

    TCP: RFC Transmission Control Protocol (TCP) 1981 : RFC : TRANSMISSION CONTROL PROTOCOL 1.5-RTT(1 ) -way handshaking) TLS 1~2-RTT TCP 0.5-RTT TLS HTTP HoL(Head of Line) Blocking: keep-alive TCP HTTP
  13. 橎⛍ىشر٭ RFC . . . Request Pseudo-Header Fields ٛؠؙتع :method

    :authority: RFC . . Host and :authority :scheme :path ٝتَ٤ت ˝ :status
  14. فؕػٛך׷׽כ׽׌׾ : نٝ٭ّ HTTP Frame { Length (24), Type (8),

    Flags (8), Reserved (1), Stream Identifier (31), Frame Payload (!"), } RFC . HTTP Frames
  15. HEADERS type=0x01 HEADERS Frame { Length (24), Type (8) =

    0x01, Unused Flags (2), PRIORITY Flag (1), Unused Flag (1), PADDED Flag (1), END_HEADERS Flag (1), Unused Flag (1), END_STREAM Flag (1), Reserved (1), Stream Identifier (31), [Pad Length (8)], [Exclusive (1)], [Stream Dependency (31)], [Weight (8)], Field Block Fragment (!"), Padding (!"2040), }
  16. ꪐ氳ط٭هٜס╈骰 Index Header Name Header Value :authority :method GET :method

    POST :path / :path /index.html :scheme http :scheme https :status 200
  17. DATA type=0x00 DATA Frame { Length (24), Type (8) =

    0x00, Unused Flags (4), PADDED Flag (1), Unused Flags (2), END_STREAM Flag (1), Reserved (1), Stream Identifier (31), [Pad Length (8)], Data (!"), Padding (!"2040), }
  18. HTTP/ RFC QUIC: A UDP-Based Multiplexed and Secure Transport UDP

    TCP TLS RFC HTTP/ HTTP QUIC RFC QPACK: Field Compression for HTTP/
  19. QUIC RFC Version-Independent Properties of QUIC RFC QUIC: A UDP-Based

    Multiplexed and Secure Transport RFC Using TLS to Secure QUIC RFC QUIC Loss Detection and Congestion Control
  20. QUIC -RTT (Early Data) PSK( ) clientearlytra c_secret : 0-RTT

    RFC : The Transport Layer Security (TLS) Protocol Version .
  21. API glibc : libdrm : Web document.getElementById : Web :

    Web API REST GET https:!"api.twitter.com/1.1/statuses/show.json? id=210462857140252672 This image is licensed under the Creative Commons Attribution- Share Alike . International license. Attribution: Shmuel Csaba Otto Traian. source https://commons.wikimedia.org/wiki/ File:Linux_API.svg
  22. GitHub ˝ GET /repos/:owner/:repo/issues/ comments/:comment_id ˝ POST /repos/:owner/:repo/issues/:issue_number/ comments ˝

    PATCH /repos/:owner/:repo/issues/ comments/:comment_id ˝ DELETE /repos/:owner/:repo/issues/ comments/:comment_id
  23. SDL (Schema De nition Language) interface Actor { login: String!

    } type Issue { author: Actor body: String! title: String! } type Repository { issue(number: Int!): Issue } type Query { repository(name: String!, owner: String!): Repository }
  24. syntax = "proto3"; package account; service Account { rpc Signup(SignupRequest)

    returns (SignupReply); } message SignupRequest { string name = 1; string password = 2; } message SignupReply { string token = 1; }
  25. ْشج٭ة㑔 message SignupRequest { string name = 1; string password

    = 2; } ϑΟʔϧυܕ ϑΟʔϧυ໊ = ϑΟʔϧυ൪߸; 1-15 1byte 19000-19999
  26. repeated Result results = 1; map<string, Project> projects = 3;

    oneof test_oneof { string name = 4; SubMessage sub_message = 9; }
  27. message SearchRequest { string query = 1; int32 page_number =

    2; int32 result_per_page = 3; enum Corpus { UNIVERSAL = 0; WEB = 1; IMAGES = 2; LOCAL = 3; NEWS = 4; PRODUCTS = 5; VIDEO = 6; } Corpus corpus = 4; }
  28. import "google/protobuf/any.proto"; message Message { google.protobuf.Any field = 1; }

    ˝ google.protobuf.Empty ˝ google.protobuf.Duration ˝ google.protobuf.Timestamp
  29. ◍䬵䓪 ˝ نؔ٭ٜغ樑⺘ int32 old_field = 6 [deprecated = true];

    reserved 2, 15, 9 to 11; reserved "foo", "bar";
  30. ؛وب٘٤ option go_package = "github.com/hatena/example/pb"; enum EnumAllowingAlias { option allow_alias

    = true; UNKNOWN = 0; STARTED = 1; RUNNING = 1; } message Example { int32 old_field = 6 [deprecated = true]; }
  31. ئ٭لتכتعٛ٭ّ service RouteGuide { rpc GetFeature(Point) returns (Feature) {} rpc

    ListFeatures(Rectangle) returns (stream Feature) {} rpc RecordRoute(stream Point) returns (RouteSummary) {} rpc RouteChat(stream RouteNote) returns (stream RouteNote) {} }
  32. gRPC API CRUD + List ˝ CreateEntity ˝ GetEntity ˝

    UpdateEntity ˝ DeleteEntity ˝ ListEntities
  33. syntax = "proto3"; option go_package = "./pb"; package welcome; service

    Welcome { rpc Greet(GreetRequest) returns (GreetReply); } message GreetRequest { string name = 1; } message GreetReply { string message = 1; }
  34. Protocol Compiler Codespaces protoc $ # localͰmacͳΒbrew install protobufͳͲͰ $

    sudo apt update $ sudo apt install protobuf-compiler $ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest $ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
  35. Protocol Compiler Go $ mkdir pb $ protoc !"go_out ./pb

    !"go-grpc_out ./pb !"go_opt=paths=source_relative !"go-grpc_opt paths=source_relative welcome.proto
  36. Protocol Compiler Go package main import ( "context" "flag" "fmt"

    "github.com/hatena/Hatena-Intern-2021-Lecture/api/grpc_adventure/pb" "google.golang.org/grpc" "google.golang.org/grpc/reflection" "log" "net" "os" "os/signal" ) var ( port = flag.Int("port", 10000, "The server port") )
  37. type welcomeServer struct { pb.UnimplementedWelcomeServer } func (s *welcomeServer) Greet(

    ctx context.Context, req *pb.GreetRequest ) (*pb.GreetReply, error) { return &pb.GreetReply{ Message: fmt.Sprintf("Welcome %s", req.Name), }, nil } func newServer() *welcomeServer { return &welcomeServer{} }
  38. func main() { lis, err !" net.Listen("tcp", fmt.Sprintf(":%d", *port)) if

    err !# nil { log.Fatalf("failed to listen: %v", err) } grpcServer !" grpc.NewServer() pb.RegisterWelcomeServer(grpcServer, newServer()) reflection.Register(grpcServer) go func() { log.Printf("start gRPC server port: %v", *port) grpcServer.Serve(lis) }() quit !" make(chan os.Signal) signal.Notify(quit, os.Interrupt) !$quit log.Printf("stopping gRPC server!!%") grpcServer.GracefulStop() }
  39. grpcurl $ go install github.com/fullstorydev/grpcurl/cmd/grpcurl@latest $ grpcurl -plaintext localhost:10000 list

    $ grpcurl -plaintext localhost:10000 describe welcome.GreetRequest $ grpcurl -plaintext -d '{"name": "John Appleseed"}' localhost:23455 welcome.Welcome/Greet usage: grpcurl -d '{"id": 1234, "tags": ["foo","bar"]}' \ grpc.server.com:443 my.custom.server.Service/ Method