I’m using Scala and I’m stuck on Stage #EJ5. I keep getting this error: Received: “” (no content received), but locally I cannot reproduce this issue. At this point I’m not sure if this is an issue on my side or if it’s a bug
I’ve tested my code with oha:
oha -n 3 -c 3 ``http://localhost:4221
Here are my logs:
accepted new connection from null:49204
accepted new connection from null:49203
accepted new connection from null:49205
Response HTTP/1.1 200 OK\r\n\r\n was sent to client on port 49203
Response HTTP/1.1 200 OK\r\n\r\n was sent to client on port 49205
Response HTTP/1.1 200 OK\r\n\r\n was sent to client on port 49204
OK
OK
OK
And here’s a snippet of my code:
@main def main(): Unit = {
val CRLF = "\r\n"
val HTTP_VERSION = "HTTP/1.1"
val serverSocket = new ServerSocket(4221)
serverSocket.setReuseAddress(true)
while (true) {
val clientSocket = serverSocket.accept()
println(
s"accepted new connection from ${clientSocket.getChannel()}:${clientSocket.getPort()}"
)
Future {
handleClient(HTTP_VERSION, clientSocket)
}.onComplete {
case Success(_) => println("OK")
case Failure(e) => println(s"Error handling client: ${e.getMessage}")
}
}
}
def handleClient(httpVersion: String, clientSocket: Socket): Unit = {
try {
val inputStream = clientSocket.getInputStream()
val reader = new BufferedReader(new InputStreamReader(inputStream))
val rawRequest = Iterator
.continually(reader.readLine())
.takeWhile(it => it != null && it.nonEmpty)
.toList
val request = HttpRequest.parse(rawRequest)
val outputStream = clientSocket.getOutputStream()
val response = request.line.path match {
case "/" => HttpResponse(StatusCode.OK)
case echo if echo.startsWith("/echo/") =>
HttpResponse(StatusCode.OK, body = echo.stripPrefix("/echo/"))
case userAgent if userAgent.startsWith("/user-agent") =>
HttpResponse(
StatusCode.OK,
body = request.headers.getOrElse(USER_AGENT, "")
)
case _ => HttpResponse(StatusCode.NOT_FOUND)
}
val responseBytes = response.toBytes(httpVersion)
outputStream.write(responseBytes)
outputStream.flush()
val a = String(responseBytes).replace("\r\n", "\\r\\n")
println(s"Response $a was sent to client on port ${clientSocket.getPort}")
} catch {
case e: IOException =>
println(s"IOException: ${e.getMessage}")
} finally {
clientSocket.close()
}
}
