Here's an example of how you could create a HTTP server in Ruby and read the request body as binary data, and return a binary response:
main.rb1229 chars49 lines
This example uses a plain TCP socket to listen for incoming connections, and reads the request line and headers using gets
. It then reads the request body as binary data by calling read
on the socket with the length specified in the Content-Length
header.
The example then builds a simple response by encoding a string as binary data (response_body.bytesize
returns the number of bytes in the string), and sends the response back to the client as binary data using write
on the socket.
Note that this example is for demo purposes only - in practice, you'd want to handle errors, timeouts, and other edge cases.
gistlibby LogSnag