Skip to content

Commit

Permalink
Add simpler HTTP server example
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed Sep 9, 2024
1 parent fe70e83 commit 7d8da3e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
4 changes: 1 addition & 3 deletions examples/http_server_multishot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ def setup_connection(fd)

parser = Http::Parser.new
parser.on_message_complete = -> {
http_send_response(fd, "Hello, world!\n") do
@ring.prep_close(fd: fd)
end
http_send_response(fd, "Hello, world!\n")
}

http_prep_read(fd, parser)
Expand Down
34 changes: 34 additions & 0 deletions examples/http_server_simpler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative '../lib/iou'
require 'socket'
require 'http/parser'

socket = TCPServer.open('127.0.0.1', 1234)
puts 'Listening on port 1234... (multishot read)'

@ring = IOU::Ring.new
@buffer_group = @ring.setup_buffer_ring(count: 1024, size: 4096)

@ring.prep_accept(fd: socket.fileno, multishot: true) do |c|
http_handle_connection(c[:result]) if c[:result] > 0
end

def http_handle_connection(fd)
parser = Http::Parser.new
parser.on_message_complete = -> { http_send_response(fd, "Hello, world!\n") }

@ring.prep_read(fd: fd, multishot: true, buffer_group: @buffer_group) do |c|
if c[:result] > 0
parser << c[:buffer]
else
puts "Connection closed on fd #{fd}"
end
end
end

def http_send_response(fd, body)
msg = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: keep-alive\r\nContent-Length: #{body.bytesize}\r\n\r\n#{body}"
@ring.prep_write(fd: fd, buffer: msg)
end

trap('SIGINT') { exit! }
@ring.process_completions_loop

0 comments on commit 7d8da3e

Please sign in to comment.