-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxtnation.rb
executable file
·38 lines (27 loc) · 952 Bytes
/
txtnation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Get sockets from stdlib
require 'socket'
# Socket to listen on port 8080
server = TCPServer.open(8080)
# Servers run forever
loop {
# Wait for a client to connect
client = server.accept
# In this case, method = "POST" and path = "/"
method, path = client.gets.split
headers = {}
# Collect HTTP headers
while line = client.gets.split(' ', 2)
# Blank line means no more headers
break if line[0] == ""
# Hash headers by type
headers[line[0].chop] = line[1].strip
end
# Read the POST data as specified in the header
data = client.read(headers["Content-Length"].to_i)
# Do what you want with the POST data
puts data
# Send response to the client
client.puts "OK"
# Disconnect from the client
client.close
}