Skip to content
This repository was archived by the owner on Oct 22, 2020. It is now read-only.

Commit 824f5ec

Browse files
committed
Allow custom headers in responses from HttpServer
1 parent c355cbc commit 824f5ec

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

lib/wpxf/net/http_server.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,33 @@ def http_server_thread
9292

9393
private
9494

95+
def send_headers(socket, body, content_type, custom_headers)
96+
headers = []
97+
headers.push 'HTTP/1.1 200 OK'
98+
headers.push "Content-Type: #{content_type}"
99+
headers.push "Content-Length: #{body.bytesize}"
100+
headers += custom_headers unless custom_headers.nil?
101+
headers.push 'Connection: close'
102+
103+
headers.each do |header|
104+
socket.print "#{header}\r\n"
105+
end
106+
end
107+
95108
def send_response(response, socket)
96109
content_type = 'text/plain'
97110
body = ''
111+
custom_headers = nil
98112

99113
if response.is_a? String
100114
body = response
101115
else
102116
body = response[:body]
103117
content_type = response[:type]
118+
custom_headers = response[:headers]
104119
end
105120

106-
socket.print "HTTP/1.1 200 OK\r\n"\
107-
"Content-Type: #{content_type}\r\n"\
108-
"Content-Length: #{body.bytesize}\r\n"\
109-
"Connection: close\r\n"
121+
send_headers(socket, body, content_type, custom_headers)
110122
socket.print "\r\n"
111123
socket.print body
112124
end

0 commit comments

Comments
 (0)