Guest User

tasynchttpserver_transferencoding.nim

a guest
Jan 13th, 2021
56
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import httpclient, asynchttpserver, asyncdispatch, asyncfutures
  2. import net
  3.  
  4. import std/asyncnet
  5. import std/nativesockets
  6.  
  7. const postBegin = """
  8. POST / HTTP/1.1
  9. Transfer-Encoding:chunked
  10.  
  11. """
  12.  
  13. block:
  14.   const expected = "hello=world"
  15.   const input = ("b\r\n" &
  16.                  "hello=world\r\n" &
  17.                  "0\r\n" &
  18.                  "\r\n")
  19.   var sanity = false
  20.   proc handler(request: Request) {.async.} =
  21.       doAssert(request.body == expected)
  22.       doAssert(request.headers.hasKey("Transfer-Encoding"))
  23.       doAssert(not request.headers.hasKey("Content-Length"))
  24.       sanity = true
  25.       await request.respond(Http200, "Good")
  26.  
  27.   let server = newAsyncHttpServer()
  28.   asyncCheck server.serve(Port(0), handler)
  29.   let port = getLocalAddr(server.getSocket.getFd, AF_INET)[1]
  30.   let data = postBegin & input
  31.   var socket = newSocket()
  32.   socket.connect("127.0.0.1", port)
  33.   socket.send(data)
  34.   waitFor sleepAsync(10)
  35.   socket.close()
  36.   server.close()
  37.  
  38.   # Verify we ran the handler and its asserts
  39.   doAssert(sanity)
  40. block:
  41.   const expected = "hello encoding"
  42.   const input = ("e\r\n" &
  43.                  "hello encoding\r\n" &
  44.                  "0\r\n" &
  45.                  "\r\n")
  46.   var sanity = false
  47.   proc handler(request: Request) {.async.} =
  48.       doAssert(request.body == expected)
  49.       doAssert(request.headers.hasKey("Transfer-Encoding"))
  50.       doAssert(not request.headers.hasKey("Content-Length"))
  51.       sanity = true
  52.       await request.respond(Http200, "Good")
  53.  
  54.   let server = newAsyncHttpServer()
  55.   asyncCheck server.serve(Port(0), handler)
  56.   let port = getLocalAddr(server.getSocket.getFd, AF_INET)[1]
  57.   let data = postBegin & input
  58.   var socket = newSocket()
  59.   socket.connect("127.0.0.1", port)
  60.   socket.send(data)
  61.   waitFor sleepAsync(10)
  62.   socket.close()
  63.   server.close()
  64.  
  65.   # Verify we ran the handler and its asserts
  66.   doAssert(sanity)
  67. # block:
  68. #   # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding
  69. #   const expected = "MozillaDeveloperNetwork"
  70. #   const input = ("7\r\n" &
  71. #                 "Mozilla\r\n" &
  72. #                 "9\r\n" &
  73. #                 "Developer\r\n" &
  74. #                 "7\r\n" &
  75. #                 "Network\r\n" &
  76. #                 "0\r\n" &
  77. #                 "\r\n")
  78. #   var sanity = false
  79. #   proc handler(request: Request) {.async.} =
  80. #       doAssert(request.body == expected)
  81. #       doAssert(request.headers.hasKey("Transfer-Encoding"))
  82. #       doAssert(not request.headers.hasKey("Content-Length"))
  83. #       sanity = true
  84. #       await request.respond(Http200, "Good")
  85.  
  86. #   let server = newAsyncHttpServer()
  87. #   asyncCheck server.serve(Port(0), handler)
  88. #   let port = getLocalAddr(server.getSocket.getFd, AF_INET)[1]
  89. #   let data = postBegin & input
  90. #   var socket = newSocket()
  91. #   socket.connect("127.0.0.1", port)
  92. #   socket.send(data)
  93. #   waitFor sleepAsync(10)
  94. #   socket.close()
  95. #   server.close()
  96.  
  97. #   # Verify we ran the handler and its asserts
  98. #   doAssert(sanity)
  99. # block:
  100. #   # https://en.wikipedia.org/wiki/Chunked_transfer_encoding#Example
  101. #   const expected = "Wikipedia in \r\n\r\nchunks."
  102. #   const input = ("4\r\n" &
  103. #                 "Wiki\r\n" &
  104. #                 "6\r\n" &
  105. #                 "pedia \r\n" &
  106. #                 "E\r\n" &
  107. #                 "in \r\n" &
  108. #                 "\r\n" &
  109. #                 "chunks.\r\n" &
  110. #                 "0\r\n" &
  111. #                 "\r\n")
  112. #   var sanity = false
  113. #   proc handler(request: Request) {.async.} =
  114. #       doAssert(request.body == expected)
  115. #       doAssert(request.headers.hasKey("Transfer-Encoding"))
  116. #       doAssert(not request.headers.hasKey("Content-Length"))
  117. #       sanity = true
  118. #       await request.respond(Http200, "Good")
  119.  
  120. #   let server = newAsyncHttpServer()
  121. #   asyncCheck server.serve(Port(0), handler)
  122. #   let port = getLocalAddr(server.getSocket.getFd, AF_INET)[1]
  123. #   let data = postBegin & input
  124. #   var socket = newSocket()
  125. #   socket.connect("127.0.0.1", port)
  126. #   socket.send(data)
  127. #   waitFor sleepAsync(10)
  128. #   socket.close()
  129. #   server.close()
  130.  
  131. #   # Verify we ran the handler and its asserts
  132. #   doAssert(sanity)
  133.  
RAW Paste Data